Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 235222 | Differences between
and this patch

Collapse All | Expand All

(-)E:/workspace/Mylyn_SoC/org.eclipse.mylyn.wikitext.ui/src/org/eclipse/mylyn/wikitext/ui/viewer/MultiSourceViewer.java (+141 lines)
Line 0 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.wikitext.ui.viewer;
10
11
import org.eclipse.jface.text.Document;
12
import org.eclipse.jface.text.IDocument;
13
import org.eclipse.jface.text.source.IAnnotationModel;
14
import org.eclipse.jface.text.source.ISourceViewer;
15
import org.eclipse.jface.text.source.IVerticalRuler;
16
import org.eclipse.jface.text.source.SourceViewer;
17
import org.eclipse.jface.viewers.Viewer;
18
import org.eclipse.mylyn.internal.wikitext.ui.editor.MarkupEditor;
19
import org.eclipse.mylyn.internal.wikitext.ui.editor.MarkupSourceViewerConfiguration;
20
import org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FastMarkupPartitioner;
21
import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
22
import org.eclipse.mylyn.wikitext.ui.WikiTextUiPlugin;
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.SelectionEvent;
27
import org.eclipse.swt.events.SelectionListener;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Control;
30
import org.eclipse.ui.PlatformUI;
31
import org.eclipse.ui.swt.IFocusService;
32
33
/**
34
 * A multitab source viewer that can edit and preview wikitext markup
35
 * (lightweight markup languages)
36
 * 
37
 * @author Jingwen Ou
38
 */
39
public abstract class MultiSourceViewer extends SourceViewer {
40
41
	private CTabFolder folder;
42
43
	private MarkupLanguage markupLanguage;
44
45
	public MultiSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
46
		super(parent, ruler, styles);
47
	}
48
49
	private void configurePartitioning(IDocument document) {
50
		if (document != null) {
51
			FastMarkupPartitioner partitioner = new FastMarkupPartitioner();
52
			partitioner.setMarkupLanguage(markupLanguage);
53
			partitioner.connect(document);
54
			document.setDocumentPartitioner(partitioner);
55
		}
56
	}
57
58
	@Override
59
	public void createControl(Composite parent, int styles) {
60
61
		folder = new CTabFolder(parent, SWT.FLAT | SWT.BOTTOM);
62
63
		// wikitext markup editor
64
		{
65
			CTabItem viewerItem = new CTabItem(folder, SWT.NONE);
66
			viewerItem.setText("Source");
67
			viewerItem.setToolTipText("Edit Source");
68
69
			super.createControl(folder, styles);
70
71
			viewerItem.setControl(this.getTextWidget());
72
			folder.setSelection(viewerItem);
73
74
			MarkupSourceViewerConfiguration configuration = new MarkupSourceViewerConfiguration(
75
					WikiTextUiPlugin.getDefault().getPreferenceStore());
76
			configuration.setMarkupLanguage(markupLanguage);
77
			configure(configuration);
78
79
			IFocusService focusService = (IFocusService) PlatformUI
80
					.getWorkbench().getService(IFocusService.class);
81
			if (focusService != null) {
82
				focusService.addFocusTracker(getTextWidget(),
83
						MarkupEditor.EDITOR_SOURCE_VIEWER);
84
			}
85
86
			getTextWidget().setData(MarkupLanguage.class.getName(),
87
					markupLanguage);
88
			getTextWidget().setData(ISourceViewer.class.getName(), this);
89
		}
90
91
		// wikitext markup viewer
92
		{
93
			CTabItem previewItem = new CTabItem(folder, SWT.NONE);
94
			previewItem.setText("Preview");
95
			previewItem.setToolTipText("Preview Source");
96
97
			final SourceViewer previewViewer = createPreviewViewer(folder,
98
					styles);
99
100
			previewItem
101
					.setControl(previewViewer instanceof Viewer ? ((Viewer) previewViewer)
102
							.getControl()
103
							: previewViewer.getTextWidget());
104
105
			folder.addSelectionListener(new SelectionListener() {
106
				public void widgetDefaultSelected(SelectionEvent selectionevent) {
107
					widgetSelected(selectionevent);
108
				}
109
110
				public void widgetSelected(SelectionEvent selectionevent) {
111
					Document document = new Document(getDocument().get());
112
					previewViewer.setDocument(document);
113
				}
114
			});
115
		}
116
	}
117
118
	protected abstract SourceViewer createPreviewViewer(Composite parent,
119
			int style);
120
121
	public Control getParentControl() {
122
		return folder != null ? folder : this.getTextWidget().getParent();
123
	}
124
125
	@Override
126
	public void setDocument(IDocument document,
127
			IAnnotationModel annotationModel, int modelRangeOffset,
128
			int modelRangeLength) {
129
		configurePartitioning(document);
130
		super.setDocument(document, annotationModel, modelRangeOffset,
131
				modelRangeLength);
132
	}
133
134
	public MarkupLanguage getMarkupLanguage() {
135
		return markupLanguage;
136
	}
137
138
	public void setMarkupLanguage(MarkupLanguage markupLanguage) {
139
		this.markupLanguage = markupLanguage;
140
	}
141
}

Return to bug 235222