KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
14
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspaceRoot;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31
32 import org.eclipse.jface.dialogs.StatusDialog;
33 import org.eclipse.jface.viewers.ILabelProvider;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.ViewerFilter;
36 import org.eclipse.jface.window.Window;
37
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
40 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
41 import org.eclipse.ui.model.WorkbenchContentProvider;
42 import org.eclipse.ui.model.WorkbenchLabelProvider;
43
44 import org.eclipse.ui.views.navigator.ResourceComparator;
45
46 import org.eclipse.jdt.internal.corext.util.Messages;
47
48 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
49 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
50 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
51 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
52 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
55 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
56 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
57 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
58
59 public class ExclusionInclusionEntryDialog extends StatusDialog {
60     
61     private StringButtonDialogField fExclusionPatternDialog;
62     private StatusInfo fExclusionPatternStatus;
63     
64     private IContainer fCurrSourceFolder;
65     private String JavaDoc fExclusionPattern;
66     private List JavaDoc fExistingPatterns;
67     private boolean fIsExclusion;
68         
69     public ExclusionInclusionEntryDialog(Shell parent, boolean isExclusion, String JavaDoc patternToEdit, List JavaDoc existingPatterns, CPListElement entryToEdit) {
70         super(parent);
71         fIsExclusion= isExclusion;
72         fExistingPatterns= existingPatterns;
73         String JavaDoc title, message;
74         if (isExclusion) {
75             if (patternToEdit == null) {
76                 title= NewWizardMessages.ExclusionInclusionEntryDialog_exclude_add_title;
77             } else {
78                 title= NewWizardMessages.ExclusionInclusionEntryDialog_exclude_edit_title;
79             }
80             message= Messages.format(NewWizardMessages.ExclusionInclusionEntryDialog_exclude_pattern_label, entryToEdit.getPath().makeRelative().toString());
81         } else {
82             if (patternToEdit == null) {
83                 title= NewWizardMessages.ExclusionInclusionEntryDialog_include_add_title;
84             } else {
85                 title= NewWizardMessages.ExclusionInclusionEntryDialog_include_edit_title;
86             }
87             message= Messages.format(NewWizardMessages.ExclusionInclusionEntryDialog_include_pattern_label, entryToEdit.getPath().makeRelative().toString());
88         }
89         setTitle(title);
90         if (patternToEdit != null) {
91             fExistingPatterns.remove(patternToEdit);
92         }
93         
94         
95         IWorkspaceRoot root= entryToEdit.getJavaProject().getProject().getWorkspace().getRoot();
96         IResource res= root.findMember(entryToEdit.getPath());
97         if (res instanceof IContainer) {
98             fCurrSourceFolder= (IContainer) res;
99         }
100         
101         fExclusionPatternStatus= new StatusInfo();
102         
103         ExclusionPatternAdapter adapter= new ExclusionPatternAdapter();
104         fExclusionPatternDialog= new StringButtonDialogField(adapter);
105         fExclusionPatternDialog.setLabelText(message);
106         fExclusionPatternDialog.setButtonLabel(NewWizardMessages.ExclusionInclusionEntryDialog_pattern_button);
107         fExclusionPatternDialog.setDialogFieldListener(adapter);
108         fExclusionPatternDialog.enableButton(fCurrSourceFolder != null);
109         
110         if (patternToEdit == null) {
111             fExclusionPatternDialog.setText(""); //$NON-NLS-1$
112
} else {
113             fExclusionPatternDialog.setText(patternToEdit.toString());
114         }
115     }
116     
117     
118     protected Control createDialogArea(Composite parent) {
119         Composite composite= (Composite)super.createDialogArea(parent);
120         
121         int widthHint= convertWidthInCharsToPixels(60);
122         
123         Composite inner= new Composite(composite, SWT.NONE);
124         GridLayout layout= new GridLayout();
125         layout.marginHeight= 0;
126         layout.marginWidth= 0;
127         layout.numColumns= 2;
128         inner.setLayout(layout);
129         
130         Label description= new Label(inner, SWT.WRAP);
131         
132         if (fIsExclusion) {
133             description.setText(NewWizardMessages.ExclusionInclusionEntryDialog_exclude_description);
134         } else {
135             description.setText(NewWizardMessages.ExclusionInclusionEntryDialog_include_description);
136         }
137         GridData gd= new GridData();
138         gd.horizontalSpan= 2;
139         gd.widthHint= convertWidthInCharsToPixels(80);
140         description.setLayoutData(gd);
141         
142         fExclusionPatternDialog.doFillIntoGrid(inner, 3);
143         
144         LayoutUtil.setWidthHint(fExclusionPatternDialog.getLabelControl(null), widthHint);
145         LayoutUtil.setHorizontalSpan(fExclusionPatternDialog.getLabelControl(null), 2);
146         
147         LayoutUtil.setWidthHint(fExclusionPatternDialog.getTextControl(null), widthHint);
148         LayoutUtil.setHorizontalGrabbing(fExclusionPatternDialog.getTextControl(null));
149                 
150         fExclusionPatternDialog.postSetFocusOnDialogField(parent.getDisplay());
151         applyDialogFont(composite);
152         return composite;
153     }
154
155         
156     // -------- ExclusionPatternAdapter --------
157

158     private class ExclusionPatternAdapter implements IDialogFieldListener, IStringButtonAdapter {
159         
160         // -------- IDialogFieldListener
161

162         public void dialogFieldChanged(DialogField field) {
163             doStatusLineUpdate();
164         }
165
166         public void changeControlPressed(DialogField field) {
167             doChangeControlPressed();
168         }
169     }
170     
171     protected void doChangeControlPressed() {
172         IPath pattern= chooseExclusionPattern();
173         if (pattern != null) {
174             fExclusionPatternDialog.setText(pattern.toString());
175         }
176     }
177
178     protected void doStatusLineUpdate() {
179         checkIfPatternValid();
180         updateStatus(fExclusionPatternStatus);
181     }
182     
183     protected void checkIfPatternValid() {
184         String JavaDoc pattern= fExclusionPatternDialog.getText().trim();
185         if (pattern.length() == 0) {
186             fExclusionPatternStatus.setError(NewWizardMessages.ExclusionInclusionEntryDialog_error_empty);
187             return;
188         }
189         IPath path= new Path(pattern);
190         if (path.isAbsolute() || path.getDevice() != null) {
191             fExclusionPatternStatus.setError(NewWizardMessages.ExclusionInclusionEntryDialog_error_notrelative);
192             return;
193         }
194         if (fExistingPatterns.contains(pattern)) {
195             fExclusionPatternStatus.setError(NewWizardMessages.ExclusionInclusionEntryDialog_error_exists);
196             return;
197         }
198         
199         fExclusionPattern= pattern;
200         fExclusionPatternStatus.setOK();
201     }
202     
203         
204     public String JavaDoc getExclusionPattern() {
205         return fExclusionPattern;
206     }
207         
208     /*
209      * @see org.eclipse.jface.window.Window#configureShell(Shell)
210      */

211     protected void configureShell(Shell newShell) {
212         super.configureShell(newShell);
213         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.EXCLUSION_PATTERN_DIALOG);
214     }
215     
216     // ---------- util method ------------
217

218     private IPath chooseExclusionPattern() {
219         String JavaDoc title, message;
220         if (fIsExclusion) {
221             title= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseExclusionPattern_title;
222             message= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseExclusionPattern_description;
223         } else {
224             title= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseInclusionPattern_title;
225             message= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseInclusionPattern_description;
226         }
227         IPath initialPath= new Path(fExclusionPatternDialog.getText());
228         
229         IPath[] res= chooseExclusionPattern(getShell(), fCurrSourceFolder, title, message, initialPath, false);
230         if (res == null) {
231             return null;
232         }
233         return res[0];
234     }
235     
236     public static IPath[] chooseExclusionPattern(Shell shell, IContainer currentSourceFolder, String JavaDoc title, String JavaDoc message, IPath initialPath, boolean multiSelection) {
237         Class JavaDoc[] acceptedClasses= new Class JavaDoc[] { IFolder.class, IFile.class };
238         ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, multiSelection);
239         ViewerFilter filter= new TypedViewerFilter(acceptedClasses);
240
241         
242         ILabelProvider lp= new WorkbenchLabelProvider();
243         ITreeContentProvider cp= new WorkbenchContentProvider();
244         
245         IResource initialElement= null;
246         if (initialPath != null) {
247             IContainer curr= currentSourceFolder;
248             int nSegments= initialPath.segmentCount();
249             for (int i= 0; i < nSegments; i++) {
250                 IResource elem= curr.findMember(initialPath.segment(i));
251                 if (elem != null) {
252                     initialElement= elem;
253                 }
254                 if (elem instanceof IContainer) {
255                     curr= (IContainer) elem;
256                 } else {
257                     break;
258                 }
259             }
260         }
261
262         ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(shell, lp, cp);
263         dialog.setTitle(title);
264         dialog.setValidator(validator);
265         dialog.setMessage(message);
266         dialog.addFilter(filter);
267         dialog.setInput(currentSourceFolder);
268         dialog.setInitialSelection(initialElement);
269         dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
270         dialog.setHelpAvailable(false);
271         
272         if (dialog.open() == Window.OK) {
273             Object JavaDoc[] objects= dialog.getResult();
274             int existingSegments= currentSourceFolder.getFullPath().segmentCount();
275             
276             IPath[] resArr= new IPath[objects.length];
277             for (int i= 0; i < objects.length; i++) {
278                 IResource currRes= (IResource) objects[i];
279                 IPath path= currRes.getFullPath().removeFirstSegments(existingSegments).makeRelative();
280                 if (currRes instanceof IContainer) {
281                     path= path.addTrailingSeparator();
282                 }
283                 resArr[i]= path;
284             }
285             return resArr;
286         }
287         return null;
288     }
289     
290
291
292 }
293
Popular Tags