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