KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > datatransfer > WizardFileSystemExportPage1


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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.wizards.datatransfer;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.dialogs.*;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.ui.dialogs.WizardExportPage;
25
26 /**
27  * Page 1 of the base resource export-to-file-system Wizard
28  * @deprecated use WizardFileSystemResourceExportPage1
29  */

30 /*package*/ class WizardFileSystemExportPage1 extends WizardExportPage implements Listener {
31
32     // widgets
33
private Combo destinationNameField;
34     private Button destinationBrowseButton;
35     private Button overwriteExistingFilesCheckbox;
36     private Button createDirectoryStructureCheckbox;
37     private Button createDirectoriesForSelectedContainersCheckbox;
38
39     // constants
40
private static final int SIZING_TEXT_FIELD_WIDTH = 250;
41     
42     // dialog store id constants
43
private static final String JavaDoc STORE_DESTINATION_NAMES_ID = "WizardFileSystemExportPage1.STORE_DESTINATION_NAMES_ID";//$NON-NLS-1$
44
private static final String JavaDoc STORE_OVERWRITE_EXISTING_FILES_ID = "WizardFileSystemExportPage1.STORE_OVERWRITE_EXISTING_FILES_ID";//$NON-NLS-1$
45
private static final String JavaDoc STORE_CREATE_STRUCTURE_ID = "WizardFileSystemExportPage1.STORE_CREATE_STRUCTURE_ID";//$NON-NLS-1$
46
private static final String JavaDoc STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID = "WizardFileSystemExportPage1.STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID";//$NON-NLS-1$
47
/**
48  * Create an instance of this class
49  */

50 protected WizardFileSystemExportPage1(String JavaDoc name, IStructuredSelection selection) {
51     super(name, selection);
52 }
53 /**
54  * Create an instance of this class
55  */

56 public WizardFileSystemExportPage1(IStructuredSelection selection) {
57     this("fileSystemExportPage1", selection);//$NON-NLS-1$
58
setTitle(DataTransferMessages.getString("DataTransfer.fileSystemTitle")); //$NON-NLS-1$
59
setDescription(DataTransferMessages.getString("FileExport.exportLocalFileSystem")); //$NON-NLS-1$
60
}
61 /**
62  * Add the passed value to self's destination widget's history
63  *
64  * @param value java.lang.String
65  */

66 protected void addDestinationItem(String JavaDoc value) {
67     destinationNameField.add(value);
68 }
69 /** (non-Javadoc)
70  * Method declared on IDialogPage.
71  */

72 public void createControl(Composite parent) {
73     super.createControl(parent);
74     giveFocusToDestination();
75 }
76 /**
77  * Create the export destination specification widgets
78  *
79  * @param parent org.eclipse.swt.widgets.Composite
80  */

81 protected void createDestinationGroup(Composite parent) {
82     // destination specification group
83
Composite destinationSelectionGroup = new Composite(parent, SWT.NONE);
84     GridLayout layout = new GridLayout();
85     layout.numColumns = 3;
86     destinationSelectionGroup.setLayout(layout);
87     destinationSelectionGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
88
89     new Label(destinationSelectionGroup, SWT.NONE).setText(getDestinationLabel());
90
91     // destination name entry field
92
destinationNameField = new Combo(destinationSelectionGroup, SWT.SINGLE | SWT.BORDER);
93     destinationNameField.addListener(SWT.Modify, this);
94     destinationNameField.addListener(SWT.Selection, this);
95     GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
96     data.widthHint = SIZING_TEXT_FIELD_WIDTH;
97     destinationNameField.setLayoutData(data);
98
99     // destination browse button
100
destinationBrowseButton = new Button(destinationSelectionGroup, SWT.PUSH);
101     destinationBrowseButton.setText(DataTransferMessages.getString("DataTransfer.browse")); //$NON-NLS-1$
102
destinationBrowseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
103     destinationBrowseButton.addListener(SWT.Selection, this);
104
105     new Label(parent, SWT.NONE); // vertical spacer
106
}
107 /**
108  * Create the export options specification widgets.
109  *
110  * @param parent org.eclipse.swt.widgets.Composite
111  */

112 protected void createOptionsGroup(Composite parent) {
113     // options group
114
Composite optionsGroup = new Composite(parent, SWT.NONE);
115     GridLayout layout = new GridLayout();
116     layout.marginHeight = 0;
117     optionsGroup.setLayout(layout);
118     optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
119
120     // overwrite... checkbox
121
overwriteExistingFilesCheckbox = new Button(optionsGroup,SWT.CHECK|SWT.LEFT);
122     overwriteExistingFilesCheckbox.setText(DataTransferMessages.getString("ExportFile.overwriteExisting")); //$NON-NLS-1$
123

124     // create directory structure checkbox
125
createDirectoryStructureCheckbox = new Button(optionsGroup,SWT.CHECK|SWT.LEFT);
126     createDirectoryStructureCheckbox.setText(DataTransferMessages.getString("ExportFile.createDirectoryStructure")); //$NON-NLS-1$
127
createDirectoryStructureCheckbox.addListener(SWT.Selection,this);
128
129     // create directory for container checkbox
130
createDirectoriesForSelectedContainersCheckbox = new Button(optionsGroup,SWT.CHECK|SWT.LEFT);
131     createDirectoriesForSelectedContainersCheckbox.setText(DataTransferMessages.getString("ExportFile.createDirectoriesForSelected")); //$NON-NLS-1$
132

133     // initial setup
134
createDirectoryStructureCheckbox.setSelection(true);
135 }
136 /**
137  * Attempts to ensure that the specified directory exists on the local file system.
138  * Answers a boolean indicating success.
139  *
140  * @return boolean
141  * @param directory java.io.File
142  */

143 protected boolean ensureDirectoryExists(File JavaDoc directory) {
144     if (!directory.exists()) {
145         if (!queryYesNoQuestion(DataTransferMessages.getString("DataTransfer.createTargetDirectory"))) //$NON-NLS-1$
146
return false;
147
148         if (!directory.mkdirs()) {
149             displayErrorDialog(DataTransferMessages.getString("DataTransfer.directoryCreationError")); //$NON-NLS-1$
150
giveFocusToDestination();
151             return false;
152         }
153     }
154
155     return true;
156 }
157 /**
158  * If the target for export does not exist then attempt to create it.
159  * Answer a boolean indicating whether the target exists (ie.- if it
160  * either pre-existed or this method was able to create it)
161  *
162  * @return boolean
163  */

164 protected boolean ensureTargetIsValid(File JavaDoc targetDirectory) {
165     if (targetDirectory.exists() && !targetDirectory.isDirectory()) {
166         displayErrorDialog(DataTransferMessages.getString("FileExport.directoryExists")); //$NON-NLS-1$
167
giveFocusToDestination();
168         return false;
169     }
170
171     return ensureDirectoryExists(targetDirectory);
172 }
173 /**
174  * Set up and execute the passed Operation. Answer a boolean indicating success.
175  *
176  * @return boolean
177  */

178 protected boolean executeExportOperation(FileSystemExportOperation op) {
179     op.setCreateContainerDirectories(createDirectoriesForSelectedContainersCheckbox.getSelection());
180     op.setCreateLeadupStructure(createDirectoryStructureCheckbox.getSelection());
181     op.setOverwriteFiles(overwriteExistingFilesCheckbox.getSelection());
182     
183     try {
184         getContainer().run(true, true, op);
185     } catch (InterruptedException JavaDoc e) {
186         return false;
187     } catch (InvocationTargetException JavaDoc e) {
188         displayErrorDialog(e.getTargetException().getMessage());
189         return false;
190     }
191
192     IStatus status = op.getStatus();
193     if (!status.isOK()) {
194         ErrorDialog.openError(getContainer().getShell(),
195             DataTransferMessages.getString("DataTransfer.exportProblems"), //$NON-NLS-1$
196
null, // no special message
197
status);
198         return false;
199     }
200     
201     return true;
202 }
203 /**
204  * The Finish button was pressed. Try to do the required work now and answer
205  * a boolean indicating success. If false is returned then the wizard will
206  * not close.
207  *
208  * @return boolean
209  */

210 public boolean finish() {
211     if (!ensureTargetIsValid(new File JavaDoc(getDestinationValue())))
212         return false;
213
214     List JavaDoc resourcesToExport = getSelectedResources();
215
216     // about to invoke the operation so save our state
217
saveWidgetValues();
218     
219     if (resourcesToExport.size() > 0)
220         return executeExportOperation(
221             new FileSystemExportOperation(
222                 getSourceResource(),
223                 resourcesToExport,
224                 getDestinationValue(),
225                 this));
226
227     MessageDialog.openInformation(
228         getContainer().getShell(),
229         DataTransferMessages.getString("DataTransfer.information"), //$NON-NLS-1$
230
DataTransferMessages.getString("FileExport.noneSelected")); //$NON-NLS-1$
231

232     return false;
233 }
234 /**
235  * Answer the string to display in self as the destination type
236  *
237  * @return java.lang.String
238  */

239 protected String JavaDoc getDestinationLabel() {
240     return DataTransferMessages.getString("DataTransfer.directory"); //$NON-NLS-1$
241
}
242 /**
243  * Answer the contents of self's destination specification widget
244  *
245  * @return java.lang.String
246  */

247 protected String JavaDoc getDestinationValue() {
248     return destinationNameField.getText().trim();
249 }
250 /**
251  * Set the current input focus to self's destination entry field
252  */

253 protected void giveFocusToDestination() {
254     destinationNameField.setFocus();
255 }
256 /**
257  * Open an appropriate destination browser so that the user can specify a source
258  * to import from
259  */

260 protected void handleDestinationBrowseButtonPressed() {
261     DirectoryDialog dialog = new DirectoryDialog(getContainer().getShell(),SWT.SAVE);
262     dialog.setMessage(DataTransferMessages.getString("DataTransfer.selectDestination")); //$NON-NLS-1$
263
dialog.setFilterPath(getDestinationValue());
264     String JavaDoc selectedDirectoryName = dialog.open();
265     
266     if (selectedDirectoryName != null)
267         setDestinationValue(selectedDirectoryName);
268 }
269 /**
270  * Handle all events and enablements for widgets in this page
271  *
272  * @param e org.eclipse.swt.widgets.Event
273  */

274 public void handleEvent(Event e) {
275     Widget source = e.widget;
276
277     if (source == destinationBrowseButton)
278         handleDestinationBrowseButtonPressed();
279
280     super.handleEvent(e);
281 }
282 /**
283  * Hook method for saving widget values for restoration by the next instance
284  * of this class.
285  */

286 protected void internalSaveWidgetValues() {
287     // update directory names history
288
IDialogSettings settings = getDialogSettings();
289     if(settings != null) {
290         String JavaDoc[] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
291         if (directoryNames == null)
292             directoryNames = new String JavaDoc[0];
293
294         directoryNames = addToHistory(directoryNames,getDestinationValue());
295         settings.put(
296             STORE_DESTINATION_NAMES_ID,
297             directoryNames);
298
299         // options
300
settings.put(
301             STORE_OVERWRITE_EXISTING_FILES_ID,
302             overwriteExistingFilesCheckbox.getSelection());
303
304         settings.put(
305             STORE_CREATE_STRUCTURE_ID,
306             createDirectoryStructureCheckbox.getSelection());
307
308         settings.put(
309             STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID,
310             createDirectoriesForSelectedContainersCheckbox.getSelection());
311
312     }
313 }
314 /**
315  * Hook method for restoring widget values to the values that they held
316  * last time this wizard was used to completion.
317  */

318 protected void restoreWidgetValues() {
319     IDialogSettings settings = getDialogSettings();
320     if(settings != null) {
321         String JavaDoc[] directoryNames = settings.getArray(STORE_DESTINATION_NAMES_ID);
322         if (directoryNames == null)
323             return; // ie.- no settings stored
324

325         // destination
326
setDestinationValue(directoryNames[0]);
327         for (int i = 0; i < directoryNames.length; i++)
328             addDestinationItem(directoryNames[i]);
329
330         // options
331
overwriteExistingFilesCheckbox.setSelection(
332             settings.getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID));
333
334         createDirectoryStructureCheckbox.setSelection(
335             settings.getBoolean(STORE_CREATE_STRUCTURE_ID));
336
337         createDirectoriesForSelectedContainersCheckbox.setSelection(
338             settings.getBoolean(STORE_CREATE_DIRECTORIES_FOR_SPECIFIED_CONTAINER_ID));
339     }
340 }
341 /**
342  * Set the contents of self's destination specification widget to
343  * the passed value
344  *
345  * @param value java.lang.String
346  */

347 protected void setDestinationValue(String JavaDoc value) {
348     destinationNameField.setText(value);
349 }
350 /**
351  * Answer a boolean indicating whether self's destination specification
352  * widgets currently all contain valid values.
353  *
354  * @return boolean
355  */

356 protected boolean validateDestinationGroup() {
357     return !getDestinationValue().equals("");//$NON-NLS-1$
358
}
359 }
360
Popular Tags