|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2007 Mylyn project committers and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
*******************************************************************************/ |
| 8 |
|
| 9 |
package org.eclipse.mylyn.internal.sandbox.ui.editors; |
| 10 |
|
| 11 |
import org.eclipse.jface.text.Document; |
| 12 |
import org.eclipse.jface.text.ITextListener; |
| 13 |
import org.eclipse.jface.text.TextEvent; |
| 14 |
import org.eclipse.jface.text.source.SourceViewer; |
| 15 |
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonThemes; |
| 16 |
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal; |
| 17 |
import org.eclipse.mylyn.tasks.core.TaskRepository; |
| 18 |
import org.eclipse.mylyn.tasks.core.data.TaskAttribute; |
| 19 |
import org.eclipse.mylyn.tasks.core.data.TaskDataModel; |
| 20 |
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor; |
| 21 |
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint; |
| 22 |
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint.ColumnSpan; |
| 23 |
import org.eclipse.mylyn.tasks.ui.editors.LayoutHint.RowSpan; |
| 24 |
import org.eclipse.swt.SWT; |
| 25 |
import org.eclipse.swt.events.DisposeEvent; |
| 26 |
import org.eclipse.swt.events.DisposeListener; |
| 27 |
import org.eclipse.swt.events.FocusEvent; |
| 28 |
import org.eclipse.swt.events.FocusListener; |
| 29 |
import org.eclipse.swt.graphics.Font; |
| 30 |
import org.eclipse.swt.widgets.Composite; |
| 31 |
import org.eclipse.ui.PlatformUI; |
| 32 |
import org.eclipse.ui.contexts.IContextActivation; |
| 33 |
import org.eclipse.ui.contexts.IContextService; |
| 34 |
import org.eclipse.ui.forms.widgets.FormToolkit; |
| 35 |
import org.eclipse.ui.themes.IThemeManager; |
| 36 |
|
| 37 |
public class ExtensionRichTextAttributeEditor extends AbstractAttributeEditor { |
| 38 |
|
| 39 |
private final AbstractTaskEditorExtension extension; |
| 40 |
|
| 41 |
private final TaskRepository taskRepository; |
| 42 |
|
| 43 |
private final int style; |
| 44 |
|
| 45 |
private SourceViewer viewer; |
| 46 |
|
| 47 |
private final IContextService contextService; |
| 48 |
|
| 49 |
private FocusListener focusListener; |
| 50 |
|
| 51 |
private IContextActivation contextActivation; |
| 52 |
|
| 53 |
private DisposeListener disposeListener; |
| 54 |
|
| 55 |
public ExtensionRichTextAttributeEditor(IContextService contextService, TaskDataModel manager, |
| 56 |
TaskRepository taskRepository, AbstractTaskEditorExtension extension, TaskAttribute taskAttribute, int style) { |
| 57 |
super(manager, taskAttribute); |
| 58 |
this.contextService = contextService; |
| 59 |
this.taskRepository = taskRepository; |
| 60 |
this.extension = extension; |
| 61 |
this.style = style; |
| 62 |
if ((style & SWT.MULTI) != 0) { |
| 63 |
setLayoutHint(new LayoutHint(RowSpan.MULTIPLE, ColumnSpan.MULTIPLE)); |
| 64 |
} else { |
| 65 |
setLayoutHint(new LayoutHint(RowSpan.SINGLE, ColumnSpan.MULTIPLE)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void createControl(Composite parent, FormToolkit toolkit) { |
| 71 |
int style = this.style; |
| 72 |
if (!isReadOnly() && (style & TasksUiInternal.SWT_NO_SCROLL) == 0) { |
| 73 |
style |= SWT.V_SCROLL; |
| 74 |
} |
| 75 |
style = SWT.FLAT | SWT.WRAP | style; |
| 76 |
|
| 77 |
if (isReadOnly()) { |
| 78 |
viewer = extension.createViewer(taskRepository, parent, style); |
| 79 |
} else { |
| 80 |
viewer = extension.createEditor(taskRepository, parent, style); |
| 81 |
} |
| 82 |
Document document = new Document(getValue()); |
| 83 |
|
| 84 |
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); |
| 85 |
Font font = themeManager.getCurrentTheme().getFontRegistry().get(CommonThemes.FONT_EDITOR_COMMENT); |
| 86 |
viewer.getTextWidget().setFont(font); |
| 87 |
|
| 88 |
if (isReadOnly()) { |
| 89 |
viewer.setEditable(false); |
| 90 |
viewer.setDocument(document); |
| 91 |
} else { |
| 92 |
viewer.setEditable(true); |
| 93 |
viewer.setDocument(document); |
| 94 |
|
| 95 |
viewer.addTextListener(new ITextListener() { |
| 96 |
public void textChanged(TextEvent event) { |
| 97 |
// filter out events caused by text presentation changes, e.g. annotation drawing |
| 98 |
String value = viewer.getTextWidget().getText(); |
| 99 |
if (!getValue().equals(value)) { |
| 100 |
setValue(value); |
| 101 |
} |
| 102 |
} |
| 103 |
}); |
| 104 |
viewer.getControl().setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); |
| 105 |
} |
| 106 |
|
| 107 |
toolkit.adapt(viewer.getTextWidget(), true, false); |
| 108 |
|
| 109 |
setControl(viewer.getTextWidget()); |
| 110 |
|
| 111 |
focusListener = new FocusListener() { |
| 112 |
|
| 113 |
public void focusGained(FocusEvent e) { |
| 114 |
setContext(); |
| 115 |
} |
| 116 |
|
| 117 |
public void focusLost(FocusEvent e) { |
| 118 |
unsetContext(); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
viewer.getTextWidget().addFocusListener(focusListener); |
| 123 |
disposeListener = new DisposeListener() { |
| 124 |
public void widgetDisposed(DisposeEvent e) { |
| 125 |
unsetContext(); |
| 126 |
} |
| 127 |
}; |
| 128 |
|
| 129 |
viewer.getTextWidget().addDisposeListener(disposeListener); |
| 130 |
} |
| 131 |
|
| 132 |
protected void unsetContext() { |
| 133 |
if (contextActivation != null) { |
| 134 |
contextService.deactivateContext(contextActivation); |
| 135 |
contextActivation = null; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
protected void setContext() { |
| 140 |
if (contextActivation != null) { |
| 141 |
contextService.deactivateContext(contextActivation); |
| 142 |
contextActivation = null; |
| 143 |
} |
| 144 |
if (contextService != null && extension.getEditorContextId() != null) { |
| 145 |
contextActivation = contextService.activateContext(extension.getEditorContextId()); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public String getValue() { |
| 150 |
return getAttributeMapper().getValue(getTaskAttribute()); |
| 151 |
} |
| 152 |
|
| 153 |
public void setValue(String value) { |
| 154 |
getAttributeMapper().setValue(getTaskAttribute(), value); |
| 155 |
attributeChanged(); |
| 156 |
} |
| 157 |
} |