|
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.jface.viewers.Viewer; |
| 16 |
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonThemes; |
| 17 |
import org.eclipse.mylyn.internal.tasks.ui.editors.RichTextAttributeEditor; |
| 18 |
import org.eclipse.mylyn.tasks.core.TaskRepository; |
| 19 |
import org.eclipse.mylyn.tasks.core.data.TaskAttribute; |
| 20 |
import org.eclipse.mylyn.tasks.core.data.TaskDataModel; |
| 21 |
import org.eclipse.swt.SWT; |
| 22 |
import org.eclipse.swt.custom.CTabFolder; |
| 23 |
import org.eclipse.swt.custom.CTabItem; |
| 24 |
import org.eclipse.swt.events.DisposeEvent; |
| 25 |
import org.eclipse.swt.events.DisposeListener; |
| 26 |
import org.eclipse.swt.events.FocusEvent; |
| 27 |
import org.eclipse.swt.events.FocusListener; |
| 28 |
import org.eclipse.swt.events.SelectionEvent; |
| 29 |
import org.eclipse.swt.events.SelectionListener; |
| 30 |
import org.eclipse.swt.graphics.Font; |
| 31 |
import org.eclipse.swt.widgets.Composite; |
| 32 |
import org.eclipse.ui.PlatformUI; |
| 33 |
import org.eclipse.ui.contexts.IContextActivation; |
| 34 |
import org.eclipse.ui.contexts.IContextService; |
| 35 |
import org.eclipse.ui.forms.widgets.FormToolkit; |
| 36 |
import org.eclipse.ui.themes.IThemeManager; |
| 37 |
|
| 38 |
/** |
| 39 |
* A multitab source viewer that can edit and preview Textile wikitext markup. |
| 40 |
* |
| 41 |
* TODO generalize RichTextAttributeEditor: add a protected method to create the SourceViewer then subclasses can then |
| 42 |
* create its own SourceViewer without overriding createControl and copying the code in it |
| 43 |
* |
| 44 |
* @author Jingwen Ou |
| 45 |
*/ |
| 46 |
public class ExtensibleRichTextAttributeEditor extends RichTextAttributeEditor { |
| 47 |
|
| 48 |
private SourceViewer source; |
| 49 |
|
| 50 |
private final TaskRepository taskRepository; |
| 51 |
|
| 52 |
private final int styles; |
| 53 |
|
| 54 |
private final IContextService contextService; |
| 55 |
|
| 56 |
private IContextActivation contextActivation; |
| 57 |
|
| 58 |
private final AbstractTaskEditorExtension extension; |
| 59 |
|
| 60 |
public ExtensibleRichTextAttributeEditor(IContextService contextService, TaskDataModel manager, |
| 61 |
TaskRepository taskRepository, AbstractTaskEditorExtension extension, TaskAttribute taskAttribute, |
| 62 |
int styles) { |
| 63 |
super(manager, taskRepository, taskAttribute, styles); |
| 64 |
this.contextService = contextService; |
| 65 |
this.taskRepository = taskRepository; |
| 66 |
this.extension = extension; |
| 67 |
this.styles = styles; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public SourceViewer getViewer() { |
| 72 |
return source; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void createControl(Composite parent, FormToolkit toolkit) { |
| 77 |
if (isReadOnly()) { |
| 78 |
source = extension.createViewer(taskRepository, parent, styles); |
| 79 |
source.setDocument(new Document(getValue())); |
| 80 |
|
| 81 |
setControl(source instanceof Viewer ? ((Viewer) source).getControl() : source.getTextWidget()); |
| 82 |
} else { |
| 83 |
CTabFolder folder = new CTabFolder(parent, SWT.FLAT | SWT.BOTTOM); |
| 84 |
|
| 85 |
/** wikitext markup editor **/ |
| 86 |
|
| 87 |
CTabItem viewerItem = new CTabItem(folder, SWT.NONE); |
| 88 |
viewerItem.setText("Source"); |
| 89 |
viewerItem.setToolTipText("Edit Source"); |
| 90 |
|
| 91 |
source = extension.createEditor(taskRepository, folder, styles | SWT.V_SCROLL); |
| 92 |
if (source.getDocument() == null) { |
| 93 |
source.setDocument(new Document(getValue())); |
| 94 |
} |
| 95 |
source.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 = source.getTextWidget().getText(); |
| 99 |
if (!getValue().equals(value)) { |
| 100 |
setValue(value); |
| 101 |
} |
| 102 |
} |
| 103 |
}); |
| 104 |
source.getControl().setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); |
| 105 |
|
| 106 |
//setting up focus stuff, copied from Daivd's solution |
| 107 |
FocusListener focusListener = new FocusListener() { |
| 108 |
|
| 109 |
public void focusGained(FocusEvent e) { |
| 110 |
setContext(); |
| 111 |
} |
| 112 |
|
| 113 |
public void focusLost(FocusEvent e) { |
| 114 |
unsetContext(); |
| 115 |
} |
| 116 |
}; |
| 117 |
source.getTextWidget().addFocusListener(focusListener); |
| 118 |
DisposeListener disposeListener = new DisposeListener() { |
| 119 |
public void widgetDisposed(DisposeEvent e) { |
| 120 |
unsetContext(); |
| 121 |
} |
| 122 |
}; |
| 123 |
source.getTextWidget().addDisposeListener(disposeListener); |
| 124 |
|
| 125 |
viewerItem.setControl(source instanceof Viewer ? ((Viewer) source).getControl() : source.getTextWidget()); |
| 126 |
folder.setSelection(viewerItem); |
| 127 |
|
| 128 |
/** wikitext markup viewer **/ |
| 129 |
|
| 130 |
CTabItem previewItem = new CTabItem(folder, SWT.NONE); |
| 131 |
previewItem.setText("Preview"); |
| 132 |
previewItem.setToolTipText("Preview Source"); |
| 133 |
|
| 134 |
final SourceViewer preview = extension.createViewer(taskRepository, folder, styles); |
| 135 |
|
| 136 |
previewItem.setControl(preview instanceof Viewer ? ((Viewer) preview).getControl() |
| 137 |
: preview.getTextWidget()); |
| 138 |
|
| 139 |
folder.addSelectionListener(new SelectionListener() { |
| 140 |
public void widgetDefaultSelected(SelectionEvent selectionevent) { |
| 141 |
widgetSelected(selectionevent); |
| 142 |
} |
| 143 |
|
| 144 |
public void widgetSelected(SelectionEvent selectionevent) { |
| 145 |
Document document = new Document(source.getDocument().get()); |
| 146 |
preview.setDocument(document); |
| 147 |
} |
| 148 |
}); |
| 149 |
|
| 150 |
setControl(folder); |
| 151 |
} |
| 152 |
|
| 153 |
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); |
| 154 |
Font font = themeManager.getCurrentTheme().getFontRegistry().get(CommonThemes.FONT_EDITOR_COMMENT); |
| 155 |
source.getTextWidget().setFont(font); |
| 156 |
toolkit.adapt(source.getControl(), true, false); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
protected void unsetContext() { |
| 161 |
if (contextActivation != null) { |
| 162 |
contextService.deactivateContext(contextActivation); |
| 163 |
contextActivation = null; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
protected void setContext() { |
| 168 |
if (contextActivation != null) { |
| 169 |
contextService.deactivateContext(contextActivation); |
| 170 |
contextActivation = null; |
| 171 |
} |
| 172 |
if (contextService != null && extension.getEditorContextId() != null) { |
| 173 |
contextActivation = contextService.activateContext(extension.getEditorContextId()); |
| 174 |
} |
| 175 |
} |
| 176 |
} |