]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/OutputLocationDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / OutputLocationDialog.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.ArrayList;
14 import java.util.List;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
24
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IStatus;
28
29 import org.eclipse.core.resources.IContainer;
30 import org.eclipse.core.resources.IFolder;
31 import org.eclipse.core.resources.IProject;
32 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.resources.IWorkspaceRoot;
34
35 import org.eclipse.jface.dialogs.StatusDialog;
36 import org.eclipse.jface.viewers.ILabelProvider;
37 import org.eclipse.jface.viewers.ITreeContentProvider;
38 import org.eclipse.jface.viewers.ViewerFilter;
39 import org.eclipse.jface.window.Window;
40
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
43 import org.eclipse.ui.model.WorkbenchContentProvider;
44 import org.eclipse.ui.model.WorkbenchLabelProvider;
45 import org.eclipse.ui.views.navigator.ResourceComparator;
46
47 import org.eclipse.jdt.core.IJavaProject;
48
49 import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject;
50 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
51 import org.eclipse.jdt.internal.corext.util.Messages;
52
53 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
54 import org.eclipse.jdt.internal.ui.JavaPlugin;
55 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
56 import org.eclipse.jdt.internal.ui.util.SWTUtil;
57 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
58 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
59 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
60 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
61 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
64 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
65 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
66
67 public class OutputLocationDialog extends StatusDialog {
68
69         private StringButtonDialogField fContainerDialogField;
70         private SelectionButtonDialogField fUseDefault;
71         private SelectionButtonDialogField fUseSpecific;
72         private IStatus fContainerFieldStatus;
73
74         private IPath fOutputLocation;
75         private final IProject fCurrProject;
76         private final CPListElement fEntryToEdit;
77         private final boolean fAllowInvalidClasspath;
78         private CPJavaProject fCPJavaProject;
79
80         public OutputLocationDialog(Shell parent, CPListElement entryToEdit, List<CPListElement> classPathList, IPath defaultOutputFolder, boolean allowInvalidClasspath) {
81                 super(parent);
82                 fEntryToEdit= entryToEdit;
83                 fAllowInvalidClasspath= allowInvalidClasspath;
84                 setTitle(NewWizardMessages.OutputLocationDialog_title);
85                 fContainerFieldStatus= new StatusInfo();
86
87                 OutputLocationAdapter adapter= new OutputLocationAdapter();
88
89                 fUseDefault= new SelectionButtonDialogField(SWT.RADIO);
90                 fUseDefault.setLabelText(Messages.format(NewWizardMessages.OutputLocationDialog_usedefault_label, BasicElementLabels.getPathLabel(defaultOutputFolder, false)));
91                 fUseDefault.setDialogFieldListener(adapter);
92
93                 String label= Messages.format(NewWizardMessages.OutputLocationDialog_usespecific_label, BasicElementLabels.getResourceName(entryToEdit.getPath().segment(0)));
94                 fUseSpecific= new SelectionButtonDialogField(SWT.RADIO);
95                 fUseSpecific.setLabelText(label);
96                 fUseSpecific.setDialogFieldListener(adapter);
97
98                 fContainerDialogField= new StringButtonDialogField(adapter);
99                 fContainerDialogField.setButtonLabel(NewWizardMessages.OutputLocationDialog_location_button);
100                 fContainerDialogField.setDialogFieldListener(adapter);
101
102                 fUseSpecific.attachDialogField(fContainerDialogField);
103
104                 IJavaProject javaProject= entryToEdit.getJavaProject();
105                 fCurrProject= javaProject.getProject();
106                 fCPJavaProject= new CPJavaProject(javaProject, classPathList, defaultOutputFolder);
107
108                 IPath outputLocation= (IPath) entryToEdit.getAttribute(CPListElement.OUTPUT);
109                 if (outputLocation == null) {
110                         fUseDefault.setSelection(true);
111                 } else {
112                         fUseSpecific.setSelection(true);
113                         fContainerDialogField.setText(outputLocation.removeFirstSegments(1).makeRelative().toString());
114                 }
115         }
116
117         @Override
118         protected Control createDialogArea(Composite parent) {
119                 Composite composite= (Composite)super.createDialogArea(parent);
120
121                 int widthHint= convertWidthInCharsToPixels(70);
122                 int indent= convertWidthInCharsToPixels(4);
123
124                 Composite inner= new Composite(composite, SWT.NONE);
125                 GridLayout layout= new GridLayout();
126                 layout.marginHeight= 0;
127                 layout.marginWidth= 0;
128                 layout.numColumns= 2;
129                 inner.setLayout(layout);
130
131                 fUseDefault.doFillIntoGrid(inner, 2);
132                 fUseSpecific.doFillIntoGrid(inner, 2);
133
134                 Text textControl= fContainerDialogField.getTextControl(inner);
135                 GridData textData= new GridData();
136                 textData.widthHint= widthHint;
137                 textData.grabExcessHorizontalSpace= true;
138                 textData.horizontalIndent= indent;
139                 textControl.setLayoutData(textData);
140
141                 Button buttonControl= fContainerDialogField.getChangeControl(inner);
142                 GridData buttonData= new GridData();
143                 buttonData.widthHint= SWTUtil.getButtonWidthHint(buttonControl);
144                 buttonControl.setLayoutData(buttonData);
145
146                 applyDialogFont(composite);
147                 return composite;
148         }
149
150
151         // -------- OutputLocationAdapter --------
152
153         private class OutputLocationAdapter implements IDialogFieldListener, IStringButtonAdapter {
154
155                 // -------- IDialogFieldListener
156
157                 public void dialogFieldChanged(DialogField field) {
158                         doStatusLineUpdate();
159                 }
160
161                 public void changeControlPressed(DialogField field) {
162                         doChangeControlPressed();
163                 }
164         }
165
166         protected void doChangeControlPressed() {
167                 IContainer container= chooseOutputLocation();
168                 if (container != null) {
169                         fContainerDialogField.setText(container.getProjectRelativePath().toString());
170                 }
171         }
172
173         protected void doStatusLineUpdate() {
174                 checkIfPathValid();
175                 updateStatus(fContainerFieldStatus);
176         }
177
178         protected void checkIfPathValid() {
179                 fOutputLocation= null;
180                 fContainerFieldStatus= StatusInfo.OK_STATUS;
181
182                 if (fUseDefault.isSelected()) {
183                         return;
184                 }
185
186                 String pathStr= fContainerDialogField.getText();
187                 if (pathStr.length() == 0) {
188                         fContainerFieldStatus= new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
189                         return;
190                 }
191
192                 IPath projectPath= fCPJavaProject.getJavaProject().getProject().getFullPath();
193                 IPath outputPath= projectPath.append(pathStr);
194
195                 try {
196                 fContainerFieldStatus= ClasspathModifier.checkSetOutputLocationPrecondition(fEntryToEdit, outputPath, fAllowInvalidClasspath, fCPJavaProject);
197                 if (fContainerFieldStatus.getSeverity() != IStatus.ERROR) {
198                         fOutputLocation= outputPath;
199                 }
200         } catch (CoreException e) {
201                 JavaPlugin.log(e);
202         }
203         }
204
205         public IPath getOutputLocation() {
206                 return fOutputLocation;
207         }
208
209         /*
210          * @see org.eclipse.jface.window.Window#configureShell(Shell)
211          */
212         @Override
213         protected void configureShell(Shell newShell) {
214                 super.configureShell(newShell);
215                 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.OUTPUT_LOCATION_DIALOG);
216         }
217
218         // ---------- util method ------------
219
220         private IContainer chooseOutputLocation() {
221                 IWorkspaceRoot root= fCurrProject.getWorkspace().getRoot();
222                 final Class<?>[] acceptedClasses= new Class[] { IProject.class, IFolder.class };
223                 IProject[] allProjects= root.getProjects();
224                 ArrayList<IProject> rejectedElements= new ArrayList<IProject>(allProjects.length);
225                 for (int i= 0; i < allProjects.length; i++) {
226                         if (!allProjects[i].equals(fCurrProject)) {
227                                 rejectedElements.add(allProjects[i]);
228                         }
229                 }
230                 ViewerFilter filter= new TypedViewerFilter(acceptedClasses, rejectedElements.toArray());
231
232                 ILabelProvider lp= new WorkbenchLabelProvider();
233                 ITreeContentProvider cp= new WorkbenchContentProvider();
234
235                 IResource initSelection= null;
236                 if (fOutputLocation != null) {
237                         initSelection= root.findMember(fOutputLocation);
238                 }
239
240                 FolderSelectionDialog dialog= new FolderSelectionDialog(getShell(), lp, cp);
241                 dialog.setTitle(NewWizardMessages.OutputLocationDialog_ChooseOutputFolder_title);
242
243         dialog.setValidator(new ISelectionStatusValidator() {
244             ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
245             public IStatus validate(Object[] selection) {
246                 IStatus typedStatus= validator.validate(selection);
247                 if (!typedStatus.isOK())
248                     return typedStatus;
249                 if (selection[0] instanceof IFolder) {
250                     IFolder folder= (IFolder) selection[0];
251                     try {
252                         IStatus result= ClasspathModifier.checkSetOutputLocationPrecondition(fEntryToEdit, folder.getFullPath(), fAllowInvalidClasspath, fCPJavaProject);
253                         if (result.getSeverity() == IStatus.ERROR)
254                                 return result;
255                     } catch (CoreException e) {
256                             JavaPlugin.log(e);
257                     }
258                     return new StatusInfo();
259                 } else {
260                         return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
261                 }
262             }
263         });
264                 dialog.setMessage(NewWizardMessages.OutputLocationDialog_ChooseOutputFolder_description);
265                 dialog.addFilter(filter);
266                 dialog.setInput(root);
267                 dialog.setInitialSelection(initSelection);
268                 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
269
270                 if (dialog.open() == Window.OK) {
271                         return (IContainer)dialog.getFirstResult();
272                 }
273                 return null;
274         }
275 }