001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import org.eclipse.jface.dialogs.Dialog;
013: import org.eclipse.jface.dialogs.IDialogConstants;
014: import org.eclipse.jface.dialogs.IDialogSettings;
015: import org.eclipse.jface.dialogs.TitleAreaDialog;
016: import org.eclipse.jface.layout.GridLayoutFactory;
017: import org.eclipse.jface.layout.LayoutConstants;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.ModifyEvent;
020: import org.eclipse.swt.events.ModifyListener;
021: import org.eclipse.swt.graphics.Point;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.widgets.Button;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.swt.widgets.Text;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
031: import org.eclipse.ui.internal.WorkbenchMessages;
032: import org.eclipse.ui.internal.WorkbenchPlugin;
033:
034: /**
035: * This class is used to prompt the user for a file name & extension.
036: */
037: public class FileExtensionDialog extends TitleAreaDialog {
038:
039: private static final String DIALOG_SETTINGS_SECTION = "FileExtensionDialogSettings"; //$NON-NLS-1$
040:
041: private String filename = ""; //$NON-NLS-1$
042:
043: private Text filenameField;
044:
045: private Button okButton;
046:
047: /**
048: * Constructs a new file extension dialog.
049: * @param parentShell the parent shell
050: */
051: public FileExtensionDialog(Shell parentShell) {
052: super (parentShell);
053: }
054:
055: /* (non-Javadoc)
056: * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
057: */
058: protected void configureShell(Shell shell) {
059: super .configureShell(shell);
060: shell.setText(WorkbenchMessages.FileExtension_shellTitle);
061: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
062: IWorkbenchHelpContextIds.FILE_EXTENSION_DIALOG);
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
067: */
068: protected Control createDialogArea(Composite parent) {
069: Composite parentComposite = (Composite) super
070: .createDialogArea(parent);
071:
072: Composite contents = new Composite(parentComposite, SWT.NONE);
073: contents.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
074: true));
075:
076: setTitle(WorkbenchMessages.FileExtension_dialogTitle);
077: setMessage(WorkbenchMessages.FileExtension_fileTypeMessage);
078:
079: new Label(contents, SWT.LEFT)
080: .setText(WorkbenchMessages.FileExtension_fileTypeLabel);
081:
082: filenameField = new Text(contents, SWT.SINGLE | SWT.BORDER);
083: filenameField.addModifyListener(new ModifyListener() {
084: public void modifyText(ModifyEvent event) {
085: if (event.widget == filenameField) {
086: filename = filenameField.getText().trim();
087: okButton.setEnabled(validateFileType());
088: }
089: }
090: });
091: filenameField.setFocus();
092:
093: Dialog.applyDialogFont(parentComposite);
094:
095: Point defaultMargins = LayoutConstants.getMargins();
096: GridLayoutFactory.fillDefaults().numColumns(2).margins(
097: defaultMargins.x, defaultMargins.y).generateLayout(
098: contents);
099:
100: return contents;
101: }
102:
103: /* (non-Javadoc)
104: * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
105: */
106: protected void createButtonsForButtonBar(Composite parent) {
107: okButton = createButton(parent, IDialogConstants.OK_ID,
108: IDialogConstants.OK_LABEL, true);
109: okButton.setEnabled(false);
110: createButton(parent, IDialogConstants.CANCEL_ID,
111: IDialogConstants.CANCEL_LABEL, false);
112: }
113:
114: /**
115: * Validate the user input for a file type
116: */
117: private boolean validateFileType() {
118: // We need kernel api to validate the extension or a filename
119:
120: // check for empty name and extension
121: if (filename.length() == 0) {
122: setErrorMessage(null);
123: return false;
124: }
125:
126: // check for empty extension if there is no name
127: int index = filename.lastIndexOf('.');
128: if (index == filename.length() - 1) {
129: if (index == 0 || (index == 1 && filename.charAt(0) == '*')) {
130: setErrorMessage(WorkbenchMessages.FileExtension_extensionEmptyMessage);
131: return false;
132: }
133: }
134:
135: // check for characters before *
136: // or no other characters
137: // or next chatacter not '.'
138: // or another *
139: index = filename.indexOf('*');
140: if (index > -1) {
141: if (filename.length() == 1) {
142: setErrorMessage(WorkbenchMessages.FileExtension_extensionEmptyMessage);
143: return false;
144: }
145: if (index != 0 || filename.charAt(1) != '.') {
146: setErrorMessage(WorkbenchMessages.FileExtension_fileNameInvalidMessage);
147: return false;
148: }
149: if (filename.length() > index
150: && filename.indexOf('*', index + 1) != -1) {
151: setErrorMessage(WorkbenchMessages.FileExtension_fileNameInvalidMessage);
152: return false;
153: }
154: }
155:
156: setErrorMessage(null);
157: return true;
158: }
159:
160: /**
161: * Get the extension.
162: *
163: * @return the extension
164: */
165: public String getExtension() {
166: // We need kernel api to validate the extension or a filename
167:
168: int index = filename.lastIndexOf('.');
169: if (index == -1) {
170: return ""; //$NON-NLS-1$
171: }
172: if (index == filename.length()) {
173: return ""; //$NON-NLS-1$
174: }
175: return filename.substring(index + 1, filename.length());
176: }
177:
178: /**
179: * Get the name.
180: *
181: * @return the name
182: */
183: public String getName() {
184: // We need kernel api to validate the extension or a filename
185:
186: int index = filename.lastIndexOf('.');
187: if (index == -1) {
188: return filename;
189: }
190: if (index == 0) {
191: return "*"; //$NON-NLS-1$
192: }
193: return filename.substring(0, index);
194: }
195:
196: /* (non-Javadoc)
197: * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
198: */
199: protected IDialogSettings getDialogBoundsSettings() {
200: IDialogSettings settings = WorkbenchPlugin.getDefault()
201: .getDialogSettings();
202: IDialogSettings section = settings
203: .getSection(DIALOG_SETTINGS_SECTION);
204: if (section == null)
205: section = settings.addNewSection(DIALOG_SETTINGS_SECTION);
206: return section;
207: }
208:
209: /*
210: * (non-Javadoc)
211: * @see org.eclipse.jface.dialogs.Dialog#isResizable()
212: */
213: protected boolean isResizable() {
214: return true;
215: }
216: }
|