KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > build > RuntimeInfoSection


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
12 package org.eclipse.pde.internal.ui.editor.build;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jdt.core.IClasspathEntry;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.IPackageFragmentRoot;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jface.action.Action;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.action.IMenuListener;
37 import org.eclipse.jface.action.IMenuManager;
38 import org.eclipse.jface.action.MenuManager;
39 import org.eclipse.jface.action.Separator;
40 import org.eclipse.jface.dialogs.IInputValidator;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredContentProvider;
43 import org.eclipse.jface.viewers.IStructuredSelection;
44 import org.eclipse.jface.viewers.ITableLabelProvider;
45 import org.eclipse.jface.viewers.LabelProvider;
46 import org.eclipse.jface.viewers.StructuredSelection;
47 import org.eclipse.jface.viewers.TableViewer;
48 import org.eclipse.jface.viewers.Viewer;
49 import org.eclipse.jface.viewers.ViewerFilter;
50 import org.eclipse.jface.window.Window;
51 import org.eclipse.pde.core.IBaseModel;
52 import org.eclipse.pde.core.IEditable;
53 import org.eclipse.pde.core.IModelChangedEvent;
54 import org.eclipse.pde.core.IModelChangedListener;
55 import org.eclipse.pde.core.build.IBuild;
56 import org.eclipse.pde.core.build.IBuildEntry;
57 import org.eclipse.pde.core.build.IBuildModel;
58 import org.eclipse.pde.core.plugin.IPluginModelBase;
59 import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
60 import org.eclipse.pde.internal.ui.PDELabelProvider;
61 import org.eclipse.pde.internal.ui.PDEPlugin;
62 import org.eclipse.pde.internal.ui.PDEPluginImages;
63 import org.eclipse.pde.internal.ui.PDEUIMessages;
64 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
65 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
66 import org.eclipse.pde.internal.ui.editor.PDESection;
67 import org.eclipse.pde.internal.ui.editor.context.InputContext;
68 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
69 import org.eclipse.pde.internal.ui.parts.StructuredViewerPart;
70 import org.eclipse.pde.internal.ui.parts.TablePart;
71 import org.eclipse.pde.internal.ui.wizards.FolderSelectionDialog;
72 import org.eclipse.pde.internal.ui.wizards.RenameDialog;
73 import org.eclipse.swt.SWT;
74 import org.eclipse.swt.custom.BusyIndicator;
75 import org.eclipse.swt.graphics.Image;
76 import org.eclipse.swt.layout.GridData;
77 import org.eclipse.swt.widgets.Button;
78 import org.eclipse.swt.widgets.Composite;
79 import org.eclipse.swt.widgets.Table;
80 import org.eclipse.ui.ISharedImages;
81 import org.eclipse.ui.PlatformUI;
82 import org.eclipse.ui.actions.ActionFactory;
83 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
84 import org.eclipse.ui.forms.widgets.FormToolkit;
85 import org.eclipse.ui.forms.widgets.Section;
86 import org.eclipse.ui.model.WorkbenchContentProvider;
87 import org.eclipse.ui.model.WorkbenchLabelProvider;
88
89 public class RuntimeInfoSection extends PDESection implements IModelChangedListener, IBuildPropertiesConstants {
90     
91     private static final int F_NEW_INDEX = 0;
92     private static final int F_UP_UNDEX = 2;
93     private static final int F_DOWN_INDEX = 3;
94     
95     protected TableViewer fLibraryViewer;
96     protected TableViewer fFolderViewer;
97     
98     protected StructuredViewerPart fLibraryPart;
99     protected StructuredViewerPart fFolderPart;
100     
101     private boolean fEnabled = true;
102     
103     class PartAdapter extends TablePart {
104         
105         public PartAdapter(String JavaDoc[] buttonLabels) {
106             super(buttonLabels);
107         }
108         
109         public void selectionChanged(IStructuredSelection selection) {
110             getPage().getPDEEditor().setSelection(selection);
111             Object JavaDoc item = selection.getFirstElement();
112             if (item instanceof IBuildEntry) {
113                 update((IBuildEntry) item);
114             } else if (selection == null || selection.isEmpty())
115                 update(null);
116             updateDirectionalButtons();
117         }
118         
119         public void handleDoubleClick(IStructuredSelection selection) {
120             Object JavaDoc element = selection.getFirstElement();
121             if (getLibrarySelection() == element)
122                 doRename();
123             else if (element instanceof String JavaDoc)
124                 handleRenameFolder((String JavaDoc)element);
125         }
126         
127         public void buttonSelected(Button button, int index) {
128             if (getViewer() == fLibraryPart.getViewer()) {
129                 switch (index) {
130                 case F_NEW_INDEX:
131                     handleNew();
132                     break;
133                 case F_UP_UNDEX: // move up
134
updateJarsCompileOrder(true);
135                     break;
136                 case F_DOWN_INDEX: // move down
137
updateJarsCompileOrder(false);
138                     break;
139                 }
140             } else if (getViewer() == fFolderPart.getViewer() && index == F_NEW_INDEX)
141                 handleNewFolder();
142             else
143                 button.getShell().setDefaultButton(null);
144         }
145     }
146     
147     public class LibraryContentProvider extends DefaultContentProvider implements IStructuredContentProvider {
148         
149         public Object JavaDoc[] getElements(Object JavaDoc parent) {
150             if (parent instanceof IBuildModel) {
151                 IBuild build = ((IBuildModel) parent).getBuild();
152                 IBuildEntry jarOrderEntry = build.getEntry(PROPERTY_JAR_ORDER);
153                 IBuildEntry[] libraries = BuildUtil.getBuildLibraries(build.getBuildEntries());
154                 if (jarOrderEntry == null)
155                     return libraries;
156                 
157                 Vector JavaDoc libList = new Vector JavaDoc();
158                 String JavaDoc[] tokens = jarOrderEntry.getTokens();
159                 for (int i = 0; i < tokens.length; i++) {
160                     IBuildEntry entry = build.getEntry(IBuildEntry.JAR_PREFIX + tokens[i]);
161                     if (entry != null)
162                         libList.add(entry);
163                 }
164                 for (int i = 0; i < libraries.length; i++)
165                     if (!libList.contains(libraries[i]))
166                         libList.add(libraries[i]);
167                 return libList.toArray();
168             }
169             return new Object JavaDoc[0];
170         }
171     }
172     
173     public class LibraryLabelProvider extends LabelProvider implements ITableLabelProvider {
174         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
175             String JavaDoc name = ((IBuildEntry) obj).getName();
176             if (name.startsWith(IBuildEntry.JAR_PREFIX))
177                 return name.substring(IBuildEntry.JAR_PREFIX.length());
178             return name;
179         }
180         
181         public Image getColumnImage(Object JavaDoc obj, int index) {
182             PDELabelProvider provider = PDEPlugin.getDefault().getLabelProvider();
183             return provider.get(PDEPluginImages.DESC_JAVA_LIB_OBJ);
184         }
185     }
186     
187     class JarsNewContentProvider extends WorkbenchContentProvider {
188         public boolean hasChildren(Object JavaDoc element) {
189             Object JavaDoc[] children = getChildren(element);
190             for (int i = 0; i < children.length; i++)
191                 if (children[i] instanceof IFolder)
192                     return true;
193             return false;
194         }
195     }
196     
197     public class FolderContentProvider extends DefaultContentProvider implements IStructuredContentProvider {
198         public Object JavaDoc[] getElements(Object JavaDoc parent) {
199             return (parent instanceof IBuildEntry) ? ((IBuildEntry) parent).getTokens() : new Object JavaDoc[0];
200         }
201     }
202     
203     public class FolderLabelProvider extends LabelProvider implements ITableLabelProvider {
204         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
205             return obj.toString();
206         }
207         
208         public Image getColumnImage(Object JavaDoc obj, int index) {
209             ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
210             return sharedImages.getImage(ISharedImages.IMG_OBJ_FOLDER);
211         }
212     }
213     
214     public RuntimeInfoSection(PDEFormPage page, Composite parent) {
215         super(page, parent, Section.DESCRIPTION);
216         getSection().setText(PDEUIMessages.BuildEditor_RuntimeInfoSection_title);
217         getSection().setDescription(PDEUIMessages.BuildEditor_RuntimeInfoSection_desc);
218         getBuildModel().addModelChangedListener(this);
219         createClient(getSection(), page.getManagedForm().getToolkit());
220     }
221     
222     private IBuildModel getBuildModel() {
223         InputContext context = getPage().getPDEEditor().getContextManager().findContext(BuildInputContext.CONTEXT_ID);
224         if (context == null)
225             return null;
226         return (IBuildModel) context.getModel();
227     }
228     
229     protected void handleLibInBinBuild(boolean isSelected, String JavaDoc libName) {
230         IBuildModel model = getBuildModel();
231         IBuildEntry binIncl = model.getBuild().getEntry(PROPERTY_BIN_INCLUDES);
232         IProject project = model.getUnderlyingResource().getProject();
233         IPath libPath;
234         if (libName.equals(".")) //$NON-NLS-1$
235
libPath = null;
236         else
237             libPath = project.getFile(libName).getProjectRelativePath();
238         try {
239             if (binIncl == null && !isSelected)
240                 return;
241             if (binIncl == null) {
242                 binIncl = model.getFactory().createEntry(PROPERTY_BIN_INCLUDES);
243                 model.getBuild().add(binIncl);
244             }
245             if (libPath != null){
246                 if (!isSelected && libPath.segmentCount() == 1 && binIncl.contains("*.jar")) { //$NON-NLS-1$
247
addAllJarsToBinIncludes(binIncl, project, model);
248                 } else if (!isSelected && libPath.segmentCount() > 1){
249                     IPath parent = libPath.removeLastSegments(1);
250                     String JavaDoc parentPath = parent.toString() + IPath.SEPARATOR;
251                     if (binIncl.contains(parentPath) && !project.exists(parent)){
252                         binIncl.removeToken(parentPath);
253                     } else if (parent.segmentCount() > 1){
254                         parent = parent.removeLastSegments(1);
255                         parentPath = parent.toString() + IPath.SEPARATOR;
256                         if (binIncl.contains(parentPath) && !project.exists(parent))
257                             binIncl.removeToken(parentPath);
258                     }
259                 }
260             }
261             if (isSelected && !binIncl.contains(libName))
262                 binIncl.addToken(libName);
263             else if (!isSelected && binIncl.contains(libName))
264                 binIncl.removeToken(libName);
265         } catch (CoreException e) {
266             PDEPlugin.logException(e);
267         }
268         
269     }
270     
271     protected void addAllJarsToBinIncludes(IBuildEntry binIncl, IProject project, IBuildModel model) {
272         try {
273             IResource[] members = project.members();
274             for (int i = 0; i < members.length; i++)
275                 if (!(members[i] instanceof IFolder) && members[i].getFileExtension().equals("jar")) //$NON-NLS-1$
276
binIncl.addToken(members[i].getName());
277             
278             IBuildEntry[] libraries = BuildUtil.getBuildLibraries(model.getBuild().getBuildEntries());
279             if (libraries.length != 0) {
280                 for (int j = 0; j < libraries.length; j++) {
281                     String JavaDoc libraryName = libraries[j].getName().substring(7);
282                     IPath path = project.getFile(libraryName).getProjectRelativePath();
283                     if (path.segmentCount() == 1 && !binIncl.contains(libraryName))
284                         binIncl.addToken(libraryName);
285                 }
286             }
287             binIncl.removeToken("*.jar"); //$NON-NLS-1$
288
} catch (CoreException e) {
289             PDEPlugin.logException(e);
290         }
291     }
292     
293     private void setOutputEntryTokens(Set JavaDoc outputFolders, IBuildEntry outputEntry) {
294         Iterator JavaDoc iter = outputFolders.iterator();
295         try {
296             while (iter.hasNext()) {
297                 String JavaDoc outputFolder = iter.next().toString();
298                 if (!outputFolder.endsWith("" + IPath.SEPARATOR)) //$NON-NLS-1$
299
outputFolder = outputFolder.concat("" + IPath.SEPARATOR); //$NON-NLS-1$
300
if (!outputEntry.contains(outputFolder.toString()))
301                     outputEntry.addToken(outputFolder.toString());
302             }
303         } catch (CoreException e) {
304             PDEPlugin.logException(e);
305         }
306     }
307     
308     private IPackageFragmentRoot[] computeSourceFolders() {
309         ArrayList JavaDoc folders = new ArrayList JavaDoc();
310         IBuildModel buildModel = getBuildModel();
311         IProject project = buildModel.getUnderlyingResource().getProject();
312         try {
313             if (project.hasNature(JavaCore.NATURE_ID)) {
314                 IJavaProject jProject = JavaCore.create(project);
315                 IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
316                 for (int i = 0; i < roots.length; i++)
317                     if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE)
318                         folders.add(roots[i]);
319             }
320         } catch (JavaModelException e) {
321             PDEPlugin.logException(e);
322         } catch (CoreException e) {
323             PDEPlugin.logException(e);
324         }
325         return (IPackageFragmentRoot[]) folders.toArray(new IPackageFragmentRoot[folders.size()]);
326     }
327     
328     public void createClient(Section section, FormToolkit toolkit) {
329         Composite container = toolkit.createComposite(section);
330         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
331         
332         createLeftSection(container, toolkit);
333         createRightSection(container, toolkit);
334         
335         toolkit.paintBordersFor(container);
336         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
337         GridData data = new GridData(GridData.FILL_HORIZONTAL);
338         data.horizontalSpan = 2;
339         section.setLayoutData(data);
340         section.setClient(container);
341     }
342     
343     private void createLeftSection(Composite parent, FormToolkit toolkit) {
344         Composite container = createContainer(parent, toolkit);
345         
346         fLibraryPart = new PartAdapter(new String JavaDoc[] {
347                 PDEUIMessages.BuildEditor_RuntimeInfoSection_addLibrary, null,
348                 PDEUIMessages.ManifestEditor_LibrarySection_up,
349                 PDEUIMessages.ManifestEditor_LibrarySection_down});
350         fLibraryPart.createControl(container, SWT.FULL_SELECTION, 2, toolkit);
351         fLibraryViewer = (TableViewer) fLibraryPart.getViewer();
352         fLibraryViewer.setContentProvider(new LibraryContentProvider());
353         fLibraryViewer.setLabelProvider(new LibraryLabelProvider());
354         fLibraryPart.setButtonEnabled(F_UP_UNDEX, false);
355         fLibraryPart.setButtonEnabled(F_DOWN_INDEX, false);
356         fLibraryViewer.setInput(getBuildModel());
357         toolkit.paintBordersFor(container);
358         
359         MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
360
menuMgr.setRemoveAllWhenShown(true);
361         menuMgr.addMenuListener(new IMenuListener() {
362             public void menuAboutToShow(IMenuManager manager) {
363                 fillLibraryContextMenu(manager);
364             }
365         });
366         fLibraryViewer.getControl().setMenu(menuMgr.createContextMenu(fLibraryViewer.getControl()));
367     }
368     
369     private void createRightSection(Composite parent, FormToolkit toolkit) {
370         Composite container = createContainer(parent, toolkit);
371         
372         fFolderPart = new PartAdapter(new String JavaDoc[] {
373                 PDEUIMessages.BuildEditor_RuntimeInfoSection_addFolder}) {
374             public void selectionChanged(IStructuredSelection selection) {
375                 // folder selection ignored
376
}
377         };
378         fFolderPart.createControl(container, SWT.FULL_SELECTION, 2, toolkit);
379         fFolderViewer = (TableViewer) fFolderPart.getViewer();
380         fFolderViewer.setContentProvider(new FolderContentProvider());
381         fFolderViewer.setLabelProvider(new FolderLabelProvider());
382         toolkit.paintBordersFor(container);
383         
384         MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
385
menuMgr.setRemoveAllWhenShown(true);
386         menuMgr.addMenuListener(new IMenuListener() {
387             public void menuAboutToShow(IMenuManager manager) {
388                 fillFolderViewerContextMenu(manager);
389             }
390         });
391         fFolderViewer.getControl().setMenu(menuMgr.createContextMenu(fFolderViewer.getControl()));
392     }
393     
394     private Composite createContainer(Composite parent, FormToolkit toolkit) {
395         Composite container = toolkit.createComposite(parent);
396         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
397         GridData gd = new GridData(GridData.FILL_BOTH);
398         gd.widthHint = 100;
399         container.setLayoutData(gd);
400         return container;
401     }
402     
403     protected void fillFolderViewerContextMenu(IMenuManager manager) {
404         final ISelection selection = fFolderViewer.getSelection();
405         ISelection libSelection = fLibraryViewer.getSelection();
406         if (libSelection != null && !libSelection.isEmpty()) {
407             Action newAction = new Action(PDEUIMessages.BuildEditor_RuntimeInfoSection_popupFolder) {
408                 public void run() {
409                     handleNewFolder();
410                 }
411             };
412             newAction.setEnabled(fEnabled);
413             manager.add(newAction);
414         }
415         
416         manager.add(new Separator());
417         
418         Action replace = new Action(PDEUIMessages.RuntimeInfoSection_replace) {
419             public void run() {
420                 handleRenameFolder(((IStructuredSelection)selection).getFirstElement().toString());
421             }
422         };
423         replace.setEnabled(!selection.isEmpty() && fEnabled);
424         manager.add(replace);
425         
426         Action deleteAction = new Action(PDEUIMessages.Actions_delete_label) {
427             public void run() {
428                 handleDeleteFolder();
429             }
430         };
431         deleteAction.setEnabled(!selection.isEmpty() && fEnabled);
432         manager.add(deleteAction);
433         
434         // defect 19550
435
getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager, false);
436     }
437     
438     protected void fillLibraryContextMenu(IMenuManager manager) {
439         ISelection selection = fLibraryViewer.getSelection();
440         Action newAction = new Action(PDEUIMessages.BuildEditor_RuntimeInfoSection_popupAdd) {
441             public void run() {
442                 handleNew();
443             }
444         };
445         newAction.setEnabled(fEnabled);
446         manager.add(newAction);
447         
448         manager.add(new Separator());
449         IAction renameAction = new Action(PDEUIMessages.EditableTablePart_renameAction) {
450             public void run() {
451                 doRename();
452             }
453         };
454         renameAction.setEnabled(!selection.isEmpty() && fEnabled);
455         manager.add(renameAction);
456         
457         Action deleteAction = new Action(PDEUIMessages.Actions_delete_label) {
458             public void run() {
459                 handleDelete();
460             }
461         };
462         deleteAction.setEnabled(!selection.isEmpty() && fEnabled);
463         manager.add(deleteAction);
464         
465         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager, false);
466     }
467     
468     protected void entryModified(IBuildEntry oldEntry, String JavaDoc newValue) {
469         final IBuildEntry entry = oldEntry;
470         IBuildModel buildModel = getBuildModel();
471         IBuild build = buildModel.getBuild();
472         String JavaDoc oldName = entry.getName().substring(7);
473         
474         try {
475             if (newValue.equals(entry.getName()))
476                 return;
477             if (!newValue.startsWith(IBuildEntry.JAR_PREFIX))
478                 newValue = IBuildEntry.JAR_PREFIX + newValue;
479             if (!newValue.endsWith(".jar") && //$NON-NLS-1$
480
!newValue.endsWith("/") && //$NON-NLS-1$
481
!newValue.equals(IBuildEntry.JAR_PREFIX + ".")) //$NON-NLS-1$
482
newValue +="/"; //$NON-NLS-1$
483

484             String JavaDoc newName = newValue.substring(7);
485             
486             // jars.compile.order
487
IBuildEntry tempEntry = build.getEntry(PROPERTY_JAR_ORDER);
488             if (tempEntry != null && tempEntry.contains(oldName))
489                 tempEntry.renameToken(oldName, newName);
490             
491             // output.{source folder}.jar
492
tempEntry = build.getEntry(PROPERTY_OUTPUT_PREFIX + oldName);
493             if (tempEntry != null)
494                 tempEntry.setName(PROPERTY_OUTPUT_PREFIX + newName);
495                 
496             // bin.includes
497
tempEntry = build.getEntry(PROPERTY_BIN_INCLUDES);
498             if (tempEntry != null && tempEntry.contains(oldName))
499                 tempEntry.renameToken(oldName, newName);
500             
501             // bin.excludes
502
tempEntry = build.getEntry(PROPERTY_BIN_EXCLUDES);
503             if (tempEntry != null && tempEntry.contains(oldName))
504                 tempEntry.renameToken(oldName, newName);
505             
506             // rename
507
entry.setName(newValue);
508             
509         } catch (CoreException e) {
510             PDEPlugin.logException(e);
511         }
512     }
513     
514     public void enableSection(boolean enable) {
515         fEnabled = enable;
516         fLibraryPart.setButtonEnabled(F_NEW_INDEX, enable);
517         updateDirectionalButtons();
518         fFolderPart.setButtonEnabled(F_NEW_INDEX, enable && !fLibraryViewer.getSelection().isEmpty());
519     }
520     
521     public boolean doGlobalAction(String JavaDoc actionId) {
522         if (actionId.equals(ActionFactory.DELETE.getId())) {
523             if (fEnabled && fLibraryViewer.getControl().isFocusControl())
524                 handleDelete();
525             else if (fEnabled)
526                 handleDeleteFolder();
527             return true;
528         }
529         return false;
530     }
531     
532     private void doRename() {
533         IStructuredSelection selection = (IStructuredSelection) fLibraryViewer.getSelection();
534         if (selection.size() == 1) {
535             IBuildEntry entry = (IBuildEntry) selection.getFirstElement();
536             String JavaDoc oldName = entry.getName().substring(7);
537             RenameDialog dialog = new RenameDialog(fLibraryViewer.getControl().getShell(),
538                     true, getLibraryNames(), oldName);
539             dialog.setInputValidator(new IInputValidator() {
540                 public String JavaDoc isValid(String JavaDoc newText) {
541                     if (newText.indexOf(' ') != -1)
542                         return PDEUIMessages.AddLibraryDialog_nospaces;
543                     return null;
544                 }
545             });
546             dialog.create();
547             dialog.setTitle(PDEUIMessages.RuntimeInfoSection_rename);
548             dialog.getShell().setSize(300, 150);
549             if (dialog.open() == Window.OK)
550                 entryModified(entry, dialog.getNewName());
551         }
552     }
553     
554     public void dispose() {
555         IBuildModel buildModel = getBuildModel();
556         if (buildModel != null)
557             buildModel.removeModelChangedListener(this);
558         super.dispose();
559     }
560     
561     private void refreshOutputKeys() {
562         if (!isJavaProject())
563             return;
564         
565         IBuildEntry buildEntry = getLibrarySelection();
566         if (buildEntry == null)
567             return;
568         Set JavaDoc outputFolders = new HashSet JavaDoc();
569         String JavaDoc[] jarFolders = buildEntry.getTokens();
570         IPackageFragmentRoot[] sourceFolders = computeSourceFolders();
571         for (int j = 0; j < jarFolders.length; j++) {
572             IPackageFragmentRoot sourceFolder = getSourceFolder(jarFolders[j], sourceFolders);
573             if (sourceFolder != null) {
574                 try {
575                     IClasspathEntry entry = sourceFolder.getRawClasspathEntry();
576                     IPath outputPath = entry.getOutputLocation();
577                     if (outputPath == null) {
578                         outputFolders.add("bin"); //$NON-NLS-1$
579
} else {
580                         outputPath = outputPath.removeFirstSegments(1);
581                         outputFolders.add(outputPath.toString());
582                     }
583                 } catch (JavaModelException e) {
584                     PDEPlugin.logException(e);
585                 }
586             }
587         }
588         if (outputFolders.size() != 0) {
589             String JavaDoc libName = buildEntry.getName().substring(7);
590             IBuildModel buildModel = getBuildModel();
591             IBuild build = buildModel.getBuild();
592             String JavaDoc outputName = PROPERTY_OUTPUT_PREFIX + libName;
593             
594             IBuildEntry outputEntry = build.getEntry(outputName);
595             if (outputEntry == null) {
596                 outputEntry = buildModel.getFactory().createEntry(outputName);
597                 try {
598                     build.add(outputEntry);
599                 } catch (CoreException e) {
600                     PDEPlugin.logException(e);
601                 }
602             }
603             setOutputEntryTokens(outputFolders, outputEntry);
604         }
605     }
606     
607     private boolean isJavaProject() {
608         try {
609             IBuildModel buildModel = getBuildModel();
610             IProject project = buildModel.getUnderlyingResource().getProject();
611             return project.hasNature(JavaCore.NATURE_ID);
612         } catch (CoreException e) {
613         }
614         return false;
615     }
616     
617     private boolean isReadOnly() {
618         IBuildModel model = getBuildModel();
619         if (model instanceof IEditable)
620             return !((IEditable) model).isEditable();
621         return true;
622     }
623     
624     private void update(IBuildEntry variable) {
625         int index = 0;
626         if (fFolderViewer.getInput() == variable)
627             index = fFolderViewer.getTable().getSelectionIndex();
628         
629         fFolderViewer.setInput(variable);
630         int count = fFolderViewer.getTable().getItemCount();
631         if (index != -1 && count > 0) {
632             if (index == count)
633                 index = index - 1;
634             fFolderViewer.getTable().select(index);
635         }
636         fFolderPart.setButtonEnabled(F_NEW_INDEX, !isReadOnly() && fEnabled && variable != null);
637     }
638     
639     protected void updateDirectionalButtons() {
640         Table table = fLibraryViewer.getTable();
641         boolean hasSelection = table.getSelection().length > 0;
642         fLibraryPart.setButtonEnabled(F_UP_UNDEX, fEnabled && hasSelection && table.getSelectionIndex() > 0);
643         fLibraryPart.setButtonEnabled(F_DOWN_INDEX, fEnabled && hasSelection && table.getSelectionIndex() < table.getItemCount() - 1);
644     }
645     
646     protected String JavaDoc[] getLibraryNames() {
647         String JavaDoc[] libNames = new String JavaDoc[fLibraryViewer.getTable().getItemCount()];
648         for (int i = 0; i < libNames.length; i++)
649             libNames[i] = fLibraryViewer.getTable().getItem(i).getText();
650         return libNames;
651     }
652     
653     protected void handleNew() {
654         final String JavaDoc[] libNames = getLibraryNames();
655         IBaseModel pmodel = getPage().getModel();
656         final IPluginModelBase pluginModelBase = (pmodel instanceof IPluginModelBase) ? (IPluginModelBase) pmodel : null;
657         
658         BusyIndicator.showWhile(fLibraryViewer.getTable().getDisplay(), new Runnable JavaDoc() {
659             public void run() {
660                 IBuildModel buildModel = getBuildModel();
661                 IBuild build = buildModel.getBuild();
662                 AddLibraryDialog dialog = new AddLibraryDialog(getSection().getShell(), libNames, pluginModelBase);
663                 dialog.create();
664                 dialog.getShell().setText(PDEUIMessages.RuntimeInfoSection_addEntry);
665                 
666                 try {
667                     if (dialog.open() == Window.OK) {
668                         String JavaDoc name = dialog.getNewName();
669                         if (!name.endsWith(".jar") //$NON-NLS-1$
670
&& !name.equals(".") //$NON-NLS-1$
671
&& !name.endsWith("/")) //$NON-NLS-1$
672
name += "/"; //$NON-NLS-1$
673

674                         String JavaDoc keyName = name;
675                         if (!keyName.startsWith(IBuildEntry.JAR_PREFIX))
676                             keyName = IBuildEntry.JAR_PREFIX + name;
677                         if (name.startsWith(IBuildEntry.JAR_PREFIX))
678                             name = name.substring(7);
679                         
680                         if (!name.endsWith(".")) //$NON-NLS-1$
681
handleLibInBinBuild(true, name);
682                         
683                         // add library to jars compile order
684
IBuildEntry jarOrderEntry = build.getEntry(PROPERTY_JAR_ORDER);
685                         int numLib = fLibraryViewer.getTable().getItemCount();
686                         
687                         if (jarOrderEntry == null) {
688                             jarOrderEntry = getBuildModel().getFactory().createEntry(PROPERTY_JAR_ORDER);
689                             
690                             // add all runtime libraries to compile order
691
for (int i = 0; i < numLib; i++) {
692                                 String JavaDoc lib = ((IBuildEntry) fLibraryViewer.getElementAt(i)).getName().substring(7);
693                                 jarOrderEntry.addToken(lib);
694                             }
695                             jarOrderEntry.addToken(name);
696                             build.add(jarOrderEntry);
697                         } else if (jarOrderEntry.getTokens().length < numLib){
698                             
699                             // remove and re-add all runtime libraries to compile order
700
String JavaDoc[] tokens = jarOrderEntry.getTokens();
701                             for (int i = 0; i<tokens.length; i++)
702                                 jarOrderEntry.removeToken(tokens[i]);
703                             
704                             for (int i = 0; i < numLib; i++) {
705                                 String JavaDoc lib = ((IBuildEntry) fLibraryViewer.getElementAt(i)).getName().substring(7);
706                                 jarOrderEntry.addToken(lib);
707                             }
708                             jarOrderEntry.addToken(name);
709                         } else {
710                             jarOrderEntry.addToken(name);
711                         }
712                         // end of jars compile order addition
713

714                         IBuildEntry library = buildModel.getFactory().createEntry(keyName);
715                         build.add(library);
716                         
717                     }
718                 } catch (CoreException e) {
719                     PDEPlugin.logException(e);
720                 }
721             }
722         });
723     }
724     
725     private IPackageFragmentRoot getSourceFolder(String JavaDoc folderName, IPackageFragmentRoot[] sourceFolders) {
726         for (int i = 0; i < sourceFolders.length; i++)
727             if (sourceFolders[i].getPath().removeFirstSegments(1).equals(new Path(folderName)))
728                 return sourceFolders[i];
729         return null;
730     }
731     
732     protected void handleDelete() {
733         int index = fLibraryViewer.getTable().getSelectionIndex();
734         if (index != -1) {
735             String JavaDoc libName = fLibraryViewer.getTable().getItem(index).getText();
736             IBuild build = getBuildModel().getBuild();
737             
738             try {
739                 // jars.compile.order
740
IBuildEntry entry = build.getEntry(PROPERTY_JAR_ORDER);
741                 int numLib = fLibraryViewer.getTable().getItemCount();
742                 
743                 if (entry == null) {
744                     entry = getBuildModel().getFactory().createEntry(PROPERTY_JAR_ORDER);
745                     
746                     // add all runtime libraries to compile order
747
for (int i = 0; i < numLib; i++) {
748                         String JavaDoc lib = ((IBuildEntry) fLibraryViewer.getElementAt(i)).getName().substring(7);
749                         entry.addToken(lib);
750                     }
751                     build.add(entry);
752                 } else if (entry.getTokens().length < numLib){
753                     
754                     // remove and re-add all runtime libraries to compile order
755
String JavaDoc[] tokens = entry.getTokens();
756                     for (int i = 0; i<tokens.length; i++)
757                         entry.removeToken(tokens[i]);
758                     
759                     for (int i = 0; i < numLib; i++) {
760                         Object JavaDoc element = fLibraryViewer.getElementAt(i);
761                         if (element == null) {
762                             continue;
763                         }
764                         String JavaDoc lib = ((IBuildEntry)element).getName().substring(7);
765                         entry.addToken(lib);
766                     }
767                 }
768                 
769                 entry.removeToken(libName);
770                 
771                 // output.{source folder}.jar
772
entry = build.getEntry(PROPERTY_OUTPUT_PREFIX + libName);
773                 if (entry != null)
774                     build.remove(entry);
775                 
776                 // bin.includes
777
entry = build.getEntry(PROPERTY_BIN_INCLUDES);
778                 if (entry != null && entry.contains(libName))
779                     entry.removeToken(libName);
780                 
781                 // bin.excludes
782
entry = build.getEntry(PROPERTY_BIN_EXCLUDES);
783                 if (entry != null && entry.contains(libName))
784                     entry.removeToken(libName);
785                 
786                 String JavaDoc entryName = IBuildEntry.JAR_PREFIX + libName;
787                 entry = build.getEntry(entryName);
788                 if (entry != null) {
789                     build.remove(entry);
790                 }
791             } catch (CoreException e) {
792                 PDEPlugin.logException(e);
793             }
794         }
795     }
796     
797     private void handleDeleteFolder() {
798         Object JavaDoc object = ((IStructuredSelection) fFolderViewer.getSelection()).getFirstElement();
799         if (object == null)
800             return;
801         IBuildEntry entry = getLibrarySelection();
802         if (entry == null)
803             return;
804         try {
805             entry.removeToken((String JavaDoc) object);
806         } catch (CoreException e) {
807             PDEPlugin.logException(e);
808         }
809     }
810     
811     private IFolder openSelectFolderDialog(final IBuildEntry entry, String JavaDoc title, String JavaDoc message) {
812         IFile file = (IFile) getBuildModel().getUnderlyingResource();
813         final IProject project = file.getProject();
814         
815         FolderSelectionDialog dialog = new FolderSelectionDialog(
816                 PDEPlugin.getActiveWorkbenchShell(),
817                 new WorkbenchLabelProvider(),
818                 new JarsNewContentProvider() {
819         });
820         
821         dialog.setInput(project.getWorkspace());
822         dialog.addFilter(new ViewerFilter() {
823             public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
824                 if (element instanceof IProject)
825                     return ((IProject) element).equals(project);
826                 return element instanceof IFolder;
827             }
828         });
829         dialog.setAllowMultiple(false);
830         dialog.setTitle(title);
831         dialog.setMessage(message);
832         
833         dialog.setValidator(new ISelectionStatusValidator() {
834             public IStatus validate(Object JavaDoc[] selection) {
835                 String JavaDoc id = PDEPlugin.getPluginId();
836                 if (selection == null || selection.length != 1 || !(selection[0] instanceof IFolder))
837                     return new Status(IStatus.ERROR, id, IStatus.ERROR, "", null); //$NON-NLS-1$
838

839                 String JavaDoc folderPath = ((IFolder) selection[0]).getProjectRelativePath().addTrailingSeparator().toString();
840                 if (entry != null && entry.contains(folderPath))
841                     return new Status(IStatus.ERROR, id, IStatus.ERROR, PDEUIMessages.BuildEditor_RuntimeInfoSection_duplicateFolder, null);
842                 
843                 return new Status(IStatus.OK, id, IStatus.OK, "", null); //$NON-NLS-1$
844
}
845         });
846         
847         if (dialog.open() == Window.OK)
848             return (IFolder)dialog.getFirstResult();
849         return null;
850     }
851     
852     private void handleNewFolder() {
853         IBuildEntry entry = getLibrarySelection();
854         IFolder folder = openSelectFolderDialog(entry,
855                 PDEUIMessages.ManifestEditor_JarsSection_dialogTitle,
856                 PDEUIMessages.ManifestEditor_JarsSection_dialogMessage);
857         if (folder != null) {
858             try {
859                 String JavaDoc folderPath = folder.getProjectRelativePath().addTrailingSeparator().toString();
860                 entry.addToken(folderPath);
861                 refreshOutputKeys();
862             } catch (CoreException e) {
863                 PDEPlugin.logException(e);
864             }
865         }
866     }
867     
868     private void handleRenameFolder(String JavaDoc oldName) {
869         IBuildEntry entry = getLibrarySelection();
870         IFolder folder = openSelectFolderDialog(entry,
871                 PDEUIMessages.RuntimeInfoSection_replacedialog,
872                 PDEUIMessages.ManifestEditor_JarsSection_dialogMessage);
873         if (folder != null) {
874             try {
875                 String JavaDoc newFolder = folder.getProjectRelativePath().addTrailingSeparator().toString();
876                 entry.renameToken(oldName, newFolder);
877                 refreshOutputKeys();
878             } catch (CoreException e) {
879                 PDEPlugin.logException(e);
880             }
881         }
882     }
883     
884     protected void updateJarsCompileOrder(boolean up) {
885         int direction = up ? -1 : 1;
886         int index = fLibraryViewer.getTable().getSelectionIndex();
887         String JavaDoc library1 = ((IBuildEntry) fLibraryViewer.getElementAt(index)).getName().substring(7);
888         String JavaDoc library2 = ((IBuildEntry) fLibraryViewer.getElementAt(index + direction)).getName().substring(7);
889         
890         IBuildModel model = getBuildModel();
891         IBuild build = model.getBuild();
892         IBuildEntry jarOrderEntry = build.getEntry(PROPERTY_JAR_ORDER);
893         try {
894             if (jarOrderEntry == null) {
895                 jarOrderEntry = model.getFactory().createEntry(PROPERTY_JAR_ORDER);
896                 build.add(jarOrderEntry);
897             } else {
898                 String JavaDoc tokens[] = jarOrderEntry.getTokens();
899                 for (int i =0; i<tokens.length; i++)
900                     jarOrderEntry.removeToken(tokens[i]);
901             }
902             
903             int numLib = fLibraryViewer.getTable().getItemCount();
904             String JavaDoc[] names = new String JavaDoc[numLib];
905             for (int i = 0; i < numLib; i++) {
906                 String JavaDoc name = ((IBuildEntry) fLibraryViewer.getElementAt(i)).getName().substring(7);
907                 if (name.equals(library1))
908                     name = library2;
909                 else if (name.equals(library2))
910                     name = library1;
911                 names[i] = name;
912             }
913             
914             for (int i = 0; i < numLib; i++)
915                 jarOrderEntry.addToken(names[i]);
916             
917         } catch (CoreException e) {
918             PDEPlugin.logException(e);
919         }
920     }
921     
922     public void modelChanged(IModelChangedEvent event) {
923         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED)
924             markStale();
925         Object JavaDoc changeObject = event.getChangedObjects()[0];
926         String JavaDoc keyName = event.getChangedProperty();
927         
928         // check if model change applies to this section
929
if (!(changeObject instanceof IBuildEntry))
930             return;
931         IBuildEntry entry = (IBuildEntry)changeObject;
932         String JavaDoc entryName = entry.getName();
933         if (!entryName.startsWith(IBuildEntry.JAR_PREFIX)
934                 && !entryName.equals(PROPERTY_JAR_ORDER)
935                 && !entryName.equals(PROPERTY_BIN_INCLUDES))
936             return;
937         
938         if (entryName.equals(PROPERTY_BIN_INCLUDES))
939             return;
940         
941         int type = event.getChangeType();
942         
943         // account for new key
944
if (entry.getName().startsWith(PROPERTY_SOURCE_PREFIX)) {
945             IStructuredSelection newSel = null;
946             if (type == IModelChangedEvent.INSERT) {
947                 fLibraryViewer.add(entry);
948                 newSel = new StructuredSelection(entry);
949             } else if (type == IModelChangedEvent.REMOVE) {
950                 int index = fLibraryViewer.getTable().getSelectionIndex();
951                 fLibraryViewer.remove(entry);
952                 Table table = fLibraryViewer.getTable();
953                 int itemCount = table.getItemCount();
954                 if (itemCount != 0) {
955                     index = index < itemCount ? index : itemCount - 1;
956                     newSel = new StructuredSelection(table.getItem(index).getData());
957                 }
958             } else if (keyName != null && keyName.startsWith(IBuildEntry.JAR_PREFIX)) {
959                 // modification to source.{libname}.jar
960
if (event.getOldValue() != null && event.getNewValue() != null)
961                     // renaming token
962
fLibraryViewer.update(entry, null);
963                 
964                 newSel = new StructuredSelection(entry);
965             }
966             fLibraryViewer.setSelection(newSel);
967         } else if (keyName!= null && keyName.equals(PROPERTY_JAR_ORDER)){
968             // account for change in jars compile order
969
if (event.getNewValue() == null && event.getOldValue() != null)
970                 // removing token from jars compile order : do nothing
971
return;
972             if (event.getOldValue() != null && event.getNewValue() != null)
973                 // renaming token from jars compile order : do nothing
974
return;
975             
976             fLibraryViewer.refresh();
977             updateDirectionalButtons();
978         }
979     }
980     
981     private IBuildEntry getLibrarySelection() {
982         IStructuredSelection selection = (IStructuredSelection)fLibraryViewer.getSelection();
983         return (IBuildEntry)selection.getFirstElement();
984     }
985 }
986
Popular Tags