KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IWorkspaceRoot;
25
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
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
46 import org.eclipse.ui.views.navigator.ResourceComparator;
47
48 import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject;
49 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
50 import org.eclipse.jdt.internal.corext.util.Messages;
51
52 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
53 import org.eclipse.jdt.internal.ui.JavaPlugin;
54 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
55 import org.eclipse.jdt.internal.ui.util.SWTUtil;
56 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
57 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
58 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter;
59 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
61 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
64
65 public class OutputLocationDialog extends StatusDialog {
66     
67     private StringButtonDialogField fContainerDialogField;
68     private SelectionButtonDialogField fUseDefault;
69     private SelectionButtonDialogField fUseSpecific;
70     private IStatus fContainerFieldStatus;
71
72     private IPath fOutputLocation;
73     private final IProject fCurrProject;
74     private final CPListElement fEntryToEdit;
75     private final boolean fAllowInvalidClasspath;
76     private CPJavaProject fCPJavaProject;
77         
78     public OutputLocationDialog(Shell parent, CPListElement entryToEdit, List JavaDoc classPathList, IPath defaultOutputFolder, boolean allowInvalidClasspath) {
79         super(parent);
80         fEntryToEdit= entryToEdit;
81         fAllowInvalidClasspath= allowInvalidClasspath;
82         setTitle(NewWizardMessages.OutputLocationDialog_title);
83         fContainerFieldStatus= new StatusInfo();
84     
85         OutputLocationAdapter adapter= new OutputLocationAdapter();
86
87         fUseDefault= new SelectionButtonDialogField(SWT.RADIO);
88         fUseDefault.setLabelText(Messages.format(NewWizardMessages.OutputLocationDialog_usedefault_label, defaultOutputFolder.makeRelative().toString()));
89         fUseDefault.setDialogFieldListener(adapter);
90
91         String JavaDoc label= Messages.format(NewWizardMessages.OutputLocationDialog_usespecific_label, entryToEdit.getPath().segment(0));
92         fUseSpecific= new SelectionButtonDialogField(SWT.RADIO);
93         fUseSpecific.setLabelText(label);
94         fUseSpecific.setDialogFieldListener(adapter);
95         
96         fContainerDialogField= new StringButtonDialogField(adapter);
97         fContainerDialogField.setButtonLabel(NewWizardMessages.OutputLocationDialog_location_button);
98         fContainerDialogField.setDialogFieldListener(adapter);
99         
100         fUseSpecific.attachDialogField(fContainerDialogField);
101         
102         fCurrProject= entryToEdit.getJavaProject().getProject();
103         fCPJavaProject= new CPJavaProject(classPathList, defaultOutputFolder);
104         
105         IPath outputLocation= (IPath) entryToEdit.getAttribute(CPListElement.OUTPUT);
106         if (outputLocation == null) {
107             fUseDefault.setSelection(true);
108         } else {
109             fUseSpecific.setSelection(true);
110             fContainerDialogField.setText(outputLocation.removeFirstSegments(1).makeRelative().toString());
111         }
112     }
113     
114     protected Control createDialogArea(Composite parent) {
115         Composite composite= (Composite)super.createDialogArea(parent);
116         
117         int widthHint= convertWidthInCharsToPixels(70);
118         int indent= convertWidthInCharsToPixels(4);
119         
120         Composite inner= new Composite(composite, SWT.NONE);
121         GridLayout layout= new GridLayout();
122         layout.marginHeight= 0;
123         layout.marginWidth= 0;
124         layout.numColumns= 2;
125         inner.setLayout(layout);
126         
127         fUseDefault.doFillIntoGrid(inner, 2);
128         fUseSpecific.doFillIntoGrid(inner, 2);
129         
130         Text textControl= fContainerDialogField.getTextControl(inner);
131         GridData textData= new GridData();
132         textData.widthHint= widthHint;
133         textData.grabExcessHorizontalSpace= true;
134         textData.horizontalIndent= indent;
135         textControl.setLayoutData(textData);
136         
137         Button buttonControl= fContainerDialogField.getChangeControl(inner);
138         GridData buttonData= new GridData();
139         buttonData.widthHint= SWTUtil.getButtonWidthHint(buttonControl);
140         buttonControl.setLayoutData(buttonData);
141         
142         applyDialogFont(composite);
143         return composite;
144     }
145
146         
147     // -------- OutputLocationAdapter --------
148

149     private class OutputLocationAdapter implements IDialogFieldListener, IStringButtonAdapter {
150         
151         // -------- IDialogFieldListener
152

153         public void dialogFieldChanged(DialogField field) {
154             doStatusLineUpdate();
155         }
156
157         public void changeControlPressed(DialogField field) {
158             doChangeControlPressed();
159         }
160     }
161     
162     protected void doChangeControlPressed() {
163         IContainer container= chooseOutputLocation();
164         if (container != null) {
165             fContainerDialogField.setText(container.getProjectRelativePath().toString());
166         }
167     }
168     
169     protected void doStatusLineUpdate() {
170         checkIfPathValid();
171         updateStatus(fContainerFieldStatus);
172     }
173
174     protected void checkIfPathValid() {
175         fOutputLocation= null;
176         fContainerFieldStatus= StatusInfo.OK_STATUS;
177
178         if (fUseDefault.isSelected()) {
179             return;
180         }
181         
182         String JavaDoc pathStr= fContainerDialogField.getText();
183         if (pathStr.length() == 0) {
184             fContainerFieldStatus= new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
185
return;
186         }
187         
188         IPath projectPath= fCPJavaProject.getJavaProject().getProject().getFullPath();
189         IPath outputPath= projectPath.append(pathStr);
190         
191         try {
192             fContainerFieldStatus= ClasspathModifier.checkSetOutputLocationPrecondition(fEntryToEdit, outputPath, fAllowInvalidClasspath, fCPJavaProject);
193             if (fContainerFieldStatus.getSeverity() != IStatus.ERROR) {
194                 fOutputLocation= outputPath;
195             }
196         } catch (CoreException e) {
197             JavaPlugin.log(e);
198         }
199     }
200
201     public IPath getOutputLocation() {
202         return fOutputLocation;
203     }
204         
205     /*
206      * @see org.eclipse.jface.window.Window#configureShell(Shell)
207      */

208     protected void configureShell(Shell newShell) {
209         super.configureShell(newShell);
210         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.OUTPUT_LOCATION_DIALOG);
211     }
212     
213     // ---------- util method ------------
214

215     private IContainer chooseOutputLocation() {
216         IWorkspaceRoot root= fCurrProject.getWorkspace().getRoot();
217         final Class JavaDoc[] acceptedClasses= new Class JavaDoc[] { IProject.class, IFolder.class };
218         IProject[] allProjects= root.getProjects();
219         ArrayList JavaDoc rejectedElements= new ArrayList JavaDoc(allProjects.length);
220         for (int i= 0; i < allProjects.length; i++) {
221             if (!allProjects[i].equals(fCurrProject)) {
222                 rejectedElements.add(allProjects[i]);
223             }
224         }
225         ViewerFilter filter= new TypedViewerFilter(acceptedClasses, rejectedElements.toArray());
226
227         ILabelProvider lp= new WorkbenchLabelProvider();
228         ITreeContentProvider cp= new WorkbenchContentProvider();
229
230         IResource initSelection= null;
231         if (fOutputLocation != null) {
232             initSelection= root.findMember(fOutputLocation);
233         }
234
235         FolderSelectionDialog dialog= new FolderSelectionDialog(getShell(), lp, cp);
236         dialog.setTitle(NewWizardMessages.OutputLocationDialog_ChooseOutputFolder_title);
237         
238         dialog.setValidator(new ISelectionStatusValidator() {
239             ISelectionStatusValidator validator= new TypedElementSelectionValidator(acceptedClasses, false);
240             public IStatus validate(Object JavaDoc[] selection) {
241                 IStatus typedStatus= validator.validate(selection);
242                 if (!typedStatus.isOK())
243                     return typedStatus;
244                 if (selection[0] instanceof IFolder) {
245                     IFolder folder= (IFolder) selection[0];
246                     try {
247                         IStatus result= ClasspathModifier.checkSetOutputLocationPrecondition(fEntryToEdit, folder.getFullPath(), fAllowInvalidClasspath, fCPJavaProject);
248                         if (result.getSeverity() == IStatus.ERROR)
249                             return result;
250                     } catch (CoreException e) {
251                         JavaPlugin.log(e);
252                     }
253                     return new StatusInfo();
254                 } else {
255                     return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
256
}
257             }
258         });
259         dialog.setMessage(NewWizardMessages.OutputLocationDialog_ChooseOutputFolder_description);
260         dialog.addFilter(filter);
261         dialog.setInput(root);
262         dialog.setInitialSelection(initSelection);
263         dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
264
265         if (dialog.open() == Window.OK) {
266             return (IContainer)dialog.getFirstResult();
267         }
268         return null;
269     }
270 }
271
Popular Tags