]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/ExclusionInclusionDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / ExclusionInclusionDialog.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  *     Matt Chapman, mpchapman@gmail.com - 89977 Make JDT .java agnostic
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Shell;
25
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.Path;
28
29 import org.eclipse.core.resources.IContainer;
30 import org.eclipse.core.resources.IProject;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.resources.IWorkspaceRoot;
33
34 import org.eclipse.jface.dialogs.StatusDialog;
35 import org.eclipse.jface.resource.ImageDescriptor;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.viewers.ViewerComparator;
38 import org.eclipse.jface.window.Window;
39
40 import org.eclipse.ui.PlatformUI;
41
42 import org.eclipse.jdt.internal.corext.util.Messages;
43
44 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
45 import org.eclipse.jdt.internal.ui.JavaPlugin;
46 import org.eclipse.jdt.internal.ui.JavaPluginImages;
47 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
48 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
49 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
50 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
51 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
52 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
54 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
55
56 public class ExclusionInclusionDialog extends StatusDialog {
57
58         private static class ExclusionInclusionLabelProvider extends LabelProvider {
59
60                 private Image fElementImage;
61
62                 public ExclusionInclusionLabelProvider(ImageDescriptor descriptor) {
63                         ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry();
64                         fElementImage= registry.get(descriptor);
65                 }
66
67                 @Override
68                 public Image getImage(Object element) {
69                         return fElementImage;
70                 }
71
72                 @Override
73                 public String getText(Object element) {
74                         return BasicElementLabels.getFilePattern((String) element);
75                 }
76
77         }
78
79         private ListDialogField<String> fInclusionPatternList;
80         private ListDialogField<String> fExclusionPatternList;
81         private CPListElement fCurrElement;
82         private IProject fCurrProject;
83
84         private IContainer fCurrSourceFolder;
85
86         private static final int IDX_ADD= 0;
87         private static final int IDX_ADD_MULTIPLE= 1;
88         private static final int IDX_EDIT= 2;
89         private static final int IDX_REMOVE= 4;
90
91
92         public ExclusionInclusionDialog(Shell parent, CPListElement entryToEdit, boolean focusOnExcluded) {
93                 super(parent);
94
95                 fCurrElement= entryToEdit;
96
97                 setTitle(NewWizardMessages.ExclusionInclusionDialog_title);
98
99                 fCurrProject= entryToEdit.getJavaProject().getProject();
100                 IWorkspaceRoot root= fCurrProject.getWorkspace().getRoot();
101                 IResource res= root.findMember(entryToEdit.getPath());
102                 if (res instanceof IContainer) {
103                         fCurrSourceFolder= (IContainer) res;
104                 }
105
106                 String excLabel= NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_label;
107                 ImageDescriptor excDescriptor= JavaPluginImages.DESC_OBJS_EXCLUSION_FILTER_ATTRIB;
108                 String[] excButtonLabels= new String[] {
109                                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add,
110                                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add_multiple,
111                                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_edit,
112                                 null,
113                                 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_remove
114                         };
115
116
117                 String incLabel= NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_label;
118                 ImageDescriptor incDescriptor= JavaPluginImages.DESC_OBJS_INCLUSION_FILTER_ATTRIB;
119                 String[] incButtonLabels= new String[] {
120                                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add,
121                                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add_multiple,
122                                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_edit,
123                                 null,
124                                 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_remove
125                         };
126
127                 fExclusionPatternList= createListContents(entryToEdit, CPListElement.EXCLUSION, excLabel, excDescriptor, excButtonLabels);
128                 fInclusionPatternList= createListContents(entryToEdit, CPListElement.INCLUSION, incLabel, incDescriptor, incButtonLabels);
129                 if (focusOnExcluded) {
130                         fExclusionPatternList.postSetFocusOnDialogField(parent.getDisplay());
131                 } else {
132                         fInclusionPatternList.postSetFocusOnDialogField(parent.getDisplay());
133                 }
134         }
135
136         /*
137          * @see org.eclipse.jface.dialogs.Dialog#isResizable()
138          * @since 3.4
139          */
140         @Override
141         protected boolean isResizable() {
142                 return true;
143         }
144
145         private ListDialogField<String> createListContents(CPListElement entryToEdit, String key, String label, ImageDescriptor descriptor, String[] buttonLabels) {
146                 ExclusionPatternAdapter adapter= new ExclusionPatternAdapter();
147
148                 ListDialogField<String> patternList= new ListDialogField<String>(adapter, buttonLabels, new ExclusionInclusionLabelProvider(descriptor));
149                 patternList.setDialogFieldListener(adapter);
150                 patternList.setLabelText(label);
151                 patternList.setRemoveButtonIndex(IDX_REMOVE);
152                 patternList.enableButton(IDX_EDIT, false);
153
154                 IPath[] pattern= (IPath[]) entryToEdit.getAttribute(key);
155
156                 ArrayList<String> elements= new ArrayList<String>(pattern.length);
157                 for (int i= 0; i < pattern.length; i++) {
158                         elements.add(pattern[i].toString());
159                 }
160                 patternList.setElements(elements);
161                 patternList.selectFirstElement();
162                 patternList.enableButton(IDX_ADD_MULTIPLE, fCurrSourceFolder != null);
163                 patternList.setViewerComparator(new ViewerComparator());
164                 return patternList;
165         }
166
167
168         @Override
169         protected Control createDialogArea(Composite parent) {
170                 Composite composite= (Composite) super.createDialogArea(parent);
171
172                 Composite inner= new Composite(composite, SWT.NONE);
173                 inner.setFont(parent.getFont());
174
175                 GridLayout layout= new GridLayout();
176                 layout.marginHeight= 0;
177                 layout.marginWidth= 0;
178                 layout.numColumns= 2;
179                 inner.setLayout(layout);
180                 inner.setLayoutData(new GridData(GridData.FILL_BOTH));
181
182                 DialogField labelField= new DialogField();
183                 labelField.setLabelText(Messages.format(NewWizardMessages.ExclusionInclusionDialog_description, BasicElementLabels.getPathLabel(fCurrElement.getPath(), false)));
184                 labelField.doFillIntoGrid(inner, 2);
185
186                 fInclusionPatternList.doFillIntoGrid(inner, 3);
187                 LayoutUtil.setHorizontalSpan(fInclusionPatternList.getLabelControl(null), 2);
188                 LayoutUtil.setHorizontalGrabbing(fInclusionPatternList.getListControl(null));
189
190                 fExclusionPatternList.doFillIntoGrid(inner, 3);
191                 LayoutUtil.setHorizontalSpan(fExclusionPatternList.getLabelControl(null), 2);
192                 LayoutUtil.setHorizontalGrabbing(fExclusionPatternList.getListControl(null));
193
194                 applyDialogFont(composite);
195                 return composite;
196         }
197
198         protected void doCustomButtonPressed(ListDialogField<String> field, int index) {
199                 if (index == IDX_ADD) {
200                         addEntry(field);
201                 } else if (index == IDX_EDIT) {
202                         editEntry(field);
203                 } else if (index == IDX_ADD_MULTIPLE) {
204                         addMultipleEntries(field);
205                 }
206         }
207
208         protected void doDoubleClicked(ListDialogField<String> field) {
209                 editEntry(field);
210         }
211
212         protected void doSelectionChanged(ListDialogField<String> field) {
213                 List<String> selected= field.getSelectedElements();
214                 field.enableButton(IDX_EDIT, canEdit(selected));
215         }
216
217         private boolean canEdit(List<String> selected) {
218                 return selected.size() == 1;
219         }
220
221         private void editEntry(ListDialogField<String> field) {
222                 List<String> selElements= field.getSelectedElements();
223                 if (selElements.size() != 1) {
224                         return;
225                 }
226                 List<String> existing= field.getElements();
227                 String entry= selElements.get(0);
228                 ExclusionInclusionEntryDialog dialog= new ExclusionInclusionEntryDialog(getShell(), isExclusion(field), entry, existing, fCurrElement);
229                 if (dialog.open() == Window.OK) {
230                         field.replaceElement(entry, dialog.getExclusionPattern());
231                 }
232         }
233
234         private boolean isExclusion(ListDialogField<String> field) {
235                 return field == fExclusionPatternList;
236         }
237
238
239         private void addEntry(ListDialogField<String> field) {
240                 List<String> existing= field.getElements();
241                 ExclusionInclusionEntryDialog dialog= new ExclusionInclusionEntryDialog(getShell(), isExclusion(field), null, existing, fCurrElement);
242                 if (dialog.open() == Window.OK) {
243                         field.addElement(dialog.getExclusionPattern());
244                 }
245         }
246
247
248
249         // -------- ExclusionPatternAdapter --------
250
251         private class ExclusionPatternAdapter implements IListAdapter<String>, IDialogFieldListener {
252                 /**
253                  * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#customButtonPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField, int)
254                  */
255                 public void customButtonPressed(ListDialogField<String> field, int index) {
256                         doCustomButtonPressed(field, index);
257                 }
258
259                 /**
260                  * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#selectionChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
261                  */
262                 public void selectionChanged(ListDialogField<String> field) {
263                         doSelectionChanged(field);
264                 }
265                 /**
266                  * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#doubleClicked(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
267                  */
268                 public void doubleClicked(ListDialogField<String> field) {
269                         doDoubleClicked(field);
270                 }
271
272                 /**
273                  * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
274                  */
275                 public void dialogFieldChanged(DialogField field) {
276                 }
277
278         }
279
280         protected void doStatusLineUpdate() {
281         }
282
283         protected void checkIfPatternValid() {
284         }
285
286
287         private IPath[] getPattern(ListDialogField<?> field) {
288                 Object[] arr= field.getElements().toArray();
289                 Arrays.sort(arr);
290                 IPath[] res= new IPath[arr.length];
291                 for (int i= 0; i < res.length; i++) {
292                         res[i]= new Path((String) arr[i]);
293                 }
294                 return res;
295         }
296
297         public IPath[] getExclusionPattern() {
298                 return getPattern(fExclusionPatternList);
299         }
300
301         public IPath[] getInclusionPattern() {
302                 return getPattern(fInclusionPatternList);
303         }
304
305         /*
306          * @see org.eclipse.jface.window.Window#configureShell(Shell)
307          */
308         @Override
309         protected void configureShell(Shell newShell) {
310                 super.configureShell(newShell);
311                 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.EXCLUSION_PATTERN_DIALOG);
312         }
313
314         private void addMultipleEntries(ListDialogField<String> field) {
315                 String title, message;
316                 if (isExclusion(field)) {
317                         title= NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_title;
318                         message= NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_description;
319                 } else {
320                         title= NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_title;
321                         message= NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_description;
322                 }
323
324                 IPath[] res= ExclusionInclusionEntryDialog.chooseExclusionPattern(getShell(), fCurrSourceFolder, title, message, null, true);
325                 if (res != null) {
326                         for (int i= 0; i < res.length; i++) {
327                                 field.addElement(res[i].toString());
328                         }
329                 }
330         }
331 }