KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > PDESourcePage


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 /*
12  * Created on Jan 26, 2004
13  *
14  * To change the template for this generated file go to
15  * Window - Preferences - Java - Code Generation - Code and Comments
16  */

17 package org.eclipse.pde.internal.ui.editor;
18
19 import java.util.ResourceBundle JavaDoc;
20
21 import org.eclipse.pde.core.IBaseModel;
22 import org.eclipse.pde.internal.core.text.AbstractEditingModel;
23 import org.eclipse.pde.internal.core.text.IDocumentNode;
24 import org.eclipse.pde.internal.core.text.IDocumentRange;
25 import org.eclipse.pde.internal.core.text.IEditingModel;
26 import org.eclipse.pde.internal.ui.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.pde.internal.ui.PDEPluginImages;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.editor.actions.FormatAction;
31 import org.eclipse.pde.internal.ui.editor.actions.HyperlinkAction;
32 import org.eclipse.pde.internal.ui.editor.actions.PDEActionConstants;
33 import org.eclipse.pde.internal.ui.editor.context.InputContext;
34 import org.eclipse.pde.internal.ui.editor.outline.IOutlineContentCreator;
35 import org.eclipse.pde.internal.ui.editor.outline.IOutlineSelectionHandler;
36 import org.eclipse.pde.internal.ui.editor.plugin.ExtensionHyperLink;
37 import org.eclipse.pde.internal.ui.editor.text.PDESelectAnnotationRulerAction;
38
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42
43 import org.eclipse.core.runtime.CoreException;
44
45 import org.eclipse.core.resources.IMarker;
46
47 import org.eclipse.jface.action.IAction;
48 import org.eclipse.jface.action.IMenuManager;
49 import org.eclipse.jface.preference.IPreferenceStore;
50 import org.eclipse.jface.viewers.ILabelProvider;
51 import org.eclipse.jface.viewers.IPostSelectionProvider;
52 import org.eclipse.jface.viewers.ISelection;
53 import org.eclipse.jface.viewers.ISelectionChangedListener;
54 import org.eclipse.jface.viewers.ISelectionProvider;
55 import org.eclipse.jface.viewers.IStructuredSelection;
56 import org.eclipse.jface.viewers.ITreeContentProvider;
57 import org.eclipse.jface.viewers.SelectionChangedEvent;
58 import org.eclipse.jface.viewers.StructuredSelection;
59 import org.eclipse.jface.viewers.ViewerComparator;
60
61 import org.eclipse.jface.text.IDocument;
62 import org.eclipse.jface.text.ITextSelection;
63 import org.eclipse.jface.text.source.ISourceViewer;
64
65 import org.eclipse.ui.editors.text.EditorsUI;
66 import org.eclipse.ui.editors.text.TextEditor;
67
68 import org.eclipse.ui.PlatformUI;
69 import org.eclipse.ui.forms.IManagedForm;
70 import org.eclipse.ui.forms.editor.FormEditor;
71 import org.eclipse.ui.forms.editor.IFormPage;
72 import org.eclipse.ui.ide.IDE;
73 import org.eclipse.ui.ide.IGotoMarker;
74 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
75 import org.eclipse.ui.texteditor.ContentAssistAction;
76 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
77 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
78 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
79 import org.eclipse.ui.texteditor.ResourceAction;
80 import org.eclipse.ui.texteditor.TextOperationAction;
81
82 public abstract class PDESourcePage extends TextEditor implements IFormPage,
83         IGotoMarker, ISelectionChangedListener, IOutlineContentCreator,
84         IOutlineSelectionHandler {
85     
86     private static String JavaDoc RES_BUNDLE_LOCATION = "org.eclipse.pde.internal.ui.editor.text.ConstructedPDEEditorMessages"; //$NON-NLS-1$
87
private static ResourceBundle JavaDoc fgBundleForConstructedKeys = ResourceBundle.getBundle(RES_BUNDLE_LOCATION);
88     public static ResourceBundle JavaDoc getBundleForConstructedKeys() {
89         return fgBundleForConstructedKeys;
90     }
91     
92     /* (non-Javadoc)
93      * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes()
94      */

95     protected void initializeKeyBindingScopes() {
96         setKeyBindingScopes(new String JavaDoc[] { "org.eclipse.pde.ui.pdeEditorContext" }); //$NON-NLS-1$
97
}
98     
99     /**
100      * Updates the OutlinePage selection and this editor's range indicator.
101      *
102      * @since 3.0
103      */

104     private class PDESourcePageChangedListener implements
105             ISelectionChangedListener {
106
107         /**
108          * Installs this selection changed listener with the given selection
109          * provider. If the selection provider is a post selection provider,
110          * post selection changed events are the preferred choice, otherwise
111          * normal selection changed events are requested.
112          *
113          * @param selectionProvider
114          */

115         public void install(ISelectionProvider selectionProvider) {
116             if (selectionProvider != null) {
117                 if (selectionProvider instanceof IPostSelectionProvider) {
118                     IPostSelectionProvider provider = (IPostSelectionProvider) selectionProvider;
119                     provider.addPostSelectionChangedListener(this);
120                 } else {
121                     selectionProvider.addSelectionChangedListener(this);
122                 }
123             }
124         }
125
126         /*
127          * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
128          */

129         public void selectionChanged(SelectionChangedEvent event) {
130             handleSelectionChangedSourcePage(event);
131         }
132
133         /**
134          * Removes this selection changed listener from the given selection
135          * provider.
136          *
137          * @param selectionProviderstyle
138          */

139         public void uninstall(ISelectionProvider selectionProvider) {
140             if (selectionProvider != null) {
141                 if (selectionProvider instanceof IPostSelectionProvider) {
142                     IPostSelectionProvider provider = (IPostSelectionProvider) selectionProvider;
143                     provider.removePostSelectionChangedListener(this);
144                 } else {
145                     selectionProvider.removeSelectionChangedListener(this);
146                 }
147             }
148         }
149
150     }
151
152     /**
153      * The editor selection changed listener.
154      *
155      * @since 3.0
156      */

157     private PDESourcePageChangedListener fEditorSelectionChangedListener;
158     private PDEFormEditor fEditor;
159     private Control fControl;
160     private int fIndex;
161     private String JavaDoc fId;
162     private InputContext fInputContext;
163     private ISortableContentOutlinePage fOutlinePage;
164     private ISelectionChangedListener fOutlineSelectionChangedListener;
165     protected Object JavaDoc fSelection;
166
167     public PDESourcePage(PDEFormEditor editor, String JavaDoc id, String JavaDoc title) {
168         fId = id;
169         initialize(editor);
170         IPreferenceStore[] stores = new IPreferenceStore[2];
171         stores[0] = PDEPlugin.getDefault().getPreferenceStore();
172         stores[1] = EditorsUI.getPreferenceStore();
173         setPreferenceStore(new ChainedPreferenceStore(stores));
174         setRangeIndicator(new DefaultRangeIndicator());
175         if (isSelectionListener())
176             getEditor().getSite().getSelectionProvider().addSelectionChangedListener(this);
177     }
178     
179     /* (non-Javadoc)
180      * @see org.eclipse.ui.forms.editor.IFormPage#initialize(org.eclipse.ui.forms.editor.FormEditor)
181      */

182     public void initialize(FormEditor editor) {
183         fEditor = (PDEFormEditor)editor;
184     }
185     
186     public void dispose() {
187         if (fEditorSelectionChangedListener != null) {
188             fEditorSelectionChangedListener.uninstall(getSelectionProvider());
189             fEditorSelectionChangedListener= null;
190         }
191         if (fOutlinePage != null) {
192             fOutlinePage.dispose();
193             fOutlinePage = null;
194         }
195         if (isSelectionListener())
196             getEditor().getSite().getSelectionProvider().removeSelectionChangedListener(this);
197         super.dispose();
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineContentCreator#createOutlineLabelProvider()
202      */

203     public abstract ILabelProvider createOutlineLabelProvider();
204     
205     /* (non-Javadoc)
206      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineContentCreator#createOutlineContentProvider()
207      */

208     public abstract ITreeContentProvider createOutlineContentProvider();
209     
210     /* (non-Javadoc)
211      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineContentCreator#createOutlineComparator()
212      */

213     public abstract ViewerComparator createOutlineComparator();
214     
215     /* (non-Javadoc)
216      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineSelectionHandler#updateSelection(org.eclipse.jface.viewers.SelectionChangedEvent)
217      */

218     public void updateSelection(SelectionChangedEvent event) {
219         ISelection sel = event.getSelection();
220         if (sel instanceof IStructuredSelection) {
221             IStructuredSelection structuredSelection = (IStructuredSelection) sel;
222             updateSelection(structuredSelection.getFirstElement());
223         }
224     }
225     
226     /* (non-Javadoc)
227      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineSelectionHandler#updateSelection(java.lang.Object)
228      */

229     public abstract void updateSelection(Object JavaDoc object);
230     
231     /* (non-Javadoc)
232      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineContentCreator#createDefaultOutlineComparator()
233      */

234     public ViewerComparator createDefaultOutlineComparator() {
235         return null;
236     }
237     
238     protected ISortableContentOutlinePage createOutlinePage() {
239         SourceOutlinePage sourceOutlinePage=
240         new SourceOutlinePage(
241                 (IEditingModel) getInputContext().getModel(),
242                 createOutlineLabelProvider(), createOutlineContentProvider(),
243                 createDefaultOutlineComparator(), createOutlineComparator());
244         fOutlinePage = sourceOutlinePage;
245         fOutlineSelectionChangedListener = new ISelectionChangedListener() {
246             public void selectionChanged(SelectionChangedEvent event) {
247                 updateSelection(event);
248             }
249         };
250         fOutlinePage.addSelectionChangedListener(fOutlineSelectionChangedListener);
251         getSelectionProvider().addSelectionChangedListener(sourceOutlinePage);
252         fEditorSelectionChangedListener= new PDESourcePageChangedListener();
253         fEditorSelectionChangedListener.install(getSelectionProvider());
254         return fOutlinePage;
255     }
256
257     /* (non-Javadoc)
258      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineSelectionHandler#getContentOutline()
259      */

260     public ISortableContentOutlinePage getContentOutline() {
261         if (fOutlinePage == null)
262             fOutlinePage = createOutlinePage();
263         return fOutlinePage;
264     }
265     
266     /* (non-Javadoc)
267      * @see org.eclipse.ui.forms.editor.IFormPage#getEditor()
268      */

269     public FormEditor getEditor() {
270         return fEditor;
271     }
272     
273     /* (non-Javadoc)
274      * @see org.eclipse.ui.forms.editor.IFormPage#getManagedForm()
275      */

276     public IManagedForm getManagedForm() {
277         // not a form page
278
return null;
279     }
280     
281     protected void firePropertyChange(int type) {
282         if (type == PROP_DIRTY) {
283             fEditor.fireSaveNeeded(getEditorInput(), true);
284         } else
285             super.firePropertyChange(type);
286     }
287     
288     /* (non-Javadoc)
289      * @see org.eclipse.ui.forms.editor.IFormPage#setActive(boolean)
290      */

291     public void setActive(boolean active) {
292         fInputContext.setSourceEditingMode(active);
293     }
294
295     public boolean canLeaveThePage() {
296         return true;
297     }
298
299     /* (non-Javadoc)
300      * @see org.eclipse.ui.forms.editor.IFormPage#isActive()
301      */

302     public boolean isActive() {
303         return this.equals(fEditor.getActivePageInstance());
304     }
305     
306     public void createPartControl(Composite parent) {
307         super.createPartControl(parent);
308         Control[] children = parent.getChildren();
309         fControl = children[children.length - 1];
310         
311         PlatformUI.getWorkbench().getHelpSystem().setHelp(fControl, IHelpContextIds.MANIFEST_SOURCE_PAGE);
312     }
313     
314     /* (non-Javadoc)
315      * @see org.eclipse.ui.forms.editor.IFormPage#getPartControl()
316      */

317     public Control getPartControl() {
318         return fControl;
319     }
320     
321     /* (non-Javadoc)
322      * @see org.eclipse.ui.forms.editor.IFormPage#getId()
323      */

324     public String JavaDoc getId() {
325         return fId;
326     }
327     
328     /* (non-Javadoc)
329      * @see org.eclipse.ui.forms.editor.IFormPage#getIndex()
330      */

331     public int getIndex() {
332         return fIndex;
333     }
334     
335     /* (non-Javadoc)
336      * @see org.eclipse.ui.forms.editor.IFormPage#setIndex(int)
337      */

338     public void setIndex(int index) {
339         fIndex = index;
340     }
341     
342     /* (non-Javadoc)
343      * @see org.eclipse.ui.forms.editor.IFormPage#isSource()
344      */

345     public boolean isEditor() {
346         return true;
347     }
348     
349     /**
350      * @return Returns the inputContext.
351      */

352     public InputContext getInputContext() {
353         return fInputContext;
354     }
355     
356     /**
357      * @param inputContext The inputContext to set.
358      */

359     public void setInputContext(InputContext inputContext) {
360         fInputContext = inputContext;
361         setDocumentProvider(inputContext.getDocumentProvider());
362     }
363     
364     /* (non-Javadoc)
365      * @see org.eclipse.ui.forms.editor.IFormPage#focusOn(java.lang.Object)
366      */

367     public boolean selectReveal(Object JavaDoc object) {
368         if (object instanceof IMarker) {
369             IDE.gotoMarker(this, (IMarker)object);
370             return true;
371         }
372         return false;
373     }
374     
375     public IDocumentRange getRangeElement(int offset, boolean searchChildren) {
376         return null;
377     }
378
379     public void setHighlightRange(IDocumentRange range, boolean moveCursor) {
380         int offset = range.getOffset();
381         if (offset == -1) {
382             resetHighlightRange();
383             return;
384         }
385         
386         ISourceViewer sourceViewer = getSourceViewer();
387         if (sourceViewer == null)
388             return;
389
390         IDocument document = sourceViewer.getDocument();
391         if (document == null)
392             return;
393
394         int length = range.getLength();
395         setHighlightRange(offset, length == -1 ? 1 : length, moveCursor);
396     }
397     
398     public void setSelectedRange(IDocumentRange range, boolean fullNodeSelection) {
399         ISourceViewer sourceViewer = getSourceViewer();
400         if (sourceViewer == null)
401             return;
402
403         IDocument document = sourceViewer.getDocument();
404         if (document == null)
405             return;
406
407         int offset;
408         int length;
409         if (range instanceof IDocumentNode && !fullNodeSelection) {
410             length = ((IDocumentNode)range).getXMLTagName().length();
411             offset = range.getOffset() + 1;
412         } else {
413             length = range.getLength();
414             offset = range.getOffset();
415         }
416         sourceViewer.setSelectedRange(offset, length);
417     }
418     
419     public int getOrientation() {
420         return SWT.LEFT_TO_RIGHT;
421     }
422     
423     protected void createActions() {
424         super.createActions();
425         PDESelectAnnotationRulerAction action = new PDESelectAnnotationRulerAction(
426                 getBundleForConstructedKeys(), "PDESelectAnnotationRulerAction.", this, getVerticalRuler()); //$NON-NLS-1$
427
setAction(ITextEditorActionConstants.RULER_CLICK, action);
428         PDEFormEditorContributor contributor = fEditor == null ? null : fEditor.getContributor();
429         if (contributor instanceof PDEFormTextEditorContributor) {
430             PDEFormTextEditorContributor textContributor = (PDEFormTextEditorContributor)contributor;
431             setAction(PDEActionConstants.OPEN, textContributor.getHyperlinkAction());
432             setAction(PDEActionConstants.FORMAT, textContributor.getFormatAction());
433             if (textContributor.supportsContentAssist())
434                 createContentAssistAction();
435         }
436         
437         // Create the quick outline action
438
createQuickOutlineAction();
439     }
440     
441     /**
442      *
443      */

444     private void createQuickOutlineAction() {
445         // Quick Outline Action
446
ResourceAction action = new TextOperationAction(
447                 getBundleForConstructedKeys(), "QuickOutline.", this, //$NON-NLS-1$
448
PDEProjectionViewer.QUICK_OUTLINE, true);
449         action.setActionDefinitionId(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE);
450         action.setText(PDEUIMessages.PDESourcePage_actionTextQuickOutline);
451         action.setId(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE);
452         action.setImageDescriptor(PDEPluginImages.DESC_OVERVIEW_OBJ);
453         setAction(PDEActionConstants.COMMAND_ID_QUICK_OUTLINE, action);
454     }
455
456     private void createContentAssistAction() {
457         IAction contentAssist = new ContentAssistAction(
458                 getBundleForConstructedKeys(), "ContentAssistProposal.", this); //$NON-NLS-1$
459
contentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
460         setAction("ContentAssist", contentAssist); //$NON-NLS-1$
461
markAsStateDependentAction("ContentAssist", true); //$NON-NLS-1$
462
}
463     
464     public final void selectionChanged(SelectionChangedEvent event) {
465         if (event.getSource() == getSelectionProvider())
466             return;
467         ISelection sel = event.getSelection();
468         if (sel instanceof ITextSelection)
469             return;
470         if (sel instanceof IStructuredSelection)
471             fSelection = ((IStructuredSelection)sel).getFirstElement();
472         else
473             fSelection = null;
474     }
475     
476     /*
477      * Locate an IDocumentRange, subclasses that want to
478      * highlight text components based on site selection
479      * should override this method.
480      */

481     protected IDocumentRange findRange() {
482         return null;
483     }
484     
485     public void updateTextSelection() {
486         IDocumentRange range = findRange();
487         if (range == null)
488             return;
489         IBaseModel model = getInputContext().getModel();
490         if (!(model instanceof AbstractEditingModel))
491             return;
492         
493         if (range.getOffset() == -1 || isDirty()) {
494             try {
495                 ((AbstractEditingModel)model).adjustOffsets(((AbstractEditingModel)model).getDocument());
496             } catch (CoreException e) {
497             }
498             range = findRange();
499         }
500         setHighlightRange(range, true);
501         setSelectedRange(range, false);
502     }
503     
504     /*
505      * Subclasses that wish to provide PDEFormPage -> PDESourcePage
506      * selection persistence should override this and return true.
507      */

508     protected boolean isSelectionListener() {
509         return false;
510     }
511     
512     public ISourceViewer getViewer() {
513         return getSourceViewer();
514     }
515
516     protected void editorContextMenuAboutToShow(IMenuManager menu) {
517         PDEFormEditorContributor contributor = fEditor == null ? null : fEditor.getContributor();
518         if (contributor instanceof PDEFormTextEditorContributor) {
519             PDEFormTextEditorContributor textContributor = (PDEFormTextEditorContributor)contributor;
520             HyperlinkAction action = textContributor.getHyperlinkAction();
521             if ((action != null) &&
522                     action.isEnabled() &&
523                     ((action.getHyperLink() instanceof ExtensionHyperLink) == false)) {
524                 // Another detector handles this the extension hyperlink case
525
// org.eclipse.pde.internal.ui.editor.plugin.ExtensionAttributePointDectector.java
526
// Implemented at a higher level. As a result, need to disable
527
// the action here to prevent duplicate entries in the context menu
528
menu.add(action);
529             }
530             FormatAction formatManifestAction = textContributor.getFormatAction();
531             if (formatManifestAction != null && formatManifestAction.isEnabled())
532                 menu.add(formatManifestAction);
533         }
534         super.editorContextMenuAboutToShow(menu);
535     }
536
537     /**
538      * @return
539      */

540     public Object JavaDoc getSelection() {
541         return fSelection;
542     }
543     
544     // TODO: MP: QO: LOW: Create method to set selection and make fSelection private
545

546     /* (non-Javadoc)
547      * @see org.eclipse.pde.internal.ui.editor.outline.IOutlineContentCreator#getOutlineInput()
548      */

549     public Object JavaDoc getOutlineInput() {
550         return getInputContext().getModel();
551     }
552
553     /**
554      * @param rangeElement
555      */

556     protected void updateOutlinePageSelection(Object JavaDoc rangeElement) {
557         // Set selection in source outline page if the 'Link with Editor'
558
// feature is on
559
if (PDEPlugin.getDefault().getPreferenceStore().getBoolean(
560                 "ToggleLinkWithEditorAction.isChecked")) { //$NON-NLS-1$
561
// Ensure we have a source outline page
562
if ((fOutlinePage instanceof SourceOutlinePage) == false) {
563                 return;
564             }
565             SourceOutlinePage outlinePage = (SourceOutlinePage)fOutlinePage;
566             // Temporarily remove the listener to prevent a selection event being fired
567
// back at this page
568
outlinePage.removeAllSelectionChangedListeners();
569             if (rangeElement != null) {
570                 outlinePage.setSelection(new StructuredSelection(rangeElement));
571             } else {
572                 outlinePage.setSelection(StructuredSelection.EMPTY);
573             }
574             outlinePage.addAllSelectionChangedListeners();
575         }
576     }
577
578     /**
579      * @param event
580      */

581     protected void handleSelectionChangedSourcePage(SelectionChangedEvent event) {
582         ISelection selection = event.getSelection();
583         if (!selection.isEmpty() && selection instanceof ITextSelection) {
584             synchronizeOutlinePage(((ITextSelection) selection).getOffset());
585         }
586     }
587
588     /**
589      * @param rangeElement
590      */

591     protected void updateHighlightRange(IDocumentRange rangeElement) {
592         if (rangeElement != null) {
593             setHighlightRange(rangeElement, false);
594         } else {
595             resetHighlightRange();
596         }
597     }
598     
599     /**
600      * @param offset
601      */

602     protected void synchronizeOutlinePage(int offset) {
603         IDocumentRange rangeElement = getRangeElement(offset, false);
604         updateHighlightRange(rangeElement);
605         updateOutlinePageSelection(rangeElement);
606     }
607
608     /**
609      * Triggered by toggling the 'Link with Editor' button in the outline
610      * view
611      * @param offset
612      */

613     public void synchronizeOutlinePage() {
614         // Get the current position of the cursor in this page
615
int current_offset = getSourceViewer().getSelectedRange().x;
616         synchronizeOutlinePage(current_offset);
617     }
618
619 }
620
Popular Tags