1 15 package org.eclipse.ui.dialogs; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IWorkspace; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 import org.eclipse.jface.dialogs.IDialogSettings; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 import org.eclipse.jface.dialogs.TitleAreaDialog; 28 import org.eclipse.osgi.util.NLS; 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.graphics.Image; 31 import org.eclipse.swt.layout.GridData; 32 import org.eclipse.swt.layout.GridLayout; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Event; 37 import org.eclipse.swt.widgets.Listener; 38 import org.eclipse.swt.widgets.Shell; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages; 41 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 42 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 43 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 44 import org.eclipse.ui.internal.ide.misc.ResourceAndContainerGroup; 45 46 56 public class SaveAsDialog extends TitleAreaDialog { 57 58 private static final String DIALOG_SETTINGS_SECTION = "SaveAsDialogSettings"; 60 private IFile originalFile = null; 61 62 private String originalName = null; 63 64 private IPath result; 65 66 private ResourceAndContainerGroup resourceGroup; 68 69 private Button okButton; 70 71 74 private Image dlgTitleImage = null; 75 76 81 public SaveAsDialog(Shell parentShell) { 82 super(parentShell); 83 setShellStyle(getShellStyle() | SWT.RESIZE); 84 } 85 86 89 protected void configureShell(Shell shell) { 90 super.configureShell(shell); 91 shell.setText(IDEWorkbenchMessages.SaveAsDialog_text); 92 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 93 IIDEHelpContextIds.SAVE_AS_DIALOG); 94 } 95 96 99 protected Control createContents(Composite parent) { 100 101 Control contents = super.createContents(parent); 102 103 initializeControls(); 104 validatePage(); 105 resourceGroup.setFocus(); 106 setTitle(IDEWorkbenchMessages.SaveAsDialog_title); 107 dlgTitleImage = IDEInternalWorkbenchImages.getImageDescriptor( 108 IDEInternalWorkbenchImages.IMG_DLGBAN_SAVEAS_DLG).createImage(); 109 setTitleImage(dlgTitleImage); 110 setMessage(IDEWorkbenchMessages.SaveAsDialog_message); 111 112 return contents; 113 } 114 115 119 public boolean close() { 120 if (dlgTitleImage != null) { 121 dlgTitleImage.dispose(); 122 } 123 return super.close(); 124 } 125 126 129 protected void createButtonsForButtonBar(Composite parent) { 130 okButton = createButton(parent, IDialogConstants.OK_ID, 131 IDialogConstants.OK_LABEL, true); 132 createButton(parent, IDialogConstants.CANCEL_ID, 133 IDialogConstants.CANCEL_LABEL, false); 134 } 135 136 139 protected Control createDialogArea(Composite parent) { 140 Composite parentComposite = (Composite) super.createDialogArea(parent); 142 143 Composite composite = new Composite(parentComposite, SWT.NONE); 145 GridLayout layout = new GridLayout(); 146 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 147 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 148 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 149 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 150 composite.setLayout(layout); 151 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 152 composite.setFont(parentComposite.getFont()); 153 154 Listener listener = new Listener() { 155 public void handleEvent(Event event) { 156 setDialogComplete(validatePage()); 157 } 158 }; 159 160 resourceGroup = new ResourceAndContainerGroup( 161 composite, 162 listener, 163 IDEWorkbenchMessages.SaveAsDialog_fileLabel, IDEWorkbenchMessages.SaveAsDialog_file); 164 resourceGroup.setAllowExistingResources(true); 165 166 return parentComposite; 167 } 168 169 179 public IPath getResult() { 180 return result; 181 } 182 183 186 private void initializeControls() { 187 if (originalFile != null) { 188 resourceGroup.setContainerFullPath(originalFile.getParent() 189 .getFullPath()); 190 resourceGroup.setResource(originalFile.getName()); 191 } else if (originalName != null) { 192 resourceGroup.setResource(originalName); 193 } 194 setDialogComplete(validatePage()); 195 } 196 197 200 protected void okPressed() { 201 IPath path = resourceGroup.getContainerFullPath().append( 203 resourceGroup.getResource()); 204 205 if (path.getFileExtension() == null) { 209 if (originalFile != null && originalFile.getFileExtension() != null) { 210 path = path.addFileExtension(originalFile.getFileExtension()); 211 } else if (originalName != null) { 212 int pos = originalName.lastIndexOf('.'); 213 if (++pos > 0 && pos < originalName.length()) { 214 path = path.addFileExtension(originalName.substring(pos)); 215 } 216 } 217 } 218 219 IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); 221 if (file.exists()) { 222 String [] buttons = new String [] { IDialogConstants.YES_LABEL, 223 IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }; 224 String question = NLS.bind(IDEWorkbenchMessages.SaveAsDialog_overwriteQuestion, path.toOSString()); 225 MessageDialog d = new MessageDialog(getShell(), 226 IDEWorkbenchMessages.Question, 227 null, question, MessageDialog.QUESTION, buttons, 0); 228 int overwrite = d.open(); 229 switch (overwrite) { 230 case 0: break; 232 case 1: return; 234 case 2: default: 236 cancelPressed(); 237 return; 238 } 239 } 240 241 result = path; 243 close(); 244 } 245 246 253 protected void setDialogComplete(boolean value) { 254 okButton.setEnabled(value); 255 } 256 257 262 public void setOriginalFile(IFile originalFile) { 263 this.originalFile = originalFile; 264 } 265 266 273 public void setOriginalName(String originalName) { 274 this.originalName = originalName; 275 } 276 277 282 private boolean validatePage() { 283 if (!resourceGroup.areAllValuesValid()) { 284 if (!resourceGroup.getResource().equals("")) { setErrorMessage(resourceGroup.getProblemMessage()); 286 } else { 287 setErrorMessage(null); 288 } 289 return false; 290 } 291 292 String resourceName = resourceGroup.getResource(); 293 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 294 295 IPath fullPath = resourceGroup.getContainerFullPath(); 297 if (fullPath != null) { 298 String projectName = fullPath.segment(0); 299 IStatus isValidProjectName = workspace.validateName(projectName, IResource.PROJECT); 300 if(isValidProjectName.isOK()) { 301 IProject project = workspace.getRoot().getProject(projectName); 302 if(!project.isOpen()) { 303 setErrorMessage(IDEWorkbenchMessages.SaveAsDialog_closedProjectMessage); 304 return false; 305 } 306 } 307 } 308 309 IStatus result = workspace.validateName(resourceName, IResource.FILE); 310 if (!result.isOK()){ 311 setErrorMessage(result.getMessage()); 312 return false; 313 } 314 315 setErrorMessage(null); 316 return true; 317 } 318 319 324 protected IDialogSettings getDialogBoundsSettings() { 325 IDialogSettings settings = IDEWorkbenchPlugin.getDefault().getDialogSettings(); 326 IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION); 327 if (section == null) { 328 section = settings.addNewSection(DIALOG_SETTINGS_SECTION); 329 } 330 return section; 331 } 332 } 333 | Popular Tags |