KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.viewers.ArrayContentProvider;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.window.Window;
27
28 import org.eclipse.ui.dialogs.ListSelectionDialog;
29 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
30
31 import org.eclipse.jdt.core.IAccessRule;
32 import org.eclipse.jdt.core.IClasspathEntry;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.JavaModelException;
36
37 import org.eclipse.jdt.ui.JavaElementComparator;
38
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.util.PixelConverter;
41 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
42 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
45 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ITreeListAdapter;
46 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
47 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
48 import org.eclipse.jdt.internal.ui.wizards.dialogfields.TreeListDialogField;
49
50
51 public class ProjectsWorkbookPage extends BuildPathBasePage {
52     
53     private final int IDX_ADDPROJECT= 0;
54     
55     private final int IDX_EDIT= 2;
56     private final int IDX_REMOVE= 3;
57     
58     private ListDialogField fClassPathList;
59     private IJavaProject fCurrJProject;
60     
61     private TreeListDialogField fProjectsList;
62     
63     private Control fSWTControl;
64
65     private final IWorkbenchPreferenceContainer fPageContainer;
66     
67     public ProjectsWorkbookPage(ListDialogField classPathList, IWorkbenchPreferenceContainer pageContainer) {
68         fClassPathList= classPathList;
69         fPageContainer= pageContainer;
70         fSWTControl= null;
71         
72         String JavaDoc[] buttonLabels= new String JavaDoc[] {
73             NewWizardMessages.ProjectsWorkbookPage_projects_add_button,
74             null,
75             NewWizardMessages.ProjectsWorkbookPage_projects_edit_button,
76             NewWizardMessages.ProjectsWorkbookPage_projects_remove_button
77         };
78         
79         ProjectsAdapter adapter= new ProjectsAdapter();
80         
81         fProjectsList= new TreeListDialogField(adapter, buttonLabels, new CPListLabelProvider());
82         fProjectsList.setDialogFieldListener(adapter);
83         fProjectsList.setLabelText(NewWizardMessages.ProjectsWorkbookPage_projects_label);
84         
85         fProjectsList.enableButton(IDX_REMOVE, false);
86         fProjectsList.enableButton(IDX_EDIT, false);
87         
88         fProjectsList.setViewerComparator(new CPListElementSorter());
89     }
90     
91     public void init(final IJavaProject jproject) {
92         fCurrJProject= jproject;
93         
94         if (Display.getCurrent() != null) {
95             updateProjectsList(jproject);
96         } else {
97             Display.getDefault().asyncExec(new Runnable JavaDoc() {
98                 public void run() {
99                     updateProjectsList(jproject);
100                 }
101             });
102         }
103     }
104         
105     private void updateProjectsList(IJavaProject currJProject) {
106         // add the projects-cpentries that are already on the class path
107
List JavaDoc cpelements= fClassPathList.getElements();
108         
109         final List JavaDoc checkedProjects= new ArrayList JavaDoc(cpelements.size());
110         
111         for (int i= cpelements.size() - 1 ; i >= 0; i--) {
112             CPListElement cpelem= (CPListElement)cpelements.get(i);
113             if (isEntryKind(cpelem.getEntryKind())) {
114                 checkedProjects.add(cpelem);
115             }
116         }
117         fProjectsList.setElements(checkedProjects);
118     }
119         
120     // -------- UI creation ---------
121

122     public Control getControl(Composite parent) {
123         PixelConverter converter= new PixelConverter(parent);
124         
125         Composite composite= new Composite(parent, SWT.NONE);
126             
127         LayoutUtil.doDefaultLayout(composite, new DialogField[] { fProjectsList }, true, SWT.DEFAULT, SWT.DEFAULT);
128         LayoutUtil.setHorizontalGrabbing(fProjectsList.getTreeControl(null));
129         
130         int buttonBarWidth= converter.convertWidthInCharsToPixels(24);
131         fProjectsList.setButtonsMinWidth(buttonBarWidth);
132         
133         fSWTControl= composite;
134                 
135         return composite;
136     }
137         
138     private void updateClasspathList() {
139         List JavaDoc projelements= fProjectsList.getElements();
140         
141         boolean remove= false;
142         List JavaDoc cpelements= fClassPathList.getElements();
143         // backwards, as entries will be deleted
144
for (int i= cpelements.size() -1; i >= 0 ; i--) {
145             CPListElement cpe= (CPListElement)cpelements.get(i);
146             if (isEntryKind(cpe.getEntryKind())) {
147                 if (!projelements.remove(cpe)) {
148                     cpelements.remove(i);
149                     remove= true;
150                 }
151             }
152         }
153         for (int i= 0; i < projelements.size(); i++) {
154             cpelements.add(projelements.get(i));
155         }
156         if (remove || (projelements.size() > 0)) {
157             fClassPathList.setElements(cpelements);
158         }
159     }
160     
161     /*
162      * @see BuildPathBasePage#getSelection
163      */

164     public List JavaDoc getSelection() {
165         return fProjectsList.getSelectedElements();
166     }
167
168     /*
169      * @see BuildPathBasePage#setSelection
170      */

171     public void setSelection(List JavaDoc selElements, boolean expand) {
172         fProjectsList.selectElements(new StructuredSelection(selElements));
173         if (expand) {
174             for (int i= 0; i < selElements.size(); i++) {
175                 fProjectsList.expandElement(selElements.get(i), 1);
176             }
177         }
178     }
179     
180     /* (non-Javadoc)
181      * @see org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage#isEntryKind(int)
182      */

183     public boolean isEntryKind(int kind) {
184         return kind == IClasspathEntry.CPE_PROJECT;
185     }
186
187
188     private class ProjectsAdapter implements IDialogFieldListener, ITreeListAdapter {
189         
190         private final Object JavaDoc[] EMPTY_ARR= new Object JavaDoc[0];
191         
192         // -------- IListAdapter --------
193
public void customButtonPressed(TreeListDialogField field, int index) {
194             projectPageCustomButtonPressed(field, index);
195         }
196         
197         public void selectionChanged(TreeListDialogField field) {
198             projectPageSelectionChanged(field);
199         }
200         
201         public void doubleClicked(TreeListDialogField field) {
202             projectPageDoubleClicked(field);
203         }
204         
205         public void keyPressed(TreeListDialogField field, KeyEvent event) {
206             projectPageKeyPressed(field, event);
207         }
208
209         public Object JavaDoc[] getChildren(TreeListDialogField field, Object JavaDoc element) {
210             if (element instanceof CPListElement) {
211                 return ((CPListElement) element).getChildren(false);
212             }
213             return EMPTY_ARR;
214         }
215
216         public Object JavaDoc getParent(TreeListDialogField field, Object JavaDoc element) {
217             if (element instanceof CPListElementAttribute) {
218                 return ((CPListElementAttribute) element).getParent();
219             }
220             return null;
221         }
222
223         public boolean hasChildren(TreeListDialogField field, Object JavaDoc element) {
224             return getChildren(field, element).length > 0;
225         }
226             
227         // ---------- IDialogFieldListener --------
228

229         public void dialogFieldChanged(DialogField field) {
230             projectPageDialogFieldChanged(field);
231         }
232     }
233     
234     private void projectPageCustomButtonPressed(DialogField field, int index) {
235         CPListElement[] entries= null;
236         switch (index) {
237         case IDX_ADDPROJECT: /* add project */
238             entries= openProjectDialog(null);
239             break;
240         case IDX_EDIT: /* edit */
241             editEntry();
242             return;
243         case IDX_REMOVE: /* remove */
244             removeEntry();
245             return;
246         }
247         if (entries != null) {
248             int nElementsChosen= entries.length;
249             // remove duplicates
250
List JavaDoc cplist= fProjectsList.getElements();
251             List JavaDoc elementsToAdd= new ArrayList JavaDoc(nElementsChosen);
252             for (int i= 0; i < nElementsChosen; i++) {
253                 CPListElement curr= entries[i];
254                 if (!cplist.contains(curr) && !elementsToAdd.contains(curr)) {
255                     elementsToAdd.add(curr);
256                 }
257             }
258                         
259             fProjectsList.addElements(elementsToAdd);
260             if (index == IDX_ADDPROJECT) {
261                 fProjectsList.refresh();
262             }
263             fProjectsList.postSetSelection(new StructuredSelection(entries));
264         }
265     }
266     
267     private void removeEntry() {
268         List JavaDoc selElements= fProjectsList.getSelectedElements();
269         for (int i= selElements.size() - 1; i >= 0 ; i--) {
270             Object JavaDoc elem= selElements.get(i);
271             if (elem instanceof CPListElementAttribute) {
272                 CPListElementAttribute attrib= (CPListElementAttribute) elem;
273                 if (attrib.isBuiltIn()) {
274                     String JavaDoc key= attrib.getKey();
275                     Object JavaDoc value= null;
276                     if (key.equals(CPListElement.ACCESSRULES)) {
277                         value= new IAccessRule[0];
278                     }
279                     attrib.getParent().setAttribute(key, value);
280                 } else {
281                     removeCustomAttribute(attrib);
282                 }
283                 selElements.remove(i);
284             }
285         }
286         if (selElements.isEmpty()) {
287             fProjectsList.refresh();
288             fClassPathList.dialogFieldChanged(); // validate
289
} else {
290             fProjectsList.removeElements(selElements);
291         }
292     }
293     
294     private boolean canRemove(List JavaDoc selElements) {
295         if (selElements.size() == 0) {
296             return false;
297         }
298
299         for (int i= 0; i < selElements.size(); i++) {
300             Object JavaDoc elem= selElements.get(i);
301             if (elem instanceof CPListElementAttribute) {
302                 CPListElementAttribute attrib= (CPListElementAttribute) elem;
303                 if (attrib.isNonModifiable()) {
304                     return false;
305                 }
306                 if (attrib.isBuiltIn()) {
307                     if (CPListElement.ACCESSRULES.equals(attrib.getKey())) {
308                         if (((IAccessRule[]) attrib.getValue()).length == 0) {
309                             return false;
310                         }
311                     } else if (attrib.getValue() == null) {
312                         return false;
313                     }
314                 } else {
315                     if (!canRemoveCustomAttribute(attrib)) {
316                         return false;
317                     }
318                 }
319             }
320         }
321         return true;
322     }
323
324     private boolean canEdit(List JavaDoc selElements) {
325         if (selElements.size() != 1) {
326             return false;
327         }
328         Object JavaDoc elem= selElements.get(0);
329         if (elem instanceof CPListElement) {
330             return false;
331         }
332         if (elem instanceof CPListElementAttribute) {
333             CPListElementAttribute attrib= (CPListElementAttribute) elem;
334             if (attrib.isNonModifiable()) {
335                 return false;
336             }
337             if (!attrib.isBuiltIn()) {
338                 return canEditCustomAttribute(attrib);
339             }
340             return true;
341         }
342         return false;
343     }
344     
345     /**
346      * Method editEntry.
347      */

348     private void editEntry() {
349         List JavaDoc selElements= fProjectsList.getSelectedElements();
350         if (selElements.size() != 1) {
351             return;
352         }
353         Object JavaDoc elem= selElements.get(0);
354         if (fProjectsList.getIndexOfElement(elem) != -1) {
355             editElementEntry((CPListElement) elem);
356         } else if (elem instanceof CPListElementAttribute) {
357             editAttributeEntry((CPListElementAttribute) elem);
358         }
359     }
360     
361     private void editAttributeEntry(CPListElementAttribute elem) {
362         String JavaDoc key= elem.getKey();
363         if (key.equals(CPListElement.ACCESSRULES)) {
364             showAccessRestrictionDialog(elem.getParent());
365         } else {
366             if (editCustomAttribute(getShell(), elem)) {
367                 fProjectsList.refresh();
368                 fClassPathList.dialogFieldChanged(); // validate
369
}
370         }
371     }
372     
373     private void showAccessRestrictionDialog(CPListElement selElement) {
374         AccessRulesDialog dialog= new AccessRulesDialog(getShell(), selElement, fCurrJProject, fPageContainer != null);
375         int res= dialog.open();
376         if (res == Window.OK || res == AccessRulesDialog.SWITCH_PAGE) {
377             selElement.setAttribute(CPListElement.ACCESSRULES, dialog.getAccessRules());
378             selElement.setAttribute(CPListElement.COMBINE_ACCESSRULES, new Boolean JavaDoc(dialog.doCombineAccessRules()));
379             fProjectsList.refresh();
380             fClassPathList.dialogFieldChanged(); // validate
381

382             if (res == AccessRulesDialog.SWITCH_PAGE) {
383                 dialog.performPageSwitch(fPageContainer);
384             }
385         }
386     }
387         
388     private void editElementEntry(CPListElement elem) {
389         CPListElement[] res= openProjectDialog(elem);
390         if (res != null && res.length > 0) {
391             CPListElement curr= res[0];
392             curr.setExported(elem.isExported());
393             fProjectsList.replaceElement(elem, curr);
394         }
395             
396     }
397     
398     private Shell getShell() {
399         if (fSWTControl != null) {
400             return fSWTControl.getShell();
401         }
402         return JavaPlugin.getActiveWorkbenchShell();
403     }
404
405
406     private CPListElement[] openProjectDialog(CPListElement elem) {
407         
408         try {
409             ArrayList JavaDoc selectable= new ArrayList JavaDoc();
410             selectable.addAll(Arrays.asList(fCurrJProject.getJavaModel().getJavaProjects()));
411             selectable.remove(fCurrJProject);
412             
413             List JavaDoc elements= fProjectsList.getElements();
414             for (int i= 0; i < elements.size(); i++) {
415                 CPListElement curr= (CPListElement) elements.get(0);
416                 IJavaProject proj= (IJavaProject) JavaCore.create(curr.getResource());
417                 selectable.remove(proj);
418             }
419             Object JavaDoc[] selectArr= selectable.toArray();
420             new JavaElementComparator().sort(null, selectArr);
421                     
422             ListSelectionDialog dialog= new ListSelectionDialog(getShell(), Arrays.asList(selectArr), new ArrayContentProvider(), new JavaUILabelProvider(), NewWizardMessages.ProjectsWorkbookPage_chooseProjects_message);
423             dialog.setTitle(NewWizardMessages.ProjectsWorkbookPage_chooseProjects_title);
424             dialog.setHelpAvailable(false);
425             if (dialog.open() == Window.OK) {
426                 Object JavaDoc[] result= dialog.getResult();
427                 CPListElement[] cpElements= new CPListElement[result.length];
428                 for (int i= 0; i < result.length; i++) {
429                     IJavaProject curr= (IJavaProject) result[i];
430                     cpElements[i]= new CPListElement(fCurrJProject, IClasspathEntry.CPE_PROJECT, curr.getPath(), curr.getResource());
431                 }
432                 return cpElements;
433             }
434         } catch (JavaModelException e) {
435             return null;
436         }
437         return null;
438     }
439
440     protected void projectPageDoubleClicked(TreeListDialogField field) {
441         List JavaDoc selection= fProjectsList.getSelectedElements();
442         if (canEdit(selection)) {
443             editEntry();
444         }
445     }
446
447     protected void projectPageKeyPressed(TreeListDialogField field, KeyEvent event) {
448         if (field == fProjectsList) {
449             if (event.character == SWT.DEL && event.stateMask == 0) {
450                 List JavaDoc selection= field.getSelectedElements();
451                 if (canRemove(selection)) {
452                     removeEntry();
453                 }
454             }
455         }
456     }
457     
458     private void projectPageDialogFieldChanged(DialogField field) {
459         if (fCurrJProject != null) {
460             // already initialized
461
updateClasspathList();
462         }
463     }
464     
465     private void projectPageSelectionChanged(DialogField field) {
466         List JavaDoc selElements= fProjectsList.getSelectedElements();
467         fProjectsList.enableButton(IDX_EDIT, canEdit(selElements));
468         fProjectsList.enableButton(IDX_REMOVE, canRemove(selElements));
469         
470         boolean noAttributes= containsOnlyTopLevelEntries(selElements);
471         fProjectsList.enableButton(IDX_ADDPROJECT, noAttributes);
472     }
473
474     /**
475      * {@inheritDoc}
476      */

477     public void setFocus() {
478         fProjectsList.setFocus();
479     }
480 }
481
Popular Tags