KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
13
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.pde.core.IModelChangedEvent;
18 import org.eclipse.pde.core.plugin.IPluginAttribute;
19 import org.eclipse.pde.core.plugin.IPluginElement;
20 import org.eclipse.pde.core.plugin.IPluginModelBase;
21 import org.eclipse.pde.internal.core.ischema.IMetaAttribute;
22 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
23 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
24 import org.eclipse.pde.internal.core.ischema.ISchemaRestriction;
25 import org.eclipse.pde.internal.core.ischema.ISchemaSimpleType;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
28 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
29 import org.eclipse.pde.internal.ui.editor.PDESection;
30 import org.eclipse.pde.internal.ui.editor.plugin.rows.BooleanAttributeRow;
31 import org.eclipse.pde.internal.ui.editor.plugin.rows.ChoiceAttributeRow;
32 import org.eclipse.pde.internal.ui.editor.plugin.rows.ClassAttributeRow;
33 import org.eclipse.pde.internal.ui.editor.plugin.rows.ExtensionAttributeRow;
34 import org.eclipse.pde.internal.ui.editor.plugin.rows.ResourceAttributeRow;
35 import org.eclipse.pde.internal.ui.editor.plugin.rows.TextAttributeRow;
36 import org.eclipse.pde.internal.ui.editor.plugin.rows.TranslatableAttributeRow;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.ui.forms.IFormPart;
41 import org.eclipse.ui.forms.widgets.ExpandableComposite;
42 import org.eclipse.ui.forms.widgets.FormToolkit;
43 import org.eclipse.ui.forms.widgets.Section;
44 import org.eclipse.ui.forms.widgets.SharedScrolledComposite;
45
46
47 public class ExtensionElementDetails extends AbstractPluginElementDetails {
48     private IPluginElement input;
49     private ISchemaElement schemaElement;
50     private ArrayList JavaDoc rows;
51     private Section section;
52
53     
54     /**
55      * @param masterSection
56      * @param schemaElement
57      */

58     public ExtensionElementDetails(PDESection masterSection,
59             ISchemaElement schemaElement) {
60         super(masterSection);
61         this.schemaElement = schemaElement;
62         rows = new ArrayList JavaDoc();
63     }
64     
65     public String JavaDoc getContextId() {
66         return PluginInputContext.CONTEXT_ID;
67     }
68     public void fireSaveNeeded() {
69         markDirty();
70         getPage().getPDEEditor().fireSaveNeeded(getContextId(), false);
71     }
72     public PDEFormPage getPage() {
73         return (PDEFormPage) getManagedForm().getContainer();
74     }
75     public boolean isEditable() {
76         return getPage().getPDEEditor().getAggregateModel().isEditable();
77     }
78     /*
79      * (non-Javadoc)
80      *
81      * @see org.eclipse.ui.forms.IDetailsPage#createContents(org.eclipse.swt.widgets.Composite)
82      */

83     public void createContents(Composite parent) {
84         parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
85         FormToolkit toolkit = getManagedForm().getToolkit();
86         section = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR
87                 | Section.DESCRIPTION);
88         section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
89         section.setText(PDEUIMessages.ExtensionElementDetails_title);
90         section.setDescription(""); //$NON-NLS-1$
91
section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
92         section.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
93         
94         // Align the master and details section headers (misalignment caused
95
// by section toolbar icons)
96
getPage().alignSectionHeaders(getMasterSection().getSection(),
97                 section);
98         
99         Composite client = toolkit.createComposite(section);
100         int span = 2;
101         GridLayout glayout = FormLayoutFactory.createSectionClientGridLayout(false, span);
102         client.setLayout(glayout);
103         client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
104         
105         if (schemaElement != null) {
106             ISchemaAttribute atts[] = schemaElement.getAttributes();
107             // Compute horizontal span
108
for (int i = 0; i < atts.length; i++) {
109                 if (isEditable() && (atts[i].getKind() == IMetaAttribute.JAVA
110                         || atts[i].getKind() == IMetaAttribute.RESOURCE)) {
111                     span = 3;
112                     break;
113                 }
114             }
115             glayout.numColumns = span;
116             // Add required attributes first
117
for (int i = 0; i < atts.length; i++) {
118                 if (atts[i].getUse() == ISchemaAttribute.REQUIRED)
119                     rows
120                             .add(createAttributeRow(atts[i], client, toolkit,
121                                     span));
122             }
123             // Add the rest
124
for (int i = 0; i < atts.length; i++) {
125                 if (atts[i].getUse() != ISchemaAttribute.REQUIRED)
126                     rows
127                             .add(createAttributeRow(atts[i], client, toolkit,
128                                     span));
129             }
130             createSpacer(toolkit, client, span);
131         }
132         else {
133             // no schema - delay until input is set
134
}
135         toolkit.paintBordersFor(client);
136         section.setClient(client);
137         // Dynamically add focus listeners to all the section client's
138
// children in order to track the last focus control
139
getPage().addLastFocusListeners(client);
140         
141         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
142         model.addModelChangedListener(this);
143         markDetailsPart(section);
144     }
145     private ExtensionAttributeRow createAttributeRow(ISchemaAttribute att,
146             Composite parent, FormToolkit toolkit, int span) {
147         ExtensionAttributeRow row;
148         if (att.getKind() == IMetaAttribute.JAVA)
149             row = new ClassAttributeRow(this, att);
150         else if (att.getKind() == IMetaAttribute.RESOURCE)
151             row = new ResourceAttributeRow(this, att);
152         else if (att.isTranslatable())
153             row = new TranslatableAttributeRow(this, att);
154         else {
155             ISchemaSimpleType type = att.getType();
156             if (type.getName().equals("boolean")) //$NON-NLS-1$
157
row = new BooleanAttributeRow(this, att);
158             else {
159                 ISchemaRestriction restriction = type.getRestriction();
160                 if (restriction != null)
161                     row = new ChoiceAttributeRow(this, att);
162                 else
163                     row = new TextAttributeRow(this, att);
164             }
165         }
166         row.createContents(parent, toolkit, span);
167         return row;
168     }
169     
170     private ExtensionAttributeRow createAttributeRow(IPluginAttribute att,
171             Composite parent, FormToolkit toolkit, int span) {
172         ExtensionAttributeRow row;
173         row = new TextAttributeRow(this, att);
174         row.createContents(parent, toolkit, span);
175         return row;
176     }
177     
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.eclipse.ui.forms.IDetailsPage#inputChanged(org.eclipse.jface.viewers.IStructuredSelection)
182      */

183     public void selectionChanged(IFormPart masterPart, ISelection selection) {
184         IStructuredSelection ssel = (IStructuredSelection) selection;
185         if (ssel.size() == 1) {
186             input = (IPluginElement) ssel.getFirstElement();
187         } else
188             input = null;
189         update();
190     }
191
192     public void modelChanged(IModelChangedEvent e) {
193         if (e.getChangeType()==IModelChangedEvent.CHANGE) {
194             Object JavaDoc obj = e.getChangedObjects()[0];
195             if (obj.equals(input)) {
196                 // do smart update (update only the row whose property changed
197
String JavaDoc property = e.getChangedProperty();
198                 if (property != null) {
199                     for (int i = 0; i < rows.size(); i++) {
200                         ExtensionAttributeRow row = (ExtensionAttributeRow) rows.get(i);
201                         ISchemaAttribute attribute = row.getAttribute();
202                         if (attribute == null) {
203                             continue;
204                         }
205                         String JavaDoc name = attribute.getName();
206                         if (name == null) {
207                             continue;
208                         }
209                         if (name.equals(property)) {
210                             row.setInput(input);
211                         }
212                     }
213                 } else
214                     refresh();
215             }
216         }
217     }
218     
219     private void update() {
220         updateDescription();
221         if (schemaElement==null)
222             updateRows();
223         for (int i = 0; i < rows.size(); i++) {
224             ExtensionAttributeRow row = (ExtensionAttributeRow) rows.get(i);
225             row.setInput(input);
226         }
227     }
228     private void updateRows() {
229         if (input==null) return;
230         IPluginAttribute [] atts = input.getAttributes();
231         FormToolkit toolkit = getManagedForm().getToolkit();
232         boolean rowsAdded=false;
233         for (int i=0; i<atts.length; i++) {
234             if (!hasAttribute(atts[i].getName())) {
235                 rows.add(createAttributeRow(atts[i], (Composite)section.getClient(),
236                         toolkit, 2));
237                 rowsAdded=true;
238             }
239         }
240         if (rowsAdded) {
241             ((Composite)section.getClient()).layout(true);
242             section.layout(true);
243             section.getParent().layout(true);
244             reflow();
245         }
246     }
247     private void reflow() {
248         Composite parent = section.getParent();
249         while (parent!=null) {
250             if (parent instanceof SharedScrolledComposite) {
251                 ((SharedScrolledComposite)parent).reflow(true);
252                 return;
253             }
254             parent = parent.getParent();
255         }
256     }
257     private boolean hasAttribute(String JavaDoc attName) {
258         for (int i=0; i<rows.size(); i++) {
259             ExtensionAttributeRow row = (ExtensionAttributeRow)rows.get(i);
260             if (row.getName().equals(attName))
261                 return true;
262         }
263         return false;
264     }
265     private void updateDescription() {
266         if (input != null) {
267             if (0 == input.getAttributeCount()) {
268                 section.setDescription(PDEUIMessages.ExtensionElementDetails_descNoAttributes);
269             } else {
270                 String JavaDoc iname = input.getName();
271                 section.setDescription(NLS.bind(PDEUIMessages.ExtensionElementDetails_setDesc, iname));
272             }
273         } else {
274             // no extensions = no description
275
section.setDescription(""); //$NON-NLS-1$
276
}
277         section.layout();
278     }
279     /*
280      * (non-Javadoc)
281      *
282      * @see org.eclipse.ui.forms.IDetailsPage#commit()
283      */

284     public void commit(boolean onSave) {
285         for (int i = 0; i < rows.size(); i++) {
286             ExtensionAttributeRow row = (ExtensionAttributeRow) rows.get(i);
287             row.commit();
288         }
289         super.commit(onSave);
290     }
291     /*
292      * (non-Javadoc)
293      *
294      * @see org.eclipse.ui.forms.IDetailsPage#setFocus()
295      */

296     public void setFocus() {
297         if (rows.size() > 0)
298             ((ExtensionAttributeRow) rows.get(0)).setFocus();
299     }
300     /*
301      * (non-Javadoc)
302      *
303      * @see org.eclipse.ui.forms.IDetailsPage#dispose()
304      */

305     public void dispose() {
306         for (int i = 0; i < rows.size(); i++) {
307             ExtensionAttributeRow row = (ExtensionAttributeRow) rows.get(i);
308             row.dispose();
309         }
310         IPluginModelBase model = (IPluginModelBase)getPage().getModel();
311         if (model!=null)
312             model.removeModelChangedListener(this);
313         super.dispose();
314     }
315     /*
316      * (non-Javadoc)
317      *
318      * @see org.eclipse.ui.forms.IDetailsPage#refresh()
319      */

320     public void refresh() {
321         update();
322         super.refresh();
323     }
324 }
325
Popular Tags