KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 java.io.ByteArrayInputStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.ProjectScope;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.NullProgressMonitor;
25 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.action.Separator;
30 import org.eclipse.jface.action.ToolBarManager;
31 import org.eclipse.jface.util.IPropertyChangeListener;
32 import org.eclipse.jface.util.PropertyChangeEvent;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.ITableLabelProvider;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.jface.window.Window;
38 import org.eclipse.pde.core.IBaseModel;
39 import org.eclipse.pde.core.IModelChangedEvent;
40 import org.eclipse.pde.core.IModelChangedListener;
41 import org.eclipse.pde.core.build.IBuild;
42 import org.eclipse.pde.core.build.IBuildEntry;
43 import org.eclipse.pde.core.build.IBuildModel;
44 import org.eclipse.pde.core.plugin.IPluginModel;
45 import org.eclipse.pde.core.plugin.IPluginModelBase;
46 import org.eclipse.pde.core.plugin.PluginRegistry;
47 import org.eclipse.pde.internal.core.ICoreConstants;
48 import org.eclipse.pde.internal.core.IPluginModelListener;
49 import org.eclipse.pde.internal.core.PDECore;
50 import org.eclipse.pde.internal.core.PluginModelDelta;
51 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModel;
52 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
53 import org.eclipse.pde.internal.core.plugin.ExternalPluginModel;
54 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModel;
55 import org.eclipse.pde.internal.ui.PDEPlugin;
56 import org.eclipse.pde.internal.ui.PDEPluginImages;
57 import org.eclipse.pde.internal.ui.PDEUIMessages;
58 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
59 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
60 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
61 import org.eclipse.pde.internal.ui.editor.TableSection;
62 import org.eclipse.pde.internal.ui.editor.actions.SortAction;
63 import org.eclipse.pde.internal.ui.editor.build.BuildInputContext;
64 import org.eclipse.pde.internal.ui.editor.context.InputContext;
65 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
66 import org.eclipse.pde.internal.ui.parts.TablePart;
67 import org.eclipse.pde.internal.ui.search.dependencies.AddNewDependenciesAction;
68 import org.eclipse.pde.internal.ui.util.SharedLabelProvider;
69 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
70 import org.eclipse.swt.SWT;
71 import org.eclipse.swt.events.DisposeEvent;
72 import org.eclipse.swt.events.DisposeListener;
73 import org.eclipse.swt.events.SelectionAdapter;
74 import org.eclipse.swt.events.SelectionEvent;
75 import org.eclipse.swt.graphics.Cursor;
76 import org.eclipse.swt.graphics.Image;
77 import org.eclipse.swt.layout.GridData;
78 import org.eclipse.swt.layout.GridLayout;
79 import org.eclipse.swt.widgets.Button;
80 import org.eclipse.swt.widgets.Composite;
81 import org.eclipse.swt.widgets.Control;
82 import org.eclipse.swt.widgets.Display;
83 import org.eclipse.swt.widgets.Table;
84 import org.eclipse.swt.widgets.ToolBar;
85 import org.eclipse.ui.actions.ActionFactory;
86 import org.eclipse.ui.forms.events.HyperlinkAdapter;
87 import org.eclipse.ui.forms.events.HyperlinkEvent;
88 import org.eclipse.ui.forms.widgets.ExpandableComposite;
89 import org.eclipse.ui.forms.widgets.FormText;
90 import org.eclipse.ui.forms.widgets.FormToolkit;
91 import org.eclipse.ui.forms.widgets.Section;
92 import org.eclipse.ui.part.FileEditorInput;
93 import org.osgi.service.prefs.BackingStoreException;
94
95 public class DependencyManagementSection extends TableSection implements IModelChangedListener, IPluginModelListener, IPropertyChangeListener {
96     
97     private TableViewer fAdditionalTable;
98     private Vector JavaDoc fAdditionalBundles;
99     private Action fNewAction;
100     private Action fRemoveAction;
101     private Action fOpenAction;
102     private Action fSortAction;
103     private Button fRequireBundleButton;
104     private Button fImportPackageButton;
105     private IProject fProject;
106     
107     private static final int ADD_INDEX = 0;
108     private static final int REMOVE_INDEX = 1;
109     private static final int UP_INDEX = 2;
110     private static final int DOWN_INDEX = 3;
111     
112     private static String JavaDoc ADD = PDEUIMessages.RequiresSection_add;
113     private static String JavaDoc REMOVE = PDEUIMessages.RequiresSection_delete;
114     private static String JavaDoc OPEN = PDEUIMessages.RequiresSection_open;
115     private static String JavaDoc UP = PDEUIMessages.RequiresSection_up;
116     private static String JavaDoc DOWN = PDEUIMessages.RequiresSection_down;
117     
118     class ContentProvider extends DefaultTableProvider {
119
120         /* (non-Javadoc)
121          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
122          */

123         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
124             if (fAdditionalBundles == null)
125                 return createAdditionalBundles();
126             return fAdditionalBundles.toArray();
127         }
128         
129         private IBuildEntry getBuildInfo() {
130             IBuildEntry entry = null;
131             IBuildModel model = getBuildModel(false);
132             if (model == null)
133                 return null;
134             IBuild buildObject = model.getBuild();
135             entry = buildObject.getEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
136             return entry;
137         }
138         
139         private Object JavaDoc[] createAdditionalBundles() {
140             IBuildEntry entry = getBuildInfo();
141             try {
142                 if (entry != null) {
143                     String JavaDoc [] tokens = entry.getTokens();
144                     fAdditionalBundles = new Vector JavaDoc(tokens.length);
145                     for (int i = 0; i < tokens.length; i++) {
146                         fAdditionalBundles.add(tokens[i].trim());
147                     }
148                     return fAdditionalBundles.toArray();
149                 }
150                 return new Object JavaDoc[0];
151             } catch (Exception JavaDoc e) {
152                 PDEPlugin.logException( e );
153                 return new Object JavaDoc[0]; //If exception happen while getting bundles, return an empty table
154
}
155         }
156     }
157     
158     class SecondaryTableLabelProvider extends SharedLabelProvider
159     implements ITableLabelProvider {
160         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
161             return obj.toString();
162         }
163         
164         public Image getColumnImage(Object JavaDoc obj, int index) {
165             String JavaDoc pluginID = obj.toString();
166             IPluginModelBase model = PluginRegistry.findModel(pluginID);
167             if (model == null)
168             {
169                 return get(PDEPluginImages.DESC_REQ_PLUGIN_OBJ, F_ERROR);
170             }
171             else if (model instanceof IBundlePluginModel || model instanceof WorkspacePluginModel)
172             {
173                 return get(PDEPluginImages.DESC_REQ_PLUGIN_OBJ);
174             }
175             else if (model instanceof ExternalPluginModel)
176             {
177                 return get(PDEPluginImages.DESC_PLUGIN_OBJ, F_EXTERNAL);
178             }
179             return null;
180         }
181     }
182
183     public DependencyManagementSection(PDEFormPage formPage, Composite parent) {
184         super(formPage, parent, ExpandableComposite.TWISTIE|ExpandableComposite.COMPACT, new String JavaDoc[] { ADD, REMOVE, UP, DOWN});
185         IBuildModel model = getBuildModel(false);
186         if (model != null) {
187             IBuildEntry entry = model.getBuild().getEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
188             if (entry != null && entry.getTokens().length > 0)
189                 getSection().setExpanded(true);
190         }
191     }
192
193     protected void createClient(Section section, FormToolkit toolkit) {
194         FormText text = toolkit.createFormText(section, true);
195         text.setText(PDEUIMessages.SecondaryBundlesSection_desc, false, false);
196         section.setDescriptionControl(text);
197         
198         Composite container = createClientContainer(section, 2, toolkit);
199         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
200         TablePart tablePart = getTablePart();
201         fAdditionalTable = tablePart.getTableViewer();
202
203         fAdditionalTable.setContentProvider(new ContentProvider());
204         fAdditionalTable.setLabelProvider(new SecondaryTableLabelProvider());
205         GridData gd = (GridData)fAdditionalTable.getTable().getLayoutData();
206         gd.heightHint = 150;
207         fAdditionalTable.getTable().setLayoutData(gd);
208         
209         gd = new GridData();
210         gd.horizontalSpan = 2;
211         FormText resolveText = toolkit.createFormText(container, true);
212         resolveText.setText(PDEUIMessages.SecondaryBundlesSection_resolve, true, true);
213         resolveText.setLayoutData(gd);
214         resolveText.addHyperlinkListener(new HyperlinkAdapter() {
215             public void linkActivated(HyperlinkEvent e) {
216                 doAddDependencies();
217             }
218         });
219         
220         Composite comp = toolkit.createComposite(container);
221         comp.setLayout(new GridLayout(2, false));
222         gd = new GridData();
223         gd.horizontalSpan = 2;
224         comp.setLayoutData(gd);
225         
226         fRequireBundleButton = toolkit.createButton(comp, "Require-Bundle", SWT.RADIO); //$NON-NLS-1$
227
gd = new GridData();
228         gd.horizontalIndent = 20;
229         fRequireBundleButton.setLayoutData(gd);
230         fRequireBundleButton.addSelectionListener(new SelectionAdapter() {
231             public void widgetSelected(SelectionEvent e) {
232                 savePreferences();
233             }
234         });
235         
236         fImportPackageButton = toolkit.createButton(comp, "Import-Package", SWT.RADIO); //$NON-NLS-1$
237
gd = new GridData();
238         gd.horizontalIndent = 20;
239         fImportPackageButton.setLayoutData(gd);
240         
241         toolkit.paintBordersFor(container);
242         makeActions();
243         section.setClient(container);
244         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
245         section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
246         section.setText(PDEUIMessages.SecondaryBundlesSection_title);
247         createSectionToolbar(section, toolkit);
248         initialize();
249     }
250
251     private void createSectionToolbar(Section section, FormToolkit toolkit) {
252         ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
253         ToolBar toolbar = toolBarManager.createControl(section);
254         final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
255         toolbar.setCursor(handCursor);
256         // Cursor needs to be explicitly disposed
257
toolbar.addDisposeListener(new DisposeListener() {
258             public void widgetDisposed(DisposeEvent e) {
259                 if ((handCursor != null) &&
260                         (handCursor.isDisposed() == false)) {
261                     handCursor.dispose();
262                 }
263             }
264         });
265         
266         // Add sort action to the tool bar
267
fSortAction = new SortAction(getTablePart().getTableViewer(),
268                 PDEUIMessages.RequiresSection_sortAlpha, null, null, this);
269         toolBarManager.add(fSortAction);
270         
271         toolBarManager.update(true);
272
273         section.setTextClient(toolbar);
274     }
275
276     private void savePreferences() {
277         if (fProject == null) {
278             IPluginModelBase model = (IPluginModelBase) getPage().getModel();
279             IResource resource = model.getUnderlyingResource();
280             if (resource == null)
281                 return;
282             fProject = resource.getProject();
283         }
284         IEclipsePreferences pref = new ProjectScope(fProject).getNode(PDECore.PLUGIN_ID);
285         
286         if (fImportPackageButton.getSelection())
287             pref.putBoolean(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE, false);
288         else
289             pref.remove(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE);
290         try {
291             pref.flush();
292         } catch (BackingStoreException e) {
293             PDEPlugin.logException(e);
294         }
295     }
296     
297     private void initialize() {
298         try {
299             IPluginModelBase model = (IPluginModelBase) getPage().getModel();
300             fAdditionalTable.setInput(model.getPluginBase());
301             TablePart part = getTablePart();
302             part.setButtonEnabled(0, model.isEditable());
303             part.setButtonEnabled(1, false);
304             part.setButtonEnabled(2, false);
305             part.setButtonEnabled(3, false);
306             
307             IBuildModel build = getBuildModel(false);
308             if (build != null)
309                 build.addModelChangedListener(this);
310             
311             IResource resource = model.getUnderlyingResource();
312             if (resource == null)
313                 return;
314             fProject = resource.getProject();
315             IEclipsePreferences pref = new ProjectScope(fProject).getNode(PDECore.PLUGIN_ID);
316             if (pref != null) {
317                 boolean useRequireBundle = pref.getBoolean(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE, true);
318                 fRequireBundleButton.setSelection(useRequireBundle);
319                 fImportPackageButton.setSelection(!useRequireBundle);
320             }
321             PDECore.getDefault().getModelManager().addPluginModelListener(this);
322         } catch (Exception JavaDoc e){
323             PDEPlugin.logException(e);
324         }
325     }
326     
327     protected void fillContextMenu(IMenuManager manager) {
328         ISelection selection = fAdditionalTable.getSelection();
329         manager.add(fNewAction);
330         manager.add(fOpenAction);
331         manager.add(new Separator());
332         
333         if (!selection.isEmpty())
334             manager.add(fRemoveAction);
335
336         // Add clipboard operations
337
getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
338     }
339     
340     public void refresh() {
341         fAdditionalBundles = null;
342         if (!fAdditionalTable.getControl().isDisposed())
343             fAdditionalTable.refresh();
344         super.refresh();
345     }
346     
347     protected void buttonSelected(int index) {
348         switch (index){
349             case ADD_INDEX:
350                 handleNew();
351                 break;
352             case REMOVE_INDEX:
353                 handleRemove();
354                 break;
355             case UP_INDEX:
356                 handleUp();
357                 break;
358             case DOWN_INDEX:
359                 handleDown();
360                 break;
361         }
362     }
363     
364     protected void handleDoubleClick(IStructuredSelection sel) {
365         handleOpen(sel);
366     }
367     
368     private void handleOpen(ISelection sel) {
369         if (sel instanceof IStructuredSelection) {
370             IStructuredSelection ssel = (IStructuredSelection) sel;
371             if (ssel.size() == 1) {
372                 Object JavaDoc obj = ssel.getFirstElement();
373                 IPluginModelBase base = PluginRegistry.findModel((String JavaDoc)obj);
374                 if (base != null)
375                     ManifestEditor.open(base.getPluginBase(), false);
376             }
377         }
378     }
379     
380     private IBuildModel getBuildModel(boolean createIfMissing) {
381         InputContext context = getPage().getPDEEditor().getContextManager()
382             .findContext(BuildInputContext.CONTEXT_ID);
383         if (context == null) {
384             if (createIfMissing) {
385                 IFile buildFile = getPage().getPDEEditor().getCommonProject().getFile("build.properties"); //$NON-NLS-1$
386
try {
387                     buildFile.create(new ByteArrayInputStream JavaDoc(new byte[0]), true, new NullProgressMonitor());
388                 } catch (CoreException e ) {
389                     return null;
390                 }
391                 FileEditorInput in = new FileEditorInput(buildFile);
392                 PDEFormEditor editor = getPage().getPDEEditor();
393                 context = new BuildInputContext(getPage().getPDEEditor(), in,
394                         false);
395                 editor.getContextManager().putContext(in, context);
396             } else
397                 return null;
398         }
399         return (IBuildModel) context.getModel();
400     }
401
402     private void makeActions() {
403         fNewAction = new Action( ADD ){
404             public void run() {
405                 handleNew();
406             }
407         };
408         
409         fOpenAction = new Action( OPEN ) {
410             public void run() {
411                 handleOpen(fAdditionalTable.getSelection());
412             }
413         };
414         
415         fRemoveAction = new Action (REMOVE) {
416             public void run() {
417                 handleRemove();
418             }
419         };
420     }
421
422     private void handleNew(){
423         PluginSelectionDialog dialog =
424             new PluginSelectionDialog(
425                 PDEPlugin.getActiveWorkbenchShell(),
426                 getAvailablePlugins(),
427                 true);
428         dialog.create();
429         if (dialog.open() == Window.OK) {
430             IBuildModel model = getBuildModel(true);
431             IBuild build = model.getBuild();
432             IBuildEntry entry = build.getEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
433             try {
434                 if (entry == null) {
435                     entry = model.getFactory().createEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
436                     build.add(entry);
437                 }
438                 Object JavaDoc[] models = dialog.getResult();
439             
440                 for (int i = 0; i < models.length; i++) {
441                     IPluginModel pmodel = (IPluginModel) models[i];
442                     entry.addToken(pmodel.getPlugin().getId());
443                 }
444                 markDirty();
445             }catch (CoreException e) {
446                 PDEPlugin.logException( e );
447             }
448         }
449     }
450     
451     private IPluginModelBase[] getAvailablePlugins() {
452         IPluginModelBase[] plugins = PluginRegistry.getActiveModels(false);
453         HashSet JavaDoc currentPlugins = new HashSet JavaDoc(
454                  (fAdditionalBundles == null) ? new Vector JavaDoc(1) : fAdditionalBundles);
455         IProject currentProj = getPage().getPDEEditor().getCommonProject();
456         IPluginModelBase model = PluginRegistry.findModel(currentProj);
457         if (model != null) {
458             currentPlugins.add(model.getPluginBase().getId());
459         }
460         
461         ArrayList JavaDoc result = new ArrayList JavaDoc();
462         for (int i = 0; i < plugins.length; i++){
463             if (!currentPlugins.contains(plugins[i].getPluginBase().getId()))
464                     result.add(plugins[i]);
465         }
466         return (IPluginModelBase []) result.toArray(new IPluginModelBase[result.size()]);
467     }
468     
469     private void handleRemove(){
470         IStructuredSelection ssel = (IStructuredSelection) fAdditionalTable.getSelection();
471         
472         IBuild build = getBuildModel(false).getBuild();
473         IBuildEntry entry = build.getEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
474         Iterator JavaDoc it = ssel.iterator();
475         try {
476             while (it.hasNext()) {
477                 String JavaDoc pluginName = (String JavaDoc) it.next();
478                 entry.removeToken(pluginName);
479             }
480             if (entry.getTokens().length == 0)
481                 build.remove(entry);
482         } catch (CoreException e){
483             PDEPlugin.logException( e );
484         }
485         refresh();
486         markDirty();
487     }
488
489     /* (non-Javadoc)
490      * @see org.eclipse.pde.internal.ui.editor.TableSection#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
491      */

492     protected void selectionChanged(IStructuredSelection sel) {
493         // Update global selection
494
getPage().getPDEEditor().setSelection(sel);
495         updateButtons();
496     }
497     
498     private void updateButtons() {
499         TablePart part = getTablePart();
500         Table table = fAdditionalTable.getTable();
501         int index = table.getSelectionIndex();
502         part.setButtonEnabled(1, index != -1);
503         updateUpDownButtons();
504     }
505
506     public void modelChanged(IModelChangedEvent event) {
507         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
508             markStale();
509             return;
510         }
511         Object JavaDoc changedObject = event.getChangedObjects()[0];
512         if ((changedObject instanceof IBuildEntry &&
513                 ((IBuildEntry) changedObject).getName().equals(IBuildEntry.SECONDARY_DEPENDENCIES))) {
514             refresh();
515         }
516     }
517     
518     /* (non-Javadoc)
519      * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
520      */

521     public boolean doGlobalAction(String JavaDoc actionId) {
522         
523         if (!isEditable()) { return false; }
524         
525         if (actionId.equals(ActionFactory.DELETE.getId())) {
526             handleRemove();
527             return true;
528         }
529         if (actionId.equals(ActionFactory.CUT.getId())) {
530             // delete here and let the editor transfer
531
// the selection to the clipboard
532
handleRemove();
533             return false;
534         }
535         if (actionId.equals(ActionFactory.PASTE.getId())) {
536             doPaste();
537             return true;
538         }
539         return false;
540         
541     }
542     
543     /* (non-Javadoc)
544      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(java.lang.Object, java.lang.Object[])
545      */

546     protected boolean canPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
547         HashSet JavaDoc secondaryDepSet = null;
548         // Only String objects representing non-duplicate secondary
549
// dependencies can be pasted
550
for (int i = 0; i < sourceObjects.length; i++) {
551             // Only String objects are allowed
552
if ((sourceObjects[i] instanceof String JavaDoc) == false) {
553                 return false;
554             }
555             // Get the current secondary dependencies and store them to
556
// assist in searching
557
if (secondaryDepSet == null) {
558                 secondaryDepSet = createSecondaryDepSet();
559             }
560             // No duplicate secondary dependencies allowed
561
String JavaDoc secondaryDep = (String JavaDoc)sourceObjects[i];
562             if (secondaryDepSet.contains(secondaryDep)) {
563                 return false;
564             }
565         }
566         return true;
567     }
568     
569     /**
570      * @return
571      */

572     private HashSet JavaDoc createSecondaryDepSet() {
573         HashSet JavaDoc secondaryDepSet = new HashSet JavaDoc();
574         // Get the build model
575
IBuildModel buildModel = getBuildModel(true);
576         // Ensure the build model is defined
577
if (buildModel == null) {
578             return secondaryDepSet;
579         }
580         // Get the root build object
581
IBuild build = buildModel.getBuild();
582         // Get the secondary dependencies build entry
583
IBuildEntry entry = build.getEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
584         // Ensure the build entry is defined
585
if (entry == null) {
586             return secondaryDepSet;
587         }
588         // Get the token values for the build entry
589
String JavaDoc[] tokens = entry.getTokens();
590         // Ensure we have token values
591
if (tokens.length == 0) {
592             return secondaryDepSet;
593         }
594         // Add all token values to the dependencies set
595
for (int i = 0; i < tokens.length; i++) {
596             secondaryDepSet.add(tokens[i]);
597         }
598         return secondaryDepSet;
599     }
600     
601     /* (non-Javadoc)
602      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(java.lang.Object, java.lang.Object[])
603      */

604     protected void doPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
605         // Get the build model
606
IBuildModel buildModel = getBuildModel(true);
607         // Ensure the build model is defined
608
if (buildModel == null) {
609             return;
610         }
611         // Get the root build object
612
IBuild build = buildModel.getBuild();
613         // Get the secondary dependencies build entry
614
IBuildEntry entry = build.getEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
615         try {
616             // Paste all source objects
617
for (int i = 0; i < sourceObjects.length; i++) {
618                 Object JavaDoc sourceObject = sourceObjects[i];
619                 if (sourceObject instanceof String JavaDoc) {
620                     // If the build entry is not defined, create one
621
if (entry == null) {
622                         entry = buildModel.getFactory().createEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
623                         build.add(entry);
624                     }
625                     // Add the source object token value to the build entry
626
entry.addToken((String JavaDoc)sourceObject);
627                 }
628             }
629         } catch (CoreException e) {
630             PDEPlugin.logException(e);
631         }
632     }
633     
634     protected void doAddDependencies() {
635         IBaseModel model = getPage().getModel();
636         if (model instanceof IBundlePluginModelBase) {
637             IProject proj = getPage().getPDEEditor().getCommonProject();
638             IBundlePluginModelBase bmodel = ((IBundlePluginModelBase)model);
639             AddNewDependenciesAction action = new AddNewDependenciesAction(proj, bmodel);
640             action.run();
641         }
642     }
643     
644     public void dispose() {
645         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
646         if (model!=null)
647             model.removeModelChangedListener(this);
648         PDECore.getDefault().getModelManager().removePluginModelListener(this);
649         super.dispose();
650     }
651
652     public void modelsChanged(PluginModelDelta delta) {
653         fAdditionalBundles = null;
654         final Control control = fAdditionalTable.getControl();
655         if (!control.isDisposed()) {
656             control.getDisplay().asyncExec(new Runnable JavaDoc() {
657                 public void run() {
658                     if (!control.isDisposed())
659                         fAdditionalTable.refresh();
660                 }
661             });
662         }
663     }
664     
665     private void handleUp() {
666         movePlugins(-1);
667     }
668
669     private void handleDown() {
670         movePlugins(1);
671     }
672
673     private void updateUpDownButtons() {
674         TablePart tablePart = getTablePart();
675         if (fSortAction.isChecked()) {
676             tablePart.setButtonEnabled(UP_INDEX, false);
677             tablePart.setButtonEnabled(DOWN_INDEX, false);
678             return;
679         }
680         Table table = fAdditionalTable.getTable();
681         int index = table.getSelectionIndex();
682         int totalElems = table.getItemCount();
683         boolean canMove = totalElems > 1 && table.getSelectionCount() == 1;
684         tablePart.setButtonEnabled(2, canMove && index > 0);
685         tablePart.setButtonEnabled(3, canMove && index >= 0 && index < totalElems - 1);
686     }
687     
688     private void movePlugins(int newOffset) {
689         int index = fAdditionalTable.getTable().getSelectionIndex();
690         if (index == -1)
691             return; // safety check
692
IBuildModel model = getBuildModel(false);
693         if (model != null) {
694             IBuild build = model.getBuild();
695             IBuildEntry entry = build.getEntry(IBuildEntry.SECONDARY_DEPENDENCIES);
696             if (entry instanceof org.eclipse.pde.internal.core.text.build.BuildEntry)
697                 ((org.eclipse.pde.internal.core.text.build.BuildEntry)entry).swap(index, index + newOffset);
698         }
699         updateButtons();
700     }
701     
702     public void propertyChange(PropertyChangeEvent event) {
703         if (fSortAction.equals(event.getSource()) && IAction.RESULT.equals(event.getProperty())) {
704             updateUpDownButtons();
705         }
706     }
707     
708     protected boolean createCount() { return true; }
709 }
710
Popular Tags