]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/ExclusionInclusionEntryDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / ExclusionInclusionEntryDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.Path;
25
26 import org.eclipse.core.resources.IContainer;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IFolder;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.resources.IWorkspaceRoot;
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 import org.eclipse.ui.views.navigator.ResourceComparator;
44
45 import org.eclipse.jdt.internal.corext.util.Messages;
46
47 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
48 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
49 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
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.StringButtonDialogField;
57
58 public class ExclusionInclusionEntryDialog extends StatusDialog {
59
60         public StringButtonDialogField fExclusionPatternDialog;
61         public StatusInfo fExclusionPatternStatus;
62
63         public IContainer fCurrSourceFolder;
64         public String fExclusionPattern;
65         public List<String> fExistingPatterns;
66         private boolean fIsExclusion;
67
68         public ExclusionInclusionEntryDialog(Shell parent, boolean isExclusion, String patternToEdit, List<String> existingPatterns, CPListElement entryToEdit) {
69                 super(parent);
70                 fIsExclusion= isExclusion;
71                 fExistingPatterns= existingPatterns;
72                 String title, message;
73                 if (isExclusion) {
74                         if (patternToEdit == null) {
75                                 title= NewWizardMessages.ExclusionInclusionEntryDialog_exclude_add_title;
76                         } else {
77                                 title= NewWizardMessages.ExclusionInclusionEntryDialog_exclude_edit_title;
78                         }
79                         message= Messages.format(NewWizardMessages.ExclusionInclusionEntryDialog_exclude_pattern_label, BasicElementLabels.getPathLabel(entryToEdit.getPath(), false));
80                 } else {
81                         if (patternToEdit == null) {
82                                 title= NewWizardMessages.ExclusionInclusionEntryDialog_include_add_title;
83                         } else {
84                                 title= NewWizardMessages.ExclusionInclusionEntryDialog_include_edit_title;
85                         }
86                         message= Messages.format(NewWizardMessages.ExclusionInclusionEntryDialog_include_pattern_label, BasicElementLabels.getPathLabel(entryToEdit.getPath(), false));
87                 }
88                 setTitle(title);
89                 if (patternToEdit != null) {
90                         fExistingPatterns.remove(patternToEdit);
91                 }
92
93
94                 IWorkspaceRoot root= entryToEdit.getJavaProject().getProject().getWorkspace().getRoot();
95                 IResource res= root.findMember(entryToEdit.getPath());
96                 if (res instanceof IContainer) {
97                         fCurrSourceFolder= (IContainer) res;
98                 }
99
100                 fExclusionPatternStatus= new StatusInfo();
101
102                 ExclusionPatternAdapter adapter= new ExclusionPatternAdapter();
103                 fExclusionPatternDialog= new StringButtonDialogField(adapter);
104                 fExclusionPatternDialog.generated_3523998976432775963(this, patternToEdit, message, adapter);
105         }
106
107
108         @Override
109         protected Control createDialogArea(Composite parent) {
110                 Composite composite= (Composite)super.createDialogArea(parent);
111
112                 int widthHint= convertWidthInCharsToPixels(60);
113
114                 Composite inner= new Composite(composite, SWT.NONE);
115                 GridLayout layout= new GridLayout();
116                 layout.marginHeight= 0;
117                 layout.marginWidth= 0;
118                 layout.numColumns= 2;
119                 inner.setLayout(layout);
120
121                 Label description= new Label(inner, SWT.WRAP);
122
123                 if (fIsExclusion) {
124                         description.setText(NewWizardMessages.ExclusionInclusionEntryDialog_exclude_description);
125                 } else {
126                         description.setText(NewWizardMessages.ExclusionInclusionEntryDialog_include_description);
127                 }
128                 GridData gd= new GridData();
129                 gd.horizontalSpan= 2;
130                 gd.widthHint= convertWidthInCharsToPixels(80);
131                 return fExclusionPatternDialog.generated_2028777985630395556(parent, composite, widthHint, inner, description, gd);
132         }
133
134
135         
136
137
138         // -------- ExclusionPatternAdapter --------
139
140         public class ExclusionPatternAdapter implements IDialogFieldListener, IStringButtonAdapter {
141
142                 // -------- IDialogFieldListener
143
144                 public void dialogFieldChanged(DialogField field) {
145                         doStatusLineUpdate();
146                 }
147
148                 public void changeControlPressed(DialogField field) {
149                         doChangeControlPressed();
150                 }
151         }
152
153         protected void doChangeControlPressed() {
154                 IPath pattern= chooseExclusionPattern();
155                 if (pattern != null) {
156                         fExclusionPatternDialog.setText(pattern.toString());
157                 }
158         }
159
160         protected void doStatusLineUpdate() {
161                 checkIfPatternValid();
162                 updateStatus(fExclusionPatternStatus);
163         }
164
165         protected void checkIfPatternValid() {
166                 String pattern= fExclusionPatternDialog.getText().trim();
167                 fExclusionPatternStatus.generated_5016018090053794507(this, pattern);
168         }
169
170
171         public String getExclusionPattern() {
172                 return fExclusionPattern;
173         }
174
175         /*
176          * @see org.eclipse.jface.window.Window#configureShell(Shell)
177          */
178         @Override
179         protected void configureShell(Shell newShell) {
180                 super.configureShell(newShell);
181                 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.EXCLUSION_PATTERN_DIALOG);
182         }
183
184         // ---------- util method ------------
185
186         private IPath chooseExclusionPattern() {
187                 String title, message;
188                 if (fIsExclusion) {
189                         title= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseExclusionPattern_title;
190                         message= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseExclusionPattern_description;
191                 } else {
192                         title= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseInclusionPattern_title;
193                         message= NewWizardMessages.ExclusionInclusionEntryDialog_ChooseInclusionPattern_description;
194                 }
195                 IPath initialPath= new Path(fExclusionPatternDialog.getText());
196
197                 IPath[] res= chooseExclusionPattern(getShell(), fCurrSourceFolder, title, message, initialPath, false);
198                 if (res == null) {
199                         return null;
200                 }
201                 return res[0];
202         }
203
204         public static IPath[] chooseExclusionPattern(Shell shell, IContainer currentSourceFolder, String title, String message, IPath initialPath, boolean multiSelection) {
205                 Class<?>[] acceptedClasses= new Class[] { IFolder.class, IFile.class };
206                 ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, multiSelection);
207                 ViewerFilter filter= new TypedViewerFilter(acceptedClasses);
208
209
210                 ILabelProvider lp= new WorkbenchLabelProvider();
211                 ITreeContentProvider cp= new WorkbenchContentProvider();
212
213                 IResource initialElement= null;
214                 if (initialPath != null) {
215                         IContainer curr= currentSourceFolder;
216                         int nSegments= initialPath.segmentCount();
217                         for (int i= 0; i < nSegments; i++) {
218                                 IResource elem= curr.findMember(initialPath.segment(i));
219                                 if (elem != null) {
220                                         initialElement= elem;
221                                 }
222                                 if (elem instanceof IContainer) {
223                                         curr= (IContainer) elem;
224                                 } else {
225                                         break;
226                                 }
227                         }
228                 }
229
230                 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(shell, lp, cp);
231                 dialog.setTitle(title);
232                 dialog.setValidator(validator);
233                 dialog.setMessage(message);
234                 dialog.addFilter(filter);
235                 dialog.setInput(currentSourceFolder);
236                 dialog.setInitialSelection(initialElement);
237                 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
238                 dialog.setHelpAvailable(false);
239
240                 if (dialog.open() == Window.OK) {
241                         Object[] objects= dialog.getResult();
242                         int existingSegments= currentSourceFolder.getFullPath().segmentCount();
243
244                         IPath[] resArr= new IPath[objects.length];
245                         for (int i= 0; i < objects.length; i++) {
246                                 IResource currRes= (IResource) objects[i];
247                                 IPath path= currRes.getFullPath().removeFirstSegments(existingSegments).makeRelative();
248                                 if (currRes instanceof IContainer) {
249                                         path= path.addTrailingSeparator();
250                                 }
251                                 resArr[i]= path;
252                         }
253                         return resArr;
254                 }
255                 return null;
256         }
257
258
259
260 }