1 11 package org.eclipse.ui.internal.dialogs; 12 13 import org.eclipse.jface.dialogs.Dialog; 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.jface.dialogs.IDialogSettings; 16 import org.eclipse.jface.dialogs.TitleAreaDialog; 17 import org.eclipse.jface.layout.GridLayoutFactory; 18 import org.eclipse.jface.layout.LayoutConstants; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.ModifyEvent; 21 import org.eclipse.swt.events.ModifyListener; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.swt.widgets.Text; 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 32 import org.eclipse.ui.internal.WorkbenchMessages; 33 import org.eclipse.ui.internal.WorkbenchPlugin; 34 35 38 public class FileExtensionDialog extends TitleAreaDialog { 39 40 private static final String DIALOG_SETTINGS_SECTION = "FileExtensionDialogSettings"; 42 private String filename = ""; 44 private Text filenameField; 45 46 private Button okButton; 47 48 52 public FileExtensionDialog(Shell parentShell) { 53 super(parentShell); 54 setShellStyle(getShellStyle() | SWT.RESIZE); 55 } 56 57 58 61 protected void configureShell(Shell shell) { 62 super.configureShell(shell); 63 shell.setText(WorkbenchMessages.FileExtension_shellTitle); 64 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 65 IWorkbenchHelpContextIds.FILE_EXTENSION_DIALOG); 66 } 67 68 69 72 protected Control createDialogArea(Composite parent) { 73 Composite parentComposite = (Composite) super.createDialogArea(parent); 74 75 Composite contents = new Composite(parentComposite, SWT.NONE); 76 contents.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 77 78 setTitle(WorkbenchMessages.FileExtension_dialogTitle); 79 setMessage(WorkbenchMessages.FileExtension_fileTypeMessage); 80 81 new Label(contents, SWT.LEFT) 82 .setText(WorkbenchMessages.FileExtension_fileTypeLabel); 83 84 filenameField = new Text(contents, SWT.SINGLE | SWT.BORDER); 85 filenameField.addModifyListener(new ModifyListener() { 86 public void modifyText(ModifyEvent event) { 87 if (event.widget == filenameField) { 88 filename = filenameField.getText().trim(); 89 okButton.setEnabled(validateFileType()); 90 } 91 } 92 }); 93 filenameField.setFocus(); 94 95 Dialog.applyDialogFont(parentComposite); 96 97 Point defaultMargins = LayoutConstants.getMargins(); 98 GridLayoutFactory.fillDefaults().numColumns(2).margins( 99 defaultMargins.x, defaultMargins.y).generateLayout(contents); 100 101 return contents; 102 } 103 104 105 108 protected void createButtonsForButtonBar(Composite parent) { 109 okButton = createButton(parent, IDialogConstants.OK_ID, 110 IDialogConstants.OK_LABEL, true); 111 okButton.setEnabled(false); 112 createButton(parent, IDialogConstants.CANCEL_ID, 113 IDialogConstants.CANCEL_LABEL, false); 114 } 115 116 119 private boolean validateFileType() { 120 122 if (filename.length() == 0) { 124 setErrorMessage(null); 125 return false; 126 } 127 128 int index = filename.lastIndexOf('.'); 130 if (index == filename.length() - 1) { 131 if (index == 0 || (index == 1 && filename.charAt(0) == '*')) { 132 setErrorMessage(WorkbenchMessages.FileExtension_extensionEmptyMessage); 133 return false; 134 } 135 } 136 137 index = filename.indexOf('*'); 142 if (index > -1) { 143 if (filename.length() == 1) { 144 setErrorMessage(WorkbenchMessages.FileExtension_extensionEmptyMessage); 145 return false; 146 } 147 if (index != 0 || filename.charAt(1) != '.') { 148 setErrorMessage(WorkbenchMessages.FileExtension_fileNameInvalidMessage); 149 return false; 150 } 151 if (filename.length() > index && filename.indexOf('*', index + 1) != -1) { 152 setErrorMessage(WorkbenchMessages.FileExtension_fileNameInvalidMessage); 153 return false; 154 } 155 } 156 157 setErrorMessage(null); 158 return true; 159 } 160 161 166 public String getExtension() { 167 169 int index = filename.lastIndexOf('.'); 170 if (index == -1) { 171 return ""; } 173 if (index == filename.length()) { 174 return ""; } 176 return filename.substring(index + 1, filename.length()); 177 } 178 179 184 public String getName() { 185 187 int index = filename.lastIndexOf('.'); 188 if (index == -1) { 189 return filename; 190 } 191 if (index == 0) { 192 return "*"; } 194 return filename.substring(0, index); 195 } 196 197 198 201 protected IDialogSettings getDialogBoundsSettings() { 202 IDialogSettings settings = WorkbenchPlugin.getDefault().getDialogSettings(); 203 IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION); 204 if (section == null) section = settings.addNewSection(DIALOG_SETTINGS_SECTION); 205 return section; 206 } 207 } 208 | Popular Tags |