KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.core.runtime.Preferences;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.preference.IPreferenceNode;
24 import org.eclipse.jface.preference.IPreferencePage;
25 import org.eclipse.jface.preference.PreferenceDialog;
26 import org.eclipse.jface.preference.PreferenceManager;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.jface.wizard.Wizard;
29 import org.eclipse.swt.custom.BusyIndicator;
30 import org.eclipse.ui.internal.WorkbenchMessages;
31 import org.eclipse.ui.internal.WorkbenchPlugin;
32
33 /**
34  * The wizard responsible for handling both the import and export of
35  * preferences to/from files. There is a lot of overlap in functionality, so
36  * these wizards are simply implemented as one wizard with two modes.
37  *
38  * @since 3.0
39  */

40 public class PreferenceImportExportWizard extends Wizard {
41
42     /**
43      * Whether this wizard should export. Once set, this value will not change
44      * during the life of the wizard.
45      */

46     private final boolean export;
47     /**
48      * The page containing the file selection controls. This is the first page
49      * shown to the user (and sometimes the only page). This value should not
50      * be <code>null</code> after the pages have been added.
51      */

52     private PreferenceImportExportFileSelectionPage fileSelectionPage;
53     /**
54      * The dialog which opened this wizard. This is used to get a handle on the
55      * preferences. This value should never be <code>null</code>.
56      */

57     private final PreferenceDialog parent;
58     
59     /**
60      * A flag representing success/failure of an operation.
61      */

62     private boolean success;
63     /**
64      * The selected file path.
65      */

66     private String JavaDoc selectedFilePath;
67     /**
68      * The selected file.
69      */

70     private File JavaDoc selectedFile;
71     /**
72      * The time at which the file was last modified.
73      */

74     private long lastModified;
75
76     /**
77      * Constructs a new instance of <code>PreferenceImportExportWizard</code>
78      * with the mode and parent dialog.
79      *
80      * @param exportWizard
81      * Whether the wizard should act as an export tool.
82      * @param parentDialog
83      * The dialog which created this wizard (<em>not</em> the
84      * wizard dialog itself). This parameter should not be <code>null</code>.
85      */

86     public PreferenceImportExportWizard(final boolean exportWizard,
87             PreferenceDialog parentDialog) {
88         super();
89         export = exportWizard;
90         parent = parentDialog;
91         if (exportWizard) {
92             setWindowTitle(WorkbenchMessages.getString("ImportExportPages.exportWindowTitle")); //$NON-NLS-1$
93
} else {
94             setWindowTitle(WorkbenchMessages.getString("ImportExportPages.importWindowTitle")); //$NON-NLS-1$
95
}
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.eclipse.jface.wizard.IWizard#addPages()
102      */

103     public void addPages() {
104         super.addPages();
105         fileSelectionPage = new PreferenceImportExportFileSelectionPage(export);
106         addPage(fileSelectionPage);
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.eclipse.jface.wizard.IWizard#canFinish()
113      */

114     public boolean canFinish() {
115         return fileSelectionPage.canFinish();
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.eclipse.jface.wizard.IWizard#performFinish()
122      */

123     public boolean performFinish() {
124         success = true;
125         BusyIndicator.showWhile(getShell().getDisplay(), new Runnable JavaDoc() {
126             public void run() {
127                 // Save all the pages and give them a chance to abort
128
success = saveAllPages();
129                 if (!success)
130                     return;
131                 // Save or load -- depending on the phase of moon.
132
IPath path = new Path(selectedFilePath);
133                 if (export) {
134                     success = exportFile(path);
135                     if (!success)
136                         return;
137                 } else {
138                     success = importFile(path);
139                     if (!success)
140                         return;
141                 }
142             }
143         });
144         // If an operation failed, return false
145
if (!success)
146             return success;
147         // See if we actually created a file (there where preferences to export)
148
showMessageDialog();
149         // We have been successful!
150
WorkbenchPlugin.getDefault().getDialogSettings().put(
151                 WorkbenchPreferenceDialog.FILE_PATH_SETTING,
152                 fileSelectionPage.getPath());
153         return true;
154     }
155     /**
156      * Save all the preference pages.
157      *
158      * @return true if successful.
159      */

160     private boolean saveAllPages() {
161         Iterator JavaDoc nodes = parent.getPreferenceManager().getElements(
162                 PreferenceManager.PRE_ORDER).iterator();
163         while (nodes.hasNext()) {
164             IPreferenceNode node = (IPreferenceNode) nodes.next();
165             IPreferencePage page = node.getPage();
166             if (page != null) {
167                 if (!page.performOk())
168                     return false;
169             }
170         }
171         selectedFilePath = fileSelectionPage.getPath();
172         selectedFile = new File JavaDoc(selectedFilePath);
173         lastModified = selectedFile.lastModified();
174         return true;
175     }
176
177     /**
178      * Export the preferences to a file.
179      *
180      * @param path
181      * The file path.
182      * @return true if successful.
183      */

184     private boolean exportFile(IPath path) {
185         if (selectedFile.exists()) {
186             if (!MessageDialog.openConfirm(getShell(), WorkbenchMessages
187                     .getString("WorkbenchPreferenceDialog.saveTitle"), //$NON-NLS-1$
188
WorkbenchMessages.format(
189                             "WorkbenchPreferenceDialog.existsErrorMessage", //$NON-NLS-1$
190
new Object JavaDoc[]{selectedFilePath})))
191                 return false;
192         }
193
194         try {
195             Preferences.exportPreferences(path);
196         } catch (CoreException e) {
197             ErrorDialog.openError(getShell(), WorkbenchMessages
198                     .getString("WorkbenchPreferenceDialog.saveErrorTitle"), //$NON-NLS-1$
199
WorkbenchMessages.format(
200                             "WorkbenchPreferenceDialog.saveErrorMessage", //$NON-NLS-1$
201
new Object JavaDoc[]{selectedFilePath}), e.getStatus());
202             return false;
203         }
204         return true;
205     }
206
207     /**
208      * Import a preference file.
209      *
210      * @param path
211      * The file path.
212      * @return true if successful.
213      */

214     private boolean importFile(IPath path) {
215         IStatus status = Preferences.validatePreferenceVersions(path);
216         if (status.getSeverity() == IStatus.ERROR) {
217             // Show the error and about
218
ErrorDialog.openError(getShell(), WorkbenchMessages
219                     .getString("WorkbenchPreferenceDialog.loadErrorTitle"), //$NON-NLS-1$
220
WorkbenchMessages.format(
221                             "WorkbenchPreferenceDialog.verifyErrorMessage", //$NON-NLS-1$
222
new Object JavaDoc[]{selectedFilePath}), status);
223             return false;
224         } else if (status.getSeverity() == IStatus.WARNING) {
225             // Show the warning and give the option to continue
226
int result = PreferenceErrorDialog
227                     .openError(
228                             getShell(),
229                             WorkbenchMessages
230                                     .getString("WorkbenchPreferenceDialog.loadErrorTitle"), //$NON-NLS-1$
231
WorkbenchMessages
232                                     .format(
233                                             "WorkbenchPreferenceDialog.verifyWarningMessage", //$NON-NLS-1$
234
new Object JavaDoc[]{selectedFilePath}),
235                             status);
236             if (result != Window.OK) {
237                 return false;
238             }
239         }
240
241         try {
242             Preferences.importPreferences(path);
243         } catch (CoreException e) {
244             ErrorDialog.openError(getShell(), WorkbenchMessages
245                     .getString("WorkbenchPreferenceDialog.loadErrorTitle"), //$NON-NLS-1$
246
WorkbenchMessages.format(
247                             "WorkbenchPreferenceDialog.loadErrorMessage", //$NON-NLS-1$
248
new Object JavaDoc[]{selectedFilePath}), e.getStatus());
249             return false;
250         }
251         return true;
252     }
253
254     /**
255      * Show the appropriate message dialog.
256      *
257      */

258     private void showMessageDialog() {
259         if (!export) {
260             MessageDialog.openInformation(getShell(), WorkbenchMessages
261                     .getString("WorkbenchPreferenceDialog.loadTitle"), //$NON-NLS-1$
262
WorkbenchMessages.format(
263                             "WorkbenchPreferenceDialog.loadMessage", //$NON-NLS-1$
264
new Object JavaDoc[]{selectedFilePath}));
265
266         } else if ((selectedFile.exists() && (selectedFile.lastModified() != lastModified))) {
267             MessageDialog.openInformation(getShell(), WorkbenchMessages
268                     .getString("WorkbenchPreferenceDialog.saveTitle"), //$NON-NLS-1$
269
WorkbenchMessages.format(
270                             "WorkbenchPreferenceDialog.saveMessage", //$NON-NLS-1$
271
new Object JavaDoc[]{selectedFilePath}));
272
273         } else {
274             MessageDialog
275                     .openError(
276                             getShell(),
277                             WorkbenchMessages
278                                     .getString("WorkbenchPreferenceDialog.saveErrorTitle"), //$NON-NLS-1$
279
WorkbenchMessages
280                                     .getString("WorkbenchPreferenceDialog.noPreferencesMessage")); //$NON-NLS-1$
281
}
282     }
283 }
284
Popular Tags