KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > ManifestSourcePage


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 package org.eclipse.pde.internal.ui.editor.plugin;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
17 import org.eclipse.jface.viewers.ILabelProvider;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ITreeContentProvider;
20 import org.eclipse.jface.viewers.LabelProvider;
21 import org.eclipse.jface.viewers.ViewerComparator;
22 import org.eclipse.pde.core.plugin.IPluginBase;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.IPluginObject;
25 import org.eclipse.pde.internal.core.plugin.ImportObject;
26 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
27 import org.eclipse.pde.internal.core.text.IDocumentNode;
28 import org.eclipse.pde.internal.core.text.IDocumentRange;
29 import org.eclipse.pde.internal.core.text.IDocumentTextNode;
30 import org.eclipse.pde.internal.core.text.plugin.PluginModelBase;
31 import org.eclipse.pde.internal.ui.PDELabelProvider;
32 import org.eclipse.pde.internal.ui.PDEPlugin;
33 import org.eclipse.pde.internal.ui.PDEPluginImages;
34 import org.eclipse.pde.internal.ui.PDEUIMessages;
35 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
36 import org.eclipse.pde.internal.ui.editor.XMLSourcePage;
37 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
38 import org.eclipse.pde.internal.ui.search.PluginSearchActionGroup;
39 import org.eclipse.pde.internal.ui.util.SharedLabelProvider;
40 import org.eclipse.swt.graphics.Image;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.ui.actions.ActionContext;
43
44 public class ManifestSourcePage extends XMLSourcePage {
45     
46     private Object JavaDoc fLibraries = new Object JavaDoc();
47     private Object JavaDoc fImports = new Object JavaDoc();
48     private Object JavaDoc fExtensionPoints = new Object JavaDoc();
49     private Object JavaDoc fExtensions = new Object JavaDoc();
50     private ExtensionAttributePointDectector fDetector;
51     private PluginSearchActionGroup fActionGroup;
52     
53     class OutlineLabelProvider extends LabelProvider {
54         private PDELabelProvider fProvider;
55         
56         public OutlineLabelProvider() {
57             fProvider = PDEPlugin.getDefault().getLabelProvider();
58         }
59         
60         /* (non-Javadoc)
61          * @see org.eclipse.jface.viewers.LabelProvider#dispose()
62          */

63         
64         public String JavaDoc getText(Object JavaDoc obj) {
65             if (obj == fLibraries)
66                 return PDEUIMessages.ManifestSourcePage_libraries;
67             if (obj == fImports)
68                 return PDEUIMessages.ManifestSourcePage_dependencies;
69             if (obj == fExtensionPoints)
70                 return PDEUIMessages.ManifestSourcePage_extensionPoints;
71             if (obj == fExtensions)
72                 return PDEUIMessages.ManifestSourcePage_extensions;
73             String JavaDoc text = fProvider.getText(obj);
74             if ((text == null || text.trim().length() == 0) && obj instanceof IDocumentNode)
75                 text = ((IDocumentNode)obj).getXMLTagName();
76             return text;
77         }
78
79         public Image getImage(Object JavaDoc obj) {
80             if (obj == fLibraries)
81                 return fProvider.get(PDEPluginImages.DESC_RUNTIME_OBJ);
82             if (obj == fImports)
83                 return fProvider.get(PDEPluginImages.DESC_REQ_PLUGINS_OBJ);
84             if (obj == fExtensionPoints)
85                 return fProvider.get(PDEPluginImages.DESC_EXT_POINTS_OBJ);
86             if (obj == fExtensions)
87                 return fProvider.get(PDEPluginImages.DESC_EXTENSIONS_OBJ);
88             
89             Image image = fProvider.getImage(obj);
90             int flags = ((IDocumentNode)obj).isErrorNode() ? SharedLabelProvider.F_ERROR : 0;
91             return (flags == 0) ? image : fProvider.get(image, flags);
92         }
93     }
94     
95     class ContentProvider extends DefaultContentProvider implements ITreeContentProvider {
96
97         /* (non-Javadoc)
98          * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
99          */

100         public Object JavaDoc[] getChildren(Object JavaDoc parent) {
101             PluginModelBase model = (PluginModelBase)getInputContext().getModel();
102
103             ArrayList JavaDoc result = new ArrayList JavaDoc();
104             if (parent instanceof IPluginBase) {
105                 IPluginBase pluginBase = (IPluginBase)parent;
106                 if (pluginBase.getLibraries().length > 0)
107                     result.add(fLibraries);
108                 if (pluginBase.getImports().length > 0)
109                     result.add(fImports);
110                 if (pluginBase.getExtensionPoints().length > 0)
111                     result.add(fExtensionPoints);
112                 if (pluginBase.getExtensions().length > 0)
113                     result.add(fExtensions);
114                 return result.toArray();
115             }
116             if (parent == fLibraries)
117                 return model.getPluginBase().getLibraries();
118             
119             if (parent == fImports)
120                 return model.getPluginBase().getImports();
121             
122             if (parent == fExtensionPoints)
123                 return model.getPluginBase().getExtensionPoints();
124             
125             if (parent == fExtensions)
126                 return model.getPluginBase().getExtensions();
127             
128             return new Object JavaDoc[0];
129         }
130
131         /* (non-Javadoc)
132          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
133          */

134         public Object JavaDoc getParent(Object JavaDoc element) {
135             if (element instanceof IDocumentNode)
136                 return ((IDocumentNode)element).getParentNode();
137             return null;
138         }
139
140         /* (non-Javadoc)
141          * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
142          */

143         public boolean hasChildren(Object JavaDoc element) {
144             if (element instanceof IPluginBase) {
145                 return ((IDocumentNode) element).getChildNodes().length > 0;
146             }
147             return element == fLibraries || element == fImports
148             || element == fExtensionPoints || element == fExtensions;
149         }
150
151         /* (non-Javadoc)
152          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
153          */

154         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
155             if (inputElement instanceof IPluginModelBase) {
156                 return new Object JavaDoc[] {((IPluginModelBase)inputElement).getPluginBase()};
157             }
158             return new Object JavaDoc[0];
159         }
160     }
161         
162     class OutlineComparator extends ViewerComparator{
163         /* (non-Javadoc)
164          * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
165          */

166         public int category(Object JavaDoc element) {
167             if (element == fLibraries)
168                 return 0;
169             if (element == fImports)
170                 return 1;
171             if (element == fExtensionPoints)
172                 return 2;
173             if (element == fExtensions)
174                 return 3;
175             return 4;
176         }
177     }
178     
179     public ManifestSourcePage(PDEFormEditor editor, String JavaDoc id, String JavaDoc title) {
180         super(editor, id, title);
181         fDetector = new ExtensionAttributePointDectector();
182         fActionGroup = new PluginSearchActionGroup();
183     }
184
185     public ILabelProvider createOutlineLabelProvider() {
186         return new OutlineLabelProvider();
187     }
188     public ITreeContentProvider createOutlineContentProvider() {
189         return new ContentProvider();
190     }
191     
192     /* (non-Javadoc)
193      * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#updateSelection(java.lang.Object)
194      */

195     public void updateSelection(Object JavaDoc object) {
196         if ((object instanceof IDocumentNode) &&
197                 !((IDocumentNode)object).isErrorNode()) {
198             fSelection = object;
199             setHighlightRange((IDocumentNode)object, true);
200             setSelectedRange((IDocumentNode)object, false);
201         } else {
202             //resetHighlightRange();
203
}
204     }
205     
206     /* (non-Javadoc)
207      * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#createOutlineSorter()
208      */

209     public ViewerComparator createOutlineComparator() {
210         return new OutlineComparator();
211     }
212     
213     public IDocumentRange getRangeElement(int offset, boolean searchChildren) {
214         IPluginBase base = ((IPluginModelBase)getInputContext().getModel()).getPluginBase();
215         IDocumentRange
216             node = findNode(base.getLibraries(), offset, searchChildren);
217         if (node == null)
218             node = findNode(base.getImports(), offset, searchChildren);
219         if (node == null)
220             node = findNode(base.getExtensionPoints(), offset, searchChildren);
221         if (node == null)
222             node = findNode(base.getExtensions(), offset, searchChildren);
223         if (node == null)
224             node = findNode(new IPluginObject[] { base }, offset, searchChildren);
225         
226         return node;
227     }
228
229     private IDocumentRange findNode(Object JavaDoc[] nodes, int offset, boolean searchChildren) {
230         for (int i = 0; i < nodes.length; i++) {
231             IDocumentNode node = (IDocumentNode)nodes[i];
232             if (node.getOffset() <= offset
233                     && offset < node.getOffset() + node.getLength()) {
234                 
235                 if (!searchChildren)
236                     return node;
237                 
238                 if (node.getOffset() < offset &&
239                         offset <= node.getOffset() + node.getXMLTagName().length() + 1)
240                     return node;
241                 
242                 IDocumentAttribute[] attrs = node.getNodeAttributes();
243                 if (attrs != null)
244                     for (int a = 0; a < attrs.length; a++)
245                         if (attrs[a].getNameOffset() <= offset &&
246                                 offset <= attrs[a].getValueOffset() + attrs[a].getValueLength())
247                             return (IDocumentNode)attrs[a];
248                 
249                 IDocumentTextNode textNode = node.getTextNode();
250                 if (textNode != null &&
251                         textNode.getOffset() <= offset &&
252                         offset < textNode.getOffset() + textNode.getLength())
253                     return textNode;
254                 
255                 IDocumentNode[] children = node.getChildNodes();
256                 if (children != null)
257                     for (int c = 0; c < children.length; c++)
258                         if (children[c].getOffset() <= offset &&
259                                 offset < children[c].getOffset() + children[c].getLength())
260                             return findNode(new Object JavaDoc[] {children[c]}, offset, searchChildren);
261                 
262                 // not contained inside any sub elements, must be inside node
263
return node;
264             }
265         }
266         return null;
267     }
268     
269     public IDocumentRange findRange() {
270         if (fSelection instanceof ImportObject)
271             fSelection = ((ImportObject)fSelection).getImport();
272         if (fSelection instanceof IDocumentNode)
273             return (IDocumentNode)fSelection;
274         return null;
275     }
276     
277     protected boolean isSelectionListener() {
278         return true;
279     }
280     
281     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
282         if (IHyperlinkDetector.class.equals(adapter))
283             return new ManifestHyperlinkDetector(this);
284         return super.getAdapter(adapter);
285     }
286     
287     /* (non-Javadoc)
288      * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
289      */

290     protected void editorContextMenuAboutToShow(IMenuManager menu) {
291         
292         ISelection selection = fDetector.getSelection();
293         if (selection != null) {
294             fActionGroup.setContext(new ActionContext(selection));
295             fActionGroup.fillContextMenu(menu);
296         }
297         super.editorContextMenuAboutToShow(menu);
298     }
299     
300     /* (non-Javadoc)
301      * @see org.eclipse.pde.internal.ui.editor.PDEProjectionSourcePage#createPartControl(org.eclipse.swt.widgets.Composite)
302      */

303     public void createPartControl(Composite parent) {
304         super.createPartControl(parent);
305         // At this point the source page is fully initialized including the
306
// underlying text viewer
307
fDetector.setTextEditor(this);
308     }
309     
310     /* (non-Javadoc)
311      * @see org.eclipse.pde.internal.ui.editor.PDEProjectionSourcePage#isQuickOutlineEnabled()
312      */

313     public boolean isQuickOutlineEnabled() {
314         return true;
315     }
316
317     /* (non-Javadoc)
318      * @see org.eclipse.pde.internal.ui.editor.PDESourcePage#setActive(boolean)
319      */

320     public void setActive(boolean active) {
321         super.setActive(active);
322         // Update the text selection if this page is being activated
323
if (active) {
324             updateTextSelection();
325         }
326     }
327     
328 }
329
Popular Tags