KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > FileExtensionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
36  * This class is used to prompt the user for a file name & extension.
37  */

38 public class FileExtensionDialog extends TitleAreaDialog {
39     
40     private static final String JavaDoc DIALOG_SETTINGS_SECTION = "FileExtensionDialogSettings"; //$NON-NLS-1$
41

42     private String JavaDoc filename = ""; //$NON-NLS-1$
43

44     private Text filenameField;
45
46     private Button okButton;
47
48     /**
49      * Constructs a new file extension dialog.
50      * @param parentShell the parent shell
51      */

52     public FileExtensionDialog(Shell parentShell) {
53         super(parentShell);
54         setShellStyle(getShellStyle() | SWT.RESIZE);
55     }
56
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
60      */

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     /* (non-Javadoc)
70      * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
71      */

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     /* (non-Javadoc)
106      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
107      */

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     /**
117      * Validate the user input for a file type
118      */

119     private boolean validateFileType() {
120         // We need kernel api to validate the extension or a filename
121

122         // check for empty name and extension
123
if (filename.length() == 0) {
124             setErrorMessage(null);
125             return false;
126         }
127
128         // check for empty extension if there is no name
129
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         // check for characters before *
138
// or no other characters
139
// or next chatacter not '.'
140
// or another *
141
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     /**
162      * Get the extension.
163      *
164      * @return the extension
165      */

166     public String JavaDoc getExtension() {
167         // We need kernel api to validate the extension or a filename
168

169         int index = filename.lastIndexOf('.');
170         if (index == -1) {
171             return ""; //$NON-NLS-1$
172
}
173         if (index == filename.length()) {
174             return ""; //$NON-NLS-1$
175
}
176         return filename.substring(index + 1, filename.length());
177     }
178
179     /**
180      * Get the name.
181      *
182      * @return the name
183      */

184     public String JavaDoc getName() {
185         // We need kernel api to validate the extension or a filename
186

187         int index = filename.lastIndexOf('.');
188         if (index == -1) {
189             return filename;
190         }
191         if (index == 0) {
192             return "*"; //$NON-NLS-1$
193
}
194         return filename.substring(0, index);
195     }
196     
197    
198     /* (non-Javadoc)
199      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
200      */

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