KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IFolder;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jdt.core.IClasspathEntry;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.action.Separator;
30 import org.eclipse.jface.viewers.IStructuredContentProvider;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.jface.viewers.TableViewer;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.jface.window.Window;
36 import org.eclipse.pde.core.IBaseModel;
37 import org.eclipse.pde.core.IModel;
38 import org.eclipse.pde.core.IModelChangedEvent;
39 import org.eclipse.pde.core.IModelChangedListener;
40 import org.eclipse.pde.core.build.IBuild;
41 import org.eclipse.pde.core.build.IBuildEntry;
42 import org.eclipse.pde.core.build.IBuildModel;
43 import org.eclipse.pde.core.plugin.IPluginBase;
44 import org.eclipse.pde.core.plugin.IPluginElement;
45 import org.eclipse.pde.core.plugin.IPluginLibrary;
46 import org.eclipse.pde.core.plugin.IPluginModelBase;
47 import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
48 import org.eclipse.pde.internal.core.ClasspathUtilCore;
49 import org.eclipse.pde.internal.core.plugin.PluginLibrary;
50 import org.eclipse.pde.internal.ui.PDEPlugin;
51 import org.eclipse.pde.internal.ui.PDEUIMessages;
52 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
53 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
54 import org.eclipse.pde.internal.ui.editor.TableSection;
55 import org.eclipse.pde.internal.ui.editor.build.BuildInputContext;
56 import org.eclipse.pde.internal.ui.editor.build.BuildSourcePage;
57 import org.eclipse.pde.internal.ui.editor.build.JARFileFilter;
58 import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
59 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
60 import org.eclipse.pde.internal.ui.parts.EditableTablePart;
61 import org.eclipse.pde.internal.ui.parts.TablePart;
62 import org.eclipse.pde.internal.ui.util.SWTUtil;
63 import org.eclipse.swt.SWT;
64 import org.eclipse.swt.events.SelectionAdapter;
65 import org.eclipse.swt.events.SelectionEvent;
66 import org.eclipse.swt.layout.GridData;
67 import org.eclipse.swt.widgets.Button;
68 import org.eclipse.swt.widgets.Composite;
69 import org.eclipse.swt.widgets.Control;
70 import org.eclipse.swt.widgets.Table;
71 import org.eclipse.ui.actions.ActionFactory;
72 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
73 import org.eclipse.ui.forms.editor.IFormPage;
74 import org.eclipse.ui.forms.widgets.FormToolkit;
75 import org.eclipse.ui.forms.widgets.Section;
76 import org.eclipse.ui.model.WorkbenchContentProvider;
77 import org.eclipse.ui.model.WorkbenchLabelProvider;
78 import org.eclipse.ui.views.navigator.ResourceComparator;
79
80 public class LibrarySection extends TableSection implements IModelChangedListener, IBuildPropertiesConstants {
81
82     private static final int NEW_INDEX = 0;
83     private static final int ADD_INDEX = 1;
84     private static final int REMOVE_INDEX = 2;
85     private static final int UP_INDEX = 3;
86     private static final int DOWN_INDEX = 4;
87     
88     private Action fRenameAction;
89     private Action fRemoveAction;
90     private Action fNewAction;
91         
92     private TableViewer fLibraryTable;
93     
94     class LibraryFilter extends JARFileFilter {
95         public LibraryFilter(HashSet JavaDoc set) {
96             super(set);
97         }
98
99         public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element) {
100             if (element instanceof IFolder)
101                 return isPathValid(((IFolder)element).getProjectRelativePath());
102             if (element instanceof IFile)
103                 return isFileValid(((IFile)element).getProjectRelativePath());
104             return false;
105         }
106     }
107     
108     class LibrarySelectionValidator extends JarSelectionValidator {
109         
110         public LibrarySelectionValidator(Class JavaDoc[] acceptedTypes, boolean allowMultipleSelection) {
111             super(acceptedTypes, allowMultipleSelection);
112         }
113
114         public boolean isValid(Object JavaDoc element) {
115             return (element instanceof IFolder) ? true : super.isValid(element);
116         }
117     }
118
119     class TableContentProvider extends DefaultContentProvider
120         implements IStructuredContentProvider {
121         public Object JavaDoc[] getElements(Object JavaDoc parent) {
122             return getModel().getPluginBase().getLibraries();
123         }
124     }
125
126     public LibrarySection(PDEFormPage page, Composite parent) {
127         super(
128             page,
129             parent,
130             Section.DESCRIPTION,
131             new String JavaDoc[] {
132                 PDEUIMessages.NewManifestEditor_LibrarySection_new,
133                 PDEUIMessages.NewManifestEditor_LibrarySection_add,
134                 PDEUIMessages.NewManifestEditor_LibrarySection_remove,
135                 PDEUIMessages.ManifestEditor_LibrarySection_up,
136                 PDEUIMessages.ManifestEditor_LibrarySection_down});
137     }
138     
139     private String JavaDoc getSectionDescription() {
140         IPluginModelBase model = getModel();
141         if (isBundle()) {
142            return (model.isFragmentModel())
143                ? PDEUIMessages.ClasspathSection_fragment
144                : PDEUIMessages.ClasspathSection_plugin;
145         }
146         return (model.isFragmentModel())
147                     ? PDEUIMessages.ManifestEditor_LibrarySection_fdesc
148                     : PDEUIMessages.ManifestEditor_LibrarySection_desc;
149     }
150     
151     protected boolean isBundle() {
152         return getBundleContext() != null;
153     }
154     
155     private BundleInputContext getBundleContext() {
156         InputContextManager manager = getPage().getPDEEditor().getContextManager();
157         return (BundleInputContext) manager.findContext(BundleInputContext.CONTEXT_ID);
158     }
159     
160     public void createClient(Section section, FormToolkit toolkit) {
161         section.setText(PDEUIMessages.ManifestEditor_LibrarySection_title);
162         section.setDescription(getSectionDescription());
163         
164         Composite container = createClientContainer(section, 2, toolkit);
165         EditableTablePart tablePart = getTablePart();
166         tablePart.setEditable(isEditable());
167
168         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
169         fLibraryTable = tablePart.getTableViewer();
170         fLibraryTable.setContentProvider(new TableContentProvider());
171         fLibraryTable.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
172         toolkit.paintBordersFor(container);
173         
174         makeActions();
175         updateButtons();
176         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
177         section.setLayoutData(new GridData(GridData.FILL_BOTH));
178         section.setClient(container);
179
180         IPluginModelBase model = getModel();
181         fLibraryTable.setInput(model.getPluginBase());
182         model.addModelChangedListener(this);
183     }
184
185     private void updateButtons() {
186         Table table = fLibraryTable.getTable();
187         boolean hasSelection = table.getSelection().length > 0;
188         boolean singleSelection = table.getSelection().length == 1;
189         int count = table.getItemCount();
190         int index = table.getSelectionIndex();
191         boolean canMoveUp = singleSelection && index > 0;
192         boolean canMoveDown = singleSelection && index < count - 1;
193         
194         TablePart tablePart = getTablePart();
195         tablePart.setButtonEnabled(ADD_INDEX, isEditable());
196         tablePart.setButtonEnabled(NEW_INDEX, isEditable());
197         tablePart.setButtonEnabled(REMOVE_INDEX, isEditable() && hasSelection);
198         tablePart.setButtonEnabled(UP_INDEX, isEditable() && canMoveUp);
199         tablePart.setButtonEnabled(DOWN_INDEX, isEditable() && canMoveDown);
200     }
201
202     private void makeActions() {
203         fNewAction = new Action(PDEUIMessages.ManifestEditor_LibrarySection_newLibrary) {
204             public void run() {
205                 handleNew();
206             }
207         };
208         fNewAction.setEnabled(isEditable());
209         
210         fRenameAction = new Action(PDEUIMessages.EditableTablePart_renameAction) {
211             public void run() {
212                 getRenameAction().run();
213             }
214         };
215         fRenameAction.setEnabled(isEditable());
216         
217         fRemoveAction = new Action(PDEUIMessages.NewManifestEditor_LibrarySection_remove) {
218             public void run() {
219                 handleRemove();
220             }
221         };
222         fRemoveAction.setEnabled(isEditable());
223     }
224
225     protected void selectionChanged(IStructuredSelection selection) {
226         getPage().getPDEEditor().setSelection(selection);
227         if (getPage().getModel().isEditable())
228             updateButtons();
229     }
230
231     protected void buttonSelected(int index) {
232         switch (index) {
233             case NEW_INDEX :
234                 handleNew();
235                 break;
236             case ADD_INDEX:
237                 handleAdd();
238                 break;
239             case REMOVE_INDEX:
240                 handleRemove();
241                 break;
242             case UP_INDEX :
243                 handleUp();
244                 break;
245             case DOWN_INDEX :
246                 handleDown();
247                 break;
248         }
249     }
250
251     public void dispose() {
252         IPluginModelBase model = getModel();
253         if (model != null)
254             model.removeModelChangedListener(this);
255         super.dispose();
256     }
257
258     /* (non-Javadoc)
259      * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
260      */

261     public boolean doGlobalAction(String JavaDoc actionId) {
262
263         if (!isEditable()) { return false; }
264         
265         if (actionId.equals(ActionFactory.DELETE.getId())) {
266             handleRemove();
267             return true;
268         }
269         if (actionId.equals(ActionFactory.CUT.getId())) {
270             // delete here and let the editor transfer
271
// the selection to the clipboard
272
handleRemove();
273             return false;
274         }
275         if (actionId.equals(ActionFactory.PASTE.getId())) {
276             doPaste();
277             return true;
278         }
279         return false;
280     }
281     
282     public boolean setFormInput(Object JavaDoc object) {
283         if (object instanceof IPluginLibrary) {
284             fLibraryTable.setSelection(new StructuredSelection(object), true);
285             return true;
286         }
287         return false;
288     }
289
290     protected void fillContextMenu(IMenuManager manager) {
291         manager.add(fNewAction);
292         if (!fLibraryTable.getSelection().isEmpty()) {
293             manager.add(new Separator());
294             manager.add(fRenameAction);
295             manager.add(fRemoveAction);
296         }
297         // Copy, cut, and paste operations not supported for plug-ins that do
298
// not have a MANIFEST.MF (not a Bundle)
299
getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager, isBundle());
300     }
301     
302     private void handleRemove() {
303         Object JavaDoc[] selection = ((IStructuredSelection) fLibraryTable.getSelection()).toArray();
304         int index = fLibraryTable.getTable().getSelectionIndex();
305         int[] indices = fLibraryTable.getTable().getSelectionIndices();
306         for (int i = 0; i < indices.length; i++)
307             if (indices[i] < index)
308                 index = indices[i];
309         
310         String JavaDoc[] remove = new String JavaDoc[selection.length];
311         for (int i = 0; i < selection.length; i++) {
312             if (selection[i] != null && selection[i] instanceof IPluginLibrary) {
313                 IPluginLibrary ep = (IPluginLibrary) selection[i];
314                 IPluginBase plugin = ep.getPluginBase();
315                 try {
316                     plugin.remove(ep);
317                 } catch (CoreException e) {
318                     PDEPlugin.logException(e);
319                 }
320                 remove[i] = ep.getName();
321             }
322         }
323         updateBuildProperties(remove, new String JavaDoc[remove.length], true);
324         updateJavaClasspathLibs(remove, new String JavaDoc[remove.length]);
325         
326         int itemCount = fLibraryTable.getTable().getItemCount();
327         if (itemCount > 0) {
328             if (index >= itemCount)
329                 index = itemCount - 1;
330             fLibraryTable.getTable().setSelection(index);
331             fLibraryTable.getTable().setFocus();
332         }
333         updateButtons();
334     }
335     private void handleDown() {
336         Table table = getTablePart().getTableViewer().getTable();
337         int index = table.getSelectionIndex();
338         if (index != table.getItemCount() - 1)
339             swap(index, index + 1);
340     }
341     
342     private void handleUp() {
343         int index = getTablePart().getTableViewer().getTable().getSelectionIndex();
344         if (index >= 1)
345             swap(index, index - 1);
346     }
347     
348     public void swap(int index1, int index2) {
349         Table table = getTablePart().getTableViewer().getTable();
350         IPluginLibrary l1 = (IPluginLibrary)table.getItem(index1).getData();
351         IPluginLibrary l2 = (IPluginLibrary)table.getItem(index2).getData();
352
353         try {
354             IPluginModelBase model = getModel();
355             IPluginBase pluginBase = model.getPluginBase();
356             pluginBase.swap(l1, l2);
357             refresh();
358             table.setSelection(index2);
359             table.setFocus();
360             updateButtons();
361         } catch (CoreException e) {
362             PDEPlugin.logException(e);
363         }
364     }
365     
366     private void handleNew(){
367         IPluginModelBase model = getModel();
368         NewRuntimeLibraryDialog dialog = new NewRuntimeLibraryDialog(
369                 getPage().getSite().getShell(),
370                 model.getPluginBase().getLibraries());
371         dialog.create();
372         dialog.getShell().setText(PDEUIMessages.ManifestEditor_LibrarySection_newLibraryEntry);
373         SWTUtil.setDialogSize(dialog, 250, 175);
374
375         if (dialog.open() == Window.OK){
376             String JavaDoc libName = dialog.getLibraryName();
377             if (libName==null || libName.length()==0)
378                 return;
379             try {
380                 IPluginLibrary library = model.getPluginFactory().createLibrary();
381                 library.setName(libName);
382                 library.setExported(true);
383                 model.getPluginBase().add(library);
384                 checkSourceRootEntry();
385                 updateBuildProperties(
386                         new String JavaDoc[] {null},
387                         new String JavaDoc[] { library.getName()},
388                         true);
389                 fLibraryTable.setSelection(new StructuredSelection(library));
390                 fLibraryTable.getTable().setFocus();
391             } catch (CoreException e) {
392                 PDEPlugin.logException(e);
393             }
394         }
395     }
396     
397     private void checkSourceRootEntry() {
398         IPluginModelBase pluginModel = getModel();
399         IPluginLibrary[] libraries = pluginModel.getPluginBase().getLibraries();
400         for (int i = 0; i < libraries.length; i++)
401             if (libraries[i].getName().equals(".")) //$NON-NLS-1$
402
return;
403         IBuildModel model = getBuildModel();
404         if (model == null)
405             return;
406         
407         IBuildEntry[] entires = model.getBuild().getBuildEntries();
408         for (int i = 0; i < entires.length; i++) {
409             if (entires[i].getName().equals(PROPERTY_SOURCE_PREFIX + '.')) {
410                 IPluginLibrary library = pluginModel.getPluginFactory().createLibrary();
411                 try {
412                     library.setName("."); //$NON-NLS-1$
413
pluginModel.getPluginBase().add(library);
414                 } catch (CoreException e) {
415                 }
416             }
417         }
418     }
419
420     private IBuildModel getBuildModel() {
421         IFormPage page = getPage().getEditor().findPage(BuildInputContext.CONTEXT_ID);
422         IBaseModel model = null;
423         if (page instanceof BuildSourcePage)
424             model = ((BuildSourcePage)page).getInputContext().getModel();
425         
426         if (model != null && model instanceof IBuildModel)
427             return (IBuildModel)model;
428         return null;
429     }
430     
431     private void configureSourceBuildEntry(IBuildModel bmodel, String JavaDoc oldPath, String JavaDoc newPath) throws CoreException {
432         IBuild build = bmodel.getBuild();
433         IBuildEntry entry = build.getEntry(PROPERTY_SOURCE_PREFIX + (oldPath != null ? oldPath : newPath));
434         try {
435             if (newPath != null) {
436                 if (entry == null) {
437                     IProject project = ((IModel)getPage().getModel()).getUnderlyingResource().getProject();
438                     IJavaProject jproject = JavaCore.create(project);
439                     ArrayList JavaDoc tokens = new ArrayList JavaDoc();
440                     IClasspathEntry[] entries = jproject.getRawClasspath();
441                     for (int i = 0; i < entries.length; i++)
442                         if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)
443                             tokens.add(entries[i].getPath().removeFirstSegments(1).addTrailingSeparator().toString());
444                     if (tokens.size() == 0)
445                         return;
446                     
447                     entry = bmodel.getFactory().createEntry(PROPERTY_SOURCE_PREFIX + newPath);
448                     for (int i = 0; i < tokens.size(); i++)
449                         entry.addToken((String JavaDoc)tokens.get(i));
450                     build.add(entry);
451                 } else
452                     entry.setName(PROPERTY_SOURCE_PREFIX + newPath);
453             } else if (entry != null && newPath == null)
454                 build.remove(entry);
455         } catch (JavaModelException e) {
456         }
457     }
458     
459     private void handleAdd() {
460         final boolean[] updateClasspath = new boolean[] {true};
461         ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
462                 getPage().getSite().getShell(),
463                 new WorkbenchLabelProvider(),
464                 new WorkbenchContentProvider()) {
465             protected Control createDialogArea(Composite parent) {
466                 Composite comp = (Composite)super.createDialogArea(parent);
467                 final Button button = new Button(comp, SWT.CHECK);
468                 button.setText(PDEUIMessages.LibrarySection_addDialogButton);
469                 button.setSelection(updateClasspath[0]);
470                 button.addSelectionListener(new SelectionAdapter() {
471                     public void widgetSelected(SelectionEvent e) {
472                         updateClasspath[0] = button.getSelection();
473                     }
474                 });
475                 applyDialogFont(button);
476                 return comp;
477             }
478         };
479                 
480         Class JavaDoc[] acceptedClasses = new Class JavaDoc[] { IFile.class };
481         dialog.setValidator(new LibrarySelectionValidator(acceptedClasses, true));
482         dialog.setTitle(PDEUIMessages.BuildEditor_ClasspathSection_jarsTitle);
483         dialog.setMessage(PDEUIMessages.ClasspathSection_jarsMessage);
484         IPluginLibrary[] libraries = getModel().getPluginBase().getLibraries();
485         HashSet JavaDoc set = new HashSet JavaDoc();
486         for (int i = 0; i < libraries.length; i++)
487             set.add(new Path(ClasspathUtilCore.expandLibraryName(libraries[i].getName())));
488         
489         dialog.addFilter(new LibraryFilter(set));
490         IProject project = ((IModel)getPage().getModel()).getUnderlyingResource().getProject();
491         dialog.setInput(project);
492         dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
493
494         if (dialog.open() == Window.OK) {
495             Object JavaDoc[] elements = dialog.getResult();
496             String JavaDoc[] filePaths = new String JavaDoc[elements.length];
497             IPluginModelBase model = getModel();
498             ArrayList JavaDoc list = new ArrayList JavaDoc();
499             for (int i = 0; i < elements.length; i++) {
500                 IResource elem = (IResource) elements[i];
501                 IPath path = elem.getProjectRelativePath();
502                 if (elem instanceof IFolder)
503                     path = path.addTrailingSeparator();
504                 filePaths[i] = path.toString();
505                 IPluginLibrary library = model.getPluginFactory().createLibrary();
506                 try {
507                     library.setName(filePaths[i]);
508                     library.setExported(true);
509                     model.getPluginBase().add(library);
510                     list.add(library);
511                 } catch (CoreException e) {
512                     PDEPlugin.logException(e);
513                 }
514             }
515             checkSourceRootEntry();
516             updateBuildProperties(new String JavaDoc[filePaths.length], filePaths, false);
517             if (updateClasspath[0])
518                 updateJavaClasspathLibs(new String JavaDoc[filePaths.length], filePaths);
519             fLibraryTable.setSelection(new StructuredSelection(list.toArray()));
520             fLibraryTable.getTable().setFocus();
521         }
522     }
523
524     private void updateBuildProperties(final String JavaDoc[] oldPaths, final String JavaDoc[] newPaths, boolean modifySourceEntry) {
525         IBuildModel bmodel = getBuildModel();
526         if (bmodel == null)
527             return;
528         
529         IBuild build = bmodel.getBuild();
530         
531         IBuildEntry entry = build.getEntry(PROPERTY_BIN_INCLUDES);
532         if (entry == null)
533             entry = bmodel.getFactory().createEntry(PROPERTY_BIN_INCLUDES);
534         
535         try {
536             // adding new entries
537
if (oldPaths[0] == null) {
538                 for (int i = 0; i < newPaths.length; i++)
539                     if (newPaths[i] != null) {
540                         entry.addToken(newPaths[i]);
541                         if (modifySourceEntry)
542                             configureSourceBuildEntry(bmodel, null, newPaths[i]);
543                     }
544             // removing entries
545
} else if (newPaths[0] == null) {
546                 for (int i = 0; i < oldPaths.length; i++)
547                     if (oldPaths[i] != null) {
548                         entry.removeToken(oldPaths[i]);
549                         if (modifySourceEntry)
550                             configureSourceBuildEntry(bmodel, oldPaths[i], null);
551                     }
552                 if (entry.getTokens().length == 0)
553                     build.remove(entry);
554             // rename entries
555
} else {
556                 for (int i = 0; i < oldPaths.length; i++)
557                     if (newPaths[i] != null && oldPaths[i] != null) {
558                         entry.renameToken(oldPaths[i], newPaths[i]);
559                         if (modifySourceEntry)
560                             configureSourceBuildEntry(bmodel, oldPaths[i], newPaths[i]);
561                     }
562             }
563         } catch (CoreException e) {
564         }
565     }
566
567     private void updateJavaClasspathLibs(String JavaDoc[] oldPaths, String JavaDoc[] newPaths) {
568         IProject project = ((IModel)getPage().getModel()).getUnderlyingResource().getProject();
569         IJavaProject jproject = JavaCore.create(project);
570         try {
571             IClasspathEntry[] entries = jproject.getRawClasspath();
572             ArrayList JavaDoc toBeAdded = new ArrayList JavaDoc();
573             int index = -1;
574             entryLoop: for (int i = 0; i < entries.length; i++) {
575                 if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
576                     if (index == -1)
577                         index = i;
578                     // do not add the old paths (handling deletion/renaming)
579
IPath path = entries[i].getPath().removeFirstSegments(1).removeTrailingSeparator();
580                     for (int j = 0; j < oldPaths.length; j++)
581                         if (oldPaths[j] != null &&
582                                 path.equals(new Path(oldPaths[j]).removeTrailingSeparator()))
583                             continue entryLoop;
584                 } else if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER)
585                     if (index == -1)
586                         index = i;
587                 toBeAdded.add(entries[i]);
588             }
589             if (index == -1)
590                 index = entries.length;
591             
592             // add paths
593
for (int i = 0; i < newPaths.length; i++) {
594                 if (newPaths[i] == null)
595                     continue;
596                 IClasspathEntry entry = JavaCore.newLibraryEntry(
597                         project.getFullPath().append(newPaths[i]), null, null, true);
598                 if (!toBeAdded.contains(entry))
599                     toBeAdded.add(index++, entry);
600             }
601             
602             if (toBeAdded.size() == entries.length)
603                 return;
604             
605             IClasspathEntry[] updated = (IClasspathEntry[])toBeAdded.toArray(new IClasspathEntry[toBeAdded.size()]);
606             jproject.setRawClasspath(updated, null);
607         } catch (JavaModelException e) {
608         }
609     }
610     
611     public void refresh() {
612         if (fLibraryTable.getControl().isDisposed())
613             return;
614         fLibraryTable.setSelection(null);
615         fLibraryTable.refresh();
616         super.refresh();
617     }
618     public void modelChanged(IModelChangedEvent event) {
619         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
620             markStale();
621             return;
622         }
623         Object JavaDoc changeObject = event.getChangedObjects()[0];
624         if (changeObject instanceof IPluginLibrary) {
625             if (event.getChangeType() == IModelChangedEvent.INSERT) {
626                 fLibraryTable.add(changeObject);
627             } else if (event.getChangeType() == IModelChangedEvent.REMOVE) {
628                 fLibraryTable.remove(changeObject);
629             } else {
630                 fLibraryTable.update(changeObject, null);
631             }
632         } else if (changeObject.equals(fLibraryTable.getInput())) {
633             markStale();
634         } else if (changeObject instanceof IPluginElement && ((IPluginElement)changeObject).getParent() instanceof IPluginLibrary) {
635             fLibraryTable.update(((IPluginElement)changeObject).getParent(), null);
636         }
637     }
638     
639     public void setFocus() {
640         fLibraryTable.getTable().setFocus();
641     }
642     
643     /* (non-Javadoc)
644      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(java.lang.Object, java.lang.Object[])
645      */

646     protected void doPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
647         // Get the model
648
IPluginModelBase model = getModel();
649         IPluginBase plugin = model.getPluginBase();
650         try {
651             // Paste all source objects
652
for (int i = 0; i < sourceObjects.length; i++) {
653                 Object JavaDoc sourceObject = sourceObjects[i];
654                 if (sourceObject instanceof PluginLibrary) {
655                     // Plugin library object
656
PluginLibrary library = (PluginLibrary) sourceObject;
657                     // Adjust all the source object transient field values to
658
// acceptable values
659
library.reconnect(model, plugin);
660                     // Add the library to the plug-in
661
plugin.add(library);
662                 }
663             }
664         } catch (CoreException e) {
665             PDEPlugin.logException(e);
666         }
667     }
668     
669     /* (non-Javadoc)
670      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(java.lang.Object, java.lang.Object[])
671      */

672     protected boolean canPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
673         HashSet JavaDoc librarySet = null;
674         // Only source objects that are plugin libraries that have not already
675
// been specified can be pasted
676
for (int i = 0; i < sourceObjects.length; i++) {
677             // Only plugin libraries are allowed
678
if ((sourceObjects[i] instanceof IPluginLibrary) == false) {
679                 return false;
680             }
681             // We have a plugin library
682
// Get the current libraries already specified and store them in
683
// a set to assist with searching
684
if (librarySet == null) {
685                 librarySet = createPluginLibrarySet();
686             }
687             // No duplicate libraries are allowed
688
IPluginLibrary library = (IPluginLibrary)sourceObjects[i];
689             if (librarySet.contains(new Path(ClasspathUtilCore.expandLibraryName(library.getName())))) {
690                 return false;
691             }
692         }
693         return true;
694     }
695     
696     /**
697      * @return
698      */

699     private HashSet JavaDoc createPluginLibrarySet() {
700         // Get the current libraries and add them to a set for easy searching
701
IPluginLibrary[] libraries = getModel().getPluginBase().getLibraries();
702         HashSet JavaDoc librarySet = new HashSet JavaDoc();
703         for (int i = 0; i < libraries.length; i++) {
704             librarySet.add(new Path(ClasspathUtilCore.expandLibraryName(libraries[i].getName())));
705         }
706         return librarySet;
707     }
708
709     protected void entryModified(Object JavaDoc entry, String JavaDoc value) {
710         try {
711             IPluginModelBase model = getModel();
712             IProject project = model.getUnderlyingResource().getProject();
713             IPluginLibrary library = (IPluginLibrary) entry;
714             model.getPluginBase().remove(library);
715             String JavaDoc[] oldValue = { library.getName() };
716             String JavaDoc[] newValue = { value };
717             library.setName(value);
718             boolean memberExists = project.findMember(value) != null;
719             updateBuildProperties(oldValue, newValue, !memberExists);
720             updateJavaClasspathLibs(oldValue, memberExists ? newValue : new String JavaDoc[] {null});
721             model.getPluginBase().add(library);
722         } catch (CoreException e) {
723             PDEPlugin.logException(e);
724         }
725     }
726     
727     /**
728      * @return
729      */

730     private IPluginModelBase getModel() {
731         return (IPluginModelBase)getPage().getModel();
732     }
733 }
734
Popular Tags