|
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.tasks.ui.editors; |
| 10 |
|
| 11 |
import org.eclipse.jface.text.Document; |
| 12 |
import org.eclipse.jface.text.IDocument; |
| 13 |
import org.eclipse.jface.text.reconciler.IReconciler; |
| 14 |
import org.eclipse.jface.text.source.IOverviewRuler; |
| 15 |
import org.eclipse.jface.text.source.ISourceViewer; |
| 16 |
import org.eclipse.jface.text.source.IVerticalRuler; |
| 17 |
import org.eclipse.jface.text.source.SourceViewer; |
| 18 |
import org.eclipse.jface.text.source.VerticalRuler; |
| 19 |
import org.eclipse.jface.viewers.Viewer; |
| 20 |
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonThemes; |
| 21 |
import org.eclipse.mylyn.internal.tasks.ui.wiki.AbstractRenderingEngine2; |
| 22 |
import org.eclipse.mylyn.internal.tasks.ui.wiki.JiraRenderingEngine; |
| 23 |
import org.eclipse.mylyn.tasks.core.data.TaskAttribute; |
| 24 |
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor; |
| 25 |
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPart; |
| 26 |
import org.eclipse.swt.SWT; |
| 27 |
import org.eclipse.swt.browser.Browser; |
| 28 |
import org.eclipse.swt.custom.CTabFolder; |
| 29 |
import org.eclipse.swt.custom.CTabItem; |
| 30 |
import org.eclipse.swt.custom.StyledText; |
| 31 |
import org.eclipse.swt.events.SelectionEvent; |
| 32 |
import org.eclipse.swt.events.SelectionListener; |
| 33 |
import org.eclipse.swt.graphics.Font; |
| 34 |
import org.eclipse.swt.graphics.Point; |
| 35 |
import org.eclipse.swt.layout.GridData; |
| 36 |
import org.eclipse.swt.layout.GridLayout; |
| 37 |
import org.eclipse.swt.widgets.Composite; |
| 38 |
import org.eclipse.ui.PlatformUI; |
| 39 |
import org.eclipse.ui.forms.widgets.ExpandableComposite; |
| 40 |
import org.eclipse.ui.forms.widgets.FormToolkit; |
| 41 |
import org.eclipse.ui.forms.widgets.Section; |
| 42 |
import org.eclipse.ui.internal.EditorAreaHelper; |
| 43 |
import org.eclipse.ui.internal.WorkbenchPage; |
| 44 |
import org.eclipse.ui.themes.IThemeManager; |
| 45 |
|
| 46 |
public class TaskEditorWikiTextPart extends AbstractTaskEditorPart { |
| 47 |
|
| 48 |
private Browser browser; |
| 49 |
|
| 50 |
private CTabFolder folder; |
| 51 |
|
| 52 |
private int sectionStyle; |
| 53 |
|
| 54 |
private Composite composite; |
| 55 |
|
| 56 |
private StyledText text; |
| 57 |
|
| 58 |
private TaskAttribute attribute; |
| 59 |
|
| 60 |
/** The width of the vertical ruler. */ |
| 61 |
protected static final int VERTICAL_RULER_WIDTH = 12; |
| 62 |
|
| 63 |
private ISourceViewer viewer; |
| 64 |
|
| 65 |
private boolean previewDirty; |
| 66 |
|
| 67 |
private IDocument document; |
| 68 |
|
| 69 |
public TaskEditorWikiTextPart() { |
| 70 |
setSectionStyle(ExpandableComposite.TITLE_BAR | ExpandableComposite.TWISTIE | ExpandableComposite.EXPANDED); |
| 71 |
} |
| 72 |
|
| 73 |
public void appendText(String text) { |
| 74 |
//ignore for now |
| 75 |
} |
| 76 |
|
| 77 |
public int getSectionStyle() { |
| 78 |
return sectionStyle; |
| 79 |
} |
| 80 |
|
| 81 |
public void setSectionStyle(int sectionStyle) { |
| 82 |
this.sectionStyle = sectionStyle; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void createControl(Composite parent, FormToolkit toolkit) { |
| 87 |
|
| 88 |
if (attribute == null) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
Section section = createSection(parent, toolkit, sectionStyle); |
| 93 |
|
| 94 |
composite = toolkit.createComposite(section); |
| 95 |
GridLayout layout = new GridLayout(); |
| 96 |
layout.numColumns = 1; |
| 97 |
composite.setLayout(layout); |
| 98 |
|
| 99 |
folder = new CTabFolder(composite, SWT.BOTTOM | SWT.FLAT | SWT.BORDER); |
| 100 |
|
| 101 |
{ |
| 102 |
CTabItem viewerItem = new CTabItem(folder, SWT.NONE); |
| 103 |
viewerItem.setText("Source"); |
| 104 |
viewerItem.setToolTipText("Edit Source"); |
| 105 |
|
| 106 |
viewer = new MarkupSourceViewer(folder, new VerticalRuler(VERTICAL_RULER_WIDTH), SWT.V_SCROLL |
| 107 |
| SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION | SWT.WRAP); |
| 108 |
document = new Document(attribute.getValue()); |
| 109 |
viewer.setEditable(true); |
| 110 |
viewer.setDocument(document); |
| 111 |
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); |
| 112 |
Font font = themeManager.getCurrentTheme().getFontRegistry().get(CommonThemes.FONT_EDITOR_COMMENT); |
| 113 |
viewer.getTextWidget().setFont(font); |
| 114 |
|
| 115 |
viewerItem.setControl(viewer instanceof Viewer ? ((Viewer) viewer).getControl() : viewer.getTextWidget()); |
| 116 |
folder.setSelection(viewerItem); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
{ |
| 121 |
CTabItem previewItem = new CTabItem(folder, SWT.NONE); |
| 122 |
previewItem.setText("Preview"); |
| 123 |
previewItem.setToolTipText("Preview Source"); |
| 124 |
|
| 125 |
browser = new Browser(folder, SWT.NONE); |
| 126 |
previewItem.setControl(browser); |
| 127 |
} |
| 128 |
|
| 129 |
folder.addSelectionListener(new SelectionListener() { |
| 130 |
public void widgetDefaultSelected(SelectionEvent selectionevent) { |
| 131 |
widgetSelected(selectionevent); |
| 132 |
} |
| 133 |
|
| 134 |
public void widgetSelected(SelectionEvent selectionevent) { |
| 135 |
updatePreview(); |
| 136 |
} |
| 137 |
|
| 138 |
}); |
| 139 |
|
| 140 |
final GridData gd = new GridData(); |
| 141 |
// wrap text at this margin, see comment below |
| 142 |
int width = getEditorWidth(); |
| 143 |
// the goal is to make the text viewer as big as the text so it does not require scrolling when first drawn |
| 144 |
// on screen |
| 145 |
Point size = folder.computeSize(width, SWT.DEFAULT, true); |
| 146 |
gd.widthHint = AbstractAttributeEditor.MAXIMUM_WIDTH; |
| 147 |
gd.horizontalAlignment = SWT.FILL; |
| 148 |
gd.grabExcessHorizontalSpace = true; |
| 149 |
// limit height to be avoid dynamic resizing of the text widget: |
| 150 |
// MAXIMUM_HEIGHT < height < MAXIMUM_HEIGHT * 4 |
| 151 |
//gd.minimumHeight = AbstractAttributeEditor.MAXIMUM_HEIGHT; |
| 152 |
gd.heightHint = Math.min(Math.max(AbstractAttributeEditor.MAXIMUM_HEIGHT, size.y), |
| 153 |
AbstractAttributeEditor.MAXIMUM_HEIGHT * 4); |
| 154 |
if (getExpandVertically()) { |
| 155 |
gd.verticalAlignment = SWT.FILL; |
| 156 |
gd.grabExcessVerticalSpace = true; |
| 157 |
} |
| 158 |
folder.setLayoutData(gd); |
| 159 |
//folder.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); |
| 160 |
|
| 161 |
//toolkit.adapt(folder, true, true); |
| 162 |
toolkit.paintBordersFor(composite); |
| 163 |
section.setClient(composite); |
| 164 |
setSection(toolkit, section); |
| 165 |
} |
| 166 |
|
| 167 |
private int getEditorWidth() { |
| 168 |
int widthHint = 0; |
| 169 |
if (getManagedForm() != null && getManagedForm().getForm() != null) { |
| 170 |
widthHint = getManagedForm().getForm().getClientArea().width - 90; |
| 171 |
} |
| 172 |
if (widthHint <= 0 && getTaskEditorPage().getEditor().getEditorSite() != null |
| 173 |
&& getTaskEditorPage().getEditor().getEditorSite().getPage() != null) { |
| 174 |
EditorAreaHelper editorManager = ((WorkbenchPage) getTaskEditorPage().getEditor().getEditorSite().getPage()).getEditorPresentation(); |
| 175 |
if (editorManager != null && editorManager.getLayoutPart() != null) { |
| 176 |
widthHint = editorManager.getLayoutPart().getControl().getBounds().width - 90; |
| 177 |
} |
| 178 |
} |
| 179 |
if (widthHint <= 0) { |
| 180 |
widthHint = AbstractAttributeEditor.MAXIMUM_WIDTH; |
| 181 |
} |
| 182 |
return widthHint; |
| 183 |
} |
| 184 |
|
| 185 |
protected Composite getComposite() { |
| 186 |
return composite; |
| 187 |
} |
| 188 |
|
| 189 |
public TaskAttribute getAttribute() { |
| 190 |
return attribute; |
| 191 |
} |
| 192 |
|
| 193 |
protected StyledText getText() { |
| 194 |
return text; |
| 195 |
} |
| 196 |
|
| 197 |
public void setAttribute(TaskAttribute attribute) { |
| 198 |
this.attribute = attribute; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void setFocus() { |
| 203 |
if (folder != null && folder.getSelection() != null) { |
| 204 |
folder.getSelection().getControl().setFocus(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
private static class MarkupSourceViewer extends SourceViewer { |
| 209 |
|
| 210 |
public MarkupSourceViewer(Composite parent, IVerticalRuler ruler, int styles) { |
| 211 |
super(parent, ruler, styles); |
| 212 |
} |
| 213 |
|
| 214 |
public MarkupSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, |
| 215 |
boolean showAnnotationsOverview, int styles) { |
| 216 |
super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles); |
| 217 |
} |
| 218 |
|
| 219 |
public IReconciler getReconciler() { |
| 220 |
return fReconciler; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
private void updatePreview() { |
| 225 |
if (/*previewDirty && */browser != null) { |
| 226 |
String xhtml = null; |
| 227 |
if (document == null) { |
| 228 |
xhtml = "<?xml version=\"1.0\" ?><html xmlns=\"http://www.w3.org/1999/xhtml\"><body></body></html>"; //$NON-NLS-1$ |
| 229 |
} else { |
| 230 |
AbstractRenderingEngine2 engine = new JiraRenderingEngine(); |
| 231 |
xhtml = engine.renderAsHtml(null, document.get()); |
| 232 |
} |
| 233 |
|
| 234 |
browser.setText(xhtml); |
| 235 |
//previewDirty = false; |
| 236 |
} |
| 237 |
} |
| 238 |
} |