KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.Separator;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredContentProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.jface.viewers.TableViewer;
29 import org.eclipse.jface.wizard.WizardDialog;
30 import org.eclipse.pde.core.IBaseModel;
31 import org.eclipse.pde.core.IModelChangeProvider;
32 import org.eclipse.pde.core.IModelChangedEvent;
33 import org.eclipse.pde.core.plugin.IPluginBase;
34 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
35 import org.eclipse.pde.core.plugin.IPluginModelBase;
36 import org.eclipse.pde.core.plugin.ISharedExtensionsModel;
37 import org.eclipse.pde.internal.core.PDECore;
38 import org.eclipse.pde.internal.core.SourceLocationManager;
39 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
40 import org.eclipse.pde.internal.core.text.IDocumentNode;
41 import org.eclipse.pde.internal.core.text.plugin.IDocumentExtensionPoint;
42 import org.eclipse.pde.internal.ui.PDEPlugin;
43 import org.eclipse.pde.internal.ui.PDEUIMessages;
44 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
45 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
46 import org.eclipse.pde.internal.ui.editor.TableSection;
47 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
48 import org.eclipse.pde.internal.ui.parts.TablePart;
49 import org.eclipse.pde.internal.ui.search.PluginSearchActionGroup;
50 import org.eclipse.pde.internal.ui.util.SWTUtil;
51 import org.eclipse.pde.internal.ui.wizards.extension.NewExtensionPointWizard;
52 import org.eclipse.swt.SWT;
53 import org.eclipse.swt.custom.BusyIndicator;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Table;
56 import org.eclipse.swt.widgets.TableItem;
57 import org.eclipse.ui.IEditorInput;
58 import org.eclipse.ui.IFileEditorInput;
59 import org.eclipse.ui.actions.ActionContext;
60 import org.eclipse.ui.actions.ActionFactory;
61 import org.eclipse.ui.forms.widgets.ExpandableComposite;
62 import org.eclipse.ui.forms.widgets.FormToolkit;
63 import org.eclipse.ui.forms.widgets.Section;
64
65 public class ExtensionPointsSection extends TableSection {
66     private TableViewer pointTable;
67
68     class TableContentProvider extends DefaultContentProvider implements
69             IStructuredContentProvider {
70         public Object JavaDoc[] getElements(Object JavaDoc parent) {
71             IPluginModelBase model = (IPluginModelBase) getPage().getModel();
72             IPluginBase pluginBase = model.getPluginBase();
73             if (pluginBase != null)
74                 return pluginBase.getExtensionPoints();
75             return new Object JavaDoc[0];
76         }
77     }
78
79     public ExtensionPointsSection(PDEFormPage page, Composite parent) {
80         super(page, parent, ExpandableComposite.TITLE_BAR | Section.DESCRIPTION, new String JavaDoc[] { PDEUIMessages.ManifestEditor_DetailExtensionPointSection_new });
81         getSection().setText(PDEUIMessages.ManifestEditor_DetailExtensionPointSection_title);
82         getSection().setDescription(PDEUIMessages.ExtensionPointsSection_sectionDescAllExtensionPoints);
83         fHandleDefaultButton = false;
84         getTablePart().setEditable(false);
85     }
86
87     public void createClient(Section section, FormToolkit toolkit) {
88         Composite container = createClientContainer(section, 2, toolkit);
89         TablePart tablePart = getTablePart();
90         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
91         pointTable = tablePart.getTableViewer();
92         pointTable.setContentProvider(new TableContentProvider());
93         pointTable.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
94         toolkit.paintBordersFor(container);
95         section.setClient(container);
96         pointTable.setInput(getPage());
97         selectFirstExtensionPoint();
98         IBaseModel model = getPage().getModel();
99         if (model instanceof IModelChangeProvider)
100             ((IModelChangeProvider) model).addModelChangedListener(this);
101         tablePart.setButtonEnabled(0, model.isEditable());
102     }
103
104     private void selectFirstExtensionPoint() {
105         Table table = pointTable.getTable();
106         TableItem[] items = table.getItems();
107         if (items.length == 0)
108             return;
109         TableItem firstItem = items[0];
110         Object JavaDoc obj = firstItem.getData();
111         pointTable.setSelection(new StructuredSelection(obj));
112     }
113
114     void fireSelection() {
115         pointTable.setSelection(pointTable.getSelection());
116     }
117
118     public void dispose() {
119         IBaseModel model = getPage().getModel();
120         if (model instanceof IModelChangeProvider)
121             ((IModelChangeProvider) model).removeModelChangedListener(this);
122         super.dispose();
123     }
124
125     public boolean doGlobalAction(String JavaDoc actionId) {
126         
127         if (!isEditable()) { return false; }
128         
129         if (actionId.equals(ActionFactory.DELETE.getId())) {
130             handleDelete();
131             return true;
132         }
133         if (actionId.equals(ActionFactory.CUT.getId())) {
134             // delete here and let the editor transfer
135
// the selection to the clipboard
136
handleDelete();
137             return false;
138         }
139         if (actionId.equals(ActionFactory.PASTE.getId())) {
140             doPaste();
141             return true;
142         }
143         return false;
144     }
145
146     public void refresh() {
147         pointTable.refresh();
148         getManagedForm().fireSelectionChanged(this, pointTable.getSelection());
149         super.refresh();
150     }
151
152     public boolean setFormInput(Object JavaDoc object) {
153         if (object instanceof IPluginExtensionPoint) {
154             pointTable.setSelection(new StructuredSelection(object), true);
155             return true;
156         }
157         return false;
158     }
159
160     protected void selectionChanged(IStructuredSelection selection) {
161         getPage().getPDEEditor().setSelection(selection);
162         super.selectionChanged(selection);
163     }
164
165     public void modelChanged(IModelChangedEvent event) {
166         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
167             markStale();
168             return;
169         }
170         Object JavaDoc changeObject = event.getChangedObjects()[0];
171         if (changeObject instanceof IPluginExtensionPoint) {
172             if (event.getChangeType() == IModelChangedEvent.INSERT) {
173                 pointTable.add(changeObject);
174                 pointTable.setSelection(
175                     new StructuredSelection(changeObject),
176                     true);
177                 pointTable.getTable().setFocus();
178             } else if (event.getChangeType() == IModelChangedEvent.REMOVE) {
179                 pointTable.remove(changeObject);
180             } else {
181                 pointTable.update(changeObject, null);
182             }
183         }
184     }
185
186     protected void fillContextMenu(IMenuManager manager) {
187         ISelection selection = pointTable.getSelection();
188
189         Action newAction = new Action(PDEUIMessages.ManifestEditor_DetailExtensionPointSection_newExtensionPoint) {
190             public void run() {
191                 handleNew();
192             }
193         };
194         newAction.setEnabled(isEditable());
195         manager.add(newAction);
196         
197         if (selection.isEmpty()) {
198             getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
199             return;
200         }
201         manager.add(new Separator());
202         IBaseModel model = getPage().getPDEEditor().getAggregateModel();
203         PluginSearchActionGroup actionGroup = new PluginSearchActionGroup();
204         actionGroup.setBaseModel(model);
205         actionGroup.setContext(new ActionContext(selection));
206         actionGroup.fillContextMenu(manager);
207         manager.add(new Separator());
208         
209         Action deleteAction = new Action(PDEUIMessages.Actions_delete_label) {
210             public void run() {
211                 handleDelete();
212             }
213         };
214         deleteAction.setEnabled(isEditable());
215         manager.add(deleteAction);
216         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
217     }
218
219     protected void buttonSelected(int index) {
220         if (index == 0)
221             handleNew();
222     }
223
224     private void handleDelete() {
225         Object JavaDoc[] selection = ((IStructuredSelection) pointTable
226                 .getSelection()).toArray();
227         for (int i = 0; i < selection.length; i++) {
228             Object JavaDoc object = selection[i];
229             if (object != null && object instanceof IPluginExtensionPoint) {
230                 IPluginExtensionPoint ep = (IPluginExtensionPoint) object;
231                 IPluginBase plugin = ep.getPluginBase();
232                 try {
233                     plugin.remove(ep);
234                     String JavaDoc schema = ep.getSchema();
235                     IProject project = ep.getModel().getUnderlyingResource()
236                             .getProject();
237                     IFile schemaFile = project.getFile(schema);
238                     if (schemaFile.exists())
239                         if (MessageDialog.openQuestion(getSection().getShell(),
240                                 PDEUIMessages.ExtensionPointsSection_title,
241                                 PDEUIMessages.ExtensionPointsSection_message1 + " " //$NON-NLS-1$
242
+ schemaFile.getProjectRelativePath().toString() + "?")) //$NON-NLS-1$
243
schemaFile.delete(true, false,
244                                     new NullProgressMonitor());
245
246                 } catch (CoreException e) {
247                     PDEPlugin.logException(e);
248                 }
249             }
250         }
251     }
252
253     private void handleNew() {
254         IFile file = ((IFileEditorInput) getPage().getPDEEditor()
255                 .getEditorInput()).getFile();
256         final IProject project = file.getProject();
257         BusyIndicator.showWhile(pointTable.getTable().getDisplay(),
258                 new Runnable JavaDoc() {
259                     public void run() {
260                         NewExtensionPointWizard wizard = new NewExtensionPointWizard(
261                                 project, (IPluginModelBase) getPage()
262                                         .getModel(), (ManifestEditor) getPage()
263                                         .getPDEEditor());
264                         WizardDialog dialog = new WizardDialog(PDEPlugin
265                                 .getActiveWorkbenchShell(), wizard);
266                         dialog.create();
267                         SWTUtil.setDialogSize(dialog, 400, 450);
268                         dialog.open();
269                     }
270                 });
271     }
272
273     public static IEditorInput getSchemaFromSourceExtension(IPluginBase plugin, IPath path) {
274         SourceLocationManager mgr = PDECore.getDefault().getSourceLocationManager();
275         File JavaDoc file = mgr.findSourceFile(plugin, path);
276         return (file != null && file.exists() && file.isFile())
277                  ? new SystemFileEditorInput(file)
278                  : null;
279     }
280
281     /**
282      * @return
283      */

284     private IPluginModelBase getPluginModelBase() {
285         // Note: This method will work with fragments as long as a fragment.xml
286
// is defined first. Otherwise, paste will not work out of the box.
287
// Get the model
288
IPluginModelBase model = (IPluginModelBase) getPage().getModel();
289         // Ensure the model is a bundle plugin model
290
if ((model instanceof IBundlePluginModelBase) == false) {
291             return null;
292         }
293         // Get the extension model
294
ISharedExtensionsModel extensionModel =
295             ((IBundlePluginModelBase)model).getExtensionsModel();
296         // Ensure the extension model is defined
297
if ((extensionModel == null) ||
298                 ((extensionModel instanceof IPluginModelBase) == false)) {
299             return null;
300         }
301         return ((IPluginModelBase)extensionModel);
302     }
303     
304     /* (non-Javadoc)
305      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(java.lang.Object, java.lang.Object[])
306      */

307     protected void doPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
308         // By default, fragment.xml does not exist until the first extension
309
// or extension point is created.
310
// Ensure the file exists before pasting because the model will be
311
// null and the paste will fail if it does not exist
312
((ManifestEditor)getPage().getEditor()).ensurePluginContextPresence();
313         // Get the model
314
IPluginModelBase model = getPluginModelBase();
315         // Ensure an editable model was actually retrieved
316
if (model == null) {
317             return;
318         }
319         IPluginBase pluginBase = model.getPluginBase();
320         try {
321             // Paste all source objects
322
// Since, the extension points are a flat non-hierarchical list,
323
// the target object is not needed
324
for (int i = 0; i < sourceObjects.length; i++) {
325                 Object JavaDoc sourceObject = sourceObjects[i];
326                 
327                 if ((sourceObject instanceof IDocumentExtensionPoint) &&
328                         (sourceObject instanceof IPluginExtensionPoint) &&
329                         (pluginBase instanceof IDocumentNode)) {
330                     // Extension point object
331
IDocumentExtensionPoint extensionPoint =
332                         (IDocumentExtensionPoint)sourceObject;
333                     // Adjust all the source object transient field values to
334
// acceptable values
335
extensionPoint.reconnect(model, (IDocumentNode)pluginBase);
336                     // Add the extension point to the plug-in
337
pluginBase.add((IPluginExtensionPoint)extensionPoint);
338                 }
339             }
340         } catch (CoreException e) {
341             PDEPlugin.logException(e);
342         }
343     }
344
345     /* (non-Javadoc)
346      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(java.lang.Object, java.lang.Object[])
347      */

348     protected boolean canPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
349         // All source objects must be extension points
350
// No restriction on duplicates
351
for (int i = 0; i < sourceObjects.length; i++) {
352             if ((sourceObjects[i] instanceof IPluginExtensionPoint) == false) {
353                 return false;
354             }
355         }
356         return true;
357     }
358     
359     protected void selectExtensionPoint(ISelection selection) {
360         pointTable.setSelection(selection, true);
361     }
362 }
363
Popular Tags