1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.io.File ; 14 15 import org.eclipse.jface.dialogs.IDialogConstants; 16 import org.eclipse.jface.wizard.IWizardContainer; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.SelectionAdapter; 19 import org.eclipse.swt.events.SelectionEvent; 20 import org.eclipse.swt.graphics.Font; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.swt.widgets.FileDialog; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.Listener; 29 import org.eclipse.swt.widgets.Text; 30 import org.eclipse.ui.internal.WorkbenchMessages; 31 import org.eclipse.ui.internal.WorkbenchPlugin; 32 import org.eclipse.ui.internal.util.Util; 33 34 40 class PreferenceImportExportFileSelectionPage extends AbstractPreferenceImportExportPage { 41 42 46 private class PageChangeListener implements Listener { 47 52 public void handleEvent(Event event) { 53 IWizardContainer container = getContainer(); 54 if (container.getCurrentPage() != null) { 55 container.updateButtons(); 56 } 57 } 58 } 59 60 63 private static final String [] DIALOG_PREFERENCE_EXTENSIONS = new String [] { "*" + PREFERENCE_EXT, "*.*"}; 67 private static final String EXPORT_MESSAGE = WorkbenchMessages.getString("ImportExportPages.exportFileSelect"); 71 private static final String IMPORT_MESSAGE = WorkbenchMessages.getString("ImportExportPages.importFileSelect"); 75 private static final String NAME = "org.eclipse.ui.preferences.importExportFileSelectionPage"; 81 public final Listener changeListener = new PageChangeListener(); 82 86 private Text fileText; 87 88 89 93 PreferenceImportExportFileSelectionPage(boolean exportWizard) { 94 super(NAME, exportWizard); 95 } 96 97 103 boolean canFinish() { 104 return validate(); 105 } 106 107 111 private void chooseFile() { 112 String currentFileName = fileText.getText(); 114 115 FileDialog fileDialog = null; 117 if(export) 118 fileDialog = new FileDialog(getShell(), SWT.SAVE); 119 else 120 fileDialog = new FileDialog(getShell(), SWT.OPEN); 121 122 fileDialog.setFileName(currentFileName); 123 fileDialog.setFilterExtensions(DIALOG_PREFERENCE_EXTENSIONS); 124 currentFileName = fileDialog.open(); 125 126 if (currentFileName == null) 127 return; 128 129 132 String fileName = new File (currentFileName).getName(); 133 if (fileName.lastIndexOf(".") == -1) { currentFileName += PREFERENCE_EXT; 135 } 136 137 fileText.setText(currentFileName); 138 canFlipToNextPage(); 139 } 140 141 146 public void createControl(Composite parent) { 147 Font parentFont = parent.getFont(); 148 final Composite page = new Composite(parent, SWT.NONE); 149 GridLayout layout = new GridLayout(3, false); 150 page.setLayout(layout); 151 initializeDialogUnits(page); 152 153 if (export) { 155 setTitle(EXPORT_TITLE); 156 setMessage(EXPORT_MESSAGE); 157 setImageDescriptor(getImageDescriptor("wizban/export_wiz.gif")); } else { 159 setTitle(IMPORT_TITLE); 160 setMessage(IMPORT_MESSAGE); 161 setImageDescriptor(getImageDescriptor("wizban/import_wiz.gif")); } 163 164 GridData layoutData; 165 166 final Label fileLabel = new Label(page, SWT.NONE); 168 fileLabel.setText(WorkbenchMessages.getString("ImportExportPages.fileLabel")); fileLabel.setFont(parentFont); 170 layoutData = new GridData(); 171 fileLabel.setLayoutData(layoutData); 172 173 fileText = new Text(page, SWT.SINGLE | SWT.BORDER); 175 fileText.setFont(parentFont); 176 layoutData = new GridData(); 177 layoutData.grabExcessHorizontalSpace = true; 178 layoutData.horizontalAlignment = GridData.FILL; 179 fileText.setLayoutData(layoutData); 180 fileText.addListener(SWT.Modify, changeListener); 181 182 final Button browseButton = new Button(page, SWT.PUSH); 184 browseButton.setFont(parentFont); 185 layoutData = new GridData(); 186 browseButton.setText(WorkbenchMessages.getString("ImportExportPages.browseButton")); layoutData.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT); 188 layoutData.widthHint = computePushButtonWidthHint(browseButton); 189 browseButton.setLayoutData(layoutData); 190 browseButton.addSelectionListener(new SelectionAdapter() { 191 public final void widgetSelected(SelectionEvent event) { 192 chooseFile(); 193 } 194 }); 195 196 final Composite verticalSpacer = new Composite(page, SWT.NONE); 198 layoutData = new GridData(); 199 layoutData.heightHint = 15; 200 layoutData.horizontalSpan = 3; 201 verticalSpacer.setLayoutData(layoutData); 202 203 setControl(page); 205 206 init(); 208 } 209 210 214 String getPath() { 215 return fileText.getText(); 216 } 217 218 222 private void init() { 223 String lastFileName = WorkbenchPlugin.getDefault().getDialogSettings() 224 .get(WorkbenchPreferenceDialog.FILE_PATH_SETTING); 225 if (lastFileName == null) { 226 if (export) { 227 fileText 228 .setText(System.getProperty("user.dir") + System.getProperty("file.separator") + WorkbenchMessages.getString("ImportExportPages.preferenceFileName") + PREFERENCE_EXT); } 230 } else if ((export) || (new File (lastFileName).exists())) { 231 fileText.setText(lastFileName); 232 } 233 } 234 235 242 boolean validate() { 243 final String fileName = fileText.getText(); 244 if (fileName.equals(Util.ZERO_LENGTH_STRING)) { 245 setErrorMessage(null); 246 return false; 247 } 248 249 final File currentFile = new File (fileName); 250 if (export) { 251 final File parentFile = currentFile.getParentFile(); 252 if (parentFile == null || !parentFile.exists()) { 253 setErrorMessage(WorkbenchMessages 254 .getString("ImportExportPages.errorDirectoryDoesNotExist")); return false; 256 } 257 } else { 258 if (!currentFile.exists()) { 259 setErrorMessage(WorkbenchMessages 260 .getString("ImportExportPages.errorImportFileDoesNotExist")); return false; 262 } 263 } 264 265 setErrorMessage(null); 266 return true; 267 } 268 269 270 } 271 | Popular Tags |