KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.ui.internal.dialogs;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.wizard.IWizardContainer;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.FileDialog;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Listener;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.ui.internal.WorkbenchMessages;
31 import org.eclipse.ui.internal.WorkbenchPlugin;
32 import org.eclipse.ui.internal.util.Util;
33
34 /**
35  * The page -- used for both importing and exporting -- that allows the user to
36  * select a file, as well as specify how many of the settings they want to use.
37  *
38  * @since 3.0
39  */

40 class PreferenceImportExportFileSelectionPage extends AbstractPreferenceImportExportPage {
41     
42     /**
43      * Listens for changes on the page, and updates the buttons.
44      * @since 3.0
45      */

46     private class PageChangeListener implements Listener {
47         /**
48          * If the current page is not <code>null</code>, then this updates the
49          * buttons on the wizard dialog.
50          * @param event Ignored.
51          */

52         public void handleEvent(Event event) {
53             IWizardContainer container = getContainer();
54             if (container.getCurrentPage() != null) {
55                 container.updateButtons();
56             }
57         }
58     }
59     
60     /**
61      * The accepted extensions for the file chooser dialog.
62      */

63     private static final String JavaDoc[] DIALOG_PREFERENCE_EXTENSIONS = new String JavaDoc[] { "*" + PREFERENCE_EXT, "*.*"}; //$NON-NLS-1$ //$NON-NLS-2$
64
/**
65      * The message to display when there are no errors on this page.
66      */

67     private static final String JavaDoc EXPORT_MESSAGE = WorkbenchMessages.getString("ImportExportPages.exportFileSelect"); //$NON-NLS-1$
68
/**
69      * The message to display when there are no errors on this page.
70      */

71     private static final String JavaDoc IMPORT_MESSAGE = WorkbenchMessages.getString("ImportExportPages.importFileSelect"); //$NON-NLS-1$
72
/**
73      * The name of this page -- used to find the page later.
74      */

75     private static final String JavaDoc NAME = "org.eclipse.ui.preferences.importExportFileSelectionPage"; //$NON-NLS-1$
76
/**
77
78     /**
79      * Updates the state of various widgets in response to changes on the page.
80      */

81     public final Listener changeListener = new PageChangeListener();
82     /**
83      * The text widget containing the absolute path to the file from which to
84      * import or to which to export.
85      */

86     private Text fileText;
87
88
89     /**
90      * Constructs a new instance of the file selection page.
91      * @param exportWizard Whether this page should be opened in export mode.
92      */

93     PreferenceImportExportFileSelectionPage(boolean exportWizard) {
94         super(NAME, exportWizard);
95     }
96     
97     /**
98      * Whether this page can finish. This page can finish if all the data is
99      * valid and the user doesn't want to select the individual preferences.
100      * @return <code>true</code> If the finish button should be enabled;
101      * <code>false</code> otherwise.
102      */

103     boolean canFinish() {
104         return validate();
105     }
106     
107     /**
108      * Chooses the file to use for exporting or importing. This opens a native
109      * file dialog and sets <code>fileText</code> to the user selection.
110      */

111     private void chooseFile() {
112         // Find the closest file/directory to what is currently entered.
113
String JavaDoc currentFileName = fileText.getText();
114         
115         // Open a dialog allowing the user to choose.
116
FileDialog fileDialog = null;
117         if(export)
118             fileDialog = new FileDialog(getShell(), SWT.SAVE);
119         else
120             fileDialog = new FileDialog(getShell(), SWT.OPEN);
121         
122         fileDialog.setFileName(currentFileName);
123         fileDialog.setFilterExtensions(DIALOG_PREFERENCE_EXTENSIONS);
124         currentFileName = fileDialog.open();
125         
126         if (currentFileName == null)
127             return;
128         
129         /* Append the default filename if none was specifed and such a file does
130          * not exist.
131          */

132         String JavaDoc fileName = new File JavaDoc(currentFileName).getName();
133         if (fileName.lastIndexOf(".") == -1) { //$NON-NLS-1$
134
currentFileName += PREFERENCE_EXT;
135         }
136         
137         fileText.setText(currentFileName);
138         canFlipToNextPage();
139     }
140     
141     /*
142      * (non-Javadoc)
143      *
144      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
145      */

146     public void createControl(Composite parent) {
147         Font parentFont = parent.getFont();
148         final Composite page = new Composite(parent, SWT.NONE);
149         GridLayout layout = new GridLayout(3, false);
150         page.setLayout(layout);
151         initializeDialogUnits(page);
152
153         // Set-up the title, subtitle and icon.
154
if (export) {
155             setTitle(EXPORT_TITLE);
156             setMessage(EXPORT_MESSAGE);
157             setImageDescriptor(getImageDescriptor("wizban/export_wiz.gif")); //$NON-NLS-1$
158
} else {
159             setTitle(IMPORT_TITLE);
160             setMessage(IMPORT_MESSAGE);
161             setImageDescriptor(getImageDescriptor("wizban/import_wiz.gif")); //$NON-NLS-1$
162
}
163
164         GridData layoutData;
165
166         // Set up the file selection label.
167
final Label fileLabel = new Label(page, SWT.NONE);
168         fileLabel.setText(WorkbenchMessages.getString("ImportExportPages.fileLabel")); //$NON-NLS-1$
169
fileLabel.setFont(parentFont);
170         layoutData = new GridData();
171         fileLabel.setLayoutData(layoutData);
172
173         // Set up the text widget containing the file selection.
174
fileText = new Text(page, SWT.SINGLE | SWT.BORDER);
175         fileText.setFont(parentFont);
176         layoutData = new GridData();
177         layoutData.grabExcessHorizontalSpace = true;
178         layoutData.horizontalAlignment = GridData.FILL;
179         fileText.setLayoutData(layoutData);
180         fileText.addListener(SWT.Modify, changeListener);
181
182         // Set up the button for choosing a file.
183
final Button browseButton = new Button(page, SWT.PUSH);
184         browseButton.setFont(parentFont);
185         layoutData = new GridData();
186         browseButton.setText(WorkbenchMessages.getString("ImportExportPages.browseButton")); //$NON-NLS-1$
187
layoutData.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
188         layoutData.widthHint = computePushButtonWidthHint(browseButton);
189         browseButton.setLayoutData(layoutData);
190         browseButton.addSelectionListener(new SelectionAdapter() {
191             public final void widgetSelected(SelectionEvent event) {
192                 chooseFile();
193             }
194         });
195
196         // Insert a chunk of space below the file selection stuff.
197
final Composite verticalSpacer = new Composite(page, SWT.NONE);
198         layoutData = new GridData();
199         layoutData.heightHint = 15;
200         layoutData.horizontalSpan = 3;
201         verticalSpacer.setLayoutData(layoutData);
202
203         // Remember the composite as the top-level control.
204
setControl(page);
205         
206         // Restore all the controls to their previous values.
207
init();
208     }
209     
210     /**
211      * An accessor for the path the user has currently selected.
212      * @return The path; may be empty (or invalid), but never <code>null</code>.
213      */

214     String JavaDoc getPath() {
215         return fileText.getText();
216     }
217
218     /**
219      * Initializes all the controls on this page by restoring their previous
220      * values.
221      */

222     private void init() {
223         String JavaDoc lastFileName = WorkbenchPlugin.getDefault().getDialogSettings()
224                 .get(WorkbenchPreferenceDialog.FILE_PATH_SETTING);
225         if (lastFileName == null) {
226             if (export) {
227                 fileText
228                         .setText(System.getProperty("user.dir") + System.getProperty("file.separator") + WorkbenchMessages.getString("ImportExportPages.preferenceFileName") + PREFERENCE_EXT); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
229
}
230         } else if ((export) || (new File JavaDoc(lastFileName).exists())) {
231             fileText.setText(lastFileName);
232         }
233     }
234     
235     /**
236      * Validates all of the data on the page to make sure that it is all valid.
237      * If some of the data is invalid, then it displays an error message on the
238      * page
239      * @return <code>true</code> If the data is all valid; <code>false</code>
240      * otherwise.
241      */

242     boolean validate() {
243         final String JavaDoc fileName = fileText.getText();
244         if (fileName.equals(Util.ZERO_LENGTH_STRING)) {
245             setErrorMessage(null);
246             return false;
247         }
248
249         final File JavaDoc currentFile = new File JavaDoc(fileName);
250         if (export) {
251             final File JavaDoc parentFile = currentFile.getParentFile();
252             if (parentFile == null || !parentFile.exists()) {
253                 setErrorMessage(WorkbenchMessages
254                         .getString("ImportExportPages.errorDirectoryDoesNotExist")); //$NON-NLS-1$
255
return false;
256             }
257         } else {
258             if (!currentFile.exists()) {
259                 setErrorMessage(WorkbenchMessages
260                         .getString("ImportExportPages.errorImportFileDoesNotExist")); //$NON-NLS-1$
261
return false;
262             }
263         }
264
265         setErrorMessage(null);
266         return true;
267     }
268
269     
270 }
271
Popular Tags