KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 import org.eclipse.core.resources.IContainer;
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerFilter;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.jface.wizard.WizardDialog;
26 import org.eclipse.pde.core.IBaseModel;
27 import org.eclipse.pde.core.IModelChangedEvent;
28 import org.eclipse.pde.core.plugin.IPluginBase;
29 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.internal.ui.IPDEUIConstants;
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.FormEntryAdapter;
36 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
37 import org.eclipse.pde.internal.ui.editor.PDEDetails;
38 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
39 import org.eclipse.pde.internal.ui.editor.actions.OpenSchemaAction;
40 import org.eclipse.pde.internal.ui.parts.FormEntry;
41 import org.eclipse.pde.internal.ui.search.FindReferencesAction;
42 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
43 import org.eclipse.pde.internal.ui.util.SWTUtil;
44 import org.eclipse.pde.internal.ui.wizards.extension.NewSchemaFileWizard;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.custom.BusyIndicator;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Display;
50 import org.eclipse.ui.IWorkbenchWindow;
51 import org.eclipse.ui.PartInitException;
52 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
53 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
54 import org.eclipse.ui.forms.IFormPart;
55 import org.eclipse.ui.forms.events.HyperlinkAdapter;
56 import org.eclipse.ui.forms.events.HyperlinkEvent;
57 import org.eclipse.ui.forms.widgets.ExpandableComposite;
58 import org.eclipse.ui.forms.widgets.FormText;
59 import org.eclipse.ui.forms.widgets.FormToolkit;
60 import org.eclipse.ui.forms.widgets.Section;
61 import org.eclipse.ui.model.WorkbenchContentProvider;
62 import org.eclipse.ui.model.WorkbenchLabelProvider;
63 import org.eclipse.ui.part.FileEditorInput;
64 import org.eclipse.ui.views.navigator.ResourceComparator;
65
66 public class ExtensionPointDetails extends PDEDetails {
67     private IPluginExtensionPoint fInput;
68     private FormEntry fIdEntry;
69     private FormEntry fNameEntry;
70     private FormEntry fSchemaEntry;
71     private FormText fRichText;
72     private String JavaDoc fRichTextData;
73
74     private static final String JavaDoc SCHEMA_RTEXT_DATA = PDEUIMessages.ExtensionPointDetails_schemaLinks;
75     private static final String JavaDoc NO_SCHEMA_RTEXT_DATA = PDEUIMessages.ExtensionPointDetails_noSchemaLinks;
76     public ExtensionPointDetails() {
77     }
78     public String JavaDoc getContextId() {
79         return PluginInputContext.CONTEXT_ID;
80     }
81     public void fireSaveNeeded() {
82         markDirty();
83         getPage().getPDEEditor().fireSaveNeeded(getContextId(), false);
84     }
85     public PDEFormPage getPage() {
86         return (PDEFormPage)getManagedForm().getContainer();
87     }
88     public boolean isEditable() {
89         return getPage().getPDEEditor().getAggregateModel().isEditable();
90     }
91     /*
92      * (non-Javadoc)
93      *
94      * @see org.eclipse.ui.forms.IDetailsPage#createContents(org.eclipse.swt.widgets.Composite)
95      */

96     public void createContents(Composite parent) {
97         parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
98         FormToolkit toolkit = getManagedForm().getToolkit();
99         Section section = toolkit.createSection(parent, Section.DESCRIPTION|ExpandableComposite.TITLE_BAR);
100         section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
101         section.setText(PDEUIMessages.ExtensionPointDetails_title);
102         section.setDescription(PDEUIMessages.ExtensionPointDetails_desc);
103         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
104         section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
105         
106         Composite client = toolkit.createComposite(section);
107         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
108         client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
109         
110         fIdEntry = new FormEntry(client, toolkit, PDEUIMessages.ExtensionPointDetails_id, null, false);
111         fIdEntry.setFormEntryListener(new FormEntryAdapter(this) {
112             public void textValueChanged(FormEntry entry) {
113                 if (fInput != null) {
114                     try {
115                         fInput.setId(fIdEntry.getValue());
116                     } catch (CoreException e) {
117                         PDEPlugin.logException(e);
118                     }
119                 }
120             }
121         });
122         fNameEntry = new FormEntry(client, toolkit, PDEUIMessages.ExtensionPointDetails_name, null, false);
123         fNameEntry.setFormEntryListener(new FormEntryAdapter(this) {
124             public void textValueChanged(FormEntry entry) {
125                 if (fInput != null)
126                     try {
127                         fInput.setName(fNameEntry.getValue());
128                     } catch (CoreException e) {
129                         PDEPlugin.logException(e);
130                     }
131             }
132         });
133         boolean editable = getPage().getModel().isEditable();
134         fSchemaEntry = new FormEntry(client, toolkit, PDEUIMessages.ExtensionPointDetails_schema, PDEUIMessages.ExtensionPointDetails_browse, editable); //
135
fSchemaEntry.setFormEntryListener(new FormEntryAdapter(this) {
136             public void textValueChanged(FormEntry entry) {
137                 if (fInput != null) {
138                     try {
139                         fInput.setSchema(fSchemaEntry.getValue());
140                     } catch (CoreException e) {
141                         PDEPlugin.logException(e);
142                     }
143                     updateRichText();
144                 }
145             }
146
147             public void linkActivated(HyperlinkEvent e) {
148                 IProject project = getPage().getPDEEditor().getCommonProject();
149                 if (fSchemaEntry.getValue() == null || fSchemaEntry.getValue().length() ==0){
150                     generateSchema();
151                     return;
152                 }
153                 IFile file = project.getFile(fSchemaEntry.getValue());
154                 if (file.exists())
155                     openSchemaFile(file);
156                 else
157                     generateSchema();
158             }
159
160             public void browseButtonSelected(FormEntry entry) {
161                 final IProject project = getPage().getPDEEditor().getCommonProject();
162                 ElementTreeSelectionDialog dialog =
163                     new ElementTreeSelectionDialog(
164                         PDEPlugin.getActiveWorkbenchShell(),
165                         new WorkbenchLabelProvider(),
166                         new WorkbenchContentProvider());
167                 dialog.setTitle(PDEUIMessages.ManifestEditor_ExtensionPointDetails_schemaLocation_title);
168                 dialog.setMessage(PDEUIMessages.ManifestEditor_ExtensionPointDetails_schemaLocation_desc);
169                 dialog.setDoubleClickSelects(false);
170                 dialog.setAllowMultiple(false);
171                 dialog.addFilter(new ViewerFilter(){
172                     public boolean select(Viewer viewer, Object JavaDoc parent,
173                             Object JavaDoc element) {
174                         if (element instanceof IFile){
175                             String JavaDoc ext = ((IFile)element).getFullPath().getFileExtension();
176                                 return "exsd".equals(ext) || "mxsd".equals(ext); //$NON-NLS-1$ //$NON-NLS-2$
177
} else if (element instanceof IContainer){ // i.e. IProject, IFolder
178
try {
179                                 IResource[] resources = ((IContainer)element).members();
180                                 for (int i = 0; i < resources.length; i++){
181                                     if (select(viewer, parent, resources[i]))
182                                         return true;
183                                 }
184                             } catch (CoreException e) {
185                                 PDEPlugin.logException(e);
186                             }
187                         }
188                         return false;
189                     }
190                 });
191                 dialog.setValidator(new ISelectionStatusValidator() {
192                     public IStatus validate(Object JavaDoc[] selection) {
193                         IPluginModelBase model = (IPluginModelBase) getPage()
194                                 .getPDEEditor().getAggregateModel();
195                         String JavaDoc pluginName = model.getPluginBase().getId();
196
197                         if (selection == null || selection.length != 1
198                                 || !(selection[0] instanceof IFile))
199                             return new Status(
200                                     IStatus.ERROR,
201                                     pluginName,
202                                     IStatus.ERROR,
203                                     PDEUIMessages.ManifestEditor_ExtensionPointDetails_validate_errorStatus,
204                                     null);
205                         IFile file = (IFile) selection[0];
206                         String JavaDoc ext = file.getFullPath().getFileExtension();
207                         if ("exsd".equals(ext) || "mxsd".equals(ext)) //$NON-NLS-1$ //$NON-NLS-2$
208
return new Status(IStatus.OK, pluginName,
209                                     IStatus.OK, "", null); //$NON-NLS-1$
210
return new Status(
211                                 IStatus.ERROR,
212                                 pluginName,
213                                 IStatus.ERROR,
214                                 PDEUIMessages.ManifestEditor_ExtensionPointDetails_validate_errorStatus,
215                                 null);
216                     }
217                 });
218                 dialog.setDoubleClickSelects(true);
219                 dialog.setStatusLineAboveButtons(true);
220                 dialog.setInput(project);
221                 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
222                 String JavaDoc filePath = fSchemaEntry.getValue();
223                 if (filePath!=null && filePath.length()!=0 && project.exists(new Path(filePath)))
224                     dialog.setInitialSelection(project.getFile(new Path(filePath)));
225                 else
226                     dialog.setInitialSelection(null);
227                 if (dialog.open() == Window.OK) {
228                     Object JavaDoc[] elements = dialog.getResult();
229                     if (elements.length >0){
230                         IResource elem = (IResource) elements[0];
231                         fSchemaEntry.setValue(elem.getProjectRelativePath().toString());
232                     }
233                 }
234             }
235         });
236         createSpacer(toolkit, client, 2);
237         
238         Composite container = toolkit.createComposite(parent, SWT.NONE);
239         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1));
240         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
241         
242         fRichText = toolkit.createFormText(container, true);
243         fRichText.setImage("open", PDEPlugin.getDefault().getLabelProvider().get( //$NON-NLS-1$
244
PDEPluginImages.DESC_SCHEMA_OBJ));
245         fRichText.setImage("desc", PDEPlugin.getDefault().getLabelProvider().get( //$NON-NLS-1$
246
PDEPluginImages.DESC_DOC_SECTION_OBJ));
247         fRichText.setImage("search", PDEPlugin.getDefault().getLabelProvider().get( //$NON-NLS-1$
248
PDEPluginImages.DESC_PSEARCH_OBJ));
249         fRichText.addHyperlinkListener(new HyperlinkAdapter() {
250             public void linkActivated(HyperlinkEvent e) {
251                 IBaseModel model = getPage().getPDEEditor().getAggregateModel();
252                 String JavaDoc pointID = null;
253                 IPluginBase base = ((IPluginModelBase)model).getPluginBase();
254                 String JavaDoc pluginID = base.getId();
255                 if (Double.parseDouble(base.getSchemaVersion()) >= 3.2) {
256                     if (fInput.getId().indexOf('.') != -1)
257                         pointID = fInput.getId();
258                 }
259                 if (pointID == null)
260                     pointID = pluginID + "." + fInput.getId(); //$NON-NLS-1$
261
if (e.getHref().equals("search")) { //$NON-NLS-1$
262
new FindReferencesAction(fInput, pluginID).run();
263                 } else if (e.getHref().equals("open")) { //$NON-NLS-1$
264
OpenSchemaAction action = new OpenSchemaAction();
265                     action.setInput(pointID);
266                     action.setEnabled(true);
267                     action.run();
268                 } else {
269                     new ShowDescriptionAction(pointID).run();
270                 }
271             }
272         });
273         
274         fIdEntry.setEditable(isEditable());
275         fNameEntry.setEditable(isEditable());
276         fSchemaEntry.setEditable(isEditable());
277         toolkit.paintBordersFor(client);
278         section.setClient(client);
279         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
280         model.addModelChangedListener(this);
281         markDetailsPart(section);
282     }
283
284     public void dispose() {
285         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
286         if (model!=null)
287             model.removeModelChangedListener(this);
288         super.dispose();
289     }
290     public void modelChanged(IModelChangedEvent e) {
291         if (e.getChangeType()==IModelChangedEvent.CHANGE) {
292             Object JavaDoc obj = e.getChangedObjects()[0];
293             if (obj.equals(fInput))
294                 refresh();
295         }
296     }
297     private void update() {
298         fIdEntry.setValue(
299                 fInput != null && fInput.getId() != null ? fInput.getId() : "", //$NON-NLS-1$
300
true);
301         fNameEntry.setValue(fInput != null && fInput.getName() != null ? fInput
302                 .getName() : "", true); //$NON-NLS-1$
303
fSchemaEntry.setValue(fInput != null && fInput.getSchema() != null ? fInput
304                 .getSchema() : "", true); //$NON-NLS-1$
305
updateRichText();
306     }
307     public void cancelEdit() {
308         fIdEntry.cancelEdit();
309         fNameEntry.cancelEdit();
310         fSchemaEntry.cancelEdit();
311         updateRichText();
312         super.cancelEdit();
313     }
314     private void updateRichText() {
315         boolean hasSchema = fSchemaEntry.getValue().length() > 0;
316         if (hasSchema && fRichTextData == SCHEMA_RTEXT_DATA)
317             return;
318         if (!hasSchema && fRichTextData == NO_SCHEMA_RTEXT_DATA)
319             return;
320         fRichTextData = hasSchema ? SCHEMA_RTEXT_DATA : NO_SCHEMA_RTEXT_DATA;
321         fRichText.setText(fRichTextData, true, false);
322         getManagedForm().getForm().reflow(true);
323     }
324     private void openSchemaFile(final IFile file) {
325         final IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
326
327         Display d = ww.getShell().getDisplay();
328         d.asyncExec(new Runnable JavaDoc() {
329             public void run() {
330                 try {
331                     String JavaDoc editorId = IPDEUIConstants.SCHEMA_EDITOR_ID;
332                     ww.getActivePage().openEditor(
333                         new FileEditorInput(file),
334                         editorId);
335                 } catch (PartInitException e) {
336                     PDEPlugin.logException(e);
337                 }
338             }
339         });
340     }
341     
342     private void generateSchema() {
343         final IProject project = getPage().getPDEEditor().getCommonProject();
344         BusyIndicator
345             .showWhile(getPage().getPartControl().getDisplay(), new Runnable JavaDoc() {
346             public void run() {
347                 NewSchemaFileWizard wizard =
348                     new NewSchemaFileWizard(project, fInput, true);
349                 WizardDialog dialog =
350                     new WizardDialog(
351                         PDEPlugin.getActiveWorkbenchShell(),
352                         wizard);
353                 dialog.create();
354                 SWTUtil.setDialogSize(dialog, 400, 450);
355                 if(dialog.open() == Window.OK)
356                     update();
357             }
358         });
359     }
360     /*
361      * (non-Javadoc)
362      *
363      * @see org.eclipse.ui.forms.IDetailsPage#inputChanged(org.eclipse.jface.viewers.IStructuredSelection)
364      */

365     public void selectionChanged(IFormPart masterPart, ISelection selection) {
366         IStructuredSelection ssel = (IStructuredSelection) selection;
367         if (ssel.size() == 1) {
368             fInput = (IPluginExtensionPoint) ssel.getFirstElement();
369         } else
370             fInput = null;
371         update();
372     }
373     /*
374      * (non-Javadoc)
375      *
376      * @see org.eclipse.ui.forms.IDetailsPage#commit()
377      */

378     public void commit(boolean onSave) {
379         fIdEntry.commit();
380         fNameEntry.commit();
381         fSchemaEntry.commit();
382         super.commit(onSave);
383     }
384     /*
385      * (non-Javadoc)
386      *
387      * @see org.eclipse.ui.forms.IDetailsPage#setFocus()
388      */

389     public void setFocus() {
390         fIdEntry.getText().setFocus();
391     }
392     /*
393      * (non-Javadoc)
394      *
395      * @see org.eclipse.ui.forms.IDetailsPage#refresh()
396      */

397     public void refresh() {
398         update();
399         super.refresh();
400     }
401     
402 }
403
Popular Tags