KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > mapper > editors > ReverseEngineeringMultiPageEditor


1 package org.hibernate.eclipse.mapper.editors;
2
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 import org.eclipse.core.resources.IResource;
8 import org.eclipse.core.resources.IResourceStatus;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.NullProgressMonitor;
13 import org.eclipse.core.runtime.Status;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.swt.events.ShellAdapter;
17 import org.eclipse.swt.events.ShellEvent;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IFileEditorInput;
23 import org.eclipse.ui.IPartListener;
24 import org.eclipse.ui.IStorageEditorInput;
25 import org.eclipse.ui.IWorkbench;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.forms.editor.FormEditor;
31 import org.eclipse.ui.forms.editor.IFormPage;
32 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
33 import org.eclipse.wst.sse.core.internal.provisional.exceptions.SourceEditingRuntimeException;
34 import org.eclipse.wst.sse.ui.internal.StructuredTextEditor;
35 import org.eclipse.wst.xml.core.internal.provisional.IXMLPreferenceNames;
36 import org.eclipse.wst.xml.ui.internal.provisional.StructuredTextEditorXML;
37 import org.hibernate.eclipse.mapper.MapperPlugin;
38 import org.hibernate.eclipse.mapper.editors.reveng.OverrideFormPage;
39 import org.hibernate.eclipse.mapper.editors.reveng.OverviewFormPage;
40 import org.hibernate.eclipse.mapper.editors.reveng.PreviewMappingFormPage;
41 import org.hibernate.eclipse.mapper.editors.reveng.SQLTypeMappingFormPage;
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44
45 /**
46  * An example showing how to create a multi-page editor.
47  * This example has 3 pages:
48  * <ul>
49  * <li>page 0 contains a nested text editor.
50  * <li>page 1 allows you to change the font used in page 2
51  * <li>page 2 shows the words in page 0 in sorted order
52  * </ul>
53  */

54 public class ReverseEngineeringMultiPageEditor extends FormEditor {
55
56     /**
57      * Internal part activation listener
58      */

59     class PartListener extends ShellAdapter implements IPartListener {
60         private IWorkbenchPart fActivePart;
61         private boolean fIsHandlingActivation = false;
62
63         private void handleActivation() {
64
65             if (fIsHandlingActivation)
66                 return;
67
68             if (fActivePart == ReverseEngineeringMultiPageEditor.this) {
69                 fIsHandlingActivation = true;
70                 try {
71                     safelySanityCheckState();
72                 } finally {
73                     fIsHandlingActivation = false;
74                 }
75             }
76         }
77
78         /**
79          * @see IPartListener#partActivated(IWorkbenchPart)
80          */

81         public void partActivated(IWorkbenchPart part) {
82             fActivePart = part;
83             handleActivation();
84         }
85
86         /**
87          * @see IPartListener#partBroughtToTop(IWorkbenchPart)
88          */

89         public void partBroughtToTop(IWorkbenchPart part) {
90         }
91
92         /**
93          * @see IPartListener#partClosed(IWorkbenchPart)
94          */

95         public void partClosed(IWorkbenchPart part) {
96         }
97
98         /**
99          * @see IPartListener#partDeactivated(IWorkbenchPart)
100          */

101         public void partDeactivated(IWorkbenchPart part) {
102             fActivePart = null;
103         }
104
105         /**
106          * @see IPartListener#partOpened(IWorkbenchPart)
107          */

108         public void partOpened(IWorkbenchPart part) {
109         }
110
111         /*
112          * @see ShellListener#shellActivated(ShellEvent)
113          */

114         public void shellActivated(ShellEvent e) {
115             handleActivation();
116         }
117     }
118     
119     /** The source page index. */
120     private int fSourcePageIndex;
121     /** The text editor. */
122     private StructuredTextEditor fTextEditor;
123
124     private IFormPage overviewPage;
125     private IFormPage typeMappingPage;
126     private IFormPage previewMappingPage;
127     private IFormPage overridePage;
128
129
130     /**
131      * StructuredTextMultiPageEditorPart constructor comment.
132      */

133     public ReverseEngineeringMultiPageEditor() {
134         super();
135     }
136
137     protected int getDefaultPageIndex() {
138         return overviewPage.getIndex();
139     }
140     
141     /*
142      * This method is just to make firePropertyChanged accessbible from some
143      * (anonomous) inner classes.
144      */

145     protected void _firePropertyChange(int property) {
146         //super.firePropertyChange(property);
147
}
148
149     /**
150      * Adds the source page of the multi-page editor.
151      */

152     protected void addSourcePage() {
153         try {
154             fSourcePageIndex = addPage(fTextEditor, getEditorInput());
155             setPageText(fSourcePageIndex, "source");
156         } catch (PartInitException exception) {
157             // dispose editor
158
dispose();
159             MapperPlugin.getDefault().logException(exception);
160             throw new SourceEditingRuntimeException(exception, "An error has occurred when {1}");
161         }
162     }
163
164     
165     protected void addPages() {
166         try {
167             // source page MUST be created before design page, now
168
createSourcePage();
169             createAndAddFormPages();
170             addSourcePage();
171
172             setActivePage();
173
174             // future_TODO: add a catch block here for any exception the
175
// design
176
// page throws and convert it into a more informative message.
177
} catch (PartInitException e) {
178             MapperPlugin.getDefault().logException(e);
179             throw new RuntimeException JavaDoc(e);
180         }
181     }
182
183     private void createAndAddFormPages() throws PartInitException {
184         overviewPage = new OverviewFormPage(this);
185         addPage(overviewPage);
186         
187         typeMappingPage = new SQLTypeMappingFormPage(this);
188         addPage(typeMappingPage);
189         
190         overridePage = new OverrideFormPage(this);
191         addPage(overridePage);
192         
193         previewMappingPage = new PreviewMappingFormPage(this);
194         addPage(previewMappingPage);
195         
196     }
197
198     /**
199      * Creates the source page of the multi-page editor.
200      */

201     protected void createSourcePage() {
202         fTextEditor = createTextEditor();
203         fTextEditor.setEditorPart(this);
204         //fTextEditor.addPropertyListener(this);
205
}
206
207     /**
208      * Method createTextEditor.
209      *
210      * @return StructuredTextEditor
211      */

212     protected StructuredTextEditor createTextEditor() {
213         return new StructuredTextEditorXML();
214     }
215
216     /*
217      * (non-Javadoc) Saves the contents of this editor. <p> Subclasses must
218      * override this method to implement the open-save-close lifecycle for an
219      * editor. For greater details, see <code> IEditorPart </code></p>
220      *
221      * @see IEditorPart
222      */

223     public void doSave(IProgressMonitor monitor) {
224         fTextEditor.doSave(monitor);
225     }
226
227     /*
228      * (non-Javadoc) Saves the contents of this editor to another object. <p>
229      * Subclasses must override this method to implement the open-save-close
230      * lifecycle for an editor. For greater details, see <code> IEditorPart
231      * </code></p>
232      *
233      * @see IEditorPart
234      */

235     public void doSaveAs() {
236         fTextEditor.doSaveAs();
237         // 253619
238
// following used to be executed here, but is
239
// now called "back" from text editor (since
240
// mulitiple paths to the performSaveAs in StructuredTextEditor.
241
//doSaveAsForStructuredTextMulitPagePart();
242
}
243
244     private void editorInputIsAcceptable(IEditorInput input) throws PartInitException {
245         if (input instanceof IFileEditorInput) {
246             // verify that it can be opened
247
CoreException[] coreExceptionArray = new CoreException[1];
248             if (fileDoesNotExist((IFileEditorInput) input, coreExceptionArray)) {
249                 CoreException coreException = coreExceptionArray[0];
250                 if (coreException.getStatus().getCode() == IResourceStatus.FAILED_READ_LOCAL) {
251                     // I'm assuming this is always 'does not exist'
252
// we'll refresh local go mimic behavior of default
253
// editor, where the
254
// troublesome file is refreshed (and will cause it to
255
// 'disappear' from Navigator.
256
try {
257                         ((IFileEditorInput) input).getFile().refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
258                     } catch (CoreException ce) {
259                         // very unlikely
260
MapperPlugin.getDefault().logException(ce);
261                     }
262                     throw new PartInitException("Resource does not exist" + input.getName());
263                 } else {
264                     throw new PartInitException("Editor could not be open" + input.getName());
265                 }
266             }
267         } else if (input instanceof IStorageEditorInput) {
268             InputStream JavaDoc contents = null;
269             try {
270                 contents = ((IStorageEditorInput) input).getStorage().getContents();
271             } catch (CoreException noStorageExc) {
272             }
273             if (contents == null) {
274                 throw new PartInitException("Editor could not be open on " + input.getName());
275             } else {
276                 try {
277                     contents.close();
278                 } catch (IOException JavaDoc e) {
279                 }
280             }
281         }
282     }
283
284     
285     /*
286      * (non-Javadoc) Initializes the editor part with a site and input. <p>
287      * Subclasses of <code> EditorPart </code> must implement this method.
288      * Within the implementation subclasses should verify that the input type
289      * is acceptable and then save the site and input. Here is sample code:
290      * </p><pre> if (!(input instanceof IFileEditorInput)) throw new
291      * PartInitException("Invalid Input: Must be IFileEditorInput");
292      * setSite(site); setInput(editorInput); </pre>
293      */

294     protected boolean fileDoesNotExist(IFileEditorInput input, Throwable JavaDoc[] coreException) {
295         boolean result = false;
296         InputStream JavaDoc inStream = null;
297         if ((!(input.exists())) || (!(input.getFile().exists()))) {
298             result = true;
299         } else {
300             try {
301                 inStream = input.getFile().getContents(true);
302             } catch (CoreException e) {
303                 // very likely to be file not found
304
result = true;
305                 coreException[0] = e;
306             } finally {
307                 if (input != null) {
308                     try {
309                         if (inStream != null) {
310                             inStream.close();
311                         }
312                     } catch (IOException JavaDoc e) {
313                         MapperPlugin.getDefault().logException(e);
314                     }
315                 }
316             }
317         }
318         return result;
319     }
320
321     public Object JavaDoc getAdapter(Class JavaDoc key) {
322         Object JavaDoc result = null;
323         
324             // DMW: I'm bullet-proofing this because
325
// its been reported (on 4.03 version) a null pointer sometimes
326
// happens here on startup, when an editor has been left
327
// open when workbench shutdown.
328
if (fTextEditor != null) {
329                 result = fTextEditor.getAdapter(key);
330             }
331         
332         return result;
333     }
334
335     /**
336      * IExtendedMarkupEditor method
337      */

338     public Node JavaDoc getCaretNode() {
339         if (getTextEditor() == null)
340             return null;
341
342         return getTextEditor().getCaretNode();
343     }
344
345     /**
346      * IExtendedSimpleEditor method
347      */

348     public int getCaretPosition() {
349         if (getTextEditor() == null)
350             return -1;
351
352         return getTextEditor().getCaretPosition();
353     }
354
355     /**
356      * IExtendedSimpleEditor method
357      */

358     public IDocument getDocument() {
359         if (getTextEditor() == null)
360             return null;
361
362         return getTextEditor().getDocument();
363     }
364
365     /**
366      * IExtendedMarkupEditor method
367      */

368     public Document getDOMDocument() {
369         if (getTextEditor() == null)
370             return null;
371
372         return getTextEditor().getDOMDocument();
373     }
374
375     /**
376      * IExtendedSimpleEditor method
377      */

378     public IEditorPart getEditorPart() {
379         return this;
380     }
381
382     protected IStructuredModel getModel() {
383         IStructuredModel model = null;
384         if (fTextEditor != null)
385             model = fTextEditor.getModel();
386         return model;
387     }
388
389     protected IPreferenceStore getPreferenceStore() {
390         return MapperPlugin.getDefault().getPreferenceStore();
391     }
392
393     public StructuredTextEditor getTextEditor() {
394         return fTextEditor;
395     }
396
397
398 /* public void init(IEditorSite site, IEditorInput input) throws PartInitException {
399         editorInputIsAcceptable(input);
400         try {
401             super.init(site, input);
402             if (partListener == null) {
403                 partListener = new PartListener();
404             }
405             //getSite().getPage().addPartListener(partListner);
406             // we want to listen for our own activation
407             IWorkbenchWindow window = getSite().getWorkbenchWindow();
408             window.getPartService().addPartListener(partListener);
409             window.getShell().addShellListener(partListener);
410         } catch (Exception e) {
411             if (e instanceof SourceEditingRuntimeException) {
412                 Throwable t = ((SourceEditingRuntimeException) e).getOriginalException();
413                 if (t instanceof IOException) {
414                     System.out.println(t);
415                     // file not found
416                 }
417             }
418         }
419         setPartName(input.getName());
420     }*/

421
422     /*
423      * (non-Javadoc) Returns whether the "save as" operation is supported by
424      * this editor. <p> Subclasses must override this method to implement the
425      * open-save-close lifecycle for an editor. For greater details, see
426      * <code> IEditorPart </code></p>
427      *
428      * @see IEditorPart
429      */

430     public boolean isSaveAsAllowed() {
431         return fTextEditor != null && fTextEditor.isSaveAsAllowed();
432     }
433
434     /*
435      * (non-Javadoc) Returns whether the contents of this editor should be
436      * saved when the editor is closed. <p> This method returns <code> true
437      * </code> if and only if the editor is dirty ( <code> isDirty </code> ).
438      * </p>
439      */

440     public boolean isSaveOnCloseNeeded() {
441         // overriding super class since it does a lowly isDirty!
442
if (fTextEditor != null)
443             return fTextEditor.isSaveOnCloseNeeded();
444         return isDirty();
445     }
446
447     /**
448      * Notifies this multi-page editor that the page with the given id has
449      * been activated. This method is called when the user selects a different
450      * tab.
451      *
452      * @param newPageIndex
453      * the index of the activated page
454      */

455     protected void pageChange(int newPageIndex) {
456         super.pageChange(newPageIndex);
457         saveLastActivePageIndex(newPageIndex);
458     }
459
460     /**
461      * Posts the update code "behind" the running operation.
462      */

463     protected void postOnDisplayQue(Runnable JavaDoc runnable) {
464         IWorkbench workbench = PlatformUI.getWorkbench();
465         IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
466         if (windows != null && windows.length > 0) {
467             Display display = windows[0].getShell().getDisplay();
468             display.asyncExec(runnable);
469         } else
470             runnable.run();
471     }
472
473
474     protected void safelySanityCheckState() {
475         // If we're called before editor is created, simply ignore since we
476
// delegate this function to our embedded TextEditor
477
if (getTextEditor() == null)
478             return;
479
480         getTextEditor().safelySanityCheckState(getEditorInput());
481
482     }
483
484     protected void saveLastActivePageIndex(int newPageIndex) {
485         // save the last active page index to preference manager
486
getPreferenceStore().setValue(IXMLPreferenceNames.LAST_ACTIVE_PAGE, newPageIndex);
487     }
488
489     /**
490      * Sets the currently active page.
491      */

492     protected void setActivePage() {
493         // retrieve the last active page index from preference manager
494
int activePageIndex = getPreferenceStore().getInt(IXMLPreferenceNames.LAST_ACTIVE_PAGE);
495
496         // We check this range since someone could hand edit the XML
497
// preference file to an invalid value ... which I know from
498
// experience :( ... if they do, we'll reset to default and continue
499
// rather than throw an assertion error in the setActivePage(int)
500
// method.
501
if (activePageIndex < 0 || activePageIndex >= getPageCount()) {
502             activePageIndex = getDefaultPageIndex();
503         }
504         setActivePage(activePageIndex);
505     }
506
507     /*
508      * (non-Javadoc)
509      *
510      * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
511      */

512     protected void setInput(IEditorInput input) {
513         // If driven from the Source page, it's "model" may not be up to date
514
// with the input just yet. We'll rely on later notification from the
515
// TextViewer to set us straight
516
super.setInput(input);
517         /*if (fDesignViewer != null)
518             fDesignViewer.setModel(getModel());*/

519         setPartName(input.getName());
520     }
521
522     /**
523      * IExtendedMarkupEditor method
524      */

525     public IStatus validateEdit(Shell context) {
526         if (getTextEditor() == null)
527             return new Status(IStatus.ERROR, MapperPlugin.ID, IStatus.INFO, "", null); //$NON-NLS-1$
528

529         return getTextEditor().validateEdit(context);
530     }
531 }
532
Popular Tags