1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.obfuscator.export;
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.List;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IWorkspaceRoot;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.dialogs.ErrorDialog;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Combo;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.DirectoryDialog;
35 import org.eclipse.swt.widgets.Event;
36 import org.eclipse.swt.widgets.Group;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Listener;
39 import org.eclipse.swt.widgets.Widget;
40 import org.eclipse.ui.dialogs.WizardExportResourcesPage;
43 * Page 1 of the base resource export-to-file-system Wizard
46 class WizardObfuscatorResourceExportPage1 extends WizardExportResourcesPage
50 private Combo destinationNameField;
52 private Button destinationBrowseButton;
54 protected Button overwriteExistingFilesCheckbox;
56 // protected Button createDirectoryStructureButton;
57 // protected Button createSelectionOnlyButton;
60 private static final int SIZING_TEXT_FIELD_WIDTH = 250;
62 // dialog store id constants
63 private static final String STORE_DESTINATION_NAMES_ID = "WizardObfuscatorResourceExportPage1.STORE_DESTINATION_NAMES_ID"; //$NON-NLS-1$
65 private static final String STORE_OVERWRITE_EXISTING_FILES_ID = "WizardObfuscatorResourceExportPage1.STORE_OVERWRITE_EXISTING_FILES_ID"; //$NON-NLS-1$
66 // private static final String STORE_CREATE_STRUCTURE_ID =
67 // "WizardObfuscatorResourceExportPage1.STORE_CREATE_STRUCTURE_ID";
71 private static final String SELECT_DESTINATION_MESSAGE = ObfuscatorExportMessages
72 .getString("FileExport.selectDestinationMessage"); //$NON-NLS-1$
74 private static final String SELECT_DESTINATION_TITLE = ObfuscatorExportMessages
75 .getString("FileExport.selectDestinationTitle"); //$NON-NLS-1$
78 * Create an instance of this class
80 protected WizardObfuscatorResourceExportPage1(String name,
81 IStructuredSelection selection) {
82 super(name, selection);
86 * Create an instance of this class
88 public WizardObfuscatorResourceExportPage1(IStructuredSelection selection) {
89 this("fileSystemExportPage1", selection); //$NON-NLS-1$
90 setTitle(ObfuscatorExportMessages
91 .getString("ObfuscatorTransfer.fileSystemTitle")); //$NON-NLS-1$
92 setDescription(ObfuscatorExportMessages
93 .getString("FileExport.exportLocalFileSystem")); //$NON-NLS-1$
97 * Add the passed value to self's destination widget's history
102 protected void addDestinationItem(String value) {
103 destinationNameField.add(value);
107 * (non-Javadoc) Method declared on IDialogPage.
109 public void createControl(Composite parent) {
110 super.createControl(parent);
111 giveFocusToDestination();
112 // WorkbenchHelp.setHelp(
114 // IDataTransferHelpContextIds.FILE_SYSTEM_EXPORT_WIZARD_PAGE);
118 * Create the export destination specification widgets
121 * org.eclipse.swt.widgets.Composite
123 protected void createDestinationGroup(Composite parent) {
125 Font font = parent.getFont();
126 // destination specification group
127 Composite destinationSelectionGroup = new Composite(parent, SWT.NONE);
128 GridLayout layout = new GridLayout();
129 layout.numColumns = 3;
130 destinationSelectionGroup.setLayout(layout);
131 destinationSelectionGroup.setLayoutData(new GridData(
132 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
133 destinationSelectionGroup.setFont(font);
135 Label destinationLabel = new Label(destinationSelectionGroup, SWT.NONE);
136 destinationLabel.setText(getDestinationLabel());
137 destinationLabel.setFont(font);
139 // destination name entry field
140 destinationNameField = new Combo(destinationSelectionGroup, SWT.SINGLE
142 destinationNameField.addListener(SWT.Modify, this);
143 destinationNameField.addListener(SWT.Selection, this);
144 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
145 | GridData.GRAB_HORIZONTAL);
146 data.widthHint = SIZING_TEXT_FIELD_WIDTH;
147 destinationNameField.setLayoutData(data);
148 destinationNameField.setFont(font);
150 // destination browse button
151 destinationBrowseButton = new Button(destinationSelectionGroup,
153 destinationBrowseButton.setText(ObfuscatorExportMessages
154 .getString("ObfuscatorTransfer.browse")); //$NON-NLS-1$
155 destinationBrowseButton.addListener(SWT.Selection, this);
156 destinationBrowseButton.setFont(font);
157 setButtonLayoutData(destinationBrowseButton);
159 new Label(parent, SWT.NONE); // vertical spacer
163 * Create the buttons in the options group.
166 protected void createOptionsGroupButtons(Group optionsGroup) {
168 Font font = optionsGroup.getFont();
169 createOverwriteExisting(optionsGroup, font);
171 // createDirectoryStructureOptions(optionsGroup, font);
175 * Create the buttons for the group that determine if the entire or selected
176 * directory structure should be created.
178 * @param optionsGroup
181 // protected void createDirectoryStructureOptions(
182 // Group optionsGroup,
184 // // create directory structure radios
185 // createDirectoryStructureButton =
186 // new Button(optionsGroup, SWT.RADIO | SWT.LEFT);
187 // createDirectoryStructureButton.setText(ObfuscatorExportMessages.getString("FileExport.createDirectoryStructure"));
189 // createDirectoryStructureButton.setSelection(false);
190 // createDirectoryStructureButton.setFont(font);
192 // // create directory structure radios
193 // createSelectionOnlyButton =
194 // new Button(optionsGroup, SWT.RADIO | SWT.LEFT);
195 // createSelectionOnlyButton.setText(
196 // ObfuscatorExportMessages.getString(
197 // "FileExport.createSelectedDirectories"));//$NON-NLS-1$
198 // createSelectionOnlyButton.setSelection(true);
199 // createSelectionOnlyButton.setFont(font);
202 * Create the button for checking if we should ask if we are going to
203 * overwrite existing files.
205 * @param optionsGroup
208 protected void createOverwriteExisting(Group optionsGroup, Font font) {
209 // overwrite... checkbox
210 overwriteExistingFilesCheckbox = new Button(optionsGroup, SWT.CHECK
212 overwriteExistingFilesCheckbox.setText(ObfuscatorExportMessages
213 .getString("ExportFile.overwriteExisting")); //$NON-NLS-1$
214 overwriteExistingFilesCheckbox.setFont(font);
218 * Attempts to ensure that the specified directory exists on the local file
219 * system. Answers a boolean indicating success.
225 protected boolean ensureDirectoryExists(File directory) {
226 if (!directory.exists()) {
227 if (!queryYesNoQuestion(ObfuscatorExportMessages
228 .getString("ObfuscatorTransfer.createTargetDirectory"))) //$NON-NLS-1$
231 if (!directory.mkdirs()) {
232 displayErrorDialog(ObfuscatorExportMessages
233 .getString("ObfuscatorTransfer.directoryCreationError")); //$NON-NLS-1$
234 giveFocusToDestination();
243 * If the target for export does not exist then attempt to create it. Answer
244 * a boolean indicating whether the target exists (ie.- if it either
245 * pre-existed or this method was able to create it)
249 protected boolean ensureTargetIsValid(File targetDirectory) {
250 if (targetDirectory.exists() && !targetDirectory.isDirectory()) {
251 displayErrorDialog(ObfuscatorExportMessages
252 .getString("FileExport.directoryExists")); //$NON-NLS-1$
253 giveFocusToDestination();
257 return ensureDirectoryExists(targetDirectory);
261 * Set up and execute the passed Operation. Answer a boolean indicating
266 protected boolean executeExportOperation(ObfuscatorExportOperation op) {
267 // op.setCreateLeadupStructure(
268 // createDirectoryStructureButton.getSelection());
269 op.setOverwriteFiles(overwriteExistingFilesCheckbox.getSelection());
272 getContainer().run(true, true, op);
273 } catch (InterruptedException e) {
275 } catch (InvocationTargetException e) {
276 displayErrorDialog(e.getTargetException());
280 IStatus status = op.getStatus();
281 if (!status.isOK()) {
282 ErrorDialog.openError(getContainer().getShell(),
283 ObfuscatorExportMessages
284 .getString("ObfuscatorTransfer.exportProblems"), //$NON-NLS-1$
285 null, // no special message
294 * The Finish button was pressed. Try to do the required work now and answer
295 * a boolean indicating success. If false is returned then the wizard will
300 public boolean finish() {
301 if (!ensureTargetIsValid(new File(getDestinationValue())))
304 List resourcesToExport = getWhiteCheckedResources();
306 // Save dirty editors if possible but do not stop if not all are saved
308 // about to invoke the operation so save our state
311 if (resourcesToExport.size() > 0)
312 return executeExportOperation(new ObfuscatorExportOperation(null,
313 resourcesToExport, getDestinationValue(), this));
315 MessageDialog.openInformation(getContainer().getShell(),
316 ObfuscatorExportMessages
317 .getString("ObfuscatorTransfer.information"), //$NON-NLS-1$
318 ObfuscatorExportMessages.getString("FileExport.noneSelected")); //$NON-NLS-1$
324 * Answer the string to display in self as the destination type
326 * @return java.lang.String
328 protected String getDestinationLabel() {
329 return ObfuscatorExportMessages.getString("FileExport.toDirectory"); //$NON-NLS-1$
333 * Answer the contents of self's destination specification widget
335 * @return java.lang.String
337 protected String getDestinationValue() {
338 return destinationNameField.getText().trim();
342 * Set the current input focus to self's destination entry field
344 protected void giveFocusToDestination() {
345 destinationNameField.setFocus();
349 * Open an appropriate destination browser so that the user can specify a
350 * source to import from
352 protected void handleDestinationBrowseButtonPressed() {
353 DirectoryDialog dialog = new DirectoryDialog(getContainer().getShell(),
355 dialog.setMessage(SELECT_DESTINATION_MESSAGE);
356 dialog.setText(SELECT_DESTINATION_TITLE);
357 dialog.setFilterPath(getDestinationValue());
358 String selectedDirectoryName = dialog.open();
360 if (selectedDirectoryName != null) {
361 setErrorMessage(null);
362 setDestinationValue(selectedDirectoryName);
367 * Handle all events and enablements for widgets in this page
372 public void handleEvent(Event e) {
373 Widget source = e.widget;
375 if (source == destinationBrowseButton)
376 handleDestinationBrowseButtonPressed();
378 updatePageCompletion();
382 * Hook method for saving widget values for restoration by the next instance
385 protected void internalSaveWidgetValues() {
386 // update directory names history
387 IDialogSettings settings = getDialogSettings();
388 if (settings != null) {
389 String[] directoryNames = settings
390 .getArray(STORE_DESTINATION_NAMES_ID);
391 if (directoryNames == null)
392 directoryNames = new String[0];
394 directoryNames = addToHistory(directoryNames, getDestinationValue());
395 settings.put(STORE_DESTINATION_NAMES_ID, directoryNames);
398 settings.put(STORE_OVERWRITE_EXISTING_FILES_ID,
399 overwriteExistingFilesCheckbox.getSelection());
402 // STORE_CREATE_STRUCTURE_ID,
403 // createDirectoryStructureButton.getSelection());
409 * Hook method for restoring widget values to the values that they held last
410 * time this wizard was used to completion.
412 protected void restoreWidgetValues() {
413 IDialogSettings settings = getDialogSettings();
414 if (settings != null) {
415 String[] directoryNames = settings
416 .getArray(STORE_DESTINATION_NAMES_ID);
417 if (directoryNames == null)
418 return; // ie.- no settings stored
421 setDestinationValue(directoryNames[0]);
422 for (int i = 0; i < directoryNames.length; i++)
423 addDestinationItem(directoryNames[i]);
426 overwriteExistingFilesCheckbox.setSelection(settings
427 .getBoolean(STORE_OVERWRITE_EXISTING_FILES_ID));
429 // boolean createDirectories =
430 // settings.getBoolean(STORE_CREATE_STRUCTURE_ID);
431 // createDirectoryStructureButton.setSelection(createDirectories);
432 // createSelectionOnlyButton.setSelection(!createDirectories);
437 * Set the contents of the receivers destination specification widget to the
441 protected void setDestinationValue(String value) {
442 destinationNameField.setText(value);
446 * Answer a boolean indicating whether the receivers destination
447 * specification widgets currently all contain valid values.
449 protected boolean validateDestinationGroup() {
450 String destinationValue = getDestinationValue();
451 if (destinationValue.length() == 0) {
452 setMessage(destinationEmptyMessage());
456 String conflictingContainer = getConflictingContainerNameFor(destinationValue);
457 if (conflictingContainer == null)
458 setErrorMessage(""); //$NON-NLS-1$
460 setErrorMessage(ObfuscatorExportMessages.format(
461 "FileExport.conflictingContainer", //$NON-NLS-1$
462 new Object[] { conflictingContainer }));
463 giveFocusToDestination();
471 * Get the message used to denote an empty destination.
473 protected String destinationEmptyMessage() {
474 return ObfuscatorExportMessages
475 .getString("FileExport.destinationEmpty"); //$NON-NLS-1$
479 * Returns the name of a container with a location that encompasses
480 * targetDirectory. Returns null if there is no conflict.
482 * @param targetDirectory
483 * the path of the directory to check.
484 * @return the conflicting container name or <code>null</code>
486 protected String getConflictingContainerNameFor(String targetDirectory) {
488 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
489 IPath testPath = new Path(targetDirectory);
491 if (root.getFullPath().isPrefixOf(testPath))
492 return ObfuscatorExportMessages.getString("FileExport.rootName"); //$NON-NLS-1$
494 IProject[] projects = root.getProjects();
496 for (int i = 0; i < projects.length; i++) {
497 if (projects[i].getFullPath().isPrefixOf(testPath))
498 return projects[i].getName();