001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font
011: * should be activated and used by other components.
012: *******************************************************************************/package org.eclipse.ui.dialogs;
013:
014: import java.util.ArrayList;
015:
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IWorkspace;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.osgi.util.NLS;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.graphics.Font;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.Event;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.swt.widgets.Listener;
031: import org.eclipse.swt.widgets.Shell;
032: import org.eclipse.swt.widgets.Text;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
035: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
036: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
037: import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
038: import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
039:
040: /**
041: * The ProjectLocationSelectionDialog is the dialog used to select the name and
042: * location of a project for copying.
043: */
044: public class ProjectLocationSelectionDialog extends
045: SelectionStatusDialog {
046: // widgets
047: private Text projectNameField;
048:
049: private IProject project;
050:
051: private ProjectContentsLocationArea locationArea;
052:
053: private static String PROJECT_NAME_LABEL = IDEWorkbenchMessages.ProjectLocationSelectionDialog_nameLabel;
054:
055: private static String PROJECT_LOCATION_SELECTION_TITLE = IDEWorkbenchMessages.ProjectLocationSelectionDialog_selectionTitle;
056:
057: // constants
058: private static final int SIZING_TEXT_FIELD_WIDTH = 250;
059:
060: private boolean firstLocationCheck;
061:
062: /**
063: * Create a ProjectLocationSelectionDialog on the supplied project parented
064: * by the parentShell.
065: *
066: * @param parentShell
067: * @param existingProject
068: */
069: public ProjectLocationSelectionDialog(Shell parentShell,
070: IProject existingProject) {
071: super (parentShell);
072: setTitle(PROJECT_LOCATION_SELECTION_TITLE);
073: setStatusLineAboveButtons(true);
074: this .project = existingProject;
075: }
076:
077: /**
078: * Check the message. If it is null then continue otherwise inform the user
079: * via the status value and disable the OK.
080: *
081: * @param errorMsg
082: * the error message to show if it is not <code>null</code>
083: */
084: private void applyValidationResult(String errorMsg) {
085: int code;
086: boolean allowFinish = false;
087:
088: if (errorMsg == null) {
089: code = IStatus.OK;
090: errorMsg = ""; //$NON-NLS-1$
091: allowFinish = true;
092: } else if (firstLocationCheck) {
093: code = IStatus.OK;
094: } else {
095: code = IStatus.ERROR;
096: }
097:
098: updateStatus(new Status(code, IDEWorkbenchPlugin.IDE_WORKBENCH,
099: code, errorMsg, null));
100: getOkButton().setEnabled(allowFinish);
101: }
102:
103: /**
104: * Check whether the entries are valid. If so return null. Otherwise return
105: * a string that indicates the problem.
106: */
107: private String checkValid() {
108: String valid = checkValidName();
109: if (valid != null) {
110: return valid;
111: }
112: return locationArea.checkValidLocation();
113: }
114:
115: /**
116: * Check if the entries in the widget are valid. If they are return null
117: * otherwise return a string that indicates the problem.
118: */
119: private String checkValidName() {
120:
121: String name = this .projectNameField.getText();
122: IWorkspace workspace = getProject().getWorkspace();
123: IStatus nameStatus = workspace.validateName(name,
124: IResource.PROJECT);
125: if (!nameStatus.isOK()) {
126: return nameStatus.getMessage();
127: }
128: IProject newProject = workspace.getRoot().getProject(name);
129: if (newProject.exists()) {
130: return NLS
131: .bind(
132: IDEWorkbenchMessages.CopyProjectAction_alreadyExists,
133: name);
134: }
135:
136: return null;
137: }
138:
139: /**
140: * The <code>ProjectLocationSelectionDialog</code> implementation of this
141: * <code>SelectionStatusDialog</code> method builds a two element list -
142: * the first element is the project name and the second one is the location.
143: */
144: protected void computeResult() {
145:
146: ArrayList list = new ArrayList();
147: list.add(this .projectNameField.getText());
148: list.add(locationArea.getProjectLocation());
149: setResult(list);
150: }
151:
152: /*
153: * (non-Javadoc) Method declared in Window.
154: */
155: protected void configureShell(Shell shell) {
156: super .configureShell(shell);
157: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
158: IIDEHelpContextIds.PROJECT_LOCATION_SELECTION_DIALOG);
159: }
160:
161: /*
162: * (non-Javadoc) Method declared on Dialog.
163: */
164: protected Control createDialogArea(Composite parent) {
165: // page group
166: Composite composite = (Composite) super
167: .createDialogArea(parent);
168:
169: composite.setLayout(new GridLayout());
170: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
171:
172: createProjectNameGroup(composite);
173: locationArea = new ProjectContentsLocationArea(
174: getErrorReporter(), composite, project);
175: locationArea.updateProjectName(projectNameField.getText());
176: return composite;
177: }
178:
179: /**
180: * Create the listener that is used to validate the entries for the receiver
181: */
182: private void createNameListener() {
183:
184: Listener listener = new Listener() {
185: public void handleEvent(Event event) {
186: setLocationForSelection();
187: applyValidationResult(checkValid());
188: }
189: };
190:
191: this .projectNameField.addListener(SWT.Modify, listener);
192: }
193:
194: /**
195: * Creates the project name specification controls.
196: *
197: * @param parent
198: * the parent composite
199: */
200: private void createProjectNameGroup(Composite parent) {
201: Font font = parent.getFont();
202: // project specification group
203: Composite projectGroup = new Composite(parent, SWT.NONE);
204: GridLayout layout = new GridLayout();
205: layout.numColumns = 2;
206: projectGroup.setLayout(layout);
207: projectGroup.setLayoutData(new GridData(
208: GridData.FILL_HORIZONTAL));
209:
210: // new project label
211: Label projectLabel = new Label(projectGroup, SWT.NONE);
212: projectLabel.setFont(font);
213: projectLabel.setText(PROJECT_NAME_LABEL);
214:
215: // new project name entry field
216: projectNameField = new Text(projectGroup, SWT.BORDER);
217: GridData data = new GridData(GridData.FILL_HORIZONTAL);
218: data.widthHint = SIZING_TEXT_FIELD_WIDTH;
219: projectNameField.setLayoutData(data);
220: projectNameField.setFont(font);
221:
222: // Set the initial value first before listener
223: // to avoid handling an event during the creation.
224: projectNameField
225: .setText(getCopyNameFor(getProject().getName()));
226: projectNameField.selectAll();
227:
228: createNameListener();
229:
230: }
231:
232: /**
233: * Generates a new name for the project that does not have any collisions.
234: */
235: private String getCopyNameFor(String projectName) {
236:
237: IWorkspace workspace = getProject().getWorkspace();
238: if (!workspace.getRoot().getProject(projectName).exists()) {
239: return projectName;
240: }
241:
242: int counter = 1;
243: while (true) {
244: String nameSegment;
245: if (counter > 1) {
246: nameSegment = NLS
247: .bind(
248: IDEWorkbenchMessages.CopyProjectAction_copyNameTwoArgs,
249: new Integer(counter), projectName);
250: } else {
251: nameSegment = NLS
252: .bind(
253: IDEWorkbenchMessages.CopyProjectAction_copyNameOneArg,
254: projectName);
255: }
256:
257: if (!workspace.getRoot().getProject(nameSegment).exists()) {
258: return nameSegment;
259: }
260:
261: counter++;
262: }
263:
264: }
265:
266: /**
267: * Get the project being manipulated.
268: */
269: private IProject getProject() {
270: return this .project;
271: }
272:
273: /**
274: * Set the location to the default location if we are set to useDefaults.
275: */
276: private void setLocationForSelection() {
277: locationArea.updateProjectName(projectNameField.getText());
278: }
279:
280: /**
281: * Get an error reporter for the receiver.
282: *
283: * @return IErrorMessageReporter
284: */
285: private IErrorMessageReporter getErrorReporter() {
286: return new IErrorMessageReporter() {
287: /*
288: * (non-Javadoc)
289: *
290: * @see org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter#reportError(java.lang.String)
291: */
292: public void reportError(String errorMessage) {
293: setMessage(errorMessage);
294:
295: }
296: };
297: }
298: }
|