1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
package multipageeditor.editors;
import java.io.StringWriter;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.ui.*;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.ide.IDE;
/**
* An example showing how to create a multi-page editor.
* This example has 3 pages:
* <ul>
* <li>page 0 contains a nested text editor.
* <li>page 1 allows you to change the font used in page 2
* <li>page 2 shows the words in page 0 in sorted order
* </ul>
*/
public class MultiPageEditor extends MultiPageEditorPart implements IResourceChangeListener{
/** The text editor used in page 0. */
private TextEditor editor;
/** The font chosen in page 1. */
private Font font;
/** The text widget used in page 2. */
private StyledText text;
/**
* Creates a multi-page editor example.
*/
public MultiPageEditor() {
super();
ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
}
/**
* Creates page 0 of the multi-page editor,
* which contains a text editor.
*/
void createPage0() {
try {
editor = new TextEditor();
int index = addPage(editor, getEditorInput());
setPageText(index, editor.getTitle());
} catch (PartInitException e) {
ErrorDialog.openError(
getSite().getShell(),
"Error creating nested text editor",
null,
e.getStatus());
}
}
/**
* Creates page 1 of the multi-page editor,
* which allows you to change the font used in page 2.
*/
void createPage1() {
Composite composite = new Composite(getContainer(), SWT.NONE);
GridLayout layout = new GridLayout();
composite.setLayout(layout);
layout.numColumns = 2;
Button fontButton = new Button(composite, SWT.NONE);
GridData gd = new GridData(GridData.BEGINNING);
gd.horizontalSpan = 2;
fontButton.setLayoutData(gd);
fontButton.setText("Change Font...");
fontButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
setFont();
}
});
int index = addPage(composite);
setPageText(index, "Properties");
}
/**
* Creates page 2 of the multi-page editor,
* which shows the sorted text.
*/
void createPage2() {
Composite composite = new Composite(getContainer(), SWT.NONE);
FillLayout layout = new FillLayout();
composite.setLayout(layout);
text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL);
text.setEditable(false);
int index = addPage(composite);
setPageText(index, "Preview");
}
/**
* Creates the pages of the multi-page editor.
*/
protected void createPages() {
createPage0();
createPage1();
createPage2();
}
/**
* The <code>MultiPageEditorPart</code> implementation of this
* <code>IWorkbenchPart</code> method disposes all nested editors.
* Subclasses may extend.
*/
public void dispose() {
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
super.dispose();
}
/**
* Saves the multi-page editor's document.
*/
public void doSave(IProgressMonitor monitor) {
getEditor(0).doSave(monitor);
}
/**
* Saves the multi-page editor's document as another file.
* Also updates the text for page 0's tab, and updates this multi-page editor's input
* to correspond to the nested editor's.
*/
public void doSaveAs() {
IEditorPart editor = getEditor(0);
editor.doSaveAs();
setPageText(0, editor.getTitle());
setInput(editor.getEditorInput());
}
/* (non-Javadoc)
* Method declared on IEditorPart
*/
public void gotoMarker(IMarker marker) {
setActivePage(0);
IDE.gotoMarker(getEditor(0), marker);
}
/**
* The <code>MultiPageEditorExample</code> implementation of this method
* checks that the input is an instance of <code>IFileEditorInput</code>.
*/
public void init(IEditorSite site, IEditorInput editorInput)
throws PartInitException {
if (!(editorInput instanceof IFileEditorInput))
throw new PartInitException("Invalid Input: Must be IFileEditorInput");
super.init(site, editorInput);
}
/* (non-Javadoc)
* Method declared on IEditorPart.
*/
public boolean isSaveAsAllowed() {
return true;
}
/**
* Calculates the contents of page 2 when the it is activated.
*/
protected void pageChange(int newPageIndex) {
super.pageChange(newPageIndex);
if (newPageIndex == 2) {
sortWords();
}
}
/**
* Closes all project files on project close.
*/
public void resourceChanged(final IResourceChangeEvent event){
if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
Display.getDefault().asyncExec(new Runnable(){
public void run(){
IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
for (int i = 0; i<pages.length; i++){
if(((FileEditorInput)editor.getEditorInput()).getFile().getProject().equals(event.getResource())){
IEditorPart editorPart = pages[i].findEditor(editor.getEditorInput());
pages[i].closeEditor(editorPart,true);
}
}
}
});
}
}
/**
* Sets the font related data to be applied to the text in page 2.
*/
void setFont() {
FontDialog fontDialog = new FontDialog(getSite().getShell());
fontDialog.setFontList(text.getFont().getFontData());
FontData fontData = fontDialog.open();
if (fontData != null) {
if (font != null)
font.dispose();
font = new Font(text.getDisplay(), fontData);
text.setFont(font);
}
}
/**
* Sorts the words in page 0, and shows them in page 2.
*/
void sortWords() {
String editorText =
editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();
StringTokenizer tokenizer =
new StringTokenizer(editorText, " \t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\");
ArrayList editorWords = new ArrayList();
while (tokenizer.hasMoreTokens()) {
editorWords.add(tokenizer.nextToken());
}
Collections.sort(editorWords, Collator.getInstance());
StringWriter displayText = new StringWriter();
for (int i = 0; i < editorWords.size(); i++) {
displayText.write(((String) editorWords.get(i)));
displayText.write(System.getProperty("line.separator"));
}
text.setText(displayText.toString());
}
} |
Partager