KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > JavaWorkingSetPage


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  * Rodrigo Kumpera <kumpera AT gmail.com> - bug 95232
11  *
12  *******************************************************************************/

13 package org.eclipse.jdt.internal.ui.workingsets;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.IAdaptable;
22
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.BusyIndicator;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.swt.widgets.TreeItem;
41
42 import org.eclipse.jface.dialogs.Dialog;
43 import org.eclipse.jface.viewers.CheckStateChangedEvent;
44 import org.eclipse.jface.viewers.CheckboxTreeViewer;
45 import org.eclipse.jface.viewers.ICheckStateListener;
46 import org.eclipse.jface.viewers.ITreeContentProvider;
47 import org.eclipse.jface.viewers.ITreeViewerListener;
48 import org.eclipse.jface.viewers.TreeExpansionEvent;
49 import org.eclipse.jface.wizard.WizardPage;
50
51 import org.eclipse.ui.IWorkbenchPage;
52 import org.eclipse.ui.IWorkbenchPart;
53 import org.eclipse.ui.IWorkingSet;
54 import org.eclipse.ui.IWorkingSetManager;
55 import org.eclipse.ui.PlatformUI;
56 import org.eclipse.ui.dialogs.IWorkingSetPage;
57
58 import org.eclipse.jdt.core.IJavaElement;
59 import org.eclipse.jdt.core.IJavaModel;
60 import org.eclipse.jdt.core.IJavaProject;
61 import org.eclipse.jdt.core.IPackageFragment;
62 import org.eclipse.jdt.core.IPackageFragmentRoot;
63 import org.eclipse.jdt.core.JavaCore;
64 import org.eclipse.jdt.core.JavaModelException;
65
66 import org.eclipse.jdt.ui.JavaElementComparator;
67 import org.eclipse.jdt.ui.JavaElementLabels;
68
69 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
70 import org.eclipse.jdt.internal.ui.JavaPlugin;
71 import org.eclipse.jdt.internal.ui.JavaPluginImages;
72 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
73 import org.eclipse.jdt.internal.ui.filters.EmptyInnerPackageFilter;
74 import org.eclipse.jdt.internal.ui.util.JavaUIHelp;
75 import org.eclipse.jdt.internal.ui.util.SWTUtil;
76 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
77 import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager;
78 import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
79 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
80
81 /**
82  * The Java working set page allows the user to create
83  * and edit a Java working set.
84  * <p>
85  * Working set elements are presented as a Java element tree.
86  * </p>
87  *
88  * @since 2.0
89  */

90 public class JavaWorkingSetPage extends WizardPage implements IWorkingSetPage {
91
92     final private static String JavaDoc PAGE_TITLE= WorkingSetMessages.JavaWorkingSetPage_title;
93     final private static String JavaDoc PAGE_ID= "javaWorkingSetPage"; //$NON-NLS-1$
94

95     private Text fWorkingSetName;
96     private CheckboxTreeViewer fTree;
97     private ITreeContentProvider fTreeContentProvider;
98     
99     private boolean fFirstCheck;
100     private IWorkingSet fWorkingSet;
101
102     /**
103      * Default constructor.
104      */

105     public JavaWorkingSetPage() {
106         super(PAGE_ID, PAGE_TITLE, JavaPluginImages.DESC_WIZBAN_JAVA_WORKINGSET);
107         setDescription(WorkingSetMessages.JavaWorkingSetPage_workingSet_description);
108         fFirstCheck= true;
109     }
110
111     /*
112      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
113      */

114     public void createControl(Composite parent) {
115         initializeDialogUnits(parent);
116         
117         Composite composite= new Composite(parent, SWT.NONE);
118         composite.setLayout(new GridLayout());
119         composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
120         setControl(composite);
121
122         Label label= new Label(composite, SWT.WRAP);
123         label.setText(WorkingSetMessages.JavaWorkingSetPage_workingSet_name);
124         GridData gd= new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
125         label.setLayoutData(gd);
126
127         fWorkingSetName= new Text(composite, SWT.SINGLE | SWT.BORDER);
128         fWorkingSetName.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
129         fWorkingSetName.addModifyListener(
130             new ModifyListener() {
131                 public void modifyText(ModifyEvent e) {
132                     validateInput();
133                 }
134             }
135         );
136         fWorkingSetName.setFocus();
137         
138         label= new Label(composite, SWT.WRAP);
139         label.setText(WorkingSetMessages.JavaWorkingSetPage_workingSet_content);
140         gd= new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
141         label.setLayoutData(gd);
142
143         fTree= new CheckboxTreeViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
144         ColoredViewersManager.install(fTree);
145         gd= new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
146         gd.heightHint= convertHeightInCharsToPixels(15);
147         fTree.getControl().setLayoutData(gd);
148         
149         fTreeContentProvider= new JavaWorkingSetPageContentProvider();
150         fTree.setContentProvider(fTreeContentProvider);
151         
152         AppearanceAwareLabelProvider fJavaElementLabelProvider=
153             new AppearanceAwareLabelProvider(
154                 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED,
155                 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS | JavaElementImageProvider.SMALL_ICONS
156             );
157         
158         fTree.setLabelProvider(new DecoratingJavaLabelProvider(fJavaElementLabelProvider));
159         fTree.setComparator(new JavaElementComparator());
160         fTree.addFilter(new EmptyInnerPackageFilter());
161         fTree.setUseHashlookup(true);
162         
163         fTree.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()));
164
165         fTree.addCheckStateListener(new ICheckStateListener() {
166             public void checkStateChanged(CheckStateChangedEvent event) {
167                 handleCheckStateChange(event);
168             }
169         });
170
171         fTree.addTreeListener(new ITreeViewerListener() {
172             public void treeCollapsed(TreeExpansionEvent event) {
173             }
174             public void treeExpanded(TreeExpansionEvent event) {
175                 final Object JavaDoc element= event.getElement();
176                 if (fTree.getGrayed(element) == false)
177                     BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc() {
178                     public void run() {
179                         setSubtreeChecked(element, fTree.getChecked(element), false);
180                     }
181                 });
182             }
183         });
184
185         // Add select / deselect all buttons for bug 46669
186
Composite buttonComposite = new Composite(composite, SWT.NONE);
187         GridLayout layout= new GridLayout(2, false);
188         layout.marginWidth= 0; layout.marginHeight= 0;
189         buttonComposite.setLayout(layout);
190         buttonComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
191         
192         Button selectAllButton = new Button(buttonComposite, SWT.PUSH);
193         selectAllButton.setText(WorkingSetMessages.JavaWorkingSetPage_selectAll_label);
194         selectAllButton.setToolTipText(WorkingSetMessages.JavaWorkingSetPage_selectAll_toolTip);
195         selectAllButton.addSelectionListener(new SelectionAdapter() {
196             public void widgetSelected(SelectionEvent selectionEvent) {
197                 fTree.setCheckedElements(fTreeContentProvider.getElements(fTree.getInput()));
198                 validateInput();
199             }
200         });
201         selectAllButton.setLayoutData(new GridData());
202         SWTUtil.setButtonDimensionHint(selectAllButton);
203
204         Button deselectAllButton = new Button(buttonComposite, SWT.PUSH);
205         deselectAllButton.setText(WorkingSetMessages.JavaWorkingSetPage_deselectAll_label);
206         deselectAllButton.setToolTipText(WorkingSetMessages.JavaWorkingSetPage_deselectAll_toolTip);
207         deselectAllButton.addSelectionListener(new SelectionAdapter() {
208             public void widgetSelected(SelectionEvent selectionEvent) {
209                 fTree.setCheckedElements(new Object JavaDoc[0]);
210                 validateInput();
211             }
212         });
213         deselectAllButton.setLayoutData(new GridData());
214         SWTUtil.setButtonDimensionHint(deselectAllButton);
215         
216         if (fWorkingSet != null)
217             fWorkingSetName.setText(fWorkingSet.getName());
218         initializeCheckedState();
219         validateInput();
220
221         Dialog.applyDialogFont(composite);
222         // Set help for the page
223
JavaUIHelp.setHelp(fTree, IJavaHelpContextIds.JAVA_WORKING_SET_PAGE);
224     }
225
226     /*
227      * Implements method from IWorkingSetPage
228      */

229     public IWorkingSet getSelection() {
230         return fWorkingSet;
231     }
232
233     /*
234      * Implements method from IWorkingSetPage
235      */

236     public void setSelection(IWorkingSet workingSet) {
237         Assert.isNotNull(workingSet, "Working set must not be null"); //$NON-NLS-1$
238
fWorkingSet= workingSet;
239         if (getContainer() != null && getShell() != null && fWorkingSetName != null) {
240             fFirstCheck= false;
241             fWorkingSetName.setText(fWorkingSet.getName());
242             initializeCheckedState();
243             validateInput();
244         }
245     }
246
247     /*
248      * Implements method from IWorkingSetPage
249      */

250     public void finish() {
251         String JavaDoc workingSetName= fWorkingSetName.getText();
252         ArrayList JavaDoc elements= new ArrayList JavaDoc(10);
253         findCheckedElements(elements, fTree.getInput());
254         if (fWorkingSet == null) {
255             IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
256             fWorkingSet= workingSetManager.createWorkingSet(workingSetName, (IAdaptable[])elements.toArray(new IAdaptable[elements.size()]));
257         } else {
258             // Add inaccessible resources
259
IAdaptable[] oldItems= fWorkingSet.getElements();
260             HashSet JavaDoc closedWithChildren= new HashSet JavaDoc(elements.size());
261             for (int i= 0; i < oldItems.length; i++) {
262                 IResource oldResource= null;
263                 if (oldItems[i] instanceof IResource) {
264                     oldResource= (IResource)oldItems[i];
265                 } else {
266                     oldResource= (IResource)oldItems[i].getAdapter(IResource.class);
267                 }
268                 if (oldResource != null && oldResource.isAccessible() == false) {
269                     IProject project= oldResource.getProject();
270                     if (closedWithChildren.contains(project) || elements.contains(project)) {
271                         elements.add(oldItems[i]);
272                         elements.remove(project);
273                         closedWithChildren.add(project);
274                     }
275                 }
276             }
277             fWorkingSet.setName(workingSetName);
278             fWorkingSet.setElements((IAdaptable[]) elements.toArray(new IAdaptable[elements.size()]));
279         }
280     }
281
282     private void validateInput() {
283         String JavaDoc errorMessage= null;
284         String JavaDoc infoMessage= null;
285         String JavaDoc newText= fWorkingSetName.getText();
286
287         if (newText.equals(newText.trim()) == false)
288             errorMessage = WorkingSetMessages.JavaWorkingSetPage_warning_nameWhitespace;
289         if (newText.equals("")) { //$NON-NLS-1$
290
if (fFirstCheck) {
291                 setPageComplete(false);
292                 fFirstCheck= false;
293                 return;
294             }
295             else
296                 errorMessage= WorkingSetMessages.JavaWorkingSetPage_warning_nameMustNotBeEmpty;
297         }
298
299         fFirstCheck= false;
300
301         if (errorMessage == null && (fWorkingSet == null || newText.equals(fWorkingSet.getName()) == false)) {
302             IWorkingSet[] workingSets= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets();
303             for (int i= 0; i < workingSets.length; i++) {
304                 if (newText.equals(workingSets[i].getName())) {
305                     errorMessage= WorkingSetMessages.JavaWorkingSetPage_warning_workingSetExists;
306                 }
307             }
308         }
309         
310         if (!hasCheckedElement())
311             infoMessage= WorkingSetMessages.JavaWorkingSetPage_warning_resourceMustBeChecked;
312
313         setMessage(infoMessage, INFORMATION);
314         setErrorMessage(errorMessage);
315         setPageComplete(errorMessage == null);
316     }
317     
318     private boolean hasCheckedElement() {
319         TreeItem[] items= fTree.getTree().getItems();
320         for (int i= 0; i < items.length; i++) {
321             if (items[i].getChecked())
322                 return true;
323         }
324         return false;
325     }
326     
327     private void findCheckedElements(List JavaDoc checkedResources, Object JavaDoc parent) {
328         Object JavaDoc[] children= fTreeContentProvider.getChildren(parent);
329         for (int i= 0; i < children.length; i++) {
330             if (fTree.getGrayed(children[i]))
331                 findCheckedElements(checkedResources, children[i]);
332             else if (fTree.getChecked(children[i]))
333                 checkedResources.add(children[i]);
334         }
335     }
336
337     void handleCheckStateChange(final CheckStateChangedEvent event) {
338         BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc() {
339             public void run() {
340                 IAdaptable element= (IAdaptable)event.getElement();
341                 boolean state= event.getChecked();
342                 fTree.setGrayed(element, false);
343                 if (isExpandable(element))
344                     setSubtreeChecked(element, state, state); // only check subtree if state is set to true
345

346                 updateParentState(element, state);
347                 validateInput();
348             }
349         });
350     }
351
352     private void setSubtreeChecked(Object JavaDoc parent, boolean state, boolean checkExpandedState) {
353         if (!(parent instanceof IAdaptable))
354             return;
355         IContainer container= (IContainer)((IAdaptable)parent).getAdapter(IContainer.class);
356         if ((!fTree.getExpandedState(parent) && checkExpandedState) || (container != null && !container.isAccessible()))
357             return;
358         
359         Object JavaDoc[] children= fTreeContentProvider.getChildren(parent);
360         for (int i= children.length - 1; i >= 0; i--) {
361             Object JavaDoc element= children[i];
362             if (state) {
363                 fTree.setChecked(element, true);
364                 fTree.setGrayed(element, false);
365             }
366             else
367                 fTree.setGrayChecked(element, false);
368             if (isExpandable(element))
369                 setSubtreeChecked(element, state, true);
370         }
371     }
372
373     private void updateParentState(Object JavaDoc child, boolean baseChildState) {
374         if (child == null)
375             return;
376         if (child instanceof IAdaptable) {
377             IResource resource= (IResource)((IAdaptable)child).getAdapter(IResource.class);
378             if (resource != null && !resource.isAccessible())
379                 return;
380         }
381         Object JavaDoc parent= fTreeContentProvider.getParent(child);
382         if (parent == null)
383             return;
384         
385         updateObjectState(parent, baseChildState);
386     }
387
388     private void updateObjectState(Object JavaDoc element, boolean baseChildState) {
389
390         boolean allSameState= true;
391         Object JavaDoc[] children= fTreeContentProvider.getChildren(element);
392
393         for (int i= children.length -1; i >= 0; i--) {
394             if (fTree.getChecked(children[i]) != baseChildState || fTree.getGrayed(children[i])) {
395                 allSameState= false;
396                 break;
397             }
398         }
399     
400         fTree.setGrayed(element, !allSameState);
401         fTree.setChecked(element, !allSameState || baseChildState);
402         
403         updateParentState(element, baseChildState);
404     }
405
406     private void initializeCheckedState() {
407
408         BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc() {
409             public void run() {
410                 Object JavaDoc[] elements;
411                 if (fWorkingSet == null) {
412                     // Use current part's selection for initialization
413
IWorkbenchPage page= JavaPlugin.getActivePage();
414                     if (page == null)
415                         return;
416                     
417                     IWorkbenchPart part= JavaPlugin.getActivePage().getActivePart();
418                     if (part == null)
419                         return;
420                     
421                     try {
422                         elements= SelectionConverter.getStructuredSelection(part).toArray();
423                         for (int i= 0; i < elements.length; i++) {
424                             if (elements[i] instanceof IResource) {
425                                 IJavaElement je= (IJavaElement)((IResource)elements[i]).getAdapter(IJavaElement.class);
426                                 if (je != null && je.exists() && je.getJavaProject().isOnClasspath((IResource)elements[i]))
427                                     elements[i]= je;
428                             }
429                         }
430                     } catch (JavaModelException e) {
431                         return;
432                     }
433                 }
434                 else
435                     elements= fWorkingSet.getElements();
436
437                 // Use closed project for elements in closed project
438
for (int i= 0; i < elements.length; i++) {
439                     Object JavaDoc element= elements[i];
440                     if (element instanceof IResource) {
441                         IProject project= ((IResource)element).getProject();
442                         if (!project.isAccessible())
443                             elements[i]= project;
444                     }
445                     if (element instanceof IJavaElement) {
446                         IJavaProject jProject= ((IJavaElement)element).getJavaProject();
447                         if (jProject != null && !jProject.getProject().isAccessible())
448                             elements[i]= jProject.getProject();
449                     }
450                 }
451
452                 fTree.setCheckedElements(elements);
453                 HashSet JavaDoc parents = new HashSet JavaDoc();
454                 for (int i= 0; i < elements.length; i++) {
455                     Object JavaDoc element= elements[i];
456                     if (isExpandable(element))
457                         setSubtreeChecked(element, true, true);
458                         
459                     if (element instanceof IAdaptable) {
460                         IResource resource= (IResource) ((IAdaptable)element).getAdapter(IResource.class);
461                         if (resource != null && !resource.isAccessible())
462                             continue;
463                     }
464                     Object JavaDoc parent= fTreeContentProvider.getParent(element);
465                     if (parent != null)
466                         parents.add(parent);
467                 }
468                 
469                 for (Iterator JavaDoc i = parents.iterator(); i.hasNext();)
470                     updateObjectState(i.next(), true);
471             }
472         });
473     }
474     
475     private boolean isExpandable(Object JavaDoc element) {
476         return (
477             element instanceof IJavaProject
478             ||
479             element instanceof IPackageFragmentRoot
480             ||
481             element instanceof IPackageFragment
482             ||
483             element instanceof IJavaModel
484             ||
485             element instanceof IContainer);
486     }
487 }
488
Popular Tags