1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.io.File ; 14 import java.util.Iterator ; 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 40 public class PreferenceImportExportWizard extends Wizard { 41 42 46 private final boolean export; 47 52 private PreferenceImportExportFileSelectionPage fileSelectionPage; 53 57 private final PreferenceDialog parent; 58 59 62 private boolean success; 63 66 private String selectedFilePath; 67 70 private File selectedFile; 71 74 private long lastModified; 75 76 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")); } else { 94 setWindowTitle(WorkbenchMessages.getString("ImportExportPages.importWindowTitle")); } 96 } 97 98 103 public void addPages() { 104 super.addPages(); 105 fileSelectionPage = new PreferenceImportExportFileSelectionPage(export); 106 addPage(fileSelectionPage); 107 } 108 109 114 public boolean canFinish() { 115 return fileSelectionPage.canFinish(); 116 } 117 118 123 public boolean performFinish() { 124 success = true; 125 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable () { 126 public void run() { 127 success = saveAllPages(); 129 if (!success) 130 return; 131 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 (!success) 146 return success; 147 showMessageDialog(); 149 WorkbenchPlugin.getDefault().getDialogSettings().put( 151 WorkbenchPreferenceDialog.FILE_PATH_SETTING, 152 fileSelectionPage.getPath()); 153 return true; 154 } 155 160 private boolean saveAllPages() { 161 Iterator 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 (selectedFilePath); 173 lastModified = selectedFile.lastModified(); 174 return true; 175 } 176 177 184 private boolean exportFile(IPath path) { 185 if (selectedFile.exists()) { 186 if (!MessageDialog.openConfirm(getShell(), WorkbenchMessages 187 .getString("WorkbenchPreferenceDialog.saveTitle"), WorkbenchMessages.format( 189 "WorkbenchPreferenceDialog.existsErrorMessage", new Object []{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"), WorkbenchMessages.format( 200 "WorkbenchPreferenceDialog.saveErrorMessage", new Object []{selectedFilePath}), e.getStatus()); 202 return false; 203 } 204 return true; 205 } 206 207 214 private boolean importFile(IPath path) { 215 IStatus status = Preferences.validatePreferenceVersions(path); 216 if (status.getSeverity() == IStatus.ERROR) { 217 ErrorDialog.openError(getShell(), WorkbenchMessages 219 .getString("WorkbenchPreferenceDialog.loadErrorTitle"), WorkbenchMessages.format( 221 "WorkbenchPreferenceDialog.verifyErrorMessage", new Object []{selectedFilePath}), status); 223 return false; 224 } else if (status.getSeverity() == IStatus.WARNING) { 225 int result = PreferenceErrorDialog 227 .openError( 228 getShell(), 229 WorkbenchMessages 230 .getString("WorkbenchPreferenceDialog.loadErrorTitle"), WorkbenchMessages 232 .format( 233 "WorkbenchPreferenceDialog.verifyWarningMessage", new Object []{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"), WorkbenchMessages.format( 247 "WorkbenchPreferenceDialog.loadErrorMessage", new Object []{selectedFilePath}), e.getStatus()); 249 return false; 250 } 251 return true; 252 } 253 254 258 private void showMessageDialog() { 259 if (!export) { 260 MessageDialog.openInformation(getShell(), WorkbenchMessages 261 .getString("WorkbenchPreferenceDialog.loadTitle"), WorkbenchMessages.format( 263 "WorkbenchPreferenceDialog.loadMessage", new Object []{selectedFilePath})); 265 266 } else if ((selectedFile.exists() && (selectedFile.lastModified() != lastModified))) { 267 MessageDialog.openInformation(getShell(), WorkbenchMessages 268 .getString("WorkbenchPreferenceDialog.saveTitle"), WorkbenchMessages.format( 270 "WorkbenchPreferenceDialog.saveMessage", new Object []{selectedFilePath})); 272 273 } else { 274 MessageDialog 275 .openError( 276 getShell(), 277 WorkbenchMessages 278 .getString("WorkbenchPreferenceDialog.saveErrorTitle"), WorkbenchMessages 280 .getString("WorkbenchPreferenceDialog.noPreferencesMessage")); } 282 } 283 } 284 | Popular Tags |