KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > GrammarSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.schema;
12
13 import org.eclipse.jface.action.*;
14 import org.eclipse.jface.viewers.*;
15 import org.eclipse.pde.core.IModelChangedEvent;
16 import org.eclipse.pde.internal.core.ischema.*;
17 import org.eclipse.pde.internal.core.schema.*;
18 import org.eclipse.pde.internal.ui.*;
19 import org.eclipse.pde.internal.ui.editor.*;
20 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.SashForm;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.*;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.ui.actions.ActionFactory;
27 import org.eclipse.ui.forms.*;
28 import org.eclipse.ui.forms.FormColors;
29 import org.eclipse.ui.forms.widgets.*;
30
31 public class GrammarSection extends PDESection implements IPartSelectionListener {
32     private TreeViewer treeViewer;
33     private Text dtdLabel;
34     private PropertiesAction propertiesAction;
35     class GrammarContentProvider
36         extends DefaultContentProvider
37         implements ITreeContentProvider {
38         public Object JavaDoc[] getChildren(Object JavaDoc parent) {
39             Object JavaDoc[] children = null;
40             if (parent instanceof ISchemaElement
41                 && !(parent instanceof SchemaElementReference)) {
42                 ISchemaType type = ((ISchemaElement) parent).getType();
43                 if (type instanceof ISchemaComplexType) {
44                     Object JavaDoc compositor =
45                         ((ISchemaComplexType) type).getCompositor();
46                     if (compositor != null) {
47                         children = new Object JavaDoc[1];
48                         children[0] = compositor;
49                     }
50                 }
51             } else if (parent instanceof ISchemaCompositor) {
52                 children = ((ISchemaCompositor) parent).getChildren();
53             }
54             if (children == null)
55                 children = new Object JavaDoc[0];
56             return children;
57         }
58         public boolean hasChildren(Object JavaDoc parent) {
59             return getChildren(parent).length > 0;
60         }
61         public Object JavaDoc getParent(Object JavaDoc child) {
62             return null;
63         }
64         public Object JavaDoc[] getElements(Object JavaDoc parent) {
65             return getChildren(parent);
66         }
67     }
68     class GrammarLabelProvider extends LabelProvider {
69         public String JavaDoc getText(Object JavaDoc o) {
70             return PDEPlugin.getDefault().getLabelProvider().getText(o);
71         }
72         public Image getImage(Object JavaDoc o) {
73             if (o instanceof ISchemaObjectReference) {
74                 ISchemaObjectReference ref = (ISchemaObjectReference) o;
75                 int flags =
76                     ref.getReferencedObject() == null
77                         ? PDELabelProvider.F_ERROR
78                         : 0;
79                 return PDEPlugin.getDefault().getLabelProvider().get(
80                     PDEPluginImages.DESC_ELREF_SC_OBJ,
81                     flags);
82             }
83             return PDEPlugin.getDefault().getLabelProvider().getImage(o);
84         }
85     }
86
87     public GrammarSection(PDEFormPage page, Composite parent) {
88         super(page, parent, Section.DESCRIPTION);
89         getSection().setText(PDEUIMessages.SchemaEditor_GrammarSection_title);
90         getSection().setDescription(PDEUIMessages.SchemaEditor_GrammarSection_desc);
91         createClient(getSection(), page.getManagedForm().getToolkit());
92     }
93     
94     public void createClient(
95         Section section,
96         FormToolkit toolkit) {
97         Composite container = toolkit.createComposite(section);
98         GridLayout layout = new GridLayout();
99         layout.marginWidth = layout.marginHeight = 2;
100         layout.verticalSpacing = toolkit.getBorderStyle()==SWT.BORDER?0:1;
101         container.setLayout(layout);
102         
103         SashForm sash = new SashForm(container, SWT.VERTICAL);
104         toolkit.adapt(sash, false, false);
105         GridData gd = new GridData(GridData.FILL_BOTH);
106         sash.setLayoutData(gd);
107
108         Composite sashCell = sash;
109         if (toolkit.getBorderStyle()==SWT.NULL)
110             sashCell = createSashCell(sash, toolkit, 1);
111         createTree(sashCell, toolkit);
112         
113         sashCell = sash;
114         if (toolkit.getBorderStyle()==SWT.NULL)
115             sashCell = createSashCell(sash, toolkit, 2);
116         dtdLabel =
117             toolkit.createText(
118                 sashCell,
119                 "", //$NON-NLS-1$
120
SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
121         //dtdLabel.setData(
122
//FormToolkit.KEY_DRAW_BORDER,
123
//FormToolkit.TREE_BORDER);
124
dtdLabel.setEditable(false);
125         dtdLabel.setForeground(
126             toolkit.getColors().getColor(FormColors.TITLE));
127         //gd = new GridData(GridData.FILL_BOTH);
128
//dtdLabel.setLayoutData(gd);
129
updateDTDLabel(null);
130         
131         sash.setWeights(new int[] {3, 1});
132
133         toolkit.paintBordersFor(container);
134         section.setClient(container);
135         propertiesAction = new PropertiesAction(getPage().getPDEEditor());
136         initialize();
137     }
138
139     private Composite createSashCell(Composite parent, FormToolkit toolkit, int marginHeight) {
140         Composite cell = toolkit.createComposite(parent);
141         FillLayout layout = new FillLayout();
142         layout.marginHeight = marginHeight;
143         layout.marginWidth = 1;
144         cell.setLayout(layout);
145         toolkit.paintBordersFor(cell);
146         return cell;
147     }
148     
149     private Control createTree(Composite parent, FormToolkit toolkit) {
150         Tree tree = toolkit.createTree(parent, SWT.SINGLE);
151
152         treeViewer = new TreeViewer(tree);
153         treeViewer.setLabelProvider(new GrammarLabelProvider());
154         treeViewer.setContentProvider(new GrammarContentProvider());
155         treeViewer.setAutoExpandLevel(999);
156         treeViewer
157             .addSelectionChangedListener(new ISelectionChangedListener() {
158             public void selectionChanged(SelectionChangedEvent e) {
159                 getPage().getPDEEditor().setSelection(e.getSelection());
160             }
161         });
162         MenuManager popupMenuManager = new MenuManager();
163         IMenuListener listener = new IMenuListener() {
164             public void menuAboutToShow(IMenuManager mng) {
165                 fillContextMenu(mng);
166             }
167         };
168         popupMenuManager.setRemoveAllWhenShown(true);
169         popupMenuManager.addMenuListener(listener);
170         Menu menu = popupMenuManager.createContextMenu(tree);
171         tree.setMenu(menu);
172         treeViewer.addDoubleClickListener(new IDoubleClickListener() {
173             public void doubleClick(DoubleClickEvent event) {
174                 propertiesAction.run();
175             }
176         });
177         return tree;
178     }
179     public void dispose() {
180         ISchema schema = (ISchema) getPage().getModel();
181         if (schema!= null) schema.removeModelChangedListener(this);
182         super.dispose();
183     }
184     
185     public boolean doGlobalAction(String JavaDoc actionId) {
186         if (actionId.equals(ActionFactory.DELETE.getId())) {
187             ISelection sel = treeViewer.getSelection();
188             Object JavaDoc obj = ((IStructuredSelection) sel).getFirstElement();
189             if (obj != null)
190                 handleDelete(obj);
191             return true;
192         }
193         return false;
194     }
195     protected void fillContextMenu(IMenuManager manager) {
196         ISelection selection = treeViewer.getSelection();
197         final Object JavaDoc object =
198             ((IStructuredSelection) selection).getFirstElement();
199         ISchemaElement sourceElement = (ISchemaElement) treeViewer.getInput();
200
201         if (sourceElement != null) {
202             ISchema schema = sourceElement.getSchema();
203
204             MenuManager submenu =
205                 new MenuManager(PDEUIMessages.Menus_new_label);
206             MenuManager cmenu =
207                 new MenuManager(
208                     PDEUIMessages.SchemaEditor_GrammarSection_compositor);
209
210             cmenu.add(
211                 new NewCompositorAction(
212                     sourceElement,
213                     object,
214                     ISchemaCompositor.ALL));
215             cmenu.add(
216                 new NewCompositorAction(
217                     sourceElement,
218                     object,
219                     ISchemaCompositor.CHOICE));
220             cmenu.add(
221                 new NewCompositorAction(
222                     sourceElement,
223                     object,
224                     ISchemaCompositor.SEQUENCE));
225             cmenu.add(
226                 new NewCompositorAction(
227                     sourceElement,
228                     object,
229                     ISchemaCompositor.GROUP));
230             submenu.add(cmenu);
231
232             if (schema.getResolvedElementCount() > 1
233                 && object != null
234                 && object instanceof SchemaCompositor) {
235                 MenuManager refMenu =
236                     new MenuManager(
237                         PDEUIMessages.SchemaEditor_GrammarSection_reference);
238                 ISchemaElement[] elements = schema.getResolvedElements();
239                 for (int i = 0; i < elements.length; i++) {
240                     ISchemaElement element = elements[i];
241                     //if (element == sourceElement)
242
//continue;
243
refMenu.add(
244                         new NewReferenceAction(sourceElement, object, element));
245                 }
246                 submenu.add(refMenu);
247             }
248             if (object == null || object instanceof SchemaCompositor) {
249                 manager.add(submenu);
250             }
251
252             if (object != null) {
253                 manager.add(new Separator());
254                 Action deleteAction = new Action() {
255                     public void run() {
256                         handleDelete(object);
257                     }
258                 };
259                 deleteAction.setText(PDEUIMessages.Actions_delete_label);
260                 deleteAction.setEnabled(schema.isEditable());
261                 manager.add(deleteAction);
262             }
263         }
264         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(
265             manager);
266         manager.add(new Separator());
267         manager.add(propertiesAction);
268     }
269     private void handleDelete(Object JavaDoc object) {
270         if (object instanceof SchemaCompositor) {
271             SchemaCompositor compositor = (SchemaCompositor) object;
272             ISchemaObject parent = compositor.getParent();
273             if (parent instanceof ISchemaElement) {
274                 // root
275
SchemaElement element = (SchemaElement) parent;
276                 SchemaComplexType complexType =
277                     (SchemaComplexType) element.getType();
278                 if (complexType.getAttributeCount() == 0)
279                     element.setType(
280                         new SchemaSimpleType(element.getSchema(), "string")); //$NON-NLS-1$
281
else
282                     complexType.setCompositor(null);
283             } else if (parent instanceof SchemaCompositor) {
284                 ((SchemaCompositor) parent).removeChild(compositor);
285             }
286         } else if (object instanceof SchemaElementReference) {
287             SchemaCompositor compositor =
288                 (SchemaCompositor) ((SchemaElementReference) object)
289                     .getCompositor();
290             compositor.removeChild((SchemaElementReference) object);
291         }
292     }
293     public void initialize() {
294         ISchema schema = (ISchema) getPage().getModel();
295         schema.addModelChangedListener(this);
296     }
297     public void modelChanged(IModelChangedEvent e) {
298         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
299             markStale();
300             return;
301         }
302         Object JavaDoc obj = e.getChangedObjects()[0];
303         if (obj instanceof ISchemaCompositor
304             || obj instanceof ISchemaObjectReference) {
305             final ISchemaObject sobj = (ISchemaObject) obj;
306             ISchemaObject parent = sobj.getParent();
307             if (e.getChangeType() == IModelChangedEvent.CHANGE) {
308                 treeViewer.update(sobj, null);
309             } else if (e.getChangeType() == IModelChangedEvent.INSERT) {
310                 treeViewer.add(parent, sobj);
311                 treeViewer.getTree().getDisplay().asyncExec(new Runnable JavaDoc() {
312                     public void run() {
313                         treeViewer.setSelection(
314                             new StructuredSelection(sobj),
315                             true);
316                     }
317                 });
318
319             } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
320                 treeViewer.remove(sobj);
321                 treeViewer.setSelection(new StructuredSelection(parent), true);
322             }
323         } else if (obj instanceof ISchemaComplexType) {
324             // first compositor added/removed
325
treeViewer.refresh();
326             if (e.getChangeType() == IModelChangedEvent.INSERT) {
327                 ISchemaComplexType type = (ISchemaComplexType) obj;
328                 final ISchemaCompositor compositor = type.getCompositor();
329                 treeViewer.getTree().getDisplay().asyncExec(new Runnable JavaDoc() {
330                     public void run() {
331                         treeViewer.setSelection(
332                             new StructuredSelection(compositor),
333                             true);
334                     }
335                 });
336             }
337         } else if (obj instanceof ISchemaElement) {
338             if (e.getChangeType() == IModelChangedEvent.CHANGE
339                 && e.getChangedProperty() == SchemaElement.P_TYPE) {
340                 treeViewer.refresh();
341             }
342         }
343
344         updateDTDLabel((ISchemaObject) treeViewer.getInput());
345     }
346     public void refresh() {
347         treeViewer.refresh();
348         super.refresh();
349     }
350     public void selectionChanged(IFormPart part, ISelection selection) {
351         if (!(part instanceof ElementSection))
352             return;
353         Object JavaDoc changeObject = ((IStructuredSelection)selection).getFirstElement();
354         if (changeObject instanceof ISchemaAttribute) {
355             changeObject = ((ISchemaAttribute) changeObject).getParent();
356         }
357         if (changeObject == treeViewer.getInput())
358             return;
359         ISchemaObject element = (ISchemaObject) changeObject;
360         updateDTDLabel(element);
361         treeViewer.setInput(changeObject);
362     }
363     private void updateDTDLabel(ISchemaObject object) {
364         String JavaDoc prefix = PDEUIMessages.SchemaEditor_GrammarSection_dtd + "\n"; //$NON-NLS-1$
365
String JavaDoc text = ""; //$NON-NLS-1$
366
if (object != null) {
367             ISchemaElement element = (ISchemaElement) object;
368             text = element.getDTDRepresentation(false);
369         }
370         dtdLabel.setText(prefix + text);
371     }
372     protected void handleDoubleClick(IStructuredSelection selection) {
373         propertiesAction.run();
374     }
375 }
376
Popular Tags