001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IncrementalProjectBuilder;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.Status;
022: import org.eclipse.core.runtime.jobs.Job;
023: import org.eclipse.jdt.core.JavaCore;
024: import org.eclipse.jdt.launching.JavaRuntime;
025: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
026: import org.eclipse.jface.action.Action;
027: import org.eclipse.jface.action.IMenuManager;
028: import org.eclipse.jface.action.Separator;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.jface.viewers.LabelProvider;
031: import org.eclipse.jface.viewers.StructuredSelection;
032: import org.eclipse.jface.viewers.TableViewer;
033: import org.eclipse.jface.viewers.ViewerDropAdapter;
034: import org.eclipse.jface.window.Window;
035: import org.eclipse.pde.core.IModelChangedEvent;
036: import org.eclipse.pde.core.plugin.IPluginModelBase;
037: import org.eclipse.pde.core.plugin.PluginRegistry;
038: import org.eclipse.pde.internal.core.ibundle.IBundle;
039: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
040: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
041: import org.eclipse.pde.internal.core.text.bundle.ExecutionEnvironment;
042: import org.eclipse.pde.internal.core.text.bundle.PDEManifestElement;
043: import org.eclipse.pde.internal.core.text.bundle.RequiredExecutionEnvironmentHeader;
044: import org.eclipse.pde.internal.ui.PDEPlugin;
045: import org.eclipse.pde.internal.ui.PDEPluginImages;
046: import org.eclipse.pde.internal.ui.PDEUIMessages;
047: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
048: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
049: import org.eclipse.pde.internal.ui.editor.TableSection;
050: import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
051: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
052: import org.eclipse.pde.internal.ui.parts.EditableTablePart;
053: import org.eclipse.pde.internal.ui.parts.TablePart;
054: import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil;
055: import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
056: import org.eclipse.swt.SWT;
057: import org.eclipse.swt.graphics.Image;
058: import org.eclipse.swt.layout.GridData;
059: import org.eclipse.swt.widgets.Composite;
060: import org.eclipse.swt.widgets.Display;
061: import org.eclipse.swt.widgets.Table;
062: import org.eclipse.ui.actions.ActionFactory;
063: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
064: import org.eclipse.ui.forms.IFormColors;
065: import org.eclipse.ui.forms.events.HyperlinkEvent;
066: import org.eclipse.ui.forms.events.IHyperlinkListener;
067: import org.eclipse.ui.forms.widgets.FormToolkit;
068: import org.eclipse.ui.forms.widgets.Hyperlink;
069: import org.eclipse.ui.forms.widgets.Section;
070: import org.eclipse.ui.forms.widgets.TableWrapData;
071: import org.osgi.framework.Constants;
072:
073: public class ExecutionEnvironmentSection extends TableSection {
074:
075: private TableViewer fEETable;
076: private Action fRemoveAction;
077: private Action fAddAction;
078:
079: class EELabelProvider extends LabelProvider {
080:
081: private Image fImage;
082:
083: public EELabelProvider() {
084: fImage = PDEPluginImages.DESC_JAVA_LIB_OBJ.createImage();
085: }
086:
087: public Image getImage(Object element) {
088: return fImage;
089: }
090:
091: public String getText(Object element) {
092: if (element instanceof IExecutionEnvironment)
093: return ((IExecutionEnvironment) element).getId();
094: return super .getText(element);
095: }
096:
097: public void dispose() {
098: if (fImage != null)
099: fImage.dispose();
100: super .dispose();
101: }
102: }
103:
104: class ContentProvider extends DefaultTableProvider {
105: public Object[] getElements(Object inputElement) {
106: if (inputElement instanceof IBundleModel) {
107: IBundleModel model = (IBundleModel) inputElement;
108: IBundle bundle = model.getBundle();
109: IManifestHeader header = bundle
110: .getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
111: if (header instanceof RequiredExecutionEnvironmentHeader) {
112: return ((RequiredExecutionEnvironmentHeader) header)
113: .getEnvironments();
114: }
115: }
116: return new Object[0];
117: }
118: }
119:
120: public ExecutionEnvironmentSection(PDEFormPage page,
121: Composite parent) {
122: super (
123: page,
124: parent,
125: Section.DESCRIPTION,
126: new String[] {
127: PDEUIMessages.RequiredExecutionEnvironmentSection_add,
128: PDEUIMessages.RequiredExecutionEnvironmentSection_remove,
129: PDEUIMessages.RequiredExecutionEnvironmentSection_up,
130: PDEUIMessages.RequiredExecutionEnvironmentSection_down });
131: createClient(getSection(), page.getEditor().getToolkit());
132: }
133:
134: protected void createClient(Section section, FormToolkit toolkit) {
135: section
136: .setText(PDEUIMessages.RequiredExecutionEnvironmentSection_title);
137: if (isFragment())
138: section
139: .setDescription(PDEUIMessages.RequiredExecutionEnvironmentSection_fragmentDesc);
140: else
141: section
142: .setDescription(PDEUIMessages.RequiredExecutionEnvironmentSection_pluginDesc);
143:
144: section.setLayout(FormLayoutFactory.createClearTableWrapLayout(
145: false, 1));
146:
147: TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB);
148: section.setLayoutData(data);
149:
150: Composite container = createClientContainer(section, 2, toolkit);
151: EditableTablePart tablePart = getTablePart();
152: tablePart.setEditable(isEditable());
153:
154: createViewerPartControl(container, SWT.FULL_SELECTION
155: | SWT.MULTI, 2, toolkit);
156: fEETable = tablePart.getTableViewer();
157: fEETable.setContentProvider(new ContentProvider());
158: fEETable.setLabelProvider(PDEPlugin.getDefault()
159: .getLabelProvider());
160:
161: Hyperlink link = toolkit
162: .createHyperlink(
163: container,
164: PDEUIMessages.BuildExecutionEnvironmentSection_configure,
165: SWT.NONE);
166: link.setForeground(toolkit.getColors().getColor(
167: IFormColors.TITLE));
168: link.addHyperlinkListener(new IHyperlinkListener() {
169: public void linkEntered(HyperlinkEvent e) {
170: }
171:
172: public void linkExited(HyperlinkEvent e) {
173: }
174:
175: public void linkActivated(HyperlinkEvent e) {
176: PDEPreferencesUtil
177: .showPreferencePage(
178: new String[] { "org.eclipse.jdt.debug.ui.jreProfiles" }, PDEPlugin.getActiveWorkbenchShell()); //$NON-NLS-1$
179: }
180: });
181: GridData gd = new GridData();
182: gd.horizontalSpan = 2;
183: link.setLayoutData(gd);
184:
185: final IProject project = getPage().getPDEEditor()
186: .getCommonProject();
187: try {
188: if (project != null
189: && project.hasNature(JavaCore.NATURE_ID)) {
190: link = toolkit
191: .createHyperlink(
192: container,
193: PDEUIMessages.ExecutionEnvironmentSection_updateClasspath,
194: SWT.NONE);
195: link.setForeground(toolkit.getColors().getColor(
196: IFormColors.TITLE));
197: link.addHyperlinkListener(new IHyperlinkListener() {
198: public void linkEntered(HyperlinkEvent e) {
199: }
200:
201: public void linkExited(HyperlinkEvent e) {
202: }
203:
204: public void linkActivated(HyperlinkEvent e) {
205: try {
206: getPage().getEditor().doSave(null);
207: IPluginModelBase model = PluginRegistry
208: .findModel(project);
209: if (model != null) {
210: ClasspathComputer.setClasspath(project,
211: model);
212: if (PDEPlugin.getWorkspace()
213: .isAutoBuilding()) {
214: doFullBuild(project);
215: }
216: }
217: } catch (CoreException e1) {
218: }
219: }
220: });
221: gd = new GridData();
222: gd.horizontalSpan = 2;
223: link.setLayoutData(gd);
224: }
225: } catch (CoreException e1) {
226: }
227:
228: makeActions();
229:
230: IBundleModel model = getBundleModel();
231: if (model != null) {
232: fEETable.setInput(model);
233: model.addModelChangedListener(this );
234: }
235: toolkit.paintBordersFor(container);
236: section.setClient(container);
237: }
238:
239: public void dispose() {
240: IBundleModel model = getBundleModel();
241: if (model != null)
242: model.removeModelChangedListener(this );
243: }
244:
245: public void refresh() {
246: fEETable.refresh();
247: updateButtons();
248: }
249:
250: protected void buttonSelected(int index) {
251: switch (index) {
252: case 0:
253: handleAdd();
254: break;
255: case 1:
256: handleRemove();
257: break;
258: case 2:
259: handleUp();
260: break;
261: case 3:
262: handleDown();
263: break;
264: }
265: }
266:
267: protected void fillContextMenu(IMenuManager manager) {
268: manager.add(fAddAction);
269: if (!fEETable.getSelection().isEmpty()) {
270: manager.add(new Separator());
271: manager.add(fRemoveAction);
272: }
273: getPage().getPDEEditor().getContributor()
274: .contextMenuAboutToShow(manager);
275: }
276:
277: private void makeActions() {
278: fAddAction = new Action(
279: PDEUIMessages.RequiredExecutionEnvironmentSection_add) {
280: public void run() {
281: handleAdd();
282: }
283: };
284: fAddAction.setEnabled(isEditable());
285:
286: fRemoveAction = new Action(
287: PDEUIMessages.NewManifestEditor_LibrarySection_remove) {
288: public void run() {
289: handleRemove();
290: }
291: };
292: fRemoveAction.setEnabled(isEditable());
293: }
294:
295: private void updateButtons() {
296: Table table = fEETable.getTable();
297: int count = table.getItemCount();
298: boolean canMoveUp = count > 0
299: && table.getSelection().length == 1
300: && table.getSelectionIndex() > 0;
301: boolean canMoveDown = count > 0
302: && table.getSelection().length == 1
303: && table.getSelectionIndex() < count - 1;
304:
305: TablePart tablePart = getTablePart();
306: tablePart.setButtonEnabled(0, isEditable());
307: tablePart.setButtonEnabled(1, isEditable()
308: && table.getSelection().length > 0);
309: tablePart.setButtonEnabled(2, isEditable() && canMoveUp);
310: tablePart.setButtonEnabled(3, isEditable() && canMoveDown);
311: }
312:
313: private void handleDown() {
314: int selection = fEETable.getTable().getSelectionIndex();
315: swap(selection, selection + 1);
316: }
317:
318: private void handleUp() {
319: int selection = fEETable.getTable().getSelectionIndex();
320: swap(selection, selection - 1);
321: }
322:
323: public void swap(int index1, int index2) {
324: RequiredExecutionEnvironmentHeader header = getHeader();
325: header.swap(index1, index2);
326: }
327:
328: private void handleRemove() {
329: IStructuredSelection ssel = (IStructuredSelection) fEETable
330: .getSelection();
331: if (ssel.size() > 0) {
332: Iterator iter = ssel.iterator();
333: while (iter.hasNext()) {
334: Object object = iter.next();
335: if (object instanceof ExecutionEnvironment) {
336: getHeader().removeExecutionEnvironment(
337: (ExecutionEnvironment) object);
338: }
339: }
340: }
341: }
342:
343: private void handleAdd() {
344: ElementListSelectionDialog dialog = new ElementListSelectionDialog(
345: PDEPlugin.getActiveWorkbenchShell(),
346: new EELabelProvider());
347: dialog.setElements(getEnvironments());
348: dialog.setAllowDuplicates(false);
349: dialog.setMultipleSelection(true);
350: dialog
351: .setTitle(PDEUIMessages.RequiredExecutionEnvironmentSection_dialog_title);
352: dialog
353: .setMessage(PDEUIMessages.RequiredExecutionEnvironmentSection_dialogMessage);
354: if (dialog.open() == Window.OK) {
355: addExecutionEnvironments(dialog.getResult());
356: }
357: }
358:
359: private void addExecutionEnvironments(Object[] result) {
360: IManifestHeader header = getHeader();
361: if (header == null) {
362: StringBuffer buffer = new StringBuffer();
363: for (int i = 0; i < result.length; i++) {
364: String id = null;
365: if (result[i] instanceof IExecutionEnvironment)
366: id = ((IExecutionEnvironment) result[i]).getId();
367: else if (result[i] instanceof ExecutionEnvironment)
368: id = ((ExecutionEnvironment) result[i]).getName();
369: else
370: continue;
371: if (buffer.length() > 0) {
372: buffer.append(","); //$NON-NLS-1$
373: buffer.append(getLineDelimiter());
374: buffer.append(" "); //$NON-NLS-1$
375: }
376: buffer.append(id);
377: }
378: getBundle().setHeader(
379: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT,
380: buffer.toString());
381: } else {
382: RequiredExecutionEnvironmentHeader ee = (RequiredExecutionEnvironmentHeader) header;
383: ee.addExecutionEnvironments(result);
384: }
385: }
386:
387: private String getLineDelimiter() {
388: BundleInputContext inputContext = getBundleContext();
389: if (inputContext != null) {
390: return inputContext.getLineDelimiter();
391: }
392: return System.getProperty("line.separator"); //$NON-NLS-1$
393: }
394:
395: private Object[] getEnvironments() {
396: RequiredExecutionEnvironmentHeader header = getHeader();
397: IExecutionEnvironment[] envs = JavaRuntime
398: .getExecutionEnvironmentsManager()
399: .getExecutionEnvironments();
400: if (header == null)
401: return envs;
402: ArrayList list = new ArrayList();
403: for (int i = 0; i < envs.length; i++) {
404: if (!header.hasExecutionEnvironment(envs[i]))
405: list.add(envs[i]);
406: }
407: return list.toArray();
408: }
409:
410: public void modelChanged(IModelChangedEvent e) {
411: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
412: markStale();
413: } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
414: Object[] objects = e.getChangedObjects();
415: for (int i = 0; i < objects.length; i++) {
416: Table table = fEETable.getTable();
417: if (objects[i] instanceof ExecutionEnvironment) {
418: int index = table.getSelectionIndex();
419: fEETable.remove(objects[i]);
420: if (canSelect()) {
421: table
422: .setSelection(index < table
423: .getItemCount() ? index : table
424: .getItemCount() - 1);
425: }
426: }
427: }
428: updateButtons();
429: } else if (e.getChangeType() == IModelChangedEvent.INSERT) {
430: Object[] objects = e.getChangedObjects();
431: if (objects.length > 0) {
432: fEETable.refresh();
433: fEETable.setSelection(new StructuredSelection(
434: objects[objects.length - 1]));
435: }
436: updateButtons();
437: } else if (Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT
438: .equals(e.getChangedProperty())) {
439: refresh();
440: // Bug 171896
441: // Since the model sends a CHANGE event instead of
442: // an INSERT event on the very first addition to the empty table
443: // Selection should fire here to take this first insertion into account
444: Object lastElement = fEETable.getElementAt(fEETable
445: .getTable().getItemCount() - 1);
446: if (lastElement != null) {
447: fEETable.setSelection(new StructuredSelection(
448: lastElement));
449: }
450: }
451: }
452:
453: private BundleInputContext getBundleContext() {
454: InputContextManager manager = getPage().getPDEEditor()
455: .getContextManager();
456: return (BundleInputContext) manager
457: .findContext(BundleInputContext.CONTEXT_ID);
458: }
459:
460: private IBundle getBundle() {
461: IBundleModel model = getBundleModel();
462: return model == null ? null : model.getBundle();
463: }
464:
465: private IBundleModel getBundleModel() {
466: BundleInputContext context = getBundleContext();
467: return context == null ? null : (IBundleModel) context
468: .getModel();
469: }
470:
471: protected RequiredExecutionEnvironmentHeader getHeader() {
472: IBundle bundle = getBundle();
473: if (bundle == null)
474: return null;
475: IManifestHeader header = bundle
476: .getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
477: if (header instanceof RequiredExecutionEnvironmentHeader)
478: return (RequiredExecutionEnvironmentHeader) header;
479: return null;
480: }
481:
482: protected boolean isFragment() {
483: InputContextManager manager = getPage().getPDEEditor()
484: .getContextManager();
485: IPluginModelBase model = (IPluginModelBase) manager
486: .getAggregateModel();
487: return model.isFragmentModel();
488: }
489:
490: public boolean doGlobalAction(String actionId) {
491: if (!isEditable()) {
492: return false;
493: }
494:
495: if (actionId.equals(ActionFactory.DELETE.getId())) {
496: handleRemove();
497: return true;
498: }
499:
500: if (actionId.equals(ActionFactory.CUT.getId())) {
501: // delete here and let the editor transfer
502: // the selection to the clipboard
503: handleRemove();
504: return false;
505: }
506:
507: if (actionId.equals(ActionFactory.PASTE.getId())) {
508: doPaste();
509: return true;
510: }
511:
512: return false;
513: }
514:
515: protected boolean canPaste(Object target, Object[] objects) {
516: RequiredExecutionEnvironmentHeader header = getHeader();
517: for (int i = 0; i < objects.length; i++) {
518: if (objects[i] instanceof ExecutionEnvironment) {
519: String env = ((ExecutionEnvironment) objects[i])
520: .getName();
521: if (header == null || !header.hasElement(env))
522: return true;
523: }
524: }
525: return false;
526: }
527:
528: protected void selectionChanged(IStructuredSelection selection) {
529: getPage().getPDEEditor().setSelection(selection);
530: if (getPage().getModel().isEditable())
531: updateButtons();
532: }
533:
534: protected void doPaste(Object target, Object[] objects) {
535: addExecutionEnvironments(objects);
536: }
537:
538: private void doFullBuild(final IProject project) {
539: Job buildJob = new Job(
540: PDEUIMessages.CompilersConfigurationBlock_building) {
541: public boolean belongsTo(Object family) {
542: return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
543: }
544:
545: protected IStatus run(IProgressMonitor monitor) {
546: try {
547: project.build(IncrementalProjectBuilder.FULL_BUILD,
548: JavaCore.BUILDER_ID, null, monitor);
549: } catch (CoreException e) {
550: }
551: return Status.OK_STATUS;
552: }
553: };
554: buildJob.setRule(ResourcesPlugin.getWorkspace()
555: .getRuleFactory().buildRule());
556: buildJob.schedule();
557: }
558:
559: /* (non-Javadoc)
560: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#isDragAndDropEnabled()
561: */
562: protected boolean isDragAndDropEnabled() {
563: return true;
564: }
565:
566: /* (non-Javadoc)
567: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canDragMove(java.lang.Object[])
568: */
569: public boolean canDragMove(Object[] sourceObjects) {
570: if (validateDragMoveSanity(sourceObjects) == false) {
571: return false;
572: }
573: return true;
574: }
575:
576: /* (non-Javadoc)
577: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canDropMove(java.lang.Object, java.lang.Object[], int)
578: */
579: public boolean canDropMove(Object targetObject,
580: Object[] sourceObjects, int targetLocation) {
581: // Sanity check
582: if (validateDropMoveSanity(targetObject, sourceObjects) == false) {
583: return false;
584: }
585: // Multiple selection not supported
586: ExecutionEnvironment sourceEEObject = (ExecutionEnvironment) sourceObjects[0];
587: ExecutionEnvironment targetEEObject = (ExecutionEnvironment) targetObject;
588: // Validate model
589: if (validateDropMoveModel(sourceEEObject, targetEEObject) == false) {
590: return false;
591: }
592: // Validate move
593: if (targetLocation == ViewerDropAdapter.LOCATION_BEFORE) {
594: // Get the previous element of the target
595: RequiredExecutionEnvironmentHeader header = getHeader();
596: // Ensure we have a header
597: if (header == null) {
598: return false;
599: }
600: // Get the previous element of the target
601: PDEManifestElement previousElement = header
602: .getPreviousElement(targetEEObject);
603: // Ensure the previous element is not the source
604: if (sourceEEObject.equals(previousElement)) {
605: return false;
606: }
607: return true;
608: } else if (targetLocation == ViewerDropAdapter.LOCATION_AFTER) {
609: // Get the next element of the target
610: RequiredExecutionEnvironmentHeader header = getHeader();
611: // Ensure we have a header
612: if (header == null) {
613: return false;
614: }
615: // Get the next element of the target
616: PDEManifestElement nextElement = header
617: .getNextElement(targetEEObject);
618: // Ensure the next element is not the source
619: if (sourceEEObject.equals(nextElement)) {
620: return false;
621: }
622: return true;
623: } else if (targetLocation == ViewerDropAdapter.LOCATION_ON) {
624: // Not supported
625: return false;
626: }
627: return false;
628: }
629:
630: /**
631: * @param sourceEEObject
632: * @param targetEEObject
633: * @return
634: */
635: private boolean validateDropMoveModel(
636: ExecutionEnvironment sourceEEObject,
637: ExecutionEnvironment targetEEObject) {
638: // Objects have to be from the same model
639: IBundleModel sourceModel = sourceEEObject.getModel();
640: IBundleModel targetModel = targetEEObject.getModel();
641: if (sourceModel.equals(targetModel)) {
642: return true;
643: }
644: return false;
645: }
646:
647: /**
648: * @param targetObject
649: * @param sourceObjects
650: * @return
651: */
652: private boolean validateDropMoveSanity(Object targetObject,
653: Object[] sourceObjects) {
654: // Validate target object
655: if ((targetObject instanceof ExecutionEnvironment) == false) {
656: return false;
657: }
658: // Validate source objects
659: if (validateDragMoveSanity(sourceObjects) == false) {
660: return false;
661: }
662: return true;
663: }
664:
665: /* (non-Javadoc)
666: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doDropMove(java.lang.Object, java.lang.Object[], int)
667: */
668: public void doDropMove(Object targetObject, Object[] sourceObjects,
669: int targetLocation) {
670: // Sanity check
671: if (validateDropMoveSanity(targetObject, sourceObjects) == false) {
672: Display.getDefault().beep();
673: return;
674: }
675: // Multiple selection not supported
676: ExecutionEnvironment sourceEEObject = (ExecutionEnvironment) sourceObjects[0];
677: ExecutionEnvironment targetEEObject = (ExecutionEnvironment) targetObject;
678: // Validate move
679: if (targetLocation == ViewerDropAdapter.LOCATION_BEFORE) {
680: // Get the header
681: RequiredExecutionEnvironmentHeader header = getHeader();
682: // Ensure we have a header
683: if (header == null) {
684: return;
685: }
686: // Get the index of the target
687: int index = header.indexOf(targetEEObject);
688: // Ensure the target index was found
689: if (index == -1) {
690: return;
691: }
692: // Add source as sibling of target (before)
693: header.addExecutionEnvironment(sourceEEObject, index);
694: return;
695: } else if (targetLocation == ViewerDropAdapter.LOCATION_AFTER) {
696: // Get the header
697: RequiredExecutionEnvironmentHeader header = getHeader();
698: // Ensure we have a header
699: if (header == null) {
700: return;
701: }
702: // Get the index of the target
703: int index = header.indexOf(targetEEObject);
704: // Ensure the target index was found
705: if (index == -1) {
706: return;
707: }
708: // Add source as sibling of target (before)
709: header.addExecutionEnvironment(sourceEEObject, index + 1);
710: return;
711: } else if (targetLocation == ViewerDropAdapter.LOCATION_ON) {
712: // NO-OP. Not supported
713: }
714: }
715:
716: /* (non-Javadoc)
717: * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doDragRemove(java.lang.Object[])
718: */
719: public void doDragRemove(Object[] sourceObjects) {
720: // Validate source
721: if (validateDragMoveSanity(sourceObjects) == false) {
722: return;
723: }
724: // Get the source
725: ExecutionEnvironment environment = (ExecutionEnvironment) sourceObjects[0];
726: // Get the header
727: RequiredExecutionEnvironmentHeader header = getHeader();
728: // Ensure we have a header
729: if (header == null) {
730: return;
731: }
732: // Remove the source
733: doSelect(false);
734: header.removeExecutionEnvironmentUnique(environment);
735: doSelect(true);
736: }
737:
738: /**
739: * @param sourceObjects
740: * @return
741: */
742: private boolean validateDragMoveSanity(Object[] sourceObjects) {
743: // Validate source
744: if (sourceObjects == null) {
745: // No objects
746: return false;
747: } else if (sourceObjects.length != 1) {
748: // Multiple selection not supported
749: return false;
750: } else if ((sourceObjects[0] instanceof ExecutionEnvironment) == false) {
751: // Must be the right type
752: return false;
753: }
754: return true;
755: }
756:
757: }
|