From: <pat...@us...> - 2011-02-09 18:22:58
|
Revision: 1202 http://cishell.svn.sourceforge.net/cishell/?rev=1202&view=rev Author: pataphil Date: 2011-02-09 18:22:52 +0000 (Wed, 09 Feb 2011) Log Message: ----------- * Cleaned file loading algorithm code (related to the service). Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadAlgorithm.java Removed 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/FileValidator.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/PrettyLabeler.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/ValidatorSelectorRunnable.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 2011-02-08 22:28:02 UTC (rev 1201) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadAlgorithm.java 2011-02-09 18:22:52 UTC (rev 1202) @@ -1,6 +1,5 @@ package org.cishell.reference.gui.persistence.load; -import java.io.File; import java.util.Dictionary; import org.cishell.app.service.fileloader.FileLoadException; @@ -11,18 +10,10 @@ import org.cishell.framework.algorithm.ProgressMonitor; import org.cishell.framework.algorithm.ProgressTrackable; import org.cishell.framework.data.Data; -import org.cishell.utilities.StringUtilities; -import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.PlatformUI; import org.osgi.framework.BundleContext; import org.osgi.service.log.LogService; public class FileLoadAlgorithm implements Algorithm, ProgressTrackable { - public static final String LOAD_DIRECTORY_PREFERENCE_KEY = "loadDir"; - - public static String defaultLoadDirectory; - private final LogService logger; private FileLoaderService fileLoader; private BundleContext bundleContext; @@ -38,11 +29,6 @@ (FileLoaderService) ciShellContext.getService(FileLoaderService.class.getName()); this.ciShellContext = ciShellContext; this.bundleContext = bundleContext; - - // This is not done upon declaration because the preference service may not have started. - if (FileLoadAlgorithm.defaultLoadDirectory == null) { - FileLoadAlgorithm.defaultLoadDirectory = determineDefaultLoadDirectory(preferences); - } } public Data[] execute() throws AlgorithmExecutionException { @@ -61,31 +47,4 @@ public void setProgressMonitor(ProgressMonitor progressMonitor) { this.progressMonitor = progressMonitor; } - - private static String determineDefaultLoadDirectory(Dictionary<String, Object> preferences) { - return StringUtilities.emptyStringIfNull(preferences.get(LOAD_DIRECTORY_PREFERENCE_KEY)); - } - - private IWorkbenchWindow getFirstWorkbenchWindow() throws AlgorithmExecutionException { - final IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); - - if (windows.length == 0) { - throw new AlgorithmExecutionException( - "Cannot obtain workbench window needed to open dialog."); - } else { - return windows[0]; - } - } - - private File[] getFilesToLoadFromUser(IWorkbenchWindow window, Display display) { - FileSelectorRunnable fileSelector = new FileSelectorRunnable(window); - - if (Thread.currentThread() != display.getThread()) { - display.syncExec(fileSelector); - } else { - fileSelector.run(); - } - - return fileSelector.getFiles(); - } } \ No newline at end of file Deleted: 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/FileSelectorRunnable.java 2011-02-08 22:28:02 UTC (rev 1201) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileSelectorRunnable.java 2011-02-09 18:22:52 UTC (rev 1202) @@ -1,66 +0,0 @@ -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[] files; - - public FileSelectorRunnable(IWorkbenchWindow window) { - this.window = window; - } - - public File[] getFiles() { - return this.files; - } - - public void run() { - this.files = getFilesFromUser(); - - if (this.files.length == 0) { - return; - /*} else if (this.files[0].isDirectory()) { - // TODO Can we kill this branch? - FileLoadAlgorithm.defaultLoadDirectory = this.files[0].getAbsolutePath(); */ - } else { - FileLoadAlgorithm.defaultLoadDirectory = - this.files[0].getParentFile().getAbsolutePath(); - } - } - - private File[] getFilesFromUser() { - FileDialog fileDialog = createFileDialog(); - fileDialog.open(); - String path = fileDialog.getFilterPath(); - String[] fileNames = fileDialog.getFileNames(); - // TODO: Ask Angela about the order here, i.e. should they be sorted alphabetically? - - if ((fileNames == null) || (fileNames.length == 0)) { - return new File[0]; - } else { - File[] files = new File[fileNames.length]; - - for (int ii = 0; ii < fileNames.length; ii++) { - String fullFileName = path + File.separator + fileNames[ii]; - files[ii] = new File(fullFileName); - } - - return files; - } - } - - private FileDialog createFileDialog() { - File currentDirectory = new File(FileLoadAlgorithm.defaultLoadDirectory); - String absolutePath = currentDirectory.getAbsolutePath(); - FileDialog fileDialog = new FileDialog(this.window.getShell(), SWT.OPEN | SWT.MULTI); - fileDialog.setFilterPath(absolutePath); - fileDialog.setText("Select Files"); - - return fileDialog; - } -} \ No newline at end of file Deleted: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileValidator.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileValidator.java 2011-02-08 22:28:02 UTC (rev 1201) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileValidator.java 2011-02-09 18:22:52 UTC (rev 1202) @@ -1,41 +0,0 @@ -package org.cishell.reference.gui.persistence.load; - -import java.io.File; -import java.util.Hashtable; - -import org.cishell.framework.CIShellContext; -import org.cishell.framework.algorithm.Algorithm; -import org.cishell.framework.algorithm.AlgorithmExecutionException; -import org.cishell.framework.algorithm.AlgorithmFactory; -import org.cishell.framework.algorithm.ProgressMonitor; -import org.cishell.framework.algorithm.ProgressTrackable; -import org.cishell.framework.data.BasicData; -import org.cishell.framework.data.Data; -import org.osgi.service.log.LogService; - -public final class FileValidator { - public static Data[] validateFile( - File file, - AlgorithmFactory validator, - ProgressMonitor progressMonitor, - CIShellContext ciShellContext, - LogService logger) throws AlgorithmExecutionException { - Data[] validationData = - new Data[] { new BasicData(file.getPath(), String.class.getName()) }; - Algorithm algorithm = validator.createAlgorithm( - validationData, new Hashtable<String, Object>(), ciShellContext); - - if ((progressMonitor != null) && (algorithm instanceof ProgressTrackable)) { - ProgressTrackable progressTrackable = (ProgressTrackable)algorithm; - progressTrackable.setProgressMonitor(progressMonitor); - } - - Data[] validatedData = algorithm.execute(); - - if (validatedData != null) { - logger.log(LogService.LOG_INFO, "Loaded: " + file.getPath()); - } - - return validatedData; - } -} \ No newline at end of file Deleted: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/PrettyLabeler.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/PrettyLabeler.java 2011-02-08 22:28:02 UTC (rev 1201) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/PrettyLabeler.java 2011-02-09 18:22:52 UTC (rev 1202) @@ -1,84 +0,0 @@ -package org.cishell.reference.gui.persistence.load; - -import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Dictionary; - -import org.cishell.framework.data.BasicData; -import org.cishell.framework.data.Data; -import org.cishell.framework.data.DataProperty; -import org.cishell.utilities.dictionary.DictionaryUtilities; - -public class PrettyLabeler { - public static Data[] relabelWithFileName(Data[] data, File file) { - File absoluteFile = file.getAbsoluteFile(); - File parent = absoluteFile.getParentFile(); - - // TODO parent == null - - String prefix; - String parentName = parent.getName(); - if (parentName.trim().length() == 0) { - prefix = File.separator; - } else { - prefix = "..." + File.separator + parentName + File.separator; - } - - Collection<Data> newData = new ArrayList<Data>(data.length); - - /* TODO: This isn't actually correct. - * It will assign the same label to all of the data items if we ever do this. - */ - for (Data datum : data) { - Dictionary<String, Object> labeledDatumMetadata = - DictionaryUtilities.copy(datum.getMetadata()); - Data labeledDatum = - new BasicData(labeledDatumMetadata, datum.getData(), datum.getFormat()); - labeledDatumMetadata.put(DataProperty.LABEL, prefix + absoluteFile.getName()); - newData.add(labeledDatum); - } - - return newData.toArray(new Data[0]); - } - - /** - * Support Hierarchy structure labeling. The algorithm will avoid labeling - * on children. - * @param data - data that need to relabel - * @param file - file that contains filename to be used for relabeling - * @return the processed data with new labels - */ - public static Data[] relabelWithFileNameHierarchy(Data[] data, File file) { - File absoluteFile = file.getAbsoluteFile(); - File parent = absoluteFile.getParentFile(); - - String prefix; - String parentName = parent.getName(); - if (parentName.trim().length() == 0) { - prefix = File.separator; - } else { - prefix = "..." + File.separator + parentName + File.separator; - } - - Collection<Data> possibleParents = new ArrayList<Data>(data.length); - - for (Data datum : data) { - Dictionary<String, Object> labeledDatumMetadata = datum.getMetadata(); - Data dataParent = getParent(labeledDatumMetadata); - if (!possibleParents.contains(dataParent)) { - labeledDatumMetadata.put(DataProperty.LABEL, prefix + absoluteFile.getName()); - } - possibleParents.add(datum); - } - - return data; - } - - /* - * Get the parent of the data - */ - private static Data getParent(Dictionary<String, Object> labeledDatumMetadata) { - return (Data) labeledDatumMetadata.get(DataProperty.PARENT); - } -} \ No newline at end of file Deleted: 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 2011-02-08 22:28:02 UTC (rev 1201) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/ValidatorSelectorRunnable.java 2011-02-09 18:22:52 UTC (rev 1202) @@ -1,101 +0,0 @@ -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, this.file); - 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, this.file); - 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. |