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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
13 import java.util.ArrayList;
14 import java.util.List;
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;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IStatus;
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;
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;
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;
47 import org.eclipse.jdt.core.IJavaProject;
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;
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;
67 public class OutputLocationDialog extends StatusDialog {
69 private StringButtonDialogField fContainerDialogField;
70 private SelectionButtonDialogField fUseDefault;
71 private SelectionButtonDialogField fUseSpecific;
72 private IStatus fContainerFieldStatus;
74 private IPath fOutputLocation;
75 private final IProject fCurrProject;
76 private final CPListElement fEntryToEdit;
77 private final boolean fAllowInvalidClasspath;
78 private CPJavaProject fCPJavaProject;
80 public OutputLocationDialog(Shell parent, CPListElement entryToEdit, List<CPListElement> classPathList, IPath defaultOutputFolder, boolean allowInvalidClasspath) {
82 fEntryToEdit= entryToEdit;
83 fAllowInvalidClasspath= allowInvalidClasspath;
84 setTitle(NewWizardMessages.OutputLocationDialog_title);
85 fContainerFieldStatus= new StatusInfo();
87 OutputLocationAdapter adapter= new OutputLocationAdapter();
89 fUseDefault= new SelectionButtonDialogField(SWT.RADIO);
90 fUseDefault.setLabelText(Messages.format(NewWizardMessages.OutputLocationDialog_usedefault_label, BasicElementLabels.getPathLabel(defaultOutputFolder, false)));
91 fUseDefault.setDialogFieldListener(adapter);
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);
98 fContainerDialogField= new StringButtonDialogField(adapter);
99 fContainerDialogField.setButtonLabel(NewWizardMessages.OutputLocationDialog_location_button);
100 fContainerDialogField.setDialogFieldListener(adapter);
102 fUseSpecific.attachDialogField(fContainerDialogField);
104 IJavaProject javaProject= entryToEdit.getJavaProject();
105 fCurrProject= javaProject.getProject();
106 fCPJavaProject= new CPJavaProject(javaProject, classPathList, defaultOutputFolder);
108 IPath outputLocation= (IPath) entryToEdit.getAttribute(CPListElement.OUTPUT);
109 if (outputLocation == null) {
110 fUseDefault.setSelection(true);
112 fUseSpecific.setSelection(true);
113 fContainerDialogField.setText(outputLocation.removeFirstSegments(1).makeRelative().toString());
118 protected Control createDialogArea(Composite parent) {
119 Composite composite= (Composite)super.createDialogArea(parent);
121 int widthHint= convertWidthInCharsToPixels(70);
122 int indent= convertWidthInCharsToPixels(4);
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);
131 fUseDefault.doFillIntoGrid(inner, 2);
132 fUseSpecific.doFillIntoGrid(inner, 2);
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);
141 Button buttonControl= fContainerDialogField.getChangeControl(inner);
142 GridData buttonData= new GridData();
143 buttonData.widthHint= SWTUtil.getButtonWidthHint(buttonControl);
144 buttonControl.setLayoutData(buttonData);
146 applyDialogFont(composite);
151 // -------- OutputLocationAdapter --------
153 private class OutputLocationAdapter implements IDialogFieldListener, IStringButtonAdapter {
155 // -------- IDialogFieldListener
157 public void dialogFieldChanged(DialogField field) {
158 doStatusLineUpdate();
161 public void changeControlPressed(DialogField field) {
162 doChangeControlPressed();
166 protected void doChangeControlPressed() {
167 IContainer container= chooseOutputLocation();
168 if (container != null) {
169 fContainerDialogField.setText(container.getProjectRelativePath().toString());
173 protected void doStatusLineUpdate() {
175 updateStatus(fContainerFieldStatus);
178 protected void checkIfPathValid() {
179 fOutputLocation= null;
180 fContainerFieldStatus= StatusInfo.OK_STATUS;
182 if (fUseDefault.isSelected()) {
186 String pathStr= fContainerDialogField.getText();
187 if (pathStr.length() == 0) {
188 fContainerFieldStatus= new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
192 IPath projectPath= fCPJavaProject.getJavaProject().getProject().getFullPath();
193 IPath outputPath= projectPath.append(pathStr);
196 fContainerFieldStatus= ClasspathModifier.checkSetOutputLocationPrecondition(fEntryToEdit, outputPath, fAllowInvalidClasspath, fCPJavaProject);
197 if (fContainerFieldStatus.getSeverity() != IStatus.ERROR) {
198 fOutputLocation= outputPath;
200 } catch (CoreException e) {
205 public IPath getOutputLocation() {
206 return fOutputLocation;
210 * @see org.eclipse.jface.window.Window#configureShell(Shell)
213 protected void configureShell(Shell newShell) {
214 super.configureShell(newShell);
215 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.OUTPUT_LOCATION_DIALOG);
218 // ---------- util method ------------
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]);
230 ViewerFilter filter= new TypedViewerFilter(acceptedClasses, rejectedElements.toArray());
232 ILabelProvider lp= new WorkbenchLabelProvider();
233 ITreeContentProvider cp= new WorkbenchContentProvider();
235 IResource initSelection= null;
236 if (fOutputLocation != null) {
237 initSelection= root.findMember(fOutputLocation);
240 FolderSelectionDialog dialog= new FolderSelectionDialog(getShell(), lp, cp);
241 dialog.setTitle(NewWizardMessages.OutputLocationDialog_ChooseOutputFolder_title);
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())
249 if (selection[0] instanceof IFolder) {
250 IFolder folder= (IFolder) selection[0];
252 IStatus result= ClasspathModifier.checkSetOutputLocationPrecondition(fEntryToEdit, folder.getFullPath(), fAllowInvalidClasspath, fCPJavaProject);
253 if (result.getSeverity() == IStatus.ERROR)
255 } catch (CoreException e) {
258 return new StatusInfo();
260 return new StatusInfo(IStatus.ERROR, ""); //$NON-NLS-1$
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));
270 if (dialog.open() == Window.OK) {
271 return (IContainer)dialog.getFirstResult();