1 package $packageName$; 2 3 4 import java.io.StringWriter ; 5 import java.text.Collator ; 6 import java.util.ArrayList ; 7 import java.util.Collections ; 8 import java.util.StringTokenizer ; 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 43 public class $editorClassName$ extends MultiPageEditorPart implements IResourceChangeListener{ 44 45 46 private TextEditor editor; 47 48 49 private Font font; 50 51 52 private StyledText text; 53 56 public $editorClassName$() { 57 super(); 58 ResourcesPlugin.getWorkspace().addResourceChangeListener(this); 59 } 60 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 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 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 120 protected void createPages() { 121 createPage0(); 122 createPage1(); 123 createPage2(); 124 } 125 130 public void dispose() { 131 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 132 super.dispose(); 133 } 134 137 public void doSave(IProgressMonitor monitor) { 138 getEditor(0).doSave(monitor); 139 } 140 145 public void doSaveAs() { 146 IEditorPart editor = getEditor(0); 147 editor.doSaveAs(); 148 setPageText(0, editor.getTitle()); 149 setInput(editor.getEditorInput()); 150 } 151 154 public void gotoMarker(IMarker marker) { 155 setActivePage(0); 156 getEditor(0).gotoMarker(marker); 157 } 158 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 171 public boolean isSaveAsAllowed() { 172 return true; 173 } 174 177 protected void pageChange(int newPageIndex) { 178 super.pageChange(newPageIndex); 179 if (newPageIndex == 2) { 180 sortWords(); 181 } 182 } 183 186 public void resourceChanged(final IResourceChangeEvent event){ 187 if(event.getType() == IResourceChangeEvent.PRE_CLOSE){ 188 Display.getDefault().asyncExec(new Runnable (){ 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 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 218 void sortWords() { 219 220 String editorText = 221 editor.getDocumentProvider().getDocument(editor.getEditorInput()).get(); 222 223 StringTokenizer tokenizer = 224 new StringTokenizer (editorText, " \t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\"); 225 ArrayList editorWords = new ArrayList (); 226 while (tokenizer.hasMoreTokens()) { 227 editorWords.add(tokenizer.nextToken()); 228 } 229 230 Collections.sort(editorWords, Collator.getInstance()); 231 StringWriter displayText = new StringWriter (); 232 for (int i = 0; i < editorWords.size(); i++) { 233 displayText.write(((String ) editorWords.get(i))); 234 displayText.write(System.getProperty("line.separator")); 235 } 236 text.setText(displayText.toString()); 237 } 238 } 239 | Popular Tags |