KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.resources.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.jface.dialogs.*;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.ui.dialogs.WizardExportResourcesPage;
27 import org.eclipse.ui.help.WorkbenchHelp;
28
29 /**
30  * Page 1 of the base resource export-to-file-system Wizard
31  */

32 /*package*/ class WizardFileSystemResourceExportPage1 extends WizardExportResourcesPage implements Listener {
33
34     // widgets
35
private Combo destinationNameField;
36     private Button destinationBrowseButton;
37     protected Button overwriteExistingFilesCheckbox;
38     protected Button createDirectoryStructureButton;
39     protected Button createSelectionOnlyButton;
40
41     // dialog store id constants
42
private static final String JavaDoc STORE_DESTINATION_NAMES_ID =
43         "WizardFileSystemResourceExportPage1.STORE_DESTINATION_NAMES_ID"; //$NON-NLS-1$
44
private static final String JavaDoc STORE_OVERWRITE_EXISTING_FILES_ID =
45         "WizardFileSystemResourceExportPage1.STORE_OVERWRITE_EXISTING_FILES_ID"; //$NON-NLS-1$
46
private static final String JavaDoc STORE_CREATE_STRUCTURE_ID =
47         "WizardFileSystemResourceExportPage1.STORE_CREATE_STRUCTURE_ID"; //$NON-NLS-1$
48
//messages
49
private static final String JavaDoc SELECT_DESTINATION_MESSAGE = DataTransferMessages.getString("FileExport.selectDestinationMessage"); //$NON-NLS-1$
50
private static final String JavaDoc SELECT_DESTINATION_TITLE = DataTransferMessages.getString("FileExport.selectDestinationTitle"); //$NON-NLS-1$
51
/**
52      * Create an instance of this class
53      */

54     protected WizardFileSystemResourceExportPage1(
55         String JavaDoc name,
56         IStructuredSelection selection) {
57         super(name, selection);
58     }
59     /**
60      * Create an instance of this class
61      */

62     public WizardFileSystemResourceExportPage1(IStructuredSelection selection) {
63         this("fileSystemExportPage1", selection); //$NON-NLS-1$
64
setTitle(DataTransferMessages.getString("DataTransfer.fileSystemTitle")); //$NON-NLS-1$
65
setDescription(DataTransferMessages.getString("FileExport.exportLocalFileSystem")); //$NON-NLS-1$
66
}
67     /**
68      * Add the passed value to self's destination widget's history
69      *
70      * @param value java.lang.String
71      */

72     protected void addDestinationItem(String JavaDoc value) {
73         destinationNameField.add(value);
74     }
75     /** (non-Javadoc)
76      * Method declared on IDialogPage.
77      */

78     public void createControl(Composite parent) {
79         super.createControl(parent);
80         giveFocusToDestination();
81         WorkbenchHelp.setHelp(
82             getControl(),
83             IDataTransferHelpContextIds.FILE_SYSTEM_EXPORT_WIZARD_PAGE);
84     }
85     /**
86      * Create the export destination specification widgets
87      *
88      * @param parent org.eclipse.swt.widgets.Composite
89      */

90     protected void createDestinationGroup(Composite parent) {
91
92         Font font = parent.getFont();
93         // destination specification group
94
Composite destinationSelectionGroup = new Composite(parent, SWT.NONE);
95         GridLayout layout = new GridLayout();
96         layout.numColumns = 3;
97         destinationSelectionGroup.setLayout(layout);
98         destinationSelectionGroup.setLayoutData(
99             new GridData(
100                 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
101         destinationSelectionGroup.setFont(font);
102
103         Label destinationLabel = new Label(destinationSelectionGroup, SWT.NONE);
104         destinationLabel.setText(getDestinationLabel());
105         destinationLabel.setFont(font);
106
107         // destination name entry field
108
destinationNameField =
109             new Combo(destinationSelectionGroup, SWT.SINGLE | SWT.BORDER);
110         destinationNameField.addListener(SWT.Modify, this);
111         destinationNameField.addListener(SWT.Selection, this);
112         GridData data =
113             new GridData(
114                 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
115         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
116         destinationNameField.setLayoutData(data);
117         destinationNameField.setFont(font);
118
119         // destination browse button
120
destinationBrowseButton =
121             new Button(destinationSelectionGroup, SWT.PUSH);
122         destinationBrowseButton.setText(DataTransferMessages.getString("DataTransfer.browse")); //$NON-NLS-1$
123
destinationBrowseButton.addListener(SWT.Selection, this);
124         destinationBrowseButton.setFont(font);
125         setButtonLayoutData(destinationBrowseButton);
126
127         new Label(parent, SWT.NONE); // vertical spacer
128
}
129
130     /**
131      * Create the buttons in the options group.
132      */

133
134     protected void createOptionsGroupButtons(Group optionsGroup) {
135
136         Font font = optionsGroup.getFont();
137         createOverwriteExisting(optionsGroup, font);
138
139         createDirectoryStructureOptions(optionsGroup, font);
140     }
141
142     /**
143      * Create the buttons for the group that determine if the entire or
144      * selected directory structure should be created.
145      * @param optionsGroup
146      * @param font
147      */

148     protected void createDirectoryStructureOptions(
149         Group optionsGroup,
150         Font font) {
151         // create directory structure radios
152
createDirectoryStructureButton =
153             new Button(optionsGroup, SWT.RADIO | SWT.LEFT);
154         createDirectoryStructureButton.setText(DataTransferMessages.getString("FileExport.createDirectoryStructure")); //$NON-NLS-1$
155
createDirectoryStructureButton.setSelection(false);
156         createDirectoryStructureButton.setFont(font);
157
158         // create directory structure radios
159
createSelectionOnlyButton =
160             new Button(optionsGroup, SWT.RADIO | SWT.LEFT);
161         createSelectionOnlyButton.setText(
162             DataTransferMessages.getString(
163                 "FileExport.createSelectedDirectories"));//$NON-NLS-1$
164
createSelectionOnlyButton.setSelection(true);
165         createSelectionOnlyButton.setFont(font);
166     }
167
168     /**
169      * Create the button for checking if we should ask if we are going to
170      * overwrite existing files.
171      * @param optionsGroup
172      * @param font
173      */

174     protected void createOverwriteExisting(Group optionsGroup, Font font) {
175         // overwrite... checkbox
176
overwriteExistingFilesCheckbox =
177             new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
178         overwriteExistingFilesCheckbox.setText(DataTransferMessages.getString("ExportFile.overwriteExisting")); //$NON-NLS-1$
179
overwriteExistingFilesCheckbox.setFont(font);
180     }
181
182     /**
183      * Attempts to ensure that the specified directory exists on the local file system.
184      * Answers a boolean indicating success.
185      *
186      * @return boolean
187      * @param directory java.io.File
188      */

189     protected boolean ensureDirectoryExists(File JavaDoc directory) {
190         if (!directory.exists()) {
191             if (!queryYesNoQuestion(DataTransferMessages.getString("DataTransfer.createTargetDirectory"))) //$NON-NLS-1$
192
return false;
193
194             if (!directory.mkdirs()) {
195                 displayErrorDialog(DataTransferMessages.getString("DataTransfer.directoryCreationError")); //$NON-NLS-1$
196
giveFocusToDestination();
197                 return false;
198             }
199         }
200
201         return true;
202     }
203     /**
204      * If the target for export does not exist then attempt to create it.
205      * Answer a boolean indicating whether the target exists (ie.- if it
206      * either pre-existed or this method was able to create it)
207      *
208      * @return boolean
209      */

210     protected boolean ensureTargetIsValid(File JavaDoc targetDirectory) {
211         if (targetDirectory.exists() && !targetDirectory.isDirectory()) {
212             displayErrorDialog(DataTransferMessages.getString("FileExport.directoryExists")); //$NON-NLS-1$
213
giveFocusToDestination();
214             return false;
215         }
216
217         return ensureDirectoryExists(targetDirectory);
218     }
219     /**
220      * Set up and execute the passed Operation. Answer a boolean indicating success.
221      *
222      * @return boolean
223      */

224     protected boolean executeExportOperation(FileSystemExportOperation op) {
225         op.setCreateLeadupStructure(
226             createDirectoryStructureButton.getSelection());
227         op.setOverwriteFiles(overwriteExistingFilesCheckbox.getSelection());
228
229         try {
230             getContainer().run(true, true, op);
231         } catch (InterruptedException JavaDoc e) {
232             return false;
233         } catch (InvocationTargetException JavaDoc e) {
234             displayErrorDialog(e.getTargetException());
235             return false;
236         }
237
238         IStatus status = op.getStatus();
239         if (!status.isOK()) {
240             ErrorDialog.openError(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.exportProblems"), //$NON-NLS-1$
241
null, // no special message
242
status);
243             return false;
244         }
245
246         return true;
247     }
248     /**
249      * The Finish button was pressed. Try to do the required work now and answer
250      * a boolean indicating success. If false is returned then the wizard will
251      * not close.
252      *
253      * @return boolean
254      */

255     public boolean finish() {
256         if (!ensureTargetIsValid(new File JavaDoc(getDestinationValue())))
257             return false;
258
259         List JavaDoc resourcesToExport = getWhiteCheckedResources();
260
261         //Save dirty editors if possible but do not stop if not all are saved
262
saveDirtyEditors();
263         // about to invoke the operation so save our state
264
saveWidgetValues();
265
266         if (resourcesToExport.size() > 0)
267             return executeExportOperation(
268                 new FileSystemExportOperation(
269                     null,
270                     resourcesToExport,
271                     getDestinationValue(),
272                     this));
273
274         MessageDialog.openInformation(getContainer().getShell(), DataTransferMessages.getString("DataTransfer.information"), //$NON-NLS-1$
275
DataTransferMessages.getString("FileExport.noneSelected")); //$NON-NLS-1$
276

277         return false;
278     }
279     /**
280      * Answer the string to display in self as the destination type
281      *
282      * @return java.lang.String
283      */

284     protected String JavaDoc getDestinationLabel() {
285         return DataTransferMessages.getString("FileExport.toDirectory"); //$NON-NLS-1$
286
}
287     /**
288      * Answer the contents of self's destination specification widget
289      *
290      * @return java.lang.String
291      */

292     protected String JavaDoc getDestinationValue() {
293         return destinationNameField.getText().trim();
294     }
295     /**
296      * Set the current input focus to self's destination entry field
297      */

298     protected void giveFocusToDestination() {
299         destinationNameField.setFocus();
300     }
301     /**
302      * Open an appropriate destination browser so that the user can specify a source
303      * to import from
304      */

305     protected void handleDestinationBrowseButtonPressed() {
306         DirectoryDialog dialog =
307             new DirectoryDialog(getContainer().getShell(), SWT.SAVE);
308         dialog.setMessage(SELECT_DESTINATION_MESSAGE);
309         dialog.setText(SELECT_DESTINATION_TITLE);
310         dialog.setFilterPath(getDestinationValue());
311         String JavaDoc selectedDirectoryName = dialog.open();
312
313         if (selectedDirectoryName != null) {
314             setErrorMessage(null);
315             setDestinationValue(selectedDirectoryName);
316         }
317     }
318     /**
319      * Handle all events and enablements for widgets in this page
320      * @param e Event
321      */

322     public void handleEvent(Event e) {
323         Widget source = e.widget;
324
325         if (source == destinationBrowseButton)
326             handleDestinationBrowseButtonPressed();
327
328         updatePageCompletion();
329     }
330     /**
331      * Hook method for saving widget values for restoration by the next instance
332      * of this class.
333      */

334     protected void internalSaveWidgetValues() {
335         // update directory names history
336
IDialogSettings settings = getDialogSettings();
337         if (settings != null) {
338             String JavaDoc[] directoryNames =
339                 settings.getArray(STORE_DESTINATION_NAMES_ID);
340             if (directoryNames == null)
341                 directoryNames = new String JavaDoc[0];
342
343             directoryNames =
344                 addToHistory(directoryNames, getDestinationValue());
345             settings.put(STORE_DESTINATION_NAMES_ID, directoryNames);
346
347             // options
348
settings.put(
349                 STORE_OVERWRITE_EXISTING_FILES_ID,
350                 overwriteExistingFilesCheckbox.getSelection());
351
352             settings.put(
353                 STORE_CREATE_STRUCTURE_ID,
354                 createDirectoryStructureButton.getSelection());
355
356         }
357     }
358     /**
359      * Hook method for restoring widget values to the values that they held
360      * last time this wizard was used to completion.
361      */

362     protected void restoreWidgetValues() {
363         IDialogSettings settings = getDialogSettings();
364         if (settings != null) {
365             String JavaDoc[] directoryNames =
366                 settings.getArray(STORE_DESTINATION_NAMES_ID);
367             if (directoryNames == null)
368                 return; // ie.- no settings stored
369

370             // destination
371
setDestinationValue(directoryNames[0]);
372             for (int i = 0; i < directoryNames.length; i++)
373                 addDestinationItem(directoryNames[i]);
374
375             // options
376
overwriteExistingFilesCheckbox.setSelection(
377                 settings.getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID));
378
379             boolean createDirectories =
380                 settings.getBoolean(STORE_CREATE_STRUCTURE_ID);
381             createDirectoryStructureButton.setSelection(createDirectories);
382             createSelectionOnlyButton.setSelection(!createDirectories);
383         }
384     }
385     /**
386      * Set the contents of the receivers destination specification widget to
387      * the passed value
388      *
389      */

390     protected void setDestinationValue(String JavaDoc value) {
391         destinationNameField.setText(value);
392     }
393     /**
394      * Answer a boolean indicating whether the receivers destination specification
395      * widgets currently all contain valid values.
396      */

397     protected boolean validateDestinationGroup() {
398         String JavaDoc destinationValue = getDestinationValue();
399         if (destinationValue.length() == 0) {
400             setMessage(destinationEmptyMessage());
401             return false;
402         }
403
404         String JavaDoc conflictingContainer =
405             getConflictingContainerNameFor(destinationValue);
406         if (conflictingContainer == null)
407             setErrorMessage(null);
408         else {
409             setErrorMessage(DataTransferMessages.format("FileExport.conflictingContainer", //$NON-NLS-1$
410
new Object JavaDoc[] { conflictingContainer }));
411             giveFocusToDestination();
412             return false;
413         }
414
415         return true;
416     }
417     
418     /**
419      * Get the message used to denote an empty destination.
420      */

421     protected String JavaDoc destinationEmptyMessage(){
422         return DataTransferMessages.getString("FileExport.destinationEmpty"); //$NON-NLS-1$
423
}
424
425     /**
426      * Returns the name of a container with a location that encompasses targetDirectory.
427      * Returns null if there is no conflict.
428      *
429      * @param targetDirectory the path of the directory to check.
430      * @return the conflicting container name or <code>null</code>
431      */

432     protected String JavaDoc getConflictingContainerNameFor(String JavaDoc targetDirectory) {
433
434         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
435         IPath testPath = new Path(targetDirectory);
436
437         if (root.getLocation().isPrefixOf(testPath))
438             return DataTransferMessages.getString("FileExport.rootName"); //$NON-NLS-1$
439

440         IProject[] projects = root.getProjects();
441
442         for (int i = 0; i < projects.length; i++) {
443             if (projects[i].getLocation().isPrefixOf(testPath))
444                 return projects[i].getName();
445         }
446
447         return null;
448
449     }
450
451 }
452
Popular Tags