Skip to main content
summaryrefslogtreecommitdiffstats
blob: 185a48c892af1e367634e670abd73cea0fdd6cbd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2004, 2009 Tasktop Technologies and others. 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.editors;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.mylyn.internal.tasks.ui.editors.RepositoryTextViewerConfiguration.Mode;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskDataModel;
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor;
import org.eclipse.mylyn.tasks.ui.editors.AbstractRenderingEngine;
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorExtension;
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint;
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint.ColumnSpan;
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint.RowSpan;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * A text attribute editor that can switch between a editor, preview and source view.
 * 
 * @author Steffen Pingel
 * @see RichTextEditor
 */
public class RichTextAttributeEditor extends AbstractAttributeEditor {

	private final RichTextEditor editor;

	protected boolean ignoreNotification;

	public RichTextAttributeEditor(TaskDataModel manager, TaskRepository taskRepository, TaskAttribute taskAttribute) {
		this(manager, taskRepository, taskAttribute, SWT.MULTI);
	}

	public RichTextAttributeEditor(TaskDataModel manager, TaskRepository taskRepository, TaskAttribute taskAttribute,
			int style) {
		this(manager, taskRepository, taskAttribute, style, null, null);
	}

	public RichTextAttributeEditor(TaskDataModel manager, TaskRepository taskRepository, TaskAttribute taskAttribute,
			int style, IContextService contextService, AbstractTaskEditorExtension extension) {
		super(manager, taskAttribute);
		this.editor = new RichTextEditor(taskRepository, style, contextService, extension, getModel().getTask()) {
			@Override
			public void valueChanged(String value) {
				if (!ignoreNotification) {
					RichTextAttributeEditor.this.setValue(value);
				}
			};
		};
		this.editor.setReadOnly(isReadOnly());
		if ((style & SWT.MULTI) != 0) {
			setLayoutHint(new LayoutHint(RowSpan.MULTIPLE, ColumnSpan.MULTIPLE));
		} else {
			setLayoutHint(new LayoutHint(RowSpan.SINGLE, ColumnSpan.MULTIPLE));
		}
		setMode(Mode.DEFAULT);
		refresh();
	}

	@Override
	public void createControl(Composite parent, FormToolkit toolkit) {
		editor.createControl(parent, toolkit);
		setControl(editor.getControl());
	}

	public RichTextEditor getEditor() {
		return editor;
	}

	public SourceViewer getEditorViewer() {
		return editor.getEditorViewer();
	}

	public Mode getMode() {
		return editor.getMode();
	}

	public AbstractRenderingEngine getRenderingEngine() {
		return editor.getRenderingEngine();
	}

	public String getValue() {
		return getAttributeMapper().getValue(getTaskAttribute());
	}

	public SourceViewer getViewer() {
		return editor.getViewer();
	}

	public IAction getViewSourceAction() {
		return editor.getViewSourceAction();
	}

	public boolean hasBrowser() {
		return editor.hasBrowser();
	}

	public boolean hasPreview() {
		return editor.hasPreview();
	}

	public boolean isSpellCheckingEnabled() {
		return editor.isSpellCheckingEnabled();
	}

	public void setMode(Mode mode) {
		editor.setMode(mode);
	}

	public void setRenderingEngine(AbstractRenderingEngine renderingEngine) {
		editor.setRenderingEngine(renderingEngine);
	}

	public void setSpellCheckingEnabled(boolean spellCheckingEnabled) {
		editor.setSpellCheckingEnabled(spellCheckingEnabled);
	}

	public void setValue(String value) {
		getAttributeMapper().setValue(getTaskAttribute(), value);
		attributeChanged();
	}

	public void showBrowser() {
		editor.showBrowser();
	}

	public void showDefault() {
		editor.showDefault();
	}

	public void showEditor() {
		editor.showEditor();
	}

	public void showPreview() {
		editor.showPreview();
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		super.setReadOnly(readOnly);
		if (editor != null) {
			editor.setReadOnly(readOnly);
		}
	}

	@Override
	public void refresh() {
		try {
			ignoreNotification = true;
			editor.setText(getValue());
		} finally {
			ignoreNotification = false;
		}
	}

	@Override
	protected void decorateIncoming(Color color) {
		super.decorateIncoming(color);
		if (editor != null) {
			editor.setBackground(color);
		}
	}

	/**
	 * Tries to enable auto toggling of preview (editable on click)
	 */
	public void enableAutoTogglePreview() {
		editor.enableAutoTogglePreview();
	}
}

Back to the top