1 13 package org.eclipse.ui.dialogs; 14 15 import java.util.ArrayList ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IWorkspace; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.graphics.Font; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.swt.widgets.Control; 29 import org.eclipse.swt.widgets.Event; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.swt.widgets.Text; 34 import org.eclipse.ui.PlatformUI; 35 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 36 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 37 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 38 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea; 39 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter; 40 41 45 public class ProjectLocationSelectionDialog extends SelectionStatusDialog { 46 private Text projectNameField; 48 49 private IProject project; 50 51 private ProjectContentsLocationArea locationArea; 52 53 private static String PROJECT_NAME_LABEL = IDEWorkbenchMessages.ProjectLocationSelectionDialog_nameLabel; 54 55 private static String PROJECT_LOCATION_SELECTION_TITLE = IDEWorkbenchMessages.ProjectLocationSelectionDialog_selectionTitle; 56 57 private static final int SIZING_TEXT_FIELD_WIDTH = 250; 59 60 private boolean firstLocationCheck; 61 62 69 public ProjectLocationSelectionDialog(Shell parentShell, 70 IProject existingProject) { 71 super(parentShell); 72 setShellStyle(getShellStyle() | SWT.RESIZE); 73 setTitle(PROJECT_LOCATION_SELECTION_TITLE); 74 setStatusLineAboveButtons(true); 75 this.project = existingProject; 76 } 77 78 85 private void applyValidationResult(String errorMsg) { 86 int code; 87 boolean allowFinish = false; 88 89 if (errorMsg == null) { 90 code = IStatus.OK; 91 errorMsg = ""; allowFinish = true; 93 } else if (firstLocationCheck) { 94 code = IStatus.OK; 95 } else { 96 code = IStatus.ERROR; 97 } 98 99 updateStatus(new Status(code, IDEWorkbenchPlugin.IDE_WORKBENCH, code, 100 errorMsg, null)); 101 getOkButton().setEnabled(allowFinish); 102 } 103 104 108 private String checkValid() { 109 String valid = checkValidName(); 110 if (valid != null) { 111 return valid; 112 } 113 return locationArea.checkValidLocation(); 114 } 115 116 120 private String checkValidName() { 121 122 String name = this.projectNameField.getText(); 123 IWorkspace workspace = getProject().getWorkspace(); 124 IStatus nameStatus = workspace.validateName(name, 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.bind( 131 IDEWorkbenchMessages.CopyProjectAction_alreadyExists, name); 132 } 133 134 return null; 135 } 136 137 142 protected void computeResult() { 143 144 ArrayList list = new ArrayList (); 145 list.add(this.projectNameField.getText()); 146 list.add(locationArea.getProjectLocation()); 147 setResult(list); 148 } 149 150 153 protected void configureShell(Shell shell) { 154 super.configureShell(shell); 155 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 156 IIDEHelpContextIds.PROJECT_LOCATION_SELECTION_DIALOG); 157 } 158 159 162 protected Control createDialogArea(Composite parent) { 163 Composite composite = (Composite) super.createDialogArea(parent); 165 166 composite.setLayout(new GridLayout()); 167 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 168 169 createProjectNameGroup(composite); 170 locationArea = new ProjectContentsLocationArea(getErrorReporter(), 171 composite, project); 172 locationArea.updateProjectName(projectNameField.getText()); 173 return composite; 174 } 175 176 179 private void createNameListener() { 180 181 Listener listener = new Listener() { 182 public void handleEvent(Event event) { 183 setLocationForSelection(); 184 applyValidationResult(checkValid()); 185 } 186 }; 187 188 this.projectNameField.addListener(SWT.Modify, listener); 189 } 190 191 197 private void createProjectNameGroup(Composite parent) { 198 Font font = parent.getFont(); 199 Composite projectGroup = new Composite(parent, SWT.NONE); 201 GridLayout layout = new GridLayout(); 202 layout.numColumns = 2; 203 projectGroup.setLayout(layout); 204 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 205 206 Label projectLabel = new Label(projectGroup, SWT.NONE); 208 projectLabel.setFont(font); 209 projectLabel.setText(PROJECT_NAME_LABEL); 210 211 projectNameField = new Text(projectGroup, SWT.BORDER); 213 GridData data = new GridData(GridData.FILL_HORIZONTAL); 214 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 215 projectNameField.setLayoutData(data); 216 projectNameField.setFont(font); 217 218 projectNameField.setText(getCopyNameFor(getProject().getName())); 221 projectNameField.selectAll(); 222 223 createNameListener(); 224 225 } 226 227 230 private String getCopyNameFor(String projectName) { 231 232 IWorkspace workspace = getProject().getWorkspace(); 233 if (!workspace.getRoot().getProject(projectName).exists()) { 234 return projectName; 235 } 236 237 int counter = 1; 238 while (true) { 239 String nameSegment; 240 if (counter > 1) { 241 nameSegment = NLS.bind( 242 IDEWorkbenchMessages.CopyProjectAction_copyNameTwoArgs, 243 new Integer (counter), projectName); 244 } else { 245 nameSegment = NLS.bind( 246 IDEWorkbenchMessages.CopyProjectAction_copyNameOneArg, 247 projectName); 248 } 249 250 if (!workspace.getRoot().getProject(nameSegment).exists()) { 251 return nameSegment; 252 } 253 254 counter++; 255 } 256 257 } 258 259 262 private IProject getProject() { 263 return this.project; 264 } 265 266 269 private void setLocationForSelection() { 270 locationArea.updateProjectName(projectNameField.getText()); 271 } 272 273 278 private IErrorMessageReporter getErrorReporter() { 279 return new IErrorMessageReporter() { 280 285 public void reportError(String errorMessage) { 286 setMessage(errorMessage); 287 288 } 289 }; 290 } 291 } 292 | Popular Tags |