KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.Collator;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Path;
26
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IWorkspaceRoot;
29 import org.eclipse.core.resources.ResourcesPlugin;
30
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.widgets.Composite;
33
34 import org.eclipse.jface.dialogs.Dialog;
35
36 import org.eclipse.ui.dialogs.PreferencesUtil;
37
38 import org.eclipse.jdt.core.IClasspathContainer;
39 import org.eclipse.jdt.core.IClasspathEntry;
40 import org.eclipse.jdt.core.IJavaProject;
41 import org.eclipse.jdt.core.JavaCore;
42 import org.eclipse.jdt.core.JavaModelException;
43
44 import org.eclipse.jdt.internal.corext.util.Messages;
45
46 import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
47 import org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension;
48 import org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension2;
49 import org.eclipse.jdt.ui.wizards.NewElementWizardPage;
50
51 import org.eclipse.jdt.internal.ui.JavaPlugin;
52 import org.eclipse.jdt.internal.ui.JavaPluginImages;
53 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
54 import org.eclipse.jdt.internal.ui.preferences.UserLibraryPreferencePage;
55 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
56 import org.eclipse.jdt.internal.ui.wizards.dialogfields.CheckedListDialogField;
57 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
58 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
59 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
61 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
62
63 /**
64  *
65  */

66 public class UserLibraryWizardPage extends NewElementWizardPage implements IClasspathContainerPage, IClasspathContainerPageExtension, IClasspathContainerPageExtension2 {
67     
68     private CheckedListDialogField fLibrarySelector;
69     private CPUserLibraryElement fEditResult;
70     private Set JavaDoc fUsedPaths;
71     private boolean fIsEditMode;
72     private IJavaProject fProject;
73     private boolean fIsExported;
74     
75     public UserLibraryWizardPage() {
76         super("UserLibraryWizardPage"); //$NON-NLS-1$
77
setTitle(NewWizardMessages.UserLibraryWizardPage_title);
78         setImageDescriptor(JavaPluginImages.DESC_WIZBAN_ADD_LIBRARY);
79         updateDescription(null);
80         fUsedPaths= new HashSet JavaDoc();
81         fProject= createPlaceholderProject();
82         
83         LibraryListAdapter adapter= new LibraryListAdapter();
84         String JavaDoc[] buttonLabels= new String JavaDoc[] {
85                 NewWizardMessages.UserLibraryWizardPage_list_config_button
86         };
87         fLibrarySelector= new CheckedListDialogField(adapter, buttonLabels, new CPListLabelProvider());
88         fLibrarySelector.setDialogFieldListener(adapter);
89         fLibrarySelector.setLabelText(NewWizardMessages.UserLibraryWizardPage_list_label);
90         fEditResult= null;
91         updateStatus(validateSetting(Collections.EMPTY_LIST));
92     }
93     
94     private static IJavaProject createPlaceholderProject() {
95         String JavaDoc name= "####internal"; //$NON-NLS-1$
96
IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
97         while (true) {
98             IProject project= root.getProject(name);
99             if (!project.exists()) {
100                 return JavaCore.create(project);
101             }
102             name += '1';
103         }
104     }
105
106     private void updateDescription(IClasspathEntry containerEntry) {
107         if (containerEntry == null || containerEntry.getPath().segmentCount() != 2) {
108             setDescription(NewWizardMessages.UserLibraryWizardPage_description_new);
109         } else {
110             setDescription(Messages.format(NewWizardMessages.UserLibraryWizardPage_description_edit, containerEntry.getPath().segment(1)));
111         }
112     }
113     
114     private List JavaDoc updateLibraryList() {
115         HashSet JavaDoc oldNames= new HashSet JavaDoc();
116         HashSet JavaDoc oldCheckedNames= new HashSet JavaDoc();
117         List JavaDoc oldElements= fLibrarySelector.getElements();
118         for (int i= 0; i < oldElements.size(); i++) {
119             CPUserLibraryElement curr= (CPUserLibraryElement) oldElements.get(i);
120             oldNames.add(curr.getName());
121             if (fLibrarySelector.isChecked(curr)) {
122                 oldCheckedNames.add(curr.getName());
123             }
124         }
125
126         ArrayList JavaDoc entriesToCheck= new ArrayList JavaDoc();
127         
128         String JavaDoc[] names= JavaCore.getUserLibraryNames();
129         Arrays.sort(names, Collator.getInstance());
130
131         ArrayList JavaDoc elements= new ArrayList JavaDoc(names.length);
132         for (int i= 0; i < names.length; i++) {
133             String JavaDoc curr= names[i];
134             IPath path= new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(curr);
135             try {
136                 IClasspathContainer container= JavaCore.getClasspathContainer(path, fProject);
137                 CPUserLibraryElement elem= new CPUserLibraryElement(curr, container, fProject);
138                 elements.add(elem);
139                 if (!oldCheckedNames.isEmpty()) {
140                     if (oldCheckedNames.contains(curr)) {
141                         entriesToCheck.add(elem);
142                     }
143                 } else {
144                     if (!oldNames.contains(curr)) {
145                         entriesToCheck.add(elem);
146                     }
147                 }
148             } catch (JavaModelException e) {
149                 JavaPlugin.log(e);
150                 // ignore
151
}
152         }
153         fLibrarySelector.setElements(elements);
154         return entriesToCheck;
155     }
156         
157     private void doDialogFieldChanged(DialogField field) {
158         if (field == fLibrarySelector) {
159             List JavaDoc list= fLibrarySelector.getCheckedElements();
160             if (fIsEditMode) {
161                 if (list.size() > 1) {
162                     if (fEditResult != null && list.remove(fEditResult)) {
163                         fLibrarySelector.setCheckedWithoutUpdate(fEditResult, false);
164                     }
165                     fEditResult= (CPUserLibraryElement) list.get(0); // take the first
166
for (int i= 1; i < list.size(); i++) { // uncheck the rest
167
fLibrarySelector.setCheckedWithoutUpdate(list.get(i), false);
168                     }
169                 } else if (list.size() == 1) {
170                     fEditResult= (CPUserLibraryElement) list.get(0);
171                 }
172             }
173             updateStatus(validateSetting(list));
174         }
175     }
176     
177     private IStatus validateSetting(List JavaDoc selected) {
178         int nSelected= selected.size();
179         if (nSelected == 0) {
180             return new StatusInfo(IStatus.ERROR, NewWizardMessages.UserLibraryWizardPage_error_selectentry);
181         } else if (fIsEditMode && nSelected > 1) {
182             return new StatusInfo(IStatus.ERROR, NewWizardMessages.UserLibraryWizardPage_error_selectonlyone);
183         }
184         for (int i= 0; i < selected.size(); i++) {
185             CPUserLibraryElement curr= (CPUserLibraryElement) selected.get(i);
186             if (fUsedPaths.contains(curr.getPath())) {
187                 return new StatusInfo(IStatus.ERROR, NewWizardMessages.UserLibraryWizardPage_error_alreadyoncp);
188             }
189         }
190         return new StatusInfo();
191     }
192     
193     private void doButtonPressed(int index) {
194         if (index == 0) {
195             HashMap JavaDoc data= new HashMap JavaDoc(3);
196             if (fEditResult != null) {
197                 data.put(UserLibraryPreferencePage.DATA_LIBRARY_TO_SELECT, fEditResult.getName());
198             }
199             String JavaDoc id= UserLibraryPreferencePage.ID;
200             PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String JavaDoc[] { id }, data).open();
201     
202             List JavaDoc newEntries= updateLibraryList();
203             if (newEntries.size() > 0) {
204                 if (fIsEditMode) {
205                     fLibrarySelector.setChecked(newEntries.get(0), true);
206                 } else {
207                     fLibrarySelector.setCheckedElements(newEntries);
208                 }
209             }
210         } else {
211             fLibrarySelector.setCheckedElements(fLibrarySelector.getSelectedElements());
212         }
213     }
214     
215     private void doDoubleClicked(ListDialogField field) {
216         List JavaDoc list= fLibrarySelector.getSelectedElements();
217         if (list.size() == 1) {
218             Object JavaDoc elem= list.get(0);
219             boolean state= fLibrarySelector.isChecked(elem);
220             if (!state || !fIsEditMode) {
221                 fLibrarySelector.setChecked(elem, !state);
222             }
223         }
224     }
225     
226     /* (non-Javadoc)
227      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
228      */

229     public void createControl(Composite parent) {
230         Composite composite= new Composite(parent, SWT.NONE);
231         composite.setFont(parent.getFont());
232         
233         LayoutUtil.doDefaultLayout(composite, new DialogField[] { fLibrarySelector }, true, SWT.DEFAULT, SWT.DEFAULT);
234         LayoutUtil.setHorizontalGrabbing(fLibrarySelector.getListControl(null));
235         Dialog.applyDialogFont(composite);
236         setControl(composite);
237     }
238     
239     /* (non-Javadoc)
240      * @see org.eclipse.jdt.ui.wizards.IClasspathContainerPage#finish()
241      */

242     public boolean finish() {
243         return true;
244     }
245
246     /* (non-Javadoc)
247      * @see org.eclipse.jdt.ui.wizards.IClasspathContainerPage#getSelection()
248      */

249     public IClasspathEntry getSelection() {
250         if (fEditResult != null) {
251             return JavaCore.newContainerEntry(fEditResult.getPath(), fIsExported);
252         }
253         return null;
254     }
255     
256     /* (non-Javadoc)
257      * @see org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension2#getNewContainers()
258      */

259     public IClasspathEntry[] getNewContainers() {
260         List JavaDoc selected= fLibrarySelector.getCheckedElements();
261         IClasspathEntry[] res= new IClasspathEntry[selected.size()];
262         for (int i= 0; i < res.length; i++) {
263             CPUserLibraryElement curr= (CPUserLibraryElement) selected.get(i);
264             res[i]= JavaCore.newContainerEntry(curr.getPath(), fIsExported);
265         }
266         return res;
267     }
268
269     /* (non-Javadoc)
270      * @see org.eclipse.jdt.ui.wizards.IClasspathContainerPage#setSelection(org.eclipse.jdt.core.IClasspathEntry)
271      */

272     public void setSelection(IClasspathEntry containerEntry) {
273         fIsExported= containerEntry != null && containerEntry.isExported();
274         
275         updateDescription(containerEntry);
276         fIsEditMode= (containerEntry != null);
277         if (containerEntry != null) {
278             fUsedPaths.remove(containerEntry.getPath());
279         }
280         
281         String JavaDoc selected= null;
282         if (containerEntry != null && containerEntry.getPath().segmentCount() == 2) {
283             selected= containerEntry.getPath().segment(1);
284         } else {
285             // get from dialog store
286
}
287         updateLibraryList();
288         if (selected != null) {
289             List JavaDoc elements= fLibrarySelector.getElements();
290             for (int i= 0; i < elements.size(); i++) {
291                 CPUserLibraryElement curr= (CPUserLibraryElement) elements.get(i);
292                 if (curr.getName().equals(selected)) {
293                     fLibrarySelector.setChecked(curr, true);
294                     return;
295                 }
296             }
297         }
298     }
299     
300     private class LibraryListAdapter implements IListAdapter, IDialogFieldListener {
301         
302         public LibraryListAdapter() {
303         }
304         
305         public void dialogFieldChanged(DialogField field) {
306             doDialogFieldChanged(field);
307         }
308
309         public void customButtonPressed(ListDialogField field, int index) {
310             doButtonPressed(index);
311         }
312
313         public void selectionChanged(ListDialogField field) {
314         }
315
316         public void doubleClicked(ListDialogField field) {
317             doDoubleClicked(field);
318         }
319     }
320
321     /* (non-Javadoc)
322      * @see org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension#initialize(org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IClasspathEntry[])
323      */

324     public void initialize(IJavaProject project, IClasspathEntry[] currentEntries) {
325         for (int i= 0; i < currentEntries.length; i++) {
326             IClasspathEntry curr= currentEntries[i];
327             if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
328                 fUsedPaths.add(curr.getPath());
329             }
330         }
331     }
332 }
333
Popular Tags