KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.pde.core.IIdentifiable;
19 import org.eclipse.pde.core.IModelChangedEvent;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.pde.core.plugin.IPluginObject;
24 import org.eclipse.pde.internal.core.ICoreConstants;
25 import org.eclipse.pde.internal.core.PDEStateHelper;
26 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
27 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
28 import org.eclipse.pde.internal.core.schema.Schema;
29 import org.eclipse.pde.internal.ui.PDEPlugin;
30 import org.eclipse.pde.internal.ui.PDEPluginImages;
31 import org.eclipse.pde.internal.ui.PDEUIMessages;
32 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
33 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
34 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
35 import org.eclipse.pde.internal.ui.editor.PDESection;
36 import org.eclipse.pde.internal.ui.editor.actions.OpenSchemaAction;
37 import org.eclipse.pde.internal.ui.parts.FormEntry;
38 import org.eclipse.pde.internal.ui.search.FindDeclarationsAction;
39 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Label;
45 import org.eclipse.ui.forms.IFormPart;
46 import org.eclipse.ui.forms.events.HyperlinkAdapter;
47 import org.eclipse.ui.forms.events.HyperlinkEvent;
48 import org.eclipse.ui.forms.widgets.ExpandableComposite;
49 import org.eclipse.ui.forms.widgets.FormText;
50 import org.eclipse.ui.forms.widgets.FormToolkit;
51 import org.eclipse.ui.forms.widgets.Section;
52
53 public class ExtensionDetails extends AbstractPluginElementDetails {
54     private IPluginExtension input;
55     private FormEntry id;
56     private FormEntry name;
57     private FormText rtext;
58
59     private static final String JavaDoc RTEXT_DATA =
60         PDEUIMessages.ExtensionDetails_extensionPointLinks;
61     
62     /**
63      * @param masterSection
64      */

65     public ExtensionDetails(PDESection masterSection) {
66         super(masterSection);
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.ui.forms.IDetailsPage#createContents(org.eclipse.swt.widgets.Composite)
71      */

72     public void createContents(Composite parent) {
73         FormToolkit toolkit = getManagedForm().getToolkit();
74         parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
75
76         Section section = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR | Section.DESCRIPTION);
77         section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
78         section.setText(PDEUIMessages.ExtensionDetails_title);
79         section.setDescription(PDEUIMessages.ExtensionDetails_desc);
80         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
81         section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
82         
83         // Align the master and details section headers (misalignment caused
84
// by section toolbar icons)
85
getPage().alignSectionHeaders(getMasterSection().getSection(),
86                 section);
87         
88         Composite client = toolkit.createComposite(section);
89         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
90         client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
91
92         createIDEntryField(toolkit, client);
93         
94         createNameEntryField(toolkit, client);
95         
96         createSpacer(toolkit, client, 2);
97         
98         Composite container = toolkit.createComposite(parent, SWT.NONE);
99         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1));
100         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
101         
102         rtext = toolkit.createFormText(container, true);
103         rtext.setImage("desc", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_DOC_SECTION_OBJ)); //$NON-NLS-1$
104
rtext.setImage("open", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_SCHEMA_OBJ)); //$NON-NLS-1$
105
rtext.setImage("search", PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_PSEARCH_OBJ)); //$NON-NLS-1$
106
rtext.addHyperlinkListener(new HyperlinkAdapter() {
107             public void linkActivated(HyperlinkEvent e) {
108                 if (e.getHref().equals("search")){ //$NON-NLS-1$
109
FindDeclarationsAction findDeclarationsAction = new FindDeclarationsAction(input);
110                     findDeclarationsAction.run();
111                 } else if (e.getHref().equals("open")) { //$NON-NLS-1$
112
OpenSchemaAction action = new OpenSchemaAction();
113                     action.setInput(input);
114                     action.setEnabled(true);
115                     action.run();
116                 } else {
117                     if (input == null || input.getPoint() == null)
118                         return;
119                     IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(input.getPoint());
120                     if (point != null){
121                         ShowDescriptionAction showDescAction = new ShowDescriptionAction(point);
122                         showDescAction.run();
123                     } else {
124                         showNoExtensionPointMessage();
125                     }
126                 }
127             }
128         });
129         rtext.setText(RTEXT_DATA, true, false);
130         id.setEditable(isEditable());
131         name.setEditable(isEditable());
132         
133         toolkit.paintBordersFor(client);
134         section.setClient(client);
135         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
136         model.addModelChangedListener(this);
137         markDetailsPart(section);
138     }
139     
140     /**
141      * @param toolkit
142      * @param client
143      */

144     private void createNameEntryField(FormToolkit toolkit, Composite client) {
145         name = new FormEntry(client, toolkit, PDEUIMessages.ExtensionDetails_name, null, false);
146         name.setFormEntryListener(new FormEntryAdapter(this) {
147             public void textValueChanged(FormEntry entry) {
148                 if (input!=null)
149                     try {
150                         input.setName(name.getValue());
151                     } catch (CoreException e) {
152                         PDEPlugin.logException(e);
153                     }
154             }
155         });
156     }
157     
158     /**
159      * @param toolkit
160      * @param client
161      */

162     private void createIDEntryField(FormToolkit toolkit, Composite client) {
163         id = new FormEntry(client, toolkit, PDEUIMessages.ExtensionDetails_id, null, false);
164         id.setFormEntryListener(new FormEntryAdapter(this) {
165             public void textValueChanged(FormEntry entry) {
166                 if (input!=null)
167                     try {
168                         input.setId(id.getValue());
169                     } catch (CoreException e) {
170                         PDEPlugin.logException(e);
171                     }
172             }
173         });
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.ui.forms.IDetailsPage#inputChanged(org.eclipse.jface.viewers.IStructuredSelection)
178      */

179     public void selectionChanged(IFormPart part, ISelection selection) {
180         IStructuredSelection ssel = (IStructuredSelection)selection;
181         if (ssel.size()==1) {
182             input = (IPluginExtension)ssel.getFirstElement();
183         }
184         else
185             input = null;
186         update();
187     }
188     
189     private void update() {
190         id.setValue(input!=null?input.getId():null, true);
191         name.setValue(input!=null?input.getName():null, true);
192         
193         // Update the ID label
194
updateLabel(isFieldRequired(IIdentifiable.P_ID), id,
195                 PDEUIMessages.ExtensionDetails_id);
196         // Update the Name label
197
updateLabel(isFieldRequired(IPluginObject.P_NAME), name,
198                 PDEUIMessages.ExtensionDetails_name);
199     }
200     
201     /**
202      * Denote a field as required by updating their label
203      * @param attributeName
204      * @param field
205      */

206     private boolean isFieldRequired(String JavaDoc attributeName) {
207         // Ensure we have input
208
if (input == null) {
209             return false;
210         }
211         // Get the associated schema
212
Object JavaDoc object = input.getSchema();
213         // Ensure we have a schema
214
if ((object == null) ||
215                 (object instanceof Schema) == false) {
216             return false;
217         }
218         Schema schema = (Schema)object;
219         // Find the extension element
220
ISchemaElement element =
221             schema.findElement(ICoreConstants.EXTENSION_NAME);
222         // Ensure we found the element
223
if (element == null) {
224             return false;
225         }
226         // Get the attribute
227
ISchemaAttribute attribute = element.getAttribute(attributeName);
228         // Ensure we found the attribute
229
if (attribute == null) {
230             return false;
231         }
232         // Determine whether the attribute is required
233
if (attribute.getUse() == ISchemaAttribute.REQUIRED) {
234             return true;
235         }
236         return false;
237     }
238     
239     /**
240      * @param field
241      * @param required
242      */

243     private void updateLabel(boolean required, FormEntry field, String JavaDoc label) {
244         // Get the label
245
Control control = field.getLabel();
246         // Ensure label is defined
247
if ((control == null) ||
248                 ((control instanceof Label) == false)) {
249             return;
250         }
251         Label labelControl = ((Label)control);
252         // If the label is required, add the '*' to indicate that
253
if (required) {
254             labelControl.setText(label + '*' + ':');
255         } else {
256             labelControl.setText(label + ':');
257         }
258         // Force the label's parent composite to relayout because
259
// clippage can occur when updating the text
260
labelControl.getParent().layout();
261     }
262     
263     public void cancelEdit() {
264         id.cancelEdit();
265         name.cancelEdit();
266         super.cancelEdit();
267     }
268     /* (non-Javadoc)
269      * @see org.eclipse.ui.forms.IDetailsPage#commit()
270      */

271     public void commit(boolean onSave) {
272         id.commit();
273         name.commit();
274         super.commit(onSave);
275     }
276     /* (non-Javadoc)
277      * @see org.eclipse.ui.forms.IDetailsPage#setFocus()
278      */

279     public void setFocus() {
280         id.getText().setFocus();
281     }
282     
283     public void dispose() {
284         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
285         if (model!=null)
286             model.removeModelChangedListener(this);
287         super.dispose();
288     }
289
290     public void modelChanged(IModelChangedEvent e) {
291         if (e.getChangeType()==IModelChangedEvent.CHANGE) {
292             Object JavaDoc obj = e.getChangedObjects()[0];
293             if (obj.equals(input))
294                 refresh();
295         }
296     }
297     /* (non-Javadoc)
298      * @see org.eclipse.ui.forms.IDetailsPage#refresh()
299      */

300     public void refresh() {
301         update();
302         super.refresh();
303     }
304     /* (non-Javadoc)
305      * @see org.eclipse.pde.internal.ui.neweditor.IContextPart#fireSaveNeeded()
306      */

307     public void fireSaveNeeded() {
308         markDirty();
309         PDEFormPage page = (PDEFormPage)getManagedForm().getContainer();
310         page.getPDEEditor().fireSaveNeeded(getContextId(), false);
311     }
312     /* (non-Javadoc)
313      * @see org.eclipse.pde.internal.ui.neweditor.IContextPart#getContextId()
314      */

315     public String JavaDoc getContextId() {
316         return PluginInputContext.CONTEXT_ID;
317     }
318     public PDEFormPage getPage() {
319         return (PDEFormPage)getManagedForm().getContainer();
320     }
321     public boolean isEditable() {
322         return getPage().getPDEEditor().getAggregateModel().isEditable();
323     }
324     private void showNoExtensionPointMessage() {
325         String JavaDoc title = PDEUIMessages.ExtensionDetails_noPoint_title;
326         String JavaDoc message = NLS.bind(PDEUIMessages.ShowDescriptionAction_noPoint_desc, input.getPoint());
327         
328         MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), title, message);
329     }
330 }
331
Popular Tags