KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Shell;
31
32 import org.eclipse.jface.dialogs.Dialog;
33 import org.eclipse.jface.resource.ImageDescriptor;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.jface.viewers.ViewerComparator;
36 import org.eclipse.jface.window.Window;
37
38 import org.eclipse.ui.PlatformUI;
39
40 import org.eclipse.jdt.core.IJavaModelStatus;
41 import org.eclipse.jdt.core.JavaConventions;
42
43 import org.eclipse.jdt.ui.wizards.NewElementWizardPage;
44
45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.JavaPluginImages;
48 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
49 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
50 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
51 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
52 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
55 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
56
57 public class SetFilterWizardPage extends NewElementWizardPage {
58     
59     private static final String JavaDoc PAGE_NAME= "SetFilterWizardPage"; //$NON-NLS-1$
60

61     private ListDialogField fInclusionPatternList;
62     private ListDialogField fExclusionPatternList;
63     private CPListElement fCurrElement;
64     private IProject fCurrProject;
65     
66     private IContainer fCurrSourceFolder;
67     
68     private static final int IDX_ADD= 0;
69     private static final int IDX_ADD_MULTIPLE= 1;
70     private static final int IDX_EDIT= 2;
71     private static final int IDX_REMOVE= 4;
72
73     private final ArrayList JavaDoc fExistingEntries;
74
75     private final IPath fOutputLocation;
76
77     public SetFilterWizardPage(CPListElement entryToEdit, ArrayList JavaDoc existingEntries, IPath outputLocation) {
78         super(PAGE_NAME);
79         fExistingEntries= existingEntries;
80         fOutputLocation= outputLocation;
81         
82         setTitle(NewWizardMessages.ExclusionInclusionDialog_title);
83         setDescription(NewWizardMessages.ExclusionInclusionDialog_description2);
84         
85         fCurrElement= entryToEdit;
86         fCurrProject= entryToEdit.getJavaProject().getProject();
87         IWorkspaceRoot root= fCurrProject.getWorkspace().getRoot();
88         IResource res= root.findMember(entryToEdit.getPath());
89         if (res instanceof IContainer) {
90             fCurrSourceFolder= (IContainer) res;
91         }
92         
93         String JavaDoc excLabel= NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_label;
94         ImageDescriptor excDescriptor= JavaPluginImages.DESC_OBJS_EXCLUSION_FILTER_ATTRIB;
95         String JavaDoc[] excButtonLabels= new String JavaDoc[] {
96                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add,
97                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add_multiple,
98                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_edit,
99                 null,
100                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_remove
101             };
102         
103         
104         String JavaDoc incLabel= NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_label;
105         ImageDescriptor incDescriptor= JavaPluginImages.DESC_OBJS_INCLUSION_FILTER_ATTRIB;
106         String JavaDoc[] incButtonLabels= new String JavaDoc[] {
107                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add,
108                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add_multiple,
109                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_edit,
110                 null,
111                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_remove
112             };
113         
114         fExclusionPatternList= createListContents(entryToEdit, CPListElement.EXCLUSION, excLabel, excDescriptor, excButtonLabels);
115         fInclusionPatternList= createListContents(entryToEdit, CPListElement.INCLUSION, incLabel, incDescriptor, incButtonLabels);
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
120      */

121     public void createControl(Composite parent) {
122         Composite inner= new Composite(parent, SWT.NONE);
123         inner.setFont(parent.getFont());
124         
125         GridLayout layout= new GridLayout();
126         layout.marginHeight= 0;
127         layout.marginWidth= 0;
128         layout.numColumns= 2;
129         inner.setLayout(layout);
130         inner.setLayoutData(new GridData(GridData.FILL_BOTH));
131         
132         fInclusionPatternList.doFillIntoGrid(inner, 3);
133         LayoutUtil.setHorizontalSpan(fInclusionPatternList.getLabelControl(null), 2);
134         LayoutUtil.setHorizontalGrabbing(fInclusionPatternList.getListControl(null));
135         
136         fExclusionPatternList.doFillIntoGrid(inner, 3);
137         LayoutUtil.setHorizontalSpan(fExclusionPatternList.getLabelControl(null), 2);
138         LayoutUtil.setHorizontalGrabbing(fExclusionPatternList.getListControl(null));
139         
140         setControl(inner);
141         Dialog.applyDialogFont(inner);
142         PlatformUI.getWorkbench().getHelpSystem().setHelp(inner, IJavaHelpContextIds.INCLUSION_EXCLUSION_WIZARD_PAGE);
143     }
144     
145     
146     private static class ExclusionInclusionLabelProvider extends LabelProvider {
147         
148         private Image fElementImage;
149
150         public ExclusionInclusionLabelProvider(ImageDescriptor descriptor) {
151             ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry();
152             fElementImage= registry.get(descriptor);
153         }
154         
155         public Image getImage(Object JavaDoc element) {
156             return fElementImage;
157         }
158
159         public String JavaDoc getText(Object JavaDoc element) {
160             return (String JavaDoc) element;
161         }
162
163     }
164     
165     private ListDialogField createListContents(CPListElement entryToEdit, String JavaDoc key, String JavaDoc label, ImageDescriptor descriptor, String JavaDoc[] buttonLabels) {
166         ExclusionPatternAdapter adapter= new ExclusionPatternAdapter();
167         
168         ListDialogField patternList= new ListDialogField(adapter, buttonLabels, new ExclusionInclusionLabelProvider(descriptor));
169         patternList.setDialogFieldListener(adapter);
170         patternList.setLabelText(label);
171         patternList.enableButton(IDX_EDIT, false);
172     
173         IPath[] pattern= (IPath[]) entryToEdit.getAttribute(key);
174         
175         ArrayList JavaDoc elements= new ArrayList JavaDoc(pattern.length);
176         for (int i= 0; i < pattern.length; i++) {
177             String JavaDoc patternName= pattern[i].toString();
178             if (patternName.length() > 0)
179                 elements.add(patternName);
180         }
181         patternList.setElements(elements);
182         patternList.selectFirstElement();
183         patternList.enableButton(IDX_ADD_MULTIPLE, fCurrSourceFolder != null);
184         patternList.setViewerComparator(new ViewerComparator());
185         return patternList;
186     }
187     
188     protected void doCustomButtonPressed(ListDialogField field, int index) {
189         if (index == IDX_ADD) {
190             addEntry(field);
191         } else if (index == IDX_EDIT) {
192             editEntry(field);
193         } else if (index == IDX_ADD_MULTIPLE) {
194             addMultipleEntries(field);
195         } else if (index == IDX_REMOVE) {
196             field.removeElements(field.getSelectedElements());
197         }
198         updateStatus();
199     }
200     
201     private void updateStatus() {
202         fCurrElement.setAttribute(CPListElement.INCLUSION, getInclusionPattern());
203         fCurrElement.setAttribute(CPListElement.EXCLUSION, getExclusionPattern());
204         IJavaModelStatus status= JavaConventions.validateClasspath(fCurrElement.getJavaProject(), CPListElement.convertToClasspathEntries(fExistingEntries), fOutputLocation);
205         if (!status.isOK()) {
206             StatusInfo statusInfo= new StatusInfo();
207             statusInfo.setError(status.getMessage());
208             updateStatus(statusInfo);
209         } else {
210             StatusInfo statusInfo= new StatusInfo();
211             statusInfo.setOK();
212             updateStatus(statusInfo);
213         }
214     }
215
216     protected void doDoubleClicked(ListDialogField field) {
217         editEntry(field);
218         updateStatus();
219     }
220     
221     protected void doSelectionChanged(ListDialogField field) {
222         List JavaDoc selected= field.getSelectedElements();
223         field.enableButton(IDX_EDIT, canEdit(selected));
224     }
225             
226     private boolean canEdit(List JavaDoc selected) {
227         return selected.size() == 1;
228     }
229     
230     private void editEntry(ListDialogField field) {
231         List JavaDoc selElements= field.getSelectedElements();
232         if (selElements.size() != 1) {
233             return;
234         }
235         List JavaDoc existing= field.getElements();
236         String JavaDoc entry= (String JavaDoc) selElements.get(0);
237         ExclusionInclusionEntryDialog dialog= new ExclusionInclusionEntryDialog(getShell(), isExclusion(field), entry, existing, fCurrElement);
238         if (dialog.open() == Window.OK) {
239             field.replaceElement(entry, dialog.getExclusionPattern());
240         }
241     }
242     
243     private boolean isExclusion(ListDialogField field) {
244         return field == fExclusionPatternList;
245     }
246
247
248     private void addEntry(ListDialogField field) {
249         List JavaDoc existing= field.getElements();
250         ExclusionInclusionEntryDialog dialog= new ExclusionInclusionEntryDialog(getShell(), isExclusion(field), null, existing, fCurrElement);
251         if (dialog.open() == Window.OK) {
252             field.addElement(dialog.getExclusionPattern());
253         }
254     }
255     
256     // -------- ExclusionPatternAdapter --------
257

258     private class ExclusionPatternAdapter implements IListAdapter, IDialogFieldListener {
259         /**
260          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#customButtonPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField, int)
261          */

262         public void customButtonPressed(ListDialogField field, int index) {
263             doCustomButtonPressed(field, index);
264         }
265
266         /**
267          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#selectionChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
268          */

269         public void selectionChanged(ListDialogField field) {
270             doSelectionChanged(field);
271         }
272         /**
273          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#doubleClicked(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
274          */

275         public void doubleClicked(ListDialogField field) {
276             doDoubleClicked(field);
277         }
278
279         /**
280          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
281          */

282         public void dialogFieldChanged(DialogField field) {
283         }
284         
285     }
286     
287     protected void doStatusLineUpdate() {
288     }
289     
290     protected void checkIfPatternValid() {
291     }
292     
293     
294     private IPath[] getPattern(ListDialogField field) {
295         Object JavaDoc[] arr= field.getElements().toArray();
296         Arrays.sort(arr);
297         IPath[] res= new IPath[arr.length];
298         for (int i= 0; i < res.length; i++) {
299             res[i]= new Path((String JavaDoc) arr[i]);
300         }
301         return res;
302     }
303     
304     public IPath[] getExclusionPattern() {
305         return getPattern(fExclusionPatternList);
306     }
307     
308     public IPath[] getInclusionPattern() {
309         return getPattern(fInclusionPatternList);
310     }
311         
312     /*
313      * @see org.eclipse.jface.window.Window#configureShell(Shell)
314      */

315     protected void configureShell(Shell newShell) {
316         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.EXCLUSION_PATTERN_DIALOG);
317     }
318     
319     private void addMultipleEntries(ListDialogField field) {
320         String JavaDoc title, message;
321         if (isExclusion(field)) {
322             title= NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_title;
323             message= NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_description;
324         } else {
325             title= NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_title;
326             message= NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_description;
327         }
328         
329         IPath[] res= ExclusionInclusionEntryDialog.chooseExclusionPattern(getShell(), fCurrSourceFolder, title, message, null, true);
330         if (res != null) {
331             for (int i= 0; i < res.length; i++) {
332                 field.addElement(res[i].toString());
333             }
334         }
335     }
336
337 }
338
Popular Tags