From: <pat...@us...> - 2011-03-01 20:09:52
|
Revision: 1230 http://cishell.svn.sourceforge.net/cishell/?rev=1230&view=rev Author: pataphil Date: 2011-03-01 20:09:46 +0000 (Tue, 01 Mar 2011) Log Message: ----------- * Refactored to use the File*Services. * Reviewed by Joseph. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF 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/save/Save.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveDataChooser.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF 2011-02-22 18:04:19 UTC (rev 1229) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF 2011-03-01 20:09:46 UTC (rev 1230) @@ -6,6 +6,7 @@ Bundle-ClassPath: . Import-Package: org.cishell.app.service.datamanager, org.cishell.app.service.fileloader, + org.cishell.app.service.filesaver, org.cishell.framework;version="1.0.0", org.cishell.framework.algorithm;version="1.0.0", org.cishell.framework.data;version="1.0.0", 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-22 18:04:19 UTC (rev 1229) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadAlgorithm.java 2011-03-01 20:09:46 UTC (rev 1230) @@ -34,7 +34,7 @@ public Data[] execute() throws AlgorithmExecutionException { try { return this.fileLoader.loadFilesFromUserSelection( - this.bundleContext, this.ciShellContext, this.logger, this.progressMonitor); + this.bundleContext, this.ciShellContext, this.logger, this.progressMonitor, false); } catch (FileLoadException e) { throw new AlgorithmExecutionException(e.getMessage(), e); } Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java 2011-02-22 18:04:19 UTC (rev 1229) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/Save.java 2011-03-01 20:09:46 UTC (rev 1230) @@ -1,31 +1,32 @@ package org.cishell.reference.gui.persistence.save; -import org.cishell.framework.CIShellContext; +import java.io.File; + +import org.cishell.app.service.filesaver.FileSaveException; +import org.cishell.app.service.filesaver.FileSaverService; import org.cishell.framework.algorithm.Algorithm; +import org.cishell.framework.algorithm.AlgorithmCanceledException; import org.cishell.framework.algorithm.AlgorithmExecutionException; import org.cishell.framework.data.Data; import org.cishell.service.conversion.Converter; -import org.cishell.service.conversion.DataConversionService; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.PlatformUI; +import org.osgi.service.log.LogService; public class Save implements Algorithm { public static final String ANY_FILE_EXTENSION = "file-ext:*"; public static final String SAVE_DIALOG_TITLE = "Save"; + private Data data; - private CIShellContext ciShellContext; + private LogService logger; + private FileSaverService fileSaver; - private Shell parentShell; - - private DataConversionService conversionManager; - public Save( - Data data, CIShellContext ciShellContext, DataConversionService conversionManager) { + Data data, + LogService logger, + FileSaverService fileSaver) { this.data = data; - this.ciShellContext = ciShellContext; - - this.conversionManager = conversionManager; + + this.logger = logger; + this.fileSaver = fileSaver; } public Data[] execute() throws AlgorithmExecutionException { @@ -36,49 +37,37 @@ private void tryToSave(final Data outData, String outFormat) throws AlgorithmExecutionException { - final Converter[] converters = conversionManager.findConverters(outData, outFormat); + try { + Converter userChosenConverter = + this.fileSaver.promptForConverter(outData, outFormat); - if (converters.length == 0) { - throw new AlgorithmExecutionException("Error: Calculated an empty converter chain."); - } + if (userChosenConverter == null) { + throw new AlgorithmCanceledException( + "User canceled file saving when choosing what kind of file to save as."); + } - this.parentShell = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getShell(); + File userChosenFile = this.fileSaver.promptForTargetFile(outData); - if (this.parentShell.isDisposed()) { - throw new AlgorithmExecutionException("Attempted to use disposed parent shell."); - } - - try { - guiRun(new Runnable() { - public void run() { - if (converters.length == 1) { - // Only one possible choice in how to save data. Do it. - Converter onlyConverter = converters[0]; - FileSaver saver = new FileSaver(Save.this.parentShell, ciShellContext); - saver.save(onlyConverter, outData); - } else { - // Multiple ways to save the data. Let user choose. - SaveDataChooser saveChooser = new SaveDataChooser( - outData, - Save.this.parentShell, - converters, - SAVE_DIALOG_TITLE, - Save.this.ciShellContext); - saveChooser.createContent(new Shell(Save.this.parentShell)); - saveChooser.open(); - } - } - }); - } catch (Exception e) { + if (userChosenFile == null) { + throw new AlgorithmCanceledException( + "User canceled file saving when choosing the destination of the file."); + } + + Data outputDatum = + this.fileSaver.save(userChosenConverter, outData, userChosenFile); + + // TODO: Should we bother handling this? sure, why not. maybe algexec would be appropriate + if (outputDatum == null) { + return; + } + + String logMessage = String.format("Saved: %s", userChosenFile.getPath()); + this.logger.log(LogService.LOG_INFO, logMessage); + } catch (FileSaveException e) { + String logMessage = String.format( + "Error occurred while converting data to saved format:\n %s", e.getMessage()); + this.logger.log(LogService.LOG_ERROR, logMessage, e); throw new AlgorithmExecutionException(e.getMessage(), e); } } - - private void guiRun(Runnable run) { - if (Thread.currentThread() == Display.getDefault().getThread()) { - run.run(); - } else { - this.parentShell.getDisplay().syncExec(run); - } - } } \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveDataChooser.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveDataChooser.java 2011-02-22 18:04:19 UTC (rev 1229) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveDataChooser.java 2011-03-01 20:09:46 UTC (rev 1230) @@ -42,6 +42,10 @@ * among several persisters that support the selected model, in the event * that more than one is found. */ +/* TODO + * If we make a file viewer service, remove this + * Otherwise move to File View algorithm + */ public class SaveDataChooser extends AbstractDialog implements AlgorithmProperty { public static final Image QUESTION_ICON = Display.getCurrent().getSystemImage(SWT.ICON_QUESTION); Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java 2011-02-22 18:04:19 UTC (rev 1229) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java 2011-03-01 20:09:46 UTC (rev 1230) @@ -2,13 +2,14 @@ import java.util.Dictionary; +import org.cishell.app.service.filesaver.FileSaverService; import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; import org.cishell.framework.algorithm.AlgorithmFactory; import org.cishell.framework.data.Data; -import org.cishell.service.conversion.DataConversionService; import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedService; +import org.osgi.service.log.LogService; /** * Create a Save object @@ -20,12 +21,14 @@ public class SaveFactory implements AlgorithmFactory, ManagedService { public Algorithm createAlgorithm( Data[] data, Dictionary<String, Object> parameters, CIShellContext ciShellContext) { + // TODO Unpack data? Data inputData = data[0]; - DataConversionService conversionManager = - (DataConversionService) ciShellContext.getService( - DataConversionService.class.getName()); + LogService logger = + (LogService) ciShellContext.getService(LogService.class.getName()); + FileSaverService fileSaver = (FileSaverService) ciShellContext.getService( + FileSaverService.class.getName()); - return new Save(inputData, ciShellContext, conversionManager); + return new Save(inputData, logger, fileSaver); } @SuppressWarnings("unchecked") This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |