001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.multipageeditor;
011:
012: import java.io.StringWriter;
013: import java.text.Collator;
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.StringTokenizer;
017:
018: import org.eclipse.core.resources.IMarker;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.jface.dialogs.ErrorDialog;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.custom.StyledText;
023: import org.eclipse.swt.events.SelectionAdapter;
024: import org.eclipse.swt.events.SelectionEvent;
025: import org.eclipse.swt.graphics.Font;
026: import org.eclipse.swt.graphics.FontData;
027: import org.eclipse.swt.layout.FillLayout;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.layout.GridLayout;
030: import org.eclipse.swt.widgets.Button;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.swt.widgets.FontDialog;
033: import org.eclipse.ui.IEditorInput;
034: import org.eclipse.ui.IEditorPart;
035: import org.eclipse.ui.IEditorSite;
036: import org.eclipse.ui.IFileEditorInput;
037: import org.eclipse.ui.PartInitException;
038: import org.eclipse.ui.editors.text.TextEditor;
039: import org.eclipse.ui.ide.IDE;
040: import org.eclipse.ui.ide.IGotoMarker;
041: import org.eclipse.ui.part.MultiPageEditorPart;
042:
043: /**
044: * An example showing how to create a multi-page editor.
045: * This example has 3 pages:
046: * <ul>
047: * <li>page 0 contains a nested text editor.
048: * <li>page 1 allows you to change the font used in page 2
049: * <li>page 2 shows the words in page 0 in sorted order
050: * </ul>
051: */
052: public class MultiPageEditorExample extends MultiPageEditorPart
053: implements IGotoMarker {
054:
055: /** The text editor used in page 0. */
056: private TextEditor editor;
057:
058: /** The index of the page containing the text editor */
059: private int editorIndex = 0;
060:
061: /** The font chosen in page 1. */
062: private Font font;
063:
064: /** The text widget used in page 2. */
065: private StyledText text;
066:
067: /**
068: * Creates a multi-page editor example.
069: */
070: public MultiPageEditorExample() {
071: super ();
072: }
073:
074: /**
075: * Creates page 0 of the multi-page editor,
076: * which contains a text editor.
077: */
078: void createPage0() {
079: try {
080: editor = new TextEditor();
081: editorIndex = addPage(editor, getEditorInput());
082: setPageText(editorIndex, MessageUtil.getString("Source")); //$NON-NLS-1$
083: } catch (PartInitException e) {
084: ErrorDialog
085: .openError(
086: getSite().getShell(),
087: MessageUtil
088: .getString("ErrorCreatingNestedEditor"), null, e.getStatus()); //$NON-NLS-1$
089: }
090: }
091:
092: /**
093: * Creates page 1 of the multi-page editor,
094: * which allows you to change the font used in page 2.
095: */
096: void createPage1() {
097:
098: Composite composite = new Composite(getContainer(), SWT.NONE);
099: GridLayout layout = new GridLayout();
100: composite.setLayout(layout);
101: layout.numColumns = 2;
102:
103: Button fontButton = new Button(composite, SWT.NONE);
104: GridData gd = new GridData(GridData.BEGINNING);
105: gd.horizontalSpan = 2;
106: fontButton.setLayoutData(gd);
107: fontButton.setText(MessageUtil.getString("ChangeFont")); //$NON-NLS-1$
108:
109: fontButton.addSelectionListener(new SelectionAdapter() {
110: public void widgetSelected(SelectionEvent event) {
111: setFont();
112: }
113: });
114:
115: int index = addPage(composite);
116: setPageText(index, MessageUtil.getString("Properties")); //$NON-NLS-1$
117: }
118:
119: /**
120: * Creates page 2 of the multi-page editor,
121: * which shows the sorted text.
122: */
123: void createPage2() {
124: Composite composite = new Composite(getContainer(), SWT.NONE);
125: FillLayout layout = new FillLayout();
126: composite.setLayout(layout);
127: text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL);
128: text.setEditable(false);
129:
130: int index = addPage(composite);
131: setPageText(index, MessageUtil.getString("Preview")); //$NON-NLS-1$
132: }
133:
134: /**
135: * Creates the pages of the multi-page editor.
136: */
137: protected void createPages() {
138: createPage0();
139: createPage1();
140: createPage2();
141: }
142:
143: /**
144: * Saves the multi-page editor's document.
145: */
146: public void doSave(IProgressMonitor monitor) {
147: getEditor(0).doSave(monitor);
148: }
149:
150: /**
151: * Saves the multi-page editor's document as another file.
152: * Also updates the text for page 0's tab, and updates this multi-page editor's input
153: * to correspond to the nested editor's.
154: */
155: public void doSaveAs() {
156: IEditorPart editor = getEditor(0);
157: editor.doSaveAs();
158: setPageText(0, editor.getTitle());
159: setInput(editor.getEditorInput());
160: }
161:
162: /**
163: * The <code>MultiPageEditorExample</code> implementation of this method
164: * checks that the input is an instance of <code>IFileEditorInput</code>.
165: */
166: public void init(IEditorSite site, IEditorInput editorInput)
167: throws PartInitException {
168: if (!(editorInput instanceof IFileEditorInput))
169: throw new PartInitException(MessageUtil
170: .getString("InvalidInput")); //$NON-NLS-1$
171: super .init(site, editorInput);
172: }
173:
174: /* (non-Javadoc)
175: * Method declared on IEditorPart.
176: */
177: public boolean isSaveAsAllowed() {
178: return true;
179: }
180:
181: /**
182: * Calculates the contents of page 2 when the it is activated.
183: */
184: protected void pageChange(int newPageIndex) {
185: super .pageChange(newPageIndex);
186: if (newPageIndex == 2) {
187: sortWords();
188: }
189: }
190:
191: /**
192: * Sets the font related data to be applied to the text in page 2.
193: */
194: void setFont() {
195: FontDialog fontDialog = new FontDialog(getSite().getShell());
196: fontDialog.setFontList(text.getFont().getFontData());
197: FontData fontData = fontDialog.open();
198: if (fontData != null) {
199: if (font != null)
200: font.dispose();
201: font = new Font(text.getDisplay(), fontData);
202: text.setFont(font);
203: }
204: }
205:
206: /**
207: * Sorts the words in page 0, and shows them in page 2.
208: */
209: void sortWords() {
210:
211: String editorText = editor.getDocumentProvider().getDocument(
212: editor.getEditorInput()).get();
213:
214: StringTokenizer tokenizer = new StringTokenizer(editorText,
215: " \t\n\r\f!@#$%^&*()-_=+`~[]{};:'\",.<>/?|\\"); //$NON-NLS-1$
216: ArrayList editorWords = new ArrayList();
217: while (tokenizer.hasMoreTokens()) {
218: editorWords.add(tokenizer.nextToken());
219: }
220:
221: Collections.sort(editorWords, Collator.getInstance());
222: StringWriter displayText = new StringWriter();
223: for (int i = 0; i < editorWords.size(); i++) {
224: displayText.write(((String) editorWords.get(i)));
225: displayText.write("\n"); //$NON-NLS-1$
226: }
227: text.setText(displayText.toString());
228: }
229:
230: /* (non-Javadoc)
231: * @see org.eclipse.ui.ide.IGotoMarker
232: */
233: public void gotoMarker(IMarker marker) {
234: setActivePage(editorIndex);
235: IDE.gotoMarker(editor, marker);
236: }
237: }
|