1 11 package org.eclipse.ui.internal.wizards.preferences; 12 13 import java.io.File ; 14 import java.io.FileNotFoundException ; 15 import java.io.FileOutputStream ; 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.IEclipsePreferences; 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.ui.internal.WorkbenchPlugin; 27 import org.eclipse.ui.internal.preferences.PreferenceTransferElement; 28 29 34 public class WizardPreferencesExportPage1 extends WizardPreferencesPage { 35 36 private static final String PREFERENCESEXPORTPAGE1 = "preferencesExportPage1"; 39 42 protected WizardPreferencesExportPage1(String name) { 43 super(name); 44 setTitle(PreferencesMessages.WizardPreferencesExportPage1_exportTitle); 45 setDescription(PreferencesMessages.WizardPreferencesExportPage1_exportDescription); 46 } 47 48 51 public WizardPreferencesExportPage1() { 52 this(PREFERENCESEXPORTPAGE1); 53 } 54 55 protected String getOutputSuffix() { 56 return ".epf"; } 58 59 64 protected String getDestinationValue() { 65 String idealSuffix = getOutputSuffix(); 66 String destinationText = super.getDestinationValue(); 67 68 if (destinationText.length() != 0 73 && !destinationText.endsWith(File.separator)) { 74 int dotIndex = destinationText.lastIndexOf('.'); 75 if (dotIndex != -1) { 76 int pathSepIndex = destinationText.lastIndexOf(File.separator); 78 if (pathSepIndex != -1 && dotIndex < pathSepIndex) { 79 destinationText += idealSuffix; 80 } 81 } else { 82 destinationText += idealSuffix; 83 } 84 } 85 86 return destinationText; 87 } 88 89 90 protected String getAllButtonText() { 91 return PreferencesMessages.WizardPreferencesExportPage1_all; 92 } 93 94 protected String getChooseButtonText() { 95 return PreferencesMessages.WizardPreferencesExportPage1_choose; 96 } 97 98 101 protected void createTransferArea(Composite composite) { 102 createTransfersList(composite); 103 createDestinationGroup(composite); 104 createOptionsGroup(composite); 105 } 106 107 112 protected String getDestinationLabel() { 113 return PreferencesMessages.WizardPreferencesExportPage1_file; 114 } 115 116 119 protected PreferenceTransferElement[] getTransfers() { 120 PreferenceTransferElement[] elements = super.getTransfers(); 121 PreferenceTransferElement[] returnElements = new PreferenceTransferElement[elements.length]; 122 IPreferenceFilter[] filters = new IPreferenceFilter[1]; 123 IPreferenceFilter[] matches; 124 IPreferencesService service = Platform.getPreferencesService(); 125 int count = 0; 126 try { 127 for (int i = 0; i < elements.length; i++) { 128 PreferenceTransferElement element = elements[i]; 129 filters[0] = element.getFilter(); 130 matches = service.matches((IEclipsePreferences) service 131 .getRootNode().node("instance"), filters); if (matches.length > 0) { 133 returnElements[count++] = element; 134 } 135 } 136 elements = new PreferenceTransferElement[count]; 137 System.arraycopy(returnElements, 0, elements, 0, count); 138 } catch (CoreException e) { 139 WorkbenchPlugin.log(e.getMessage(), e); 140 return new PreferenceTransferElement[0]; 141 } 142 return elements; 143 } 144 145 150 protected boolean transfer(IPreferenceFilter[] transfers) { 151 File exportFile = new File (getDestinationValue()); 152 if (!ensureTargetIsValid(exportFile)) { 153 return false; 154 } 155 FileOutputStream fos = null; 156 try { 157 if (transfers.length > 0) { 158 try { 159 fos = new FileOutputStream (exportFile); 160 } catch (FileNotFoundException e) { 161 WorkbenchPlugin.log(e.getMessage(), e); 162 MessageDialog.openError(getControl().getShell(), new String (), e.getLocalizedMessage()); 163 return false; 164 } 165 IPreferencesService service = Platform.getPreferencesService(); 166 try { 167 service.exportPreferences(service.getRootNode(), transfers, 168 fos); 169 } catch (CoreException e) { 170 WorkbenchPlugin.log(e.getMessage(), e); 171 MessageDialog.openError(getControl().getShell(), new String (), e.getLocalizedMessage()); 172 return false; 173 } 174 } 175 } finally { 176 if (fos != null) { 177 try { 178 fos.close(); 179 } catch (IOException e) { 180 WorkbenchPlugin.log(e.getMessage(), e); 181 MessageDialog.openError(getControl().getShell(), new String (), e.getLocalizedMessage()); 182 return false; 183 } 184 } 185 } 186 return true; 187 } 188 189 protected String getFileDialogTitle() { 190 return PreferencesMessages.WizardPreferencesExportPage1_title; 191 } 192 193 protected int getFileDialogStyle() { 194 return SWT.SAVE; 195 } 196 197 200 protected String getInvalidDestinationMessage() { 201 return PreferencesMessages.WizardPreferencesExportPage1_noPrefFile; 202 } 203 } 204 | Popular Tags |