|
From: <pat...@us...> - 2010-05-28 20:09:59
|
Revision: 1071
http://cishell.svn.sourceforge.net/cishell/?rev=1071&view=rev
Author: pataphil
Date: 2010-05-28 20:09:51 +0000 (Fri, 28 May 2010)
Log Message:
-----------
* File Load now operates exactly as before, only better. (The user is prompted to select a format/validator until one succeeds or File Load is canceled.)
Modified Paths:
--------------
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadAlgorithm.java
Added Paths:
-----------
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileSelectorRunnable.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/ValidatorSelectorRunnable.java
Removed Paths:
-------------
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadUserInputRunnable.java
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadAlgorithm.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadAlgorithm.java 2010-05-28 19:41:39 UTC (rev 1070)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadAlgorithm.java 2010-05-28 20:09:51 UTC (rev 1071)
@@ -1,7 +1,6 @@
package org.cishell.reference.gui.persistence.load;
import java.io.File;
-import java.util.Collection;
import java.util.Dictionary;
import org.cishell.framework.CIShellContext;
@@ -43,35 +42,11 @@
}
public Data[] execute() throws AlgorithmExecutionException {
- // Prepare to run load dialog in GUI thread.
-
IWorkbenchWindow window = getFirstWorkbenchWindow();
Display display = PlatformUI.getWorkbench().getDisplay();
- FileLoadUserInputRunnable userInputGetter = new FileLoadUserInputRunnable(
- window, this.bundleContext, this.ciShellContext);
+ File file = getFileToLoadFromUser(window, display);
- // Run load dialog in GUI thread.
-
- if (Thread.currentThread() != display.getThread()) {
- display.syncExec(userInputGetter);
- } else {
- userInputGetter.run();
- }
-
- // Return loaded file data.
-
- File file = userInputGetter.getFile();
- AlgorithmFactory validator = userInputGetter.getValidator();
-
- if ((file == null) || (validator == null)) {
- String logMessage = "File loading canceled";
- this.logger.log(LogService.LOG_WARNING, logMessage);
-
- return null;
- } else {
- return FileValidator.validateFile(
- file, validator, this.progressMonitor, this.ciShellContext, this.logger);
- }
+ return validateFile(window, display, file);
}
public ProgressMonitor getProgressMonitor() {
@@ -86,7 +61,7 @@
return StringUtilities.emptyStringIfNull(preferences.get(LOAD_DIRECTORY_PREFERENCE_KEY));
}
- private static IWorkbenchWindow getFirstWorkbenchWindow() throws AlgorithmExecutionException {
+ private IWorkbenchWindow getFirstWorkbenchWindow() throws AlgorithmExecutionException {
final IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
if (windows.length == 0) {
@@ -96,4 +71,59 @@
return windows[0];
}
}
+
+ private File getFileToLoadFromUser(IWorkbenchWindow window, Display display) {
+ FileSelectorRunnable fileSelector = new FileSelectorRunnable(window);
+
+ if (Thread.currentThread() != display.getThread()) {
+ display.syncExec(fileSelector);
+ } else {
+ fileSelector.run();
+ }
+
+ return fileSelector.getFile();
+ }
+
+ private Data[] validateFile(IWorkbenchWindow window, Display display, File file)
+ throws AlgorithmExecutionException {
+ AlgorithmFactory validator = null;
+ boolean shouldTryValidator = true;
+
+ while (shouldTryValidator) {
+ try {
+ validator = getValidatorFromUser(window, display, file);
+
+ if ((file == null) || (validator == null)) {
+ String logMessage = "File loading canceled";
+ this.logger.log(LogService.LOG_WARNING, logMessage);
+
+ shouldTryValidator = false;
+ } else {
+ return FileValidator.validateFile(
+ file, validator, this.progressMonitor, this.ciShellContext, this.logger);
+ }
+ } catch (Throwable e) {
+ String logMessage =
+ "The chosen file is not compatible with the chosen file. " +
+ "Please try a different format or cancel.";
+ this.logger.log(LogService.LOG_ERROR, logMessage);
+ }
+ }
+
+ return null;
+ }
+
+ private AlgorithmFactory getValidatorFromUser(
+ IWorkbenchWindow window, Display display, File file) {
+ ValidatorSelectorRunnable validatorSelector =
+ new ValidatorSelectorRunnable(window, this.bundleContext, file);
+
+ if (Thread.currentThread() != display.getThread()) {
+ display.syncExec(validatorSelector);
+ } else {
+ validatorSelector.run();
+ }
+
+ return validatorSelector.getValidator();
+ }
}
\ No newline at end of file
Deleted: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadUserInputRunnable.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadUserInputRunnable.java 2010-05-28 19:41:39 UTC (rev 1070)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadUserInputRunnable.java 2010-05-28 20:09:51 UTC (rev 1071)
@@ -1,147 +0,0 @@
-package org.cishell.reference.gui.persistence.load;
-
-import java.io.File;
-
-import org.cishell.framework.CIShellContext;
-import org.cishell.framework.algorithm.AlgorithmFactory;
-import org.cishell.utilities.FileUtilities;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.FileDialog;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-
-public final class FileLoadUserInputRunnable implements Runnable {
- private IWorkbenchWindow window;
- private BundleContext bundleContext;
- private CIShellContext ciShellContext;
- private File file;
- private AlgorithmFactory validator;
-
- public FileLoadUserInputRunnable(
- IWorkbenchWindow window, BundleContext bundleContext, CIShellContext ciShellContext) {
- this.window = window;
- this.bundleContext = bundleContext;
- this.ciShellContext = ciShellContext;
- }
-
- public File getFile() {
- return this.file;
- }
-
- public AlgorithmFactory getValidator() {
- return this.validator;
- }
-
- public void run() {
- this.file = getFileFromUser();
-
- if (file == null) {
- return;
- } else if (this.file.isDirectory()) {
- FileLoadAlgorithm.defaultLoadDirectory = this.file.getAbsolutePath();
- } else {
- FileLoadAlgorithm.defaultLoadDirectory = this.file.getParentFile().getAbsolutePath();
- }
-
- // Validate the loaded file, "casting" it to a certain MIME type.
-
- // Extract the file's file extension.
-
- String fileExtension =
- FileUtilities.getFileExtension(this.file).toLowerCase().substring(1);
-
- // TODO split here?
-
- // Get all the validators which support this file extension...
-
- ServiceReference[] supportingValidators = getSupportingValidators(fileExtension);
-
- // If there are no supporting validators...
- if (supportingValidators.length == 0) {
- // Let the user choose from all the validators available.
-
- ServiceReference[] allValidators = getAllValidators();
-
- FileFormatSelector validatorSelector = new FileFormatSelector(
- "Load", window.getShell(), this.bundleContext, allValidators);
- validatorSelector.open();
- this.validator = validatorSelector.getValidator();
- } else if (supportingValidators.length == 1) {
- ServiceReference onlyPossibleValidator = supportingValidators[0];
- this.validator =
- (AlgorithmFactory)this.bundleContext.getService(onlyPossibleValidator);
- }
-
- if (supportingValidators.length > 1) {
- FileFormatSelector validatorSelector = new FileFormatSelector(
- "Load", window.getShell(), this.bundleContext, supportingValidators);
- validatorSelector.open();
- this.validator = validatorSelector.getValidator();
- }
- }
-
- private File getFileFromUser() {
- FileDialog fileDialog = createFileDialog();
- String fileName = fileDialog.open();
-
- if (fileName == null) {
- return null;
- } else {
- return new File(fileName);
- }
- }
-
- private FileDialog createFileDialog() {
- File currentDirectory = new File(FileLoadAlgorithm.defaultLoadDirectory);
- String absolutePath = currentDirectory.getAbsolutePath();
- FileDialog fileDialog = new FileDialog(this.window.getShell(), SWT.OPEN);
- fileDialog.setFilterPath(absolutePath);
- fileDialog.setText("Select a File");
-
- return fileDialog;
- }
-
- private ServiceReference[] getSupportingValidators(String fileExtension) {
- try {
- String validatorsQuery =
- "(& (type=validator)" +
- "(|" +
- "(in_data=file-ext:" + fileExtension + ")" +
- "(also_validates=" + fileExtension + ")" +
- "))";
-
- ServiceReference[] supportingValidators = this.bundleContext.getAllServiceReferences(
- AlgorithmFactory.class.getName(), validatorsQuery);
-
- if (supportingValidators == null) {
- return new ServiceReference[0];
- } else {
- return supportingValidators;
- }
- } catch (InvalidSyntaxException e) {
- e.printStackTrace();
-
- return new ServiceReference[]{};
- }
- }
-
- private ServiceReference[] getAllValidators() {
- try {
- String validatorsQuery = "(&(type=validator)(in_data=file-ext:*))";
- ServiceReference[] allValidators = this.bundleContext.getAllServiceReferences(
- AlgorithmFactory.class.getName(), validatorsQuery);
-
- if (allValidators == null) {
- return new ServiceReference[0];
- } else {
- return allValidators;
- }
- } catch (InvalidSyntaxException e) {
- e.printStackTrace();
-
- return new ServiceReference[0];
- }
- }
-}
\ No newline at end of file
Copied: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileSelectorRunnable.java (from rev 1068, trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadUserInputRunnable.java)
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileSelectorRunnable.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileSelectorRunnable.java 2010-05-28 20:09:51 UTC (rev 1071)
@@ -0,0 +1,54 @@
+package org.cishell.reference.gui.persistence.load;
+
+import java.io.File;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.ui.IWorkbenchWindow;
+
+public final class FileSelectorRunnable implements Runnable {
+ private IWorkbenchWindow window;
+
+ private File file;
+
+ public FileSelectorRunnable(IWorkbenchWindow window) {
+ this.window = window;
+ }
+
+ public File getFile() {
+ return this.file;
+ }
+
+ public void run() {
+ this.file = getFileFromUser();
+
+ if (this.file == null) {
+ return;
+ } else if (this.file.isDirectory()) {
+ FileLoadAlgorithm.defaultLoadDirectory = this.file.getAbsolutePath();
+ } else {
+ FileLoadAlgorithm.defaultLoadDirectory = this.file.getParentFile().getAbsolutePath();
+ }
+ }
+
+ private File getFileFromUser() {
+ FileDialog fileDialog = createFileDialog();
+ String fileName = fileDialog.open();
+
+ if (fileName == null) {
+ return null;
+ } else {
+ return new File(fileName);
+ }
+ }
+
+ private FileDialog createFileDialog() {
+ File currentDirectory = new File(FileLoadAlgorithm.defaultLoadDirectory);
+ String absolutePath = currentDirectory.getAbsolutePath();
+ FileDialog fileDialog = new FileDialog(this.window.getShell(), SWT.OPEN);
+ fileDialog.setFilterPath(absolutePath);
+ fileDialog.setText("Select a File");
+
+ return fileDialog;
+ }
+}
\ No newline at end of file
Property changes on: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileSelectorRunnable.java
___________________________________________________________________
Added: svn:mergeinfo
+
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/ValidatorSelectorRunnable.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/ValidatorSelectorRunnable.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/ValidatorSelectorRunnable.java 2010-05-28 20:09:51 UTC (rev 1071)
@@ -0,0 +1,101 @@
+package org.cishell.reference.gui.persistence.load;
+
+import java.io.File;
+
+import org.cishell.framework.algorithm.AlgorithmFactory;
+import org.cishell.utilities.FileUtilities;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+public final class ValidatorSelectorRunnable implements Runnable {
+ private IWorkbenchWindow window;
+ private BundleContext bundleContext;
+
+ private File file;
+ private AlgorithmFactory validator;
+
+ public ValidatorSelectorRunnable(
+ IWorkbenchWindow window, BundleContext bundleContext, File file) {
+ this.window = window;
+ this.bundleContext = bundleContext;
+ this.file = file;
+ }
+
+ public AlgorithmFactory getValidator() {
+ return this.validator;
+ }
+
+ public void run() {
+ String fileExtension =
+ FileUtilities.getFileExtension(this.file).toLowerCase().substring(1);
+
+ ServiceReference[] supportingValidators = getSupportingValidators(fileExtension);
+
+ // If there are no supporting validators...
+ if (supportingValidators.length == 0) {
+ // Let the user choose from all the validators available.
+
+ ServiceReference[] allValidators = getAllValidators();
+
+ FileFormatSelector validatorSelector = new FileFormatSelector(
+ "Load", window.getShell(), this.bundleContext, allValidators);
+ validatorSelector.open();
+ this.validator = validatorSelector.getValidator();
+ } else if (supportingValidators.length == 1) {
+ ServiceReference onlyPossibleValidator = supportingValidators[0];
+ this.validator =
+ (AlgorithmFactory)this.bundleContext.getService(onlyPossibleValidator);
+ }
+
+ if (supportingValidators.length > 1) {
+ FileFormatSelector validatorSelector = new FileFormatSelector(
+ "Load", window.getShell(), this.bundleContext, supportingValidators);
+ validatorSelector.open();
+ this.validator = validatorSelector.getValidator();
+ }
+ }
+
+ private ServiceReference[] getSupportingValidators(String fileExtension) {
+ try {
+ String validatorsQuery =
+ "(& (type=validator)" +
+ "(|" +
+ "(in_data=file-ext:" + fileExtension + ")" +
+ "(also_validates=" + fileExtension + ")" +
+ "))";
+
+ ServiceReference[] supportingValidators = this.bundleContext.getAllServiceReferences(
+ AlgorithmFactory.class.getName(), validatorsQuery);
+
+ if (supportingValidators == null) {
+ return new ServiceReference[0];
+ } else {
+ return supportingValidators;
+ }
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace();
+
+ return new ServiceReference[]{};
+ }
+ }
+
+ private ServiceReference[] getAllValidators() {
+ try {
+ String validatorsQuery = "(&(type=validator)(in_data=file-ext:*))";
+ ServiceReference[] allValidators = this.bundleContext.getAllServiceReferences(
+ AlgorithmFactory.class.getName(), validatorsQuery);
+
+ if (allValidators == null) {
+ return new ServiceReference[0];
+ } else {
+ return allValidators;
+ }
+ } catch (InvalidSyntaxException e) {
+ e.printStackTrace();
+
+ return new ServiceReference[0];
+ }
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|