KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.schema;
12 import java.util.Iterator JavaDoc;
13
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.action.Separator;
19 import org.eclipse.jface.action.ToolBarManager;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.pde.core.IModelChangedEvent;
28 import org.eclipse.pde.internal.core.ICoreConstants;
29 import org.eclipse.pde.internal.core.ischema.ISchema;
30 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
31 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType;
32 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
33 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
34 import org.eclipse.pde.internal.core.ischema.ISchemaInclude;
35 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
36 import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference;
37 import org.eclipse.pde.internal.core.ischema.ISchemaRootElement;
38 import org.eclipse.pde.internal.core.ischema.ISchemaType;
39 import org.eclipse.pde.internal.core.schema.Schema;
40 import org.eclipse.pde.internal.core.schema.SchemaAttribute;
41 import org.eclipse.pde.internal.core.schema.SchemaCompositor;
42 import org.eclipse.pde.internal.core.schema.SchemaElement;
43 import org.eclipse.pde.internal.core.schema.SchemaElementReference;
44 import org.eclipse.pde.internal.ui.PDEPlugin;
45 import org.eclipse.pde.internal.ui.PDEUIMessages;
46 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
47 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
48 import org.eclipse.pde.internal.ui.editor.TreeSection;
49 import org.eclipse.pde.internal.ui.editor.actions.CollapseAction;
50 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
51 import org.eclipse.pde.internal.ui.parts.TreePart;
52 import org.eclipse.swt.SWT;
53 import org.eclipse.swt.dnd.Clipboard;
54 import org.eclipse.swt.dnd.DND;
55 import org.eclipse.swt.dnd.TextTransfer;
56 import org.eclipse.swt.dnd.Transfer;
57 import org.eclipse.swt.events.DisposeEvent;
58 import org.eclipse.swt.events.DisposeListener;
59 import org.eclipse.swt.graphics.Cursor;
60 import org.eclipse.swt.widgets.Composite;
61 import org.eclipse.swt.widgets.Display;
62 import org.eclipse.swt.widgets.ToolBar;
63 import org.eclipse.ui.actions.ActionFactory;
64 import org.eclipse.ui.forms.widgets.FormToolkit;
65 import org.eclipse.ui.forms.widgets.Section;
66
67 public class ElementSection extends TreeSection {
68     private TreeViewer fTreeViewer;
69     private Schema fSchema;
70     private NewElementAction fNewElementAction = new NewElementAction();
71     private NewAttributeAction fNewAttributeAction = new NewAttributeAction();
72     private Clipboard fClipboard;
73     private SchemaRearranger fRearranger;
74     private CollapseAction fCollapseAction;
75
76     class ContentProvider extends DefaultContentProvider implements ITreeContentProvider {
77         public Object JavaDoc[] getElements(Object JavaDoc object) {
78             if (object instanceof Schema) {
79                 Schema schema = (Schema) object;
80                 return schema.getElements();
81             }
82             return new Object JavaDoc[0];
83         }
84
85         public Object JavaDoc[] getChildren(Object JavaDoc parent) {
86             Object JavaDoc[] children = new Object JavaDoc[0];
87             if (parent instanceof ISchemaElement) {
88                 Object JavaDoc[] types = new Object JavaDoc[0];
89                 Object JavaDoc[] attributes = ((ISchemaElement) parent).getAttributes();
90                 ISchemaType type = ((ISchemaElement) parent).getType();
91                 if (type instanceof ISchemaComplexType) {
92                     Object JavaDoc compositor = ((ISchemaComplexType) type).getCompositor();
93                     if (compositor != null)
94                         types = new Object JavaDoc[] { compositor };
95                 }
96                 children = new Object JavaDoc[types.length + attributes.length];
97                 System.arraycopy(types, 0, children, 0, types.length);
98                 System.arraycopy(attributes, 0, children, types.length, attributes.length);
99             } else if (parent instanceof ISchemaCompositor) {
100                 children = ((ISchemaCompositor) parent).getChildren();
101             }
102             return children;
103         }
104
105         public Object JavaDoc getParent(Object JavaDoc child) {
106             if (child instanceof ISchemaObject)
107                 return ((ISchemaObject) child).getParent();
108             return null;
109         }
110
111         public boolean hasChildren(Object JavaDoc parent) {
112             if (parent instanceof ISchemaAttribute || parent instanceof ISchemaObjectReference)
113                 return false;
114             return getChildren(parent).length > 0;
115         }
116     }
117
118     public ElementSection(PDEFormPage page, Composite parent) {
119         super(page, parent, Section.DESCRIPTION, new String JavaDoc[] {
120                 PDEUIMessages.SchemaEditor_ElementSection_newElement,
121                 PDEUIMessages.SchemaEditor_ElementSection_newAttribute });
122         getSection().setText(PDEUIMessages.SchemaEditor_ElementSection_title);
123         getSection().setDescription(PDEUIMessages.SchemaEditor_ElementSection_desc);
124     }
125
126     public void createClient(Section section, FormToolkit toolkit) {
127         Composite container = createClientContainer(section, 2, toolkit);
128         createTree(container, toolkit);
129         toolkit.paintBordersFor(container);
130         section.setClient(container);
131         initialize();
132         createSectionToolbar(section, toolkit);
133     }
134
135     /**
136      * @param section
137      * @param toolkit
138      */

139     private void createSectionToolbar(Section section, FormToolkit toolkit) {
140         
141         ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
142         ToolBar toolbar = toolBarManager.createControl(section);
143         final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
144         toolbar.setCursor(handCursor);
145         // Cursor needs to be explicitly disposed
146
toolbar.addDisposeListener(new DisposeListener() {
147             public void widgetDisposed(DisposeEvent e) {
148                 if ((handCursor != null) &&
149                         (handCursor.isDisposed() == false)) {
150                     handCursor.dispose();
151                 }
152             }
153         });
154         // Add collapse action to the tool bar
155
fCollapseAction = new CollapseAction(fTreeViewer,
156                 PDEUIMessages.ExtensionsPage_collapseAll);
157         toolBarManager.add(fCollapseAction);
158
159         toolBarManager.update(true);
160
161         section.setTextClient(toolbar);
162     }
163     
164     private void createTree(Composite container, FormToolkit toolkit) {
165         TreePart treePart = getTreePart();
166         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
167         fTreeViewer = treePart.getTreeViewer();
168         fTreeViewer.setContentProvider(new ContentProvider());
169         fTreeViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
170         PDEPlugin.getDefault().getLabelProvider().connect(this);
171         initDragAndDrop();
172     }
173
174     protected void initDragAndDrop() {
175         fClipboard = new Clipboard(fTreeViewer.getControl().getDisplay());
176         int ops = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
177         Transfer[] transfers = new Transfer[] {ModelDataTransfer.getInstance(), TextTransfer.getInstance() };
178         ElementSectionDragAdapter dragAdapter = new ElementSectionDragAdapter(fTreeViewer);
179         fTreeViewer.addDragSupport(ops, transfers, dragAdapter);
180         fTreeViewer.addDropSupport(ops | DND.DROP_DEFAULT, transfers, new ElementSectionDropAdapter(dragAdapter, this));
181     }
182
183     protected TreeViewer getTreeViewer() {
184         return fTreeViewer;
185     }
186
187     public void refresh() {
188         fTreeViewer.refresh();
189         super.refresh();
190         
191         if (fTreeViewer.getSelection().isEmpty() && fSchema.getElementCount() > 0)
192         { fTreeViewer.setSelection(new StructuredSelection(fSchema.getElements()[0]));
193         }
194     }
195     
196     protected void buttonSelected(int index) {
197         switch (index) {
198         case 0:
199             handleNewElement();
200             break;
201         case 1:
202             handleNewAttribute();
203             break;
204         }
205     }
206
207     public void dispose() {
208         if (fClipboard != null) {
209             fClipboard.dispose();
210             fClipboard = null;
211         }
212         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
213         super.dispose();
214     }
215
216     public boolean doGlobalAction(String JavaDoc actionId) {
217         boolean cut = actionId.equals(ActionFactory.CUT.getId());
218         if (cut || actionId.equals(ActionFactory.DELETE.getId())) {
219             // Get the current selection
220
IStructuredSelection sel = (IStructuredSelection)fTreeViewer.getSelection();
221             // Get the first selected object
222
Object JavaDoc selectedObject = sel.getFirstElement();
223             // Ensure we have a selection
224
if (selectedObject == null) {
225                 return true;
226             }
227             // Get the current index of the first selected object in the model
228
int index = fSchema.indexOf(selectedObject);
229             // Delete the selection
230
handleDelete(sel);
231             // The the item deleted was the last item, select the second-last
232
// item (if their is one)
233
if (index == fSchema.getElementCount()) {
234                 --index;
235             }
236             // Make the selection if the index exists (e.g. there are still
237
// items after the deletion)
238
if (index != -1) {
239                 fTreeViewer.setSelection(
240                         new StructuredSelection(fSchema.getElementAt(index)));
241             }
242             // if cutting delete here and let the editor transfer
243
// the selection to the clipboard
244
return !cut;
245         }
246         if (actionId.equals(ActionFactory.PASTE.getId())) {
247             doPaste();
248             return true;
249         }
250         return false;
251     }
252
253     public boolean setFormInput(Object JavaDoc object) {
254         if (object instanceof ISchemaElement
255                 || object instanceof ISchemaAttribute
256                 || object instanceof ISchemaCompositor) {
257             fTreeViewer.setSelection(new StructuredSelection(object), true);
258             
259             ISelection selection = fTreeViewer.getSelection();
260             if (selection != null && !selection.isEmpty())
261                 return true;
262             if (object instanceof ISchemaElement) {
263                 ISchemaElement found = fSchema.findElement(((ISchemaElement)object).getName());
264                 if (found != null)
265                     fTreeViewer.setSelection(new StructuredSelection(found), true);
266                 return found != null;
267             }
268         }
269         return false;
270     }
271
272     protected void fillContextMenu(IMenuManager manager) {
273         final ISelection selection = fTreeViewer.getSelection();
274         final Object JavaDoc object = ((IStructuredSelection) selection).getFirstElement();
275
276         MenuManager submenu = new MenuManager(PDEUIMessages.Menus_new_label);
277         if (object == null || object instanceof SchemaElement) {
278             fNewElementAction.setSchema(fSchema);
279             fNewElementAction.setEnabled(fSchema.isEditable());
280             submenu.add(fNewElementAction);
281         }
282         if (object != null) {
283             ISchemaElement element = null;
284             if (object instanceof SchemaElement)
285                 element = (SchemaElement) object;
286             else if (object instanceof SchemaAttribute)
287                 element = (SchemaElement) ((SchemaAttribute) object).getParent();
288             
289             if (element != null && !(element instanceof ISchemaRootElement)
290                     && !(element instanceof ISchemaObjectReference)) { //$NON-NLS-1$
291
fNewAttributeAction.setElement((SchemaElement) element);
292                 fNewAttributeAction.setEnabled(fSchema.isEditable());
293                 submenu.add(fNewAttributeAction);
294             }
295         }
296         if (object instanceof SchemaElement || object instanceof SchemaCompositor) {
297             ISchemaElement sourceElement = null;
298             ISchemaObject schemaObject = (ISchemaObject) object;
299             while (schemaObject != null) {
300                 if (schemaObject instanceof ISchemaElement) {
301                     sourceElement = (ISchemaElement) schemaObject;
302                     break;
303                 }
304                 schemaObject = schemaObject.getParent();
305             }
306             if (sourceElement != null) {
307                 ISchema schema = sourceElement.getSchema();
308                 MenuManager cmenu = new MenuManager(PDEUIMessages.ElementSection_compositorMenu);
309                 cmenu.add(new NewCompositorAction(sourceElement, object, ISchemaCompositor.CHOICE));
310                 cmenu.add(new NewCompositorAction(sourceElement, object, ISchemaCompositor.SEQUENCE));
311                 if (submenu.getItems().length > 0)
312                     submenu.add(new Separator());
313                 submenu.add(cmenu);
314                 if (object instanceof SchemaCompositor) {
315                     MenuManager refMenu = new MenuManager(PDEUIMessages.ElementSection_referenceMenu);
316                     ISchemaElement[] elements = schema.getResolvedElements();
317                     for (int i = 0; i < elements.length; i++) {
318                         refMenu.add(new NewReferenceAction(sourceElement,object, elements[i]));
319                     }
320                     if (!refMenu.isEmpty())
321                         submenu.add(refMenu);
322                 }
323             }
324         }
325         manager.add(submenu);
326         if (object != null) {
327             if (!(object instanceof ISchemaRootElement)) { //$NON-NLS-1$
328
manager.add(new Separator());
329                 Action deleteAction = new Action() {
330                     public void run() {
331                         handleDelete((IStructuredSelection) selection);
332                     }
333                 };
334                 deleteAction.setText(PDEUIMessages.Actions_delete_label);
335                 deleteAction.setEnabled(fSchema.isEditable());
336                 manager.add(deleteAction);
337             }
338         }
339         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
340         manager.add(new Separator());
341     }
342
343     private void handleDelete(IStructuredSelection selection) {
344         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
345             handleDelete(iter.next());
346         }
347     }
348
349     private void handleDelete(Object JavaDoc object) {
350         if (object instanceof SchemaElementReference) {
351             fRearranger.deleteReference((SchemaElementReference)object);
352         } else if (object instanceof ISchemaElement) {
353             fRearranger.deleteElement((ISchemaElement)object);
354         } else if (object instanceof ISchemaAttribute) {
355             fRearranger.deleteAttribute((ISchemaAttribute)object);
356         } else if (object instanceof ISchemaCompositor) {
357             fRearranger.deleteCompositor((ISchemaCompositor)object);
358         }
359     }
360
361     private void handleNewAttribute() {
362         Object JavaDoc object = ((IStructuredSelection) fTreeViewer.getSelection()).getFirstElement();
363         if (object != null) {
364             SchemaElement element = null;
365             if (object instanceof SchemaElement)
366                 element = (SchemaElement) object;
367             else if (object instanceof SchemaAttribute)
368                 element = (SchemaElement) ((SchemaAttribute) object).getParent();
369             
370             if (element != null && !(element instanceof ISchemaRootElement)) { //$NON-NLS-1$
371
fNewAttributeAction.setElement(element);
372                 fNewAttributeAction.run();
373             }
374         }
375     }
376
377     private void handleNewElement() {
378         fNewElementAction.setSchema(fSchema);
379         fNewElementAction.run();
380     }
381
382     public void initialize() {
383         this.fSchema = (Schema) getPage().getModel();
384         fRearranger = new SchemaRearranger(fSchema);
385         fTreeViewer.setInput(fSchema);
386         getTreePart().setButtonEnabled(0, fSchema.isEditable());
387         getTreePart().setButtonEnabled(1, false);
388     }
389
390     public void handleModelChanged(IModelChangedEvent e) {
391         if (e.getChangedProperty() != null
392                 && e.getChangedProperty().equals(ISchemaObject.P_DESCRIPTION))
393             return;
394         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
395             handleModelEventWorldChanged(e);
396             return;
397         }
398         Object JavaDoc[] objects = e.getChangedObjects();
399         for (int i = 0; i < objects.length; i++) {
400             Object JavaDoc obj = objects[0];
401             if (obj instanceof SchemaElementReference) {
402                 fTreeViewer.refresh(((SchemaElementReference)obj).getCompositor());
403                 if (e.getChangeType() == IModelChangedEvent.INSERT)
404                     fTreeViewer.setSelection(new StructuredSelection(obj), true);
405             } else if (obj instanceof ISchemaElement || obj instanceof ISchemaAttribute) {
406                 if (e.getChangeType() == IModelChangedEvent.CHANGE) {
407                     String JavaDoc changeProp = e.getChangedProperty();
408                     if (changeProp != null
409                             && (changeProp.equals(ISchemaObject.P_NAME)
410                             || changeProp.equals(SchemaAttribute.P_KIND)))
411                         fTreeViewer.update(obj, null);
412                     Object JavaDoc typeCheck = e.getNewValue();
413                     if (typeCheck instanceof ISchemaComplexType
414                             && changeProp.equals(SchemaElement.P_TYPE)
415                             && obj instanceof ISchemaElement) {
416                         fTreeViewer.refresh(typeCheck);
417                         fTreeViewer.setSelection(new StructuredSelection(typeCheck), true);
418                     } else {
419                         fTreeViewer.refresh(obj);
420                     }
421                 } else if (e.getChangeType() == IModelChangedEvent.INSERT) {
422                     ISchemaObject sobj = (ISchemaObject) obj;
423                     ISchemaObject parent = sobj.getParent();
424                     fTreeViewer.refresh(parent);
425                     fTreeViewer.setSelection(new StructuredSelection(obj), true);
426                 } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
427                     ISchemaObject sobj = (ISchemaObject) obj;
428                     ISchemaObject parent = sobj.getParent();
429                     fTreeViewer.remove(obj);
430                     fTreeViewer.setSelection(new StructuredSelection(parent), true);
431                 }
432             } else if (obj instanceof ISchemaCompositor || obj instanceof ISchemaObjectReference) {
433                 final ISchemaObject sobj = (ISchemaObject) obj;
434                 ISchemaObject parent = sobj.getParent();
435                 if (e.getChangeType() == IModelChangedEvent.CHANGE) {
436                     fTreeViewer.refresh(sobj);
437                 } else if (e.getChangeType() == IModelChangedEvent.INSERT) {
438                     fTreeViewer.add(parent, sobj);
439                     fTreeViewer.getTree().getDisplay().asyncExec(new Runnable JavaDoc() {
440                         public void run() {
441                             fTreeViewer.setSelection(new StructuredSelection(sobj), true);
442                         }
443                     });
444                 } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
445                     fTreeViewer.remove(sobj);
446                     fTreeViewer.setSelection(new StructuredSelection(parent), true);
447                 }
448             } else if (obj instanceof ISchemaComplexType) {
449                 // first compositor added/removed
450
ISchemaCompositor comp = ((ISchemaComplexType)obj).getCompositor();
451                 fTreeViewer.refresh(comp);
452                 if (comp != null)
453                     fTreeViewer.refresh(comp.getParent());
454                 
455                 if (e.getChangeType() == IModelChangedEvent.INSERT ||
456                         e.getChangeType() == IModelChangedEvent.CHANGE) {
457                     ISchemaComplexType type = (ISchemaComplexType) obj;
458                     final ISchemaCompositor compositor = type.getCompositor();
459                     if (compositor != null) {
460                         fTreeViewer.getTree().getDisplay().asyncExec(new Runnable JavaDoc() {
461                             public void run() {
462                                 fTreeViewer.setSelection(new StructuredSelection(compositor), true);
463                             }
464                         });
465                     }
466                 }
467             } else if (obj instanceof ISchema) {
468                 if (!ISchemaObject.P_NAME.equals(e.getChangedProperty()))
469                     fTreeViewer.refresh();
470             }
471         }
472     }
473
474     /**
475      * @param event
476      */

477     private void handleModelEventWorldChanged(IModelChangedEvent event) {
478         // Note: Cannot use event. There are no changed objects within it
479
// This method acts like a refresh
480
initialize();
481         // TODO: MP: REVERT: LOW: Update initialize with this once Bug #171897 is fixed
482
ISchemaElement root = fSchema.getSchema().findElement(
483                 ICoreConstants.EXTENSION_NAME);
484         // Ensure the root element is present
485
if (root == null) {
486             return;
487         }
488         // Select the root extension element
489
fTreeViewer.setSelection(new StructuredSelection(root), true);
490         // Collapse tree to the first level
491
fTreeViewer.expandToLevel(1);
492     }
493
494     protected void selectionChanged(IStructuredSelection selection) {
495 // getPage().getManagedForm().fireSelectionChanged(this, selection);
496
// getPage().getPDEEditor().setSelection(selection);
497
updateButtons();
498     }
499
500     /* (non-Javadoc)
501      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#setFocus()
502      */

503     public void setFocus() {
504         if (fTreeViewer != null) {
505             fTreeViewer.getTree().setFocus();
506             getPage().getPDEEditor().setSelection(fTreeViewer.getSelection());
507         }
508     }
509
510     private void updateButtons() {
511         if (!fSchema.isEditable())
512             return;
513         Object JavaDoc object = ((IStructuredSelection) fTreeViewer.getSelection()).getFirstElement();
514         ISchemaObject sobject = (ISchemaObject) object;
515
516         boolean canAddAttribute = false;
517         if (sobject != null) {
518             if (sobject instanceof ISchemaElement) {
519                 if (!(sobject instanceof ISchemaRootElement)
520                         && !(sobject instanceof ISchemaObjectReference))
521                     canAddAttribute = true;
522             } else if (sobject instanceof ISchemaAttribute) {
523                 ISchemaElement element = (ISchemaElement) (sobject.getParent());
524                 if (!(element instanceof ISchemaRootElement))
525                     canAddAttribute = true;
526             }
527         }
528         getTreePart().setButtonEnabled(1, canAddAttribute);
529     }
530
531     private ISchemaObject getSibling(Object JavaDoc target, Object JavaDoc object) {
532         if (target instanceof ISchemaElement && object instanceof ISchemaElement)
533             return (ISchemaElement)target;
534         if (target instanceof ISchemaAttribute && object instanceof ISchemaAttribute)
535             return (ISchemaAttribute)target;
536         if (target instanceof SchemaElementReference && object instanceof ISchemaElement)
537             return (SchemaElementReference)target;
538         return null;
539     }
540
541     private ISchemaObject getRealTarget(Object JavaDoc target, Object JavaDoc object) {
542         if (object instanceof ISchemaElement || object instanceof ISchemaObjectReference) {
543             if (target instanceof SchemaElementReference)
544                 return ((SchemaElementReference)target).getCompositor();
545             if (target instanceof ISchemaCompositor)
546                 return (ISchemaCompositor)target;
547             if (object instanceof ISchemaElement)
548                 return fSchema;
549         }
550         if (object instanceof ISchemaAttribute) {
551             if (target instanceof ISchemaAttribute) {
552                 // add it to the parent of the selected attribute
553
return ((ISchemaAttribute) target).getParent();
554             }
555             if (target instanceof ISchemaElement)
556                 return (ISchemaElement)target;
557         }
558         if (object instanceof ISchemaCompositor) {
559             if (target instanceof SchemaElementReference)
560                 return ((SchemaElementReference)target).getCompositor();
561             if (target instanceof ISchemaElement)
562                 return (ISchemaElement)target;
563             if (target instanceof ISchemaCompositor)
564                 return (ISchemaCompositor)target;
565         }
566         return null;
567     }
568
569     protected boolean canPaste(Object JavaDoc target, Object JavaDoc[] objects) {
570         for (int i = 0; i < objects.length; i++) {
571             Object JavaDoc obj = objects[i];
572             if (obj instanceof ISchemaAttribute && target instanceof ISchemaAttribute) {
573                 continue;
574             } else if (obj instanceof ISchemaObjectReference && target instanceof ISchemaCompositor) {
575                 continue;
576             } else if (target instanceof ISchemaElement
577                     && !(target instanceof ISchemaObjectReference)
578                     && !(obj instanceof ISchemaRootElement)) {
579                 continue;
580             }
581             return false;
582         }
583         return true;
584     }
585
586     protected void handleDoubleClick(IStructuredSelection selection) {
587         Object JavaDoc object = selection.getFirstElement();
588         if (object instanceof SchemaElementReference) {
589             ISchemaElement element = ((SchemaElementReference) object).getReferencedElement();
590             if (element == null) {
591                 String JavaDoc name = ((SchemaElementReference)object).getName();
592                 MessageDialog.openWarning(
593                         getPage().getSite().getShell(),
594                         PDEUIMessages.ElementSection_missingRefElement,
595                         NLS.bind(PDEUIMessages.SchemaIncludesSection_missingWarningMessage, name));
596                 return;
597             }
598             ISchema schema = element.getSchema();
599             if (schema.equals(fSchema))
600                 fireSelection(new StructuredSelection(element));
601             else {
602                 ISchemaInclude[] includes = fSchema.getIncludes();
603                 for (int i = 0; i < includes.length; i++) {
604                     if (includes[i].getIncludedSchema().equals(schema)) {
605                         String JavaDoc location = includes[i].getLocation();
606                         SchemaEditor.openToElement(new Path(location), element);
607                         break;
608                     }
609                 }
610             }
611         }
612     }
613     protected void fireSelection(ISelection selection) {
614         if (selection == null) selection = fTreeViewer.getSelection();
615         fTreeViewer.setSelection(selection);
616     }
617
618     protected void doPaste(Object JavaDoc target, Object JavaDoc[] objects) {
619         handleOp(target, objects, DND.DROP_COPY);
620     }
621
622     public void handleOp(Object JavaDoc currentTarget, Object JavaDoc[] objects, int currentOperation) {
623         for (int i = 0; i < objects.length; i++) {
624             if (!(objects[i] instanceof ISchemaObject))
625                 continue;
626             ISchemaObject object = (ISchemaObject)objects[i];
627             ISchemaObject realTarget = getRealTarget(currentTarget, object);
628             ISchemaObject sibling = getSibling(currentTarget, object);
629             if (realTarget == null)
630                 continue;
631             switch (currentOperation) {
632             case DND.DROP_COPY:
633                 doPaste(realTarget, sibling, object);
634                 break;
635             case DND.DROP_MOVE:
636                 doMove(realTarget, sibling, object);
637                 break;
638             case DND.DROP_LINK:
639                 doLink(realTarget, sibling, object);
640                 break;
641             }
642         }
643     }
644
645     private void doLink(ISchemaObject realTarget, ISchemaObject sibling, ISchemaObject object) {
646         if (realTarget instanceof ISchemaCompositor
647                 && object instanceof ISchemaElement) {
648             fRearranger.linkReference(
649                     (ISchemaCompositor)realTarget,
650                     (ISchemaElement)object,
651                     sibling);
652         }
653     }
654     
655     private void doMove(ISchemaObject realTarget, ISchemaObject sibling, ISchemaObject object) {
656         if (object instanceof ISchemaCompositor) {
657             fRearranger.moveCompositor(
658                     realTarget,
659                     (ISchemaCompositor)object);
660         } else if (object instanceof SchemaElementReference) {
661             fRearranger.moveReference(
662                     (SchemaElementReference)object,
663                     (ISchemaCompositor)realTarget,
664                     sibling);
665         } else if (object instanceof ISchemaElement) {
666             fRearranger.moveElement(
667                     realTarget,
668                     (ISchemaElement)object,
669                     sibling);
670         } else if (object instanceof ISchemaAttribute) {
671             fRearranger.moveAttribute(
672                     (ISchemaElement)realTarget,
673                     (ISchemaAttribute)object,
674                     sibling != null ? (ISchemaAttribute)sibling : null);
675         }
676     }
677     
678     private void doPaste(ISchemaObject realTarget, ISchemaObject sibling, ISchemaObject object) {
679         if (object instanceof ISchemaCompositor) {
680             fRearranger.pasteCompositor(
681                     realTarget,
682                     (ISchemaCompositor)object,
683                     sibling);
684         } else if (object instanceof SchemaElementReference) {
685             fRearranger.pasteReference(
686                     realTarget,
687                     (SchemaElementReference)object,
688                     sibling);
689         } else if (object instanceof ISchemaElement) {
690             fRearranger.pasteElement(
691                     (ISchemaElement)object,
692                     sibling);
693         } else if (object instanceof ISchemaAttribute) {
694             fRearranger.pasteAttribute(
695                     (ISchemaElement)realTarget,
696                     (ISchemaAttribute)object,
697                     sibling);
698         }
699     }
700 }
701
Popular Tags