KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > LibrariesWorkbookPage


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.jdt.internal.ui.wizards.buildpaths;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Map.Entry;
21
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
25
26 import org.eclipse.core.resources.IContainer;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IFolder;
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.IWorkspaceRoot;
32 import org.eclipse.core.resources.IWorkspaceRunnable;
33
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.events.KeyEvent;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.swt.widgets.Shell;
40
41 import org.eclipse.jface.dialogs.MessageDialog;
42 import org.eclipse.jface.viewers.StructuredSelection;
43 import org.eclipse.jface.window.Window;
44 import org.eclipse.jface.wizard.WizardDialog;
45
46 import org.eclipse.ui.IImportWizard;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
49
50 import org.eclipse.jdt.core.IAccessRule;
51 import org.eclipse.jdt.core.IClasspathEntry;
52 import org.eclipse.jdt.core.IJavaProject;
53 import org.eclipse.jdt.core.IPackageFragmentRoot;
54 import org.eclipse.jdt.core.JavaCore;
55 import org.eclipse.jdt.core.JavaModelException;
56
57 import org.eclipse.jdt.internal.corext.util.Messages;
58
59 import org.eclipse.jdt.launching.JavaRuntime;
60
61 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
62
63 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
64 import org.eclipse.jdt.internal.ui.JavaPlugin;
65 import org.eclipse.jdt.internal.ui.actions.JarImportWizardAction;
66 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
67 import org.eclipse.jdt.internal.ui.jarimport.JarImportWizard;
68 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
69 import org.eclipse.jdt.internal.ui.util.PixelConverter;
70 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
71 import org.eclipse.jdt.internal.ui.wizards.dialogfields.CheckedListDialogField;
72 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
73 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
74 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ITreeListAdapter;
75 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
76 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
77 import org.eclipse.jdt.internal.ui.wizards.dialogfields.TreeListDialogField;
78
79 public class LibrariesWorkbookPage extends BuildPathBasePage {
80     
81     private ListDialogField fClassPathList;
82     private IJavaProject fCurrJProject;
83     
84     private TreeListDialogField fLibrariesList;
85     
86     private Control fSWTControl;
87     private final IWorkbenchPreferenceContainer fPageContainer;
88
89     private final int IDX_ADDJAR= 0;
90     private final int IDX_ADDEXT= 1;
91     private final int IDX_ADDVAR= 2;
92     private final int IDX_ADDLIB= 3;
93     private final int IDX_ADDFOL= 4;
94     
95     private final int IDX_EDIT= 6;
96     private final int IDX_REMOVE= 7;
97
98     private final int IDX_REPLACE= 9;
99         
100     public LibrariesWorkbookPage(CheckedListDialogField classPathList, IWorkbenchPreferenceContainer pageContainer) {
101         fClassPathList= classPathList;
102         fPageContainer= pageContainer;
103         fSWTControl= null;
104         
105         String JavaDoc[] buttonLabels= new String JavaDoc[] {
106             NewWizardMessages.LibrariesWorkbookPage_libraries_addjar_button,
107             NewWizardMessages.LibrariesWorkbookPage_libraries_addextjar_button,
108             NewWizardMessages.LibrariesWorkbookPage_libraries_addvariable_button,
109             NewWizardMessages.LibrariesWorkbookPage_libraries_addlibrary_button,
110             NewWizardMessages.LibrariesWorkbookPage_libraries_addclassfolder_button,
111             /* */ null,
112             NewWizardMessages.LibrariesWorkbookPage_libraries_edit_button,
113             NewWizardMessages.LibrariesWorkbookPage_libraries_remove_button,
114             /* */ null,
115             NewWizardMessages.LibrariesWorkbookPage_libraries_replace_button
116         };
117                 
118         LibrariesAdapter adapter= new LibrariesAdapter();
119                 
120         fLibrariesList= new TreeListDialogField(adapter, buttonLabels, new CPListLabelProvider());
121         fLibrariesList.setDialogFieldListener(adapter);
122         fLibrariesList.setLabelText(NewWizardMessages.LibrariesWorkbookPage_libraries_label);
123
124         fLibrariesList.enableButton(IDX_REMOVE, false);
125         fLibrariesList.enableButton(IDX_EDIT, false);
126         fLibrariesList.enableButton(IDX_REPLACE, false);
127
128         fLibrariesList.setViewerComparator(new CPListElementSorter());
129
130     }
131         
132     public void init(IJavaProject jproject) {
133         fCurrJProject= jproject;
134         if (Display.getCurrent() != null) {
135             updateLibrariesList();
136         } else {
137             Display.getDefault().asyncExec(new Runnable JavaDoc() {
138                 public void run() {
139                     updateLibrariesList();
140                 }
141             });
142         }
143     }
144     
145     private void updateLibrariesList() {
146         List JavaDoc cpelements= fClassPathList.getElements();
147         List JavaDoc libelements= new ArrayList JavaDoc(cpelements.size());
148         
149         int nElements= cpelements.size();
150         for (int i= 0; i < nElements; i++) {
151             CPListElement cpe= (CPListElement)cpelements.get(i);
152             if (isEntryKind(cpe.getEntryKind())) {
153                 libelements.add(cpe);
154             }
155         }
156         fLibrariesList.setElements(libelements);
157     }
158         
159     // -------- UI creation
160

161     public Control getControl(Composite parent) {
162         PixelConverter converter= new PixelConverter(parent);
163         
164         Composite composite= new Composite(parent, SWT.NONE);
165             
166         LayoutUtil.doDefaultLayout(composite, new DialogField[] { fLibrariesList }, true, SWT.DEFAULT, SWT.DEFAULT);
167         LayoutUtil.setHorizontalGrabbing(fLibrariesList.getTreeControl(null));
168         
169         int buttonBarWidth= converter.convertWidthInCharsToPixels(24);
170         fLibrariesList.setButtonsMinWidth(buttonBarWidth);
171         
172         fLibrariesList.setViewerComparator(new CPListElementSorter());
173         
174         fSWTControl= composite;
175                 
176         return composite;
177     }
178     
179     private Shell getShell() {
180         if (fSWTControl != null) {
181             return fSWTControl.getShell();
182         }
183         return JavaPlugin.getActiveWorkbenchShell();
184     }
185     
186     
187     private class LibrariesAdapter implements IDialogFieldListener, ITreeListAdapter {
188         
189         private final Object JavaDoc[] EMPTY_ARR= new Object JavaDoc[0];
190         
191         // -------- IListAdapter --------
192
public void customButtonPressed(TreeListDialogField field, int index) {
193             libaryPageCustomButtonPressed(field, index);
194         }
195         
196         public void selectionChanged(TreeListDialogField field) {
197             libaryPageSelectionChanged(field);
198         }
199         
200         public void doubleClicked(TreeListDialogField field) {
201             libaryPageDoubleClicked(field);
202         }
203         
204         public void keyPressed(TreeListDialogField field, KeyEvent event) {
205             libaryPageKeyPressed(field, event);
206         }
207
208         public Object JavaDoc[] getChildren(TreeListDialogField field, Object JavaDoc element) {
209             if (element instanceof CPListElement) {
210                 return ((CPListElement) element).getChildren(false);
211             } else if (element instanceof CPListElementAttribute) {
212                 CPListElementAttribute attribute= (CPListElementAttribute) element;
213                 if (CPListElement.ACCESSRULES.equals(attribute.getKey())) {
214                     return (IAccessRule[]) attribute.getValue();
215                 }
216             }
217             return EMPTY_ARR;
218         }
219
220         public Object JavaDoc getParent(TreeListDialogField field, Object JavaDoc element) {
221             if (element instanceof CPListElementAttribute) {
222                 return ((CPListElementAttribute) element).getParent();
223             }
224             return null;
225         }
226
227         public boolean hasChildren(TreeListDialogField field, Object JavaDoc element) {
228             return getChildren(field, element).length > 0;
229         }
230             
231         // ---------- IDialogFieldListener --------
232

233         public void dialogFieldChanged(DialogField field) {
234             libaryPageDialogFieldChanged(field);
235         }
236     }
237     
238     private void libaryPageCustomButtonPressed(DialogField field, int index) {
239         CPListElement[] libentries= null;
240         switch (index) {
241         case IDX_ADDJAR: /* add jar */
242             libentries= openJarFileDialog(null);
243             break;
244         case IDX_ADDEXT: /* add external jar */
245             libentries= openExtJarFileDialog(null);
246             break;
247         case IDX_ADDVAR: /* add variable */
248             libentries= openVariableSelectionDialog(null);
249             break;
250         case IDX_ADDLIB: /* add library */
251             libentries= openContainerSelectionDialog(null);
252             break;
253         case IDX_ADDFOL: /* add folder */
254             libentries= openClassFolderDialog(null);
255             break;
256         case IDX_EDIT: /* edit */
257             editEntry();
258             return;
259         case IDX_REMOVE: /* remove */
260             removeEntry();
261             return;
262         case IDX_REPLACE: /* replace */
263             replaceJarFile();
264             return;
265         }
266         if (libentries != null) {
267             int nElementsChosen= libentries.length;
268             // remove duplicates
269
List JavaDoc cplist= fLibrariesList.getElements();
270             List JavaDoc elementsToAdd= new ArrayList JavaDoc(nElementsChosen);
271             
272             for (int i= 0; i < nElementsChosen; i++) {
273                 CPListElement curr= libentries[i];
274                 if (!cplist.contains(curr) && !elementsToAdd.contains(curr)) {
275                     elementsToAdd.add(curr);
276                     curr.setAttribute(CPListElement.SOURCEATTACHMENT, BuildPathSupport.guessSourceAttachment(curr));
277                     curr.setAttribute(CPListElement.JAVADOC, BuildPathSupport.guessJavadocLocation(curr));
278                 }
279             }
280             if (!elementsToAdd.isEmpty() && (index == IDX_ADDFOL)) {
281                 askForAddingExclusionPatternsDialog(elementsToAdd);
282             }
283             
284             fLibrariesList.addElements(elementsToAdd);
285             if (index == IDX_ADDLIB || index == IDX_ADDVAR) {
286                 fLibrariesList.refresh();
287             }
288             fLibrariesList.postSetSelection(new StructuredSelection(libentries));
289         }
290     }
291     
292     /* (non-Javadoc)
293      * @see org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage#addElement(org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement)
294      */

295     public void addElement(CPListElement element) {
296         fLibrariesList.addElement(element);
297         fLibrariesList.postSetSelection(new StructuredSelection(element));
298     }
299     
300     private void askForAddingExclusionPatternsDialog(List JavaDoc newEntries) {
301         HashSet JavaDoc modified= new HashSet JavaDoc();
302         List JavaDoc existing= fClassPathList.getElements();
303         fixNestingConflicts((CPListElement[])newEntries.toArray(new CPListElement[newEntries.size()]), (CPListElement[])existing.toArray(new CPListElement[existing.size()]), modified);
304         if (!modified.isEmpty()) {
305             String JavaDoc title= NewWizardMessages.LibrariesWorkbookPage_exclusion_added_title;
306             String JavaDoc message= NewWizardMessages.LibrariesWorkbookPage_exclusion_added_message;
307             MessageDialog.openInformation(getShell(), title, message);
308         }
309     }
310     
311     protected void libaryPageDoubleClicked(TreeListDialogField field) {
312         List JavaDoc selection= fLibrariesList.getSelectedElements();
313         if (canEdit(selection)) {
314             editEntry();
315         }
316     }
317
318     protected void libaryPageKeyPressed(TreeListDialogField field, KeyEvent event) {
319         if (field == fLibrariesList) {
320             if (event.character == SWT.DEL && event.stateMask == 0) {
321                 List JavaDoc selection= field.getSelectedElements();
322                 if (canRemove(selection)) {
323                     removeEntry();
324                 }
325             }
326         }
327     }
328
329     private void replaceJarFile() {
330         final IPackageFragmentRoot root= getSelectedPackageFragmentRoot();
331         if (root != null) {
332             final IImportWizard wizard= new JarImportWizard(false);
333             wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(root));
334             final WizardDialog dialog= new WizardDialog(getShell(), wizard);
335             dialog.create();
336             dialog.getShell().setSize(Math.max(JarImportWizardAction.SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x), JarImportWizardAction.SIZING_WIZARD_HEIGHT);
337             PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);
338             dialog.open();
339         }
340     }
341
342     private IPackageFragmentRoot getSelectedPackageFragmentRoot() {
343         final List JavaDoc elements= fLibrariesList.getSelectedElements();
344         if (elements.size() == 1) {
345             final Object JavaDoc object= elements.get(0);
346             if (object instanceof CPListElement) {
347                 final CPListElement element= (CPListElement) object;
348                 final IClasspathEntry entry= element.getClasspathEntry();
349                 if (JarImportWizard.isValidClassPathEntry(entry)) {
350                     final IJavaProject project= element.getJavaProject();
351                     if (project != null) {
352                         try {
353                             final IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
354                             for (int index= 0; index < roots.length; index++) {
355                                 if (entry.equals(roots[index].getRawClasspathEntry()))
356                                     return roots[index];
357                             }
358                         } catch (JavaModelException exception) {
359                             JavaPlugin.log(exception);
360                         }
361                     }
362                 }
363             }
364         }
365         return null;
366     }
367
368     private void removeEntry() {
369         List JavaDoc selElements= fLibrariesList.getSelectedElements();
370         HashMap JavaDoc containerEntriesToUpdate= new HashMap JavaDoc();
371         for (int i= selElements.size() - 1; i >= 0 ; i--) {
372             Object JavaDoc elem= selElements.get(i);
373             if (elem instanceof CPListElementAttribute) {
374                 CPListElementAttribute attrib= (CPListElementAttribute) elem;
375                 String JavaDoc key= attrib.getKey();
376                 if (attrib.isBuiltIn()) {
377                     Object JavaDoc value= null;
378                     if (key.equals(CPListElement.ACCESSRULES)) {
379                         value= new IAccessRule[0];
380                     }
381                     attrib.setValue(value);
382                 } else {
383                     removeCustomAttribute(attrib);
384                 }
385                 selElements.remove(i);
386                 if (attrib.getParent().getParentContainer() instanceof CPListElement) { // inside a container: apply changes right away
387
CPListElement containerEntry= attrib.getParent();
388                     HashSet JavaDoc changedAttributes= (HashSet JavaDoc) containerEntriesToUpdate.get(containerEntry);
389                     if (changedAttributes == null) {
390                         changedAttributes= new HashSet JavaDoc();
391                         containerEntriesToUpdate.put(containerEntry, changedAttributes);
392                     }
393                     changedAttributes.add(key); // collect the changed attributes
394
}
395             }
396         }
397         if (selElements.isEmpty()) {
398             fLibrariesList.refresh();
399             fClassPathList.dialogFieldChanged(); // validate
400
} else {
401             fLibrariesList.removeElements(selElements);
402         }
403         for (Iterator JavaDoc iter= containerEntriesToUpdate.entrySet().iterator(); iter.hasNext();) {
404             Map.Entry JavaDoc entry= (Entry) iter.next();
405             CPListElement curr= (CPListElement) entry.getKey();
406             HashSet JavaDoc attribs= (HashSet JavaDoc) entry.getValue();
407             String JavaDoc[] changedAttributes= (String JavaDoc[]) attribs.toArray(new String JavaDoc[attribs.size()]);
408             IClasspathEntry changedEntry= curr.getClasspathEntry();
409             updateContainerEntry(changedEntry, changedAttributes, fCurrJProject, ((CPListElement) curr.getParentContainer()).getPath());
410         }
411     }
412     
413     private boolean canRemove(List JavaDoc selElements) {
414         if (selElements.size() == 0) {
415             return false;
416         }
417         for (int i= 0; i < selElements.size(); i++) {
418             Object JavaDoc elem= selElements.get(i);
419             if (elem instanceof CPListElementAttribute) {
420                 CPListElementAttribute attrib= (CPListElementAttribute) elem;
421                 if (attrib.isNonModifiable()) {
422                     return false;
423                 }
424                 if (attrib.isBuiltIn()) {
425                     if (attrib.getParent().isInContainer(JavaRuntime.JRE_CONTAINER) && CPListElement.ACCESSRULES.equals(attrib.getKey())) {
426                         return false; // workaround for 166519 until we have full story
427
}
428                     if (attrib.getKey().equals(CPListElement.ACCESSRULES)) {
429                         return ((IAccessRule[]) attrib.getValue()).length > 0;
430                     }
431                     if (attrib.getValue() == null) {
432                         return false;
433                     }
434                 } else {
435                     if (!canRemoveCustomAttribute(attrib)) {
436                         return false;
437                     }
438                 }
439             } else if (elem instanceof CPListElement) {
440                 CPListElement curr= (CPListElement) elem;
441                 if (curr.getParentContainer() != null) {
442                     return false;
443                 }
444             } else { // unknown element
445
return false;
446             }
447         }
448         return true;
449     }
450
451     /**
452      * Method editEntry.
453      */

454     private void editEntry() {
455         List JavaDoc selElements= fLibrariesList.getSelectedElements();
456         if (selElements.size() != 1) {
457             return;
458         }
459         Object JavaDoc elem= selElements.get(0);
460         if (fLibrariesList.getIndexOfElement(elem) != -1) {
461             editElementEntry((CPListElement) elem);
462         } else if (elem instanceof CPListElementAttribute) {
463             editAttributeEntry((CPListElementAttribute) elem);
464         }
465     }
466     
467     private void editAttributeEntry(CPListElementAttribute elem) {
468         String JavaDoc key= elem.getKey();
469         CPListElement selElement= elem.getParent();
470         
471         if (key.equals(CPListElement.SOURCEATTACHMENT)) {
472             IClasspathEntry result= BuildPathDialogAccess.configureSourceAttachment(getShell(), selElement.getClasspathEntry());
473             if (result != null) {
474                 selElement.setAttribute(CPListElement.SOURCEATTACHMENT, result.getSourceAttachmentPath());
475                 String JavaDoc[] changedAttributes= { CPListElement.SOURCEATTACHMENT };
476                 attributeUpdated(selElement, changedAttributes);
477                 fLibrariesList.refresh(elem);
478                 fLibrariesList.update(selElement); // image
479
fClassPathList.refresh(); // images
480
updateEnabledState();
481             }
482         } else if (key.equals(CPListElement.ACCESSRULES)) {
483             AccessRulesDialog dialog= new AccessRulesDialog(getShell(), selElement, fCurrJProject, fPageContainer != null);
484             int res= dialog.open();
485             if (res == Window.OK || res == AccessRulesDialog.SWITCH_PAGE) {
486                 selElement.setAttribute(CPListElement.ACCESSRULES, dialog.getAccessRules());
487                 String JavaDoc[] changedAttributes= { CPListElement.ACCESSRULES };
488                 attributeUpdated(selElement, changedAttributes);
489                 
490                 fLibrariesList.refresh(elem);
491                 fClassPathList.dialogFieldChanged(); // validate
492
updateEnabledState();
493                 
494                 if (res == AccessRulesDialog.SWITCH_PAGE) { // switch after updates and validation
495
dialog.performPageSwitch(fPageContainer);
496                 }
497             }
498         } else {
499             if (editCustomAttribute(getShell(), elem)) {
500                 String JavaDoc[] changedAttributes= { key };
501                 attributeUpdated(selElement, changedAttributes);
502                 fLibrariesList.refresh(elem);
503                 fClassPathList.dialogFieldChanged(); // validate
504
updateEnabledState();
505             }
506         }
507     }
508     
509     private void attributeUpdated(CPListElement selElement, String JavaDoc[] changedAttributes) {
510         Object JavaDoc parentContainer= selElement.getParentContainer();
511         if (parentContainer instanceof CPListElement) { // inside a container: apply changes right away
512
IClasspathEntry updatedEntry= selElement.getClasspathEntry();
513             updateContainerEntry(updatedEntry, changedAttributes, fCurrJProject, ((CPListElement) parentContainer).getPath());
514         }
515     }
516
517     private void updateContainerEntry(final IClasspathEntry newEntry, final String JavaDoc[] changedAttributes, final IJavaProject jproject, final IPath containerPath) {
518         try {
519             IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
520                 public void run(IProgressMonitor monitor) throws CoreException {
521                     BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath, monitor);
522                 }
523             };
524             PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(runnable));
525
526         } catch (InvocationTargetException JavaDoc e) {
527             String JavaDoc title= NewWizardMessages.LibrariesWorkbookPage_configurecontainer_error_title;
528             String JavaDoc message= NewWizardMessages.LibrariesWorkbookPage_configurecontainer_error_message;
529             ExceptionHandler.handle(e, getShell(), title, message);
530         } catch (InterruptedException JavaDoc e) {
531             //
532
}
533     }
534         
535     private void editElementEntry(CPListElement elem) {
536         CPListElement[] res= null;
537         
538         switch (elem.getEntryKind()) {
539         case IClasspathEntry.CPE_CONTAINER:
540             res= openContainerSelectionDialog(elem);
541             break;
542         case IClasspathEntry.CPE_LIBRARY:
543             IResource resource= elem.getResource();
544             if (resource == null) {
545                 res= openExtJarFileDialog(elem);
546             } else if (resource.getType() == IResource.FOLDER) {
547                 if (resource.exists()) {
548                     res= openClassFolderDialog(elem);
549                 } else {
550                     res= openNewClassFolderDialog(elem);
551                 }
552             } else if (resource.getType() == IResource.FILE) {
553                 res= openJarFileDialog(elem);
554             }
555             break;
556         case IClasspathEntry.CPE_VARIABLE:
557             res= openVariableSelectionDialog(elem);
558             break;
559         }
560         if (res != null && res.length > 0) {
561             CPListElement curr= res[0];
562             curr.setExported(elem.isExported());
563             curr.setAttributesFromExisting(elem);
564             fLibrariesList.replaceElement(elem, curr);
565             if (elem.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
566                 fLibrariesList.refresh();
567             }
568         }
569             
570     }
571
572     private void libaryPageSelectionChanged(DialogField field) {
573         updateEnabledState();
574     }
575
576     private void updateEnabledState() {
577         List JavaDoc selElements= fLibrariesList.getSelectedElements();
578         fLibrariesList.enableButton(IDX_EDIT, canEdit(selElements));
579         fLibrariesList.enableButton(IDX_REMOVE, canRemove(selElements));
580         fLibrariesList.enableButton(IDX_REPLACE, getSelectedPackageFragmentRoot() != null);
581         
582         boolean noAttributes= containsOnlyTopLevelEntries(selElements);
583         fLibrariesList.enableButton(IDX_ADDEXT, noAttributes);
584         fLibrariesList.enableButton(IDX_ADDFOL, noAttributes);
585         fLibrariesList.enableButton(IDX_ADDJAR, noAttributes);
586         fLibrariesList.enableButton(IDX_ADDLIB, noAttributes);
587         fLibrariesList.enableButton(IDX_ADDVAR, noAttributes);
588     }
589     
590     private boolean canEdit(List JavaDoc selElements) {
591         if (selElements.size() != 1) {
592             return false;
593         }
594         Object JavaDoc elem= selElements.get(0);
595         if (elem instanceof CPListElement) {
596             CPListElement curr= (CPListElement) elem;
597             return !(curr.getResource() instanceof IFolder) && curr.getParentContainer() == null;
598         }
599         if (elem instanceof CPListElementAttribute) {
600             CPListElementAttribute attrib= (CPListElementAttribute) elem;
601             if (attrib.isNonModifiable()) {
602                 return false;
603             }
604             if (!attrib.isBuiltIn()) {
605                 return canEditCustomAttribute(attrib);
606             }
607             return true;
608         }
609         return false;
610     }
611     
612     private void libaryPageDialogFieldChanged(DialogField field) {
613         if (fCurrJProject != null) {
614             // already initialized
615
updateClasspathList();
616         }
617     }
618         
619     private void updateClasspathList() {
620         List JavaDoc projelements= fLibrariesList.getElements();
621         
622         List JavaDoc cpelements= fClassPathList.getElements();
623         int nEntries= cpelements.size();
624         // backwards, as entries will be deleted
625
int lastRemovePos= nEntries;
626         for (int i= nEntries - 1; i >= 0; i--) {
627             CPListElement cpe= (CPListElement)cpelements.get(i);
628             int kind= cpe.getEntryKind();
629             if (isEntryKind(kind)) {
630                 if (!projelements.remove(cpe)) {
631                     cpelements.remove(i);
632                     lastRemovePos= i;
633                 }
634             }
635         }
636         
637         cpelements.addAll(lastRemovePos, projelements);
638
639         if (lastRemovePos != nEntries || !projelements.isEmpty()) {
640             fClassPathList.setElements(cpelements);
641         }
642     }
643     
644         
645     private CPListElement[] openNewClassFolderDialog(CPListElement existing) {
646         String JavaDoc title= (existing == null) ? NewWizardMessages.LibrariesWorkbookPage_NewClassFolderDialog_new_title : NewWizardMessages.LibrariesWorkbookPage_NewClassFolderDialog_edit_title;
647         IProject currProject= fCurrJProject.getProject();
648         
649         NewContainerDialog dialog= new NewContainerDialog(getShell(), title, currProject, getUsedContainers(existing), existing);
650         IPath projpath= currProject.getFullPath();
651         dialog.setMessage(Messages.format(NewWizardMessages.LibrariesWorkbookPage_NewClassFolderDialog_description, projpath.toString()));
652         if (dialog.open() == Window.OK) {
653             IFolder folder= dialog.getFolder();
654             return new CPListElement[] { newCPLibraryElement(folder) };
655         }
656         return null;
657     }
658             
659             
660     private CPListElement[] openClassFolderDialog(CPListElement existing) {
661         if (existing == null) {
662             IPath[] selected= BuildPathDialogAccess.chooseClassFolderEntries(getShell(), fCurrJProject.getPath(), getUsedContainers(existing));
663             if (selected != null) {
664                 IWorkspaceRoot root= fCurrJProject.getProject().getWorkspace().getRoot();
665                 ArrayList JavaDoc res= new ArrayList JavaDoc();
666                 for (int i= 0; i < selected.length; i++) {
667                     IPath curr= selected[i];
668                     IResource resource= root.findMember(curr);
669                     if (resource instanceof IContainer) {
670                         res.add(newCPLibraryElement(resource));
671                     }
672                 }
673                 return (CPListElement[]) res.toArray(new CPListElement[res.size()]);
674             }
675         } else {
676             // disabled
677
}
678         return null;
679     }
680     
681     private CPListElement[] openJarFileDialog(CPListElement existing) {
682         IWorkspaceRoot root= fCurrJProject.getProject().getWorkspace().getRoot();
683         
684         if (existing == null) {
685             IPath[] selected= BuildPathDialogAccess.chooseJAREntries(getShell(), fCurrJProject.getPath(), getUsedJARFiles(existing));
686             if (selected != null) {
687                 ArrayList JavaDoc res= new ArrayList JavaDoc();
688                 
689                 for (int i= 0; i < selected.length; i++) {
690                     IPath curr= selected[i];
691                     IResource resource= root.findMember(curr);
692                     if (resource instanceof IFile) {
693                         res.add(newCPLibraryElement(resource));
694                     }
695                 }
696                 return (CPListElement[]) res.toArray(new CPListElement[res.size()]);
697             }
698         } else {
699             IPath configured= BuildPathDialogAccess.configureJAREntry(getShell(), existing.getPath(), getUsedJARFiles(existing));
700             if (configured != null) {
701                 IResource resource= root.findMember(configured);
702                 if (resource instanceof IFile) {
703                     return new CPListElement[] { newCPLibraryElement(resource) };
704                 }
705             }
706         }
707         return null;
708     }
709     
710     private IPath[] getUsedContainers(CPListElement existing) {
711         ArrayList JavaDoc res= new ArrayList JavaDoc();
712         if (fCurrJProject.exists()) {
713             try {
714                 IPath outputLocation= fCurrJProject.getOutputLocation();
715                 if (outputLocation != null && outputLocation.segmentCount() > 1) { // != Project
716
res.add(outputLocation);
717                 }
718             } catch (JavaModelException e) {
719                 // ignore it here, just log
720
JavaPlugin.log(e.getStatus());
721             }
722         }
723             
724         List JavaDoc cplist= fLibrariesList.getElements();
725         for (int i= 0; i < cplist.size(); i++) {
726             CPListElement elem= (CPListElement)cplist.get(i);
727             if (elem.getEntryKind() == IClasspathEntry.CPE_LIBRARY && (elem != existing)) {
728                 IResource resource= elem.getResource();
729                 if (resource instanceof IContainer && !resource.equals(existing)) {
730                     res.add(resource.getFullPath());
731                 }
732             }
733         }
734         return (IPath[]) res.toArray(new IPath[res.size()]);
735     }
736     
737     private IPath[] getUsedJARFiles(CPListElement existing) {
738         List JavaDoc res= new ArrayList JavaDoc();
739         List JavaDoc cplist= fLibrariesList.getElements();
740         for (int i= 0; i < cplist.size(); i++) {
741             CPListElement elem= (CPListElement)cplist.get(i);
742             if (elem.getEntryKind() == IClasspathEntry.CPE_LIBRARY && (elem != existing)) {
743                 IResource resource= elem.getResource();
744                 if (resource instanceof IFile) {
745                     res.add(resource.getFullPath());
746                 }
747             }
748         }
749         return (IPath[]) res.toArray(new IPath[res.size()]);
750     }
751     
752     private CPListElement newCPLibraryElement(IResource res) {
753         return new CPListElement(fCurrJProject, IClasspathEntry.CPE_LIBRARY, res.getFullPath(), res);
754     }
755
756     private CPListElement[] openExtJarFileDialog(CPListElement existing) {
757         if (existing == null) {
758             IPath[] selected= BuildPathDialogAccess.chooseExternalJAREntries(getShell());
759             if (selected != null) {
760                 ArrayList JavaDoc res= new ArrayList JavaDoc();
761                 for (int i= 0; i < selected.length; i++) {
762                     res.add(new CPListElement(fCurrJProject, IClasspathEntry.CPE_LIBRARY, selected[i], null));
763                 }
764                 return (CPListElement[]) res.toArray(new CPListElement[res.size()]);
765             }
766         } else {
767             IPath configured= BuildPathDialogAccess.configureExternalJAREntry(getShell(), existing.getPath());
768             if (configured != null) {
769                 return new CPListElement[] { new CPListElement(fCurrJProject, IClasspathEntry.CPE_LIBRARY, configured, null) };
770             }
771         }
772         return null;
773     }
774         
775     private CPListElement[] openVariableSelectionDialog(CPListElement existing) {
776         List JavaDoc existingElements= fLibrariesList.getElements();
777         ArrayList JavaDoc existingPaths= new ArrayList JavaDoc(existingElements.size());
778         for (int i= 0; i < existingElements.size(); i++) {
779             CPListElement elem= (CPListElement) existingElements.get(i);
780             if (elem.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
781                 existingPaths.add(elem.getPath());
782             }
783         }
784         IPath[] existingPathsArray= (IPath[]) existingPaths.toArray(new IPath[existingPaths.size()]);
785         
786         if (existing == null) {
787             IPath[] paths= BuildPathDialogAccess.chooseVariableEntries(getShell(), existingPathsArray);
788             if (paths != null) {
789                 ArrayList JavaDoc result= new ArrayList JavaDoc();
790                 for (int i = 0; i < paths.length; i++) {
791                     IPath path= paths[i];
792                     CPListElement elem= createCPVariableElement(path);
793                     if (!existingElements.contains(elem)) {
794                         result.add(elem);
795                     }
796                 }
797                 return (CPListElement[]) result.toArray(new CPListElement[result.size()]);
798             }
799         } else {
800             IPath path= BuildPathDialogAccess.configureVariableEntry(getShell(), existing.getPath(), existingPathsArray);
801             if (path != null) {
802                 return new CPListElement[] { createCPVariableElement(path) };
803             }
804         }
805         return null;
806     }
807
808     private CPListElement createCPVariableElement(IPath path) {
809         CPListElement elem= new CPListElement(fCurrJProject, IClasspathEntry.CPE_VARIABLE, path, null);
810         IPath resolvedPath= JavaCore.getResolvedVariablePath(path);
811         elem.setIsMissing((resolvedPath == null) || !resolvedPath.toFile().exists());
812         return elem;
813     }
814
815     private CPListElement[] openContainerSelectionDialog(CPListElement existing) {
816         if (existing == null) {
817             IClasspathEntry[] created= BuildPathDialogAccess.chooseContainerEntries(getShell(), fCurrJProject, getRawClasspath());
818             if (created != null) {
819                 CPListElement[] res= new CPListElement[created.length];
820                 for (int i= 0; i < res.length; i++) {
821                     res[i]= new CPListElement(fCurrJProject, IClasspathEntry.CPE_CONTAINER, created[i].getPath(), null);
822                 }
823                 return res;
824             }
825         } else {
826             IClasspathEntry created= BuildPathDialogAccess.configureContainerEntry(getShell(), existing.getClasspathEntry(), fCurrJProject, getRawClasspath());
827             if (created != null) {
828                 CPListElement elem= new CPListElement(fCurrJProject, IClasspathEntry.CPE_CONTAINER, created.getPath(), null);
829                 return new CPListElement[] { elem };
830             }
831         }
832         return null;
833     }
834         
835     private IClasspathEntry[] getRawClasspath() {
836         IClasspathEntry[] currEntries= new IClasspathEntry[fClassPathList.getSize()];
837         for (int i= 0; i < currEntries.length; i++) {
838             CPListElement curr= (CPListElement) fClassPathList.getElement(i);
839             currEntries[i]= curr.getClasspathEntry();
840         }
841         return currEntries;
842     }
843         
844     /* (non-Javadoc)
845      * @see org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage#isEntryKind(int)
846      */

847     public boolean isEntryKind(int kind) {
848         return kind == IClasspathEntry.CPE_LIBRARY || kind == IClasspathEntry.CPE_VARIABLE || kind == IClasspathEntry.CPE_CONTAINER;
849     }
850     
851     /*
852      * @see BuildPathBasePage#getSelection
853      */

854     public List JavaDoc getSelection() {
855         return fLibrariesList.getSelectedElements();
856     }
857
858     /*
859      * @see BuildPathBasePage#setSelection
860      */

861     public void setSelection(List JavaDoc selElements, boolean expand) {
862         fLibrariesList.selectElements(new StructuredSelection(selElements));
863         if (expand) {
864             for (int i= 0; i < selElements.size(); i++) {
865                 fLibrariesList.expandElement(selElements.get(i), 1);
866             }
867         }
868     }
869
870     /**
871      * {@inheritDoc}
872      */

873     public void setFocus() {
874         fLibrariesList.setFocus();
875     }
876
877 }
878
Popular Tags