1 11 package org.eclipse.ui.internal.wizards.preferences; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.IOException ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.core.runtime.preferences.IExportedPreferences; 21 import org.eclipse.core.runtime.preferences.IPreferenceFilter; 22 import org.eclipse.core.runtime.preferences.IPreferencesService; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Event; 27 import org.eclipse.ui.internal.WorkbenchPlugin; 28 import org.eclipse.ui.internal.preferences.PreferenceTransferElement; 29 30 36 public class WizardPreferencesImportPage1 extends WizardPreferencesPage { 37 38 42 protected WizardPreferencesImportPage1(String pageName) { 43 super(pageName); 44 setTitle(PreferencesMessages.WizardPreferencesImportPage1_importTitle); 45 setDescription(PreferencesMessages.WizardPreferencesImportPage1_importDescription); 46 } 47 48 51 public WizardPreferencesImportPage1() { 52 this("preferencesImportPage1"); } 54 55 58 protected String getAllButtonText() { 59 return PreferencesMessages.WizardPreferencesImportPage1_all; 60 } 61 62 65 protected String getChooseButtonText() { 66 return PreferencesMessages.WizardPreferencesImportPage1_choose; 67 } 68 69 70 73 protected PreferenceTransferElement[] getTransfers() { 74 if (validFromFile()) { 75 FileInputStream fis; 76 77 try { 78 fis = new FileInputStream (getDestinationValue()); 79 } catch (FileNotFoundException e) { 80 WorkbenchPlugin.log(e.getMessage(), e); 81 return new PreferenceTransferElement[0]; 82 } 83 IPreferencesService service = Platform.getPreferencesService(); 84 try { 85 IExportedPreferences prefs; 86 prefs = service.readPreferences(fis); 87 PreferenceTransferElement[] transfers = super.getTransfers(); 88 IPreferenceFilter[] filters = new IPreferenceFilter[transfers.length]; 89 for (int i = 0; i < transfers.length; i++) { 90 PreferenceTransferElement transfer = transfers[i]; 91 filters[i] = transfer.getFilter(); 92 } 93 IPreferenceFilter[] matches = service.matches(prefs, filters); 94 PreferenceTransferElement[] returnTransfers = new PreferenceTransferElement[matches.length]; 95 int index = 0; 96 for (int i = 0; i < matches.length; i++) { 97 IPreferenceFilter filter = matches[i]; 98 for (int j = 0; j < transfers.length; j++) { 99 PreferenceTransferElement element = transfers[j]; 100 if (element.getFilter().equals(filter)) { 101 returnTransfers[index++] = element; 102 } 103 } 104 } 105 106 PreferenceTransferElement[] destTransfers = new PreferenceTransferElement[index]; 107 System.arraycopy(returnTransfers, 0, destTransfers, 0, index); 108 return destTransfers; 109 } catch (CoreException e) { 110 } finally { 113 try { 114 fis.close(); 115 } catch (IOException e) { 116 WorkbenchPlugin.log(e.getMessage(), e); 117 } 118 } 119 } 120 121 return new PreferenceTransferElement[0]; 122 } 123 124 129 private boolean validFromFile() { 130 File fromFile = new File (getDestinationValue()); 131 return fromFile.exists() && !fromFile.isDirectory(); 132 } 133 134 protected void setPreferenceTransfers() { 135 super.setPreferenceTransfers(); 136 137 if(validFromFile() && (transfersTable.getItemCount() == 0)) { 138 text.setText(PreferencesMessages.WizardPreferences_noSpecificPreferenceDescription); 139 } else { 140 text.setText(""); } 142 } 143 144 147 protected void createTransferArea(Composite composite) { 148 createDestinationGroup(composite); 149 createTransfersList(composite); 150 } 151 152 157 protected String getDestinationLabel() { 158 return PreferencesMessages.WizardPreferencesImportPage1_file; 159 } 160 161 166 protected boolean transfer(IPreferenceFilter[] filters) { 167 File importFile = new File (getDestinationValue()); 168 FileInputStream fis = null; 169 try { 170 if (filters.length > 0) { 171 try { 172 fis = new FileInputStream (importFile); 173 } catch (FileNotFoundException e) { 174 WorkbenchPlugin.log(e.getMessage(), e); 175 MessageDialog.openError(getControl().getShell(), new String (), e.getLocalizedMessage()); 176 return false; 177 } 178 IPreferencesService service = Platform.getPreferencesService(); 179 try { 180 IExportedPreferences prefs = service.readPreferences(fis); 181 182 service.applyPreferences(prefs, filters); 183 } catch (CoreException e) { 184 WorkbenchPlugin.log(e.getMessage(), e); 185 MessageDialog.openError(getControl().getShell(), new String (), e.getLocalizedMessage()); 186 return false; 187 } 188 } 189 } finally { 190 if (fis != null) { 191 try { 192 fis.close(); 193 } catch (IOException e) { 194 WorkbenchPlugin.log(e.getMessage(), e); 195 MessageDialog.openError(getControl().getShell(), new String (), e.getLocalizedMessage()); 196 } 197 } 198 } 199 return true; 200 } 201 202 208 public void handleEvent(Event e) { 209 if (e.widget == destinationNameField) { 210 setPreferenceTransfers(); 211 } 212 213 super.handleEvent(e); 214 } 215 216 219 protected String getFileDialogTitle(){ 220 return PreferencesMessages.WizardPreferencesImportPage1_title; 221 } 222 223 226 protected int getFileDialogStyle() { 227 return SWT.OPEN; 228 } 229 230 233 protected boolean validDestination() { 234 return super.validDestination() && validFromFile(); 235 } 236 237 240 protected String getInvalidDestinationMessage() { 241 return PreferencesMessages.WizardPreferencesImportPage1_invalidPrefFile; 242 } 243 } 244 | Popular Tags |