KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > $packageName$ > $editorClassName$


1 package $packageName$;
2
3
4 import java.io.StringWriter JavaDoc;
5 import java.text.Collator JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.eclipse.core.resources.IMarker;
11 import org.eclipse.core.resources.IResourceChangeEvent;
12 import org.eclipse.core.resources.IResourceChangeListener;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.dialogs.ErrorDialog;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.StyledText;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.graphics.FontData;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.FontDialog;
29 import org.eclipse.ui.*;
30 import org.eclipse.ui.editors.text.TextEditor;
31 import org.eclipse.ui.part.FileEditorInput;
32 import org.eclipse.ui.part.MultiPageEditorPart;
33
34 /**
35  * An example showing how to create a multi-page editor.
36  * This example has 3 pages:
37  * <ul>
38  * <li>page 0 contains a nested text editor.
39  * <li>page 1 allows you to change the font used in page 2
40  * <li>page 2 shows the words in page 0 in sorted order
41  * </ul>
42  */

43 public class $editorClassName$ extends MultiPageEditorPart implements IResourceChangeListener{
44
45     /** The text editor used in page 0. */
46     private TextEditor editor;
47
48     /** The font chosen in page 1. */
49     private Font font;
50
51     /** The text widget used in page 2. */
52     private StyledText text;
53     /**
54      * Creates a multi-page editor example.
55      */

56     public $editorClassName$() {
57         super();
58         ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
59     }
60     /**
61      * Creates page 0 of the multi-page editor,
62      * which contains a text editor.
63      */

64     void createPage0() {
65         try {
66             editor = new TextEditor();
67             int index = addPage(editor, getEditorInput());
68             setPageText(index, editor.getTitle());
69         } catch (PartInitException e) {
70             ErrorDialog.openError(
71                 getSite().getShell(),
72                 "Error creating nested text editor",
73                 null,
74                 e.getStatus());
75         }
76     }
77     /**
78      * Creates page 1 of the multi-page editor,
79      * which allows you to change the font used in page 2.
80      */

81     void createPage1() {
82
83         Composite composite = new Composite(getContainer(), SWT.NONE);
84         GridLayout layout = new GridLayout();
85         composite.setLayout(layout);
86         layout.numColumns = 2;
87
88         Button fontButton = new Button(composite, SWT.NONE);
89         GridData gd = new GridData(GridData.BEGINNING);
90         gd.horizontalSpan = 2;
91         fontButton.setLayoutData(gd);
92         fontButton.setText("Change Font...");
93         
94         fontButton.addSelectionListener(new SelectionAdapter() {
95             public void widgetSelected(SelectionEvent event) {
96                 setFont();
97             }
98         });
99
100         int index = addPage(composite);
101         setPageText(index, "Properties");
102     }
103     /**
104      * Creates page 2 of the multi-page editor,
105      * which shows the sorted text.
106      */

107     void createPage2() {
108         Composite composite = new Composite(getContainer(), SWT.NONE);
109         FillLayout layout = new FillLayout();
110         composite.setLayout(layout);
111         text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL);
112         text.setEditable(false);
113
114         int index = addPage(composite);
115         setPageText(index, "Preview");
116     }
117     /**
118      * Creates the pages of the multi-page editor.
119      */

120     protected void createPages() {
121         createPage0();
122         createPage1();
123         createPage2();
124     }
125     /**
126      * The <code>MultiPageEditorPart</code> implementation of this
127      * <code>IWorkbenchPart</code> method disposes all nested editors.
128      * Subclasses may extend.
129      */

130     public void dispose() {
131         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
132         super.dispose();
133     }
134     /**
135      * Saves the multi-page editor's document.
136      */

137     public void doSave(IProgressMonitor monitor) {
138         getEditor(0).doSave(monitor);
139     }
140     /**
141      * Saves the multi-page editor's document as another file.
142      * Also updates the text for page 0's tab, and updates this multi-page editor's input
143      * to correspond to the nested editor's.
144      */

145     public void doSaveAs() {
146         IEditorPart editor = getEditor(0);
147         editor.doSaveAs();
148         setPageText(0, editor.getTitle());
149         setInput(editor.getEditorInput());
150     }
151     /* (non-Javadoc)
152      * Method declared on IEditorPart
153      */

154     public void gotoMarker(IMarker marker) {
155         setActivePage(0);
156         getEditor(0).gotoMarker(marker);
157     }
158     /**
159      * The <code>MultiPageEditorExample</code> implementation of this method
160      * checks that the input is an instance of <code>IFileEditorInput</code>.
161      */

162     public void init(IEditorSite site, IEditorInput editorInput)
163         throws PartInitException {
164         if (!(editorInput instanceof IFileEditorInput))
165             throw new PartInitException("Invalid Input: Must be IFileEditorInput");
166         super.init(site, editorInput);
167     }
168     /* (non-Javadoc)
169      * Method declared on IEditorPart.
170      */

171     public boolean isSaveAsAllowed() {
172         return true;
173     }
174     /**
175      * Calculates the contents of page 2 when the it is activated.
176      */

177     protected void pageChange(int newPageIndex) {
178         super.pageChange(newPageIndex);
179         if (newPageIndex == 2) {
180             sortWords();
181         }
182     }
183     /**
184      * Closes all project files on project close.
185      */

186     public void resourceChanged(final IResourceChangeEvent event){
187         if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
188             Display.getDefault().asyncExec(new Runnable JavaDoc(){
189                 public void run(){
190                     IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
191                     for (int i = 0; i<pages.length; i++){
192                         if(((FileEditorInput)editor.getEditorInput()).getFile().getProject().equals(event.getResource())){
193                             IEditorPart editorPart = pages[i].findEditor((FileEditorInput)editor.getEditorInput());
194                             pages[i].closeEditor(editorPart,true);
195                         }
196                     }
197                 }
198             });
199         }
200     }
201     /**
202      * Sets the font related data to be applied to the text in page 2.
203      */

204     void setFont() {
205         FontDialog fontDialog = new FontDialog(getSite().getShell());
206         fontDialog.setFontList(text.getFont().getFontData());
207         FontData fontData = fontDialog.open();
208         if (fontData != null) {
209             if (font != null)
210                 font.dispose();
211             font = new Font(text.getDisplay(), fontData);
212             text.setFont(font);
213         }
214     }
215     /**
216      * Sorts the words in page 0, and shows them in page 2.
217      */

218     void sortWords() {
219
220         String JavaDoc editorText =
221             editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();
222
223         StringTokenizer JavaDoc tokenizer =
224             new StringTokenizer JavaDoc(editorText, " \t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\");
225         ArrayList JavaDoc editorWords = new ArrayList JavaDoc();
226         while (tokenizer.hasMoreTokens()) {
227             editorWords.add(tokenizer.nextToken());
228         }
229
230         Collections.sort(editorWords, Collator.getInstance());
231         StringWriter JavaDoc displayText = new StringWriter JavaDoc();
232         for (int i = 0; i < editorWords.size(); i++) {
233             displayText.write(((String JavaDoc) editorWords.get(i)));
234             displayText.write(System.getProperty("line.separator"));
235         }
236         text.setText(displayText.toString());
237     }
238 }
239
Popular Tags