|
From: <pat...@us...> - 2009-10-12 17:37:23
|
Revision: 959
http://cishell.svn.sourceforge.net/cishell/?rev=959&view=rev
Author: pataphil
Date: 2009-10-12 17:37:06 +0000 (Mon, 12 Oct 2009)
Log Message:
-----------
* Improved "View" functionality so CSV-compatible data types (both file-based and in-memory) are viewed in Excel after conversion.
* Also significantly refactored code.
Modified Paths:
--------------
trunk/clients/gui/org.cishell.reference.gui.persistence/.classpath
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/FileUtil.java
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/view/FileView.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/ViewDataChooser.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWith.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWithFactory.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/ViewWithDataChooser.java
Added Paths:
-----------
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataForViewingException.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataToFileAndPrepareForViewingException.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertInMemoryDataToTextFileException.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/FileViewingException.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/NoProgramFoundException.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/PrepareTextFileForViewingException.java
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/UserCanceledDataViewSelectionException.java
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/.classpath
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/.classpath 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/.classpath 2009-10-12 17:37:06 UTC (rev 959)
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/j2sdk1.4.2_19"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
</classpath>
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 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF 2009-10-12 17:37:06 UTC (rev 959)
@@ -13,6 +13,7 @@
org.cishell.reference.service.metatype,
org.cishell.service.conversion;version="1.0.0",
org.cishell.service.guibuilder;version="1.0.0",
+ org.cishell.utilities,
org.osgi.framework;version="1.3.0",
org.osgi.service.cm;version="1.2.0",
org.osgi.service.component;version="1.0.0",
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -6,10 +6,9 @@
import org.osgi.service.log.LogService;
public class FileUtil {
-
private static int uniqueIntForTempFile = 1;
- public static File getTempFile(String fileName, String extension, LogService logger){
+ public static File getTempFile(String fileName, String extension, LogService logger) {
File tempFile;
if (fileName == null || fileName.equals("")) {
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 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/SaveFactory.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -1,48 +1,44 @@
package org.cishell.reference.gui.persistence.save;
-import java.io.File;
import java.util.Dictionary;
import org.cishell.framework.CIShellContext;
import org.cishell.framework.LocalCIShellContext;
import org.cishell.framework.algorithm.Algorithm;
import org.cishell.framework.algorithm.AlgorithmFactory;
-import org.cishell.framework.algorithm.DataValidator;
import org.cishell.framework.data.Data;
-import org.cishell.service.conversion.Converter;
-import org.cishell.service.conversion.DataConversionService;
import org.osgi.service.component.ComponentContext;
-import org.osgi.service.metatype.MetaTypeProvider;
/**
* Create a Save object
*
* TODO: Should also support if we can convert to file, but have
* no final file:X->file-ext:* converter.
- *
- * @author bmarkine
*
*/
public class SaveFactory implements AlgorithmFactory {
- private CIShellContext context;
+ private CIShellContext ciShellContext;
/**
* Create a local CIShell context
- * @param ctxt The current CIShell context
+ * @param componentContext The current CIShell context
*/
- protected void activate(ComponentContext ctxt) {
- context = new LocalCIShellContext(ctxt.getBundleContext());
+ protected void activate(ComponentContext componentContext) {
+ ciShellContext =
+ new LocalCIShellContext(componentContext.getBundleContext());
}
/**
* Create a Save algorithm
* @param data The data objects to save
* @param parameters The parameters for the algorithm
- * @param context Reference to services provided by CIShell
+ * @param ciShellContext Reference to services provided by CIShell
* @return An instance of the Save algorithm
*/
- public Algorithm createAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) {
- this.context = context;
- return new Save(data, parameters, context);
+ public Algorithm createAlgorithm(Data[] data,
+ Dictionary parameters,
+ CIShellContext ciShellContext) {
+ this.ciShellContext = ciShellContext;
+ return new Save(data, parameters, ciShellContext);
}
}
\ No newline at end of file
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -1,295 +1,47 @@
package org.cishell.reference.gui.persistence.view;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.nio.channels.FileChannel;
import java.util.Dictionary;
import org.cishell.framework.CIShellContext;
import org.cishell.framework.algorithm.Algorithm;
import org.cishell.framework.algorithm.AlgorithmExecutionException;
import org.cishell.framework.data.Data;
-import org.cishell.framework.data.DataProperty;
-import org.cishell.reference.gui.persistence.FileUtil;
+import org.cishell.reference.gui.persistence.view.core.FileViewer;
import org.cishell.service.conversion.ConversionException;
-import org.cishell.service.conversion.Converter;
import org.cishell.service.conversion.DataConversionService;
-import org.eclipse.swt.program.Program;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
-import org.osgi.service.log.LogService;
-/*
- * @author Weixia(Bonnie) Huang (hu...@in...)
- */
public class FileView implements Algorithm {
- public static final String ANY_FILE_EXTENSION_FILTER = "file-ext:*";
- private static final String CSV_FILE_EXT = "file-ext:csv";
- private static final String CSV_MIME_TYPE = "file:text/csv";
- public static final String FILE_EXTENSION_PREFIX = "file-ext:";
- public static final String ANY_TEXT_MIME_TYPE = "file:text/";
- private Data[] data;
- private CIShellContext context;
+ private Data[] dataToView;
+ private CIShellContext ciShellContext;
private DataConversionService conversionManager;
- private LogService logger;
- private Program program;
- private File tempFile;
-
public FileView(Data[] data, Dictionary parameters, CIShellContext context) {
- this.data = data;
- this.context = context;
+ this.dataToView = data;
+ this.ciShellContext = context;
this.conversionManager = (DataConversionService) context
.getService(DataConversionService.class.getName());
-
- this.logger = (LogService) context.getService(LogService.class.getName());
}
- // Show the contents of a file to the user
+ // Show the contents of a file to the user.
public Data[] execute() throws AlgorithmExecutionException {
try {
- boolean lastSaveSuccessful = false;
- boolean isCSVFile = false;
- String format;
-
- // For each data item we want to view...
- for (int i = 0; i < data.length; i++) {
- Object theData = data[i].getData();
- format = data[i].getFormat();
- String label = (String) data[i].getMetadata().get(
- DataProperty.LABEL);
-
- // If it is a text file...
- if (theData instanceof File
- || format.startsWith(ANY_TEXT_MIME_TYPE)
- || format.startsWith(FILE_EXTENSION_PREFIX)) {
-
- // If it is a CSV text file...
- if (format.startsWith(CSV_MIME_TYPE)
- || format.startsWith(CSV_FILE_EXT)) {
- // Prepare to open it like a CSV file
- tempFile = getTempFileCSV();
- isCSVFile = true;
- } else {
- // Prepare to open it like a normal text file
- String fileName = FileUtil.extractFileName(label);
- String extension = FileUtil.extractExtension(format);
- tempFile = FileUtil.getTempFile(fileName, extension,
- logger);
- }
-
- // Copy out data into the temp file we just created.
- copy((File) data[i].getData(), tempFile);
- lastSaveSuccessful = true;
-
- } else {
- /* The data item is in an in-memory format, and must be
- * converted to a file format before the user can see it.
- */
- final Converter[] convertersCSV =
- conversionManager.findConverters(data[i], CSV_FILE_EXT);
-
- // If the data item can be converted to a CSV file, do so.
- if (convertersCSV.length == 1) {
- Data newDataCSV = convertersCSV[0].convert(data[i]);
- tempFile = getTempFileCSV();
- isCSVFile = true;
- copy((File) newDataCSV.getData(), tempFile);
- lastSaveSuccessful = true;
-
- } else if (convertersCSV.length > 1) {
- Data newDataCSV = convertersCSV[0].convert(data[i]);
- for (int j = 1; j < convertersCSV.length; j++) {
- newDataCSV = convertersCSV[j].convert(newDataCSV);
- }
- tempFile = getTempFileCSV();
- isCSVFile = true;
- copy((File) newDataCSV.getData(), tempFile);
- lastSaveSuccessful = true;
- } else { // it cannot be converted to a .csv
-
- // try to convert it to any other file format
-
- final Converter[] converters =
- conversionManager.findConverters(
- data[i], ANY_FILE_EXTENSION_FILTER);
-
- // if it can't be converted to any file format...
- if (converters.length < 1) {
- // throw an error
- throw new AlgorithmExecutionException(
- "No valid converters for data type: "
- + data[i].getData().getClass()
- .getName()
- + ". Please install a plugin that will save the data type to a file");
- } else if (converters.length == 1) { // if there is only
- // file format
- // it can be
- // converted to
- // go ahead and convert the data item to that format
- Data newData = converters[0].convert(data[i]);
-
- String fileName = FileUtil.extractFileName(label);
- String extension = FileUtil
- .extractExtension(newData.getFormat());
- tempFile = FileUtil.getTempFile(fileName,
- extension, logger);
- copy((File) newData.getData(), tempFile);
- lastSaveSuccessful = true;
- } else {
- // there is more than one format that the data
- // item could be converted to
- // let the user choose
- // (get some eclipse UI stuff that we need to open
- // the data viewer)
-
- Display display;
- IWorkbenchWindow[] windows;
- final Shell parentShell;
-
- windows = PlatformUI.getWorkbench()
- .getWorkbenchWindows();
- if (windows.length == 0) {
- throw new AlgorithmExecutionException(
- "Cannot get workbench window.");
- }
- parentShell = windows[0].getShell();
- display = PlatformUI.getWorkbench().getDisplay();
-
- // (open the data viewer, which lets the user choose
- // which format they want to see the data item in.)
-
- if (!parentShell.isDisposed()) {
- DataViewer dataViewer = new DataViewer(
- parentShell, data[i], converters);
- display.syncExec(dataViewer);
- lastSaveSuccessful = dataViewer.isSaved;
- tempFile = dataViewer.outputFile;
- }
- }
- }
- }
-
- // If it's a CSV file
- if (isCSVFile) {// TC181
- // prepare to open the file with the default csv program
- Display.getDefault().syncExec(new Runnable() {
- public void run() {
- program = Program.findProgram("csv");
- }
- });
- } else {
- // Prepare to open it with the standard text editor.
- Display.getDefault().syncExec(
- new Runnable() {
- public void run() {
- program = Program.findProgram("txt");
- }
- }
- );
- }
-
- // If we can't find any program to open the file...
- if (program == null) {
- throw new AlgorithmExecutionException(
- "No valid text viewer for the .txt file. "
- + "The file is located at: "
- + tempFile.getAbsolutePath()
- + ". Unable to open default text viewer. "
- + "File is located at: "
- + tempFile.getAbsolutePath());
- } else {
- // We found a program to open the file. Open it.
- if (lastSaveSuccessful == true) {
- Display.getDefault().syncExec(
- new Runnable() {
- public void run() {
- program.execute(tempFile.getAbsolutePath());
- }
- }
- );
- }
- }
+ for (int ii = 0; ii < this.dataToView.length; ii++) {
+ FileViewer.viewDataFile(this.dataToView[ii],
+ this.ciShellContext,
+ this.conversionManager);
}
return null;
- } catch (ConversionException e) {
+ } catch (ConversionException conversionException) {
+ String exceptionMessage = "Error: Unable to view data:\n " +
+ conversionException.getMessage();
+
throw new AlgorithmExecutionException(
- "Error: Unable to view data:\n " + e.getMessage(), e);
- } catch (Throwable e) {
- throw new AlgorithmExecutionException(e);
+ exceptionMessage, conversionException);
+ } catch (Throwable thrownObject) {
+ throw new AlgorithmExecutionException(thrownObject);
}
}
-
- public File getTempFileCSV() {
- File tempFile;
-
- String tempPath = System.getProperty("java.io.tmpdir");
- File tempDir = new File(tempPath + File.separator + "temp");
- if (!tempDir.exists())
- tempDir.mkdir();
- try {
- tempFile = File.createTempFile("xxx-Session-", ".csv", tempDir);
-
- } catch (IOException e) {
- logger.log(LogService.LOG_ERROR, e.toString(), e);
- tempFile = new File(tempPath + File.separator + "temp"
- + File.separator + "temp.csv");
-
- }
- return tempFile;
- }
-
- public static boolean copy(File in, File out)
- throws AlgorithmExecutionException {
- try {
- FileInputStream fis = new FileInputStream(in);
- FileOutputStream fos = new FileOutputStream(out);
-
- FileChannel readableChannel = fis.getChannel();
- FileChannel writableChannel = fos.getChannel();
-
- writableChannel.truncate(0);
- writableChannel.transferFrom(readableChannel, 0, readableChannel
- .size());
- fis.close();
- fos.close();
- return true;
- } catch (IOException ioe) {
- throw new AlgorithmExecutionException("IOException during copy",
- ioe);
- }
- }
-
- final class DataViewer implements Runnable {
- public static final String VIEW_DIALOG_TITLE = "View";
- private Shell shell;
- private boolean isSaved;
- private File outputFile;
- private Data data;
- private Converter[] converters;
-
-
- DataViewer(Shell parentShell, Data data, Converter[] converters) {
- this.shell = parentShell;
- this.data = data;
- this.converters = converters;
- }
-
-
- public void run() {
- // Lots of persisters found, return the chooser
- ViewDataChooser vdc = new ViewDataChooser(
- VIEW_DIALOG_TITLE, shell, data, converters, context, logger);
- vdc.open();
- isSaved = vdc.isSaved();
- outputFile = vdc.outputFile;
- }
- }
}
\ No newline at end of file
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileViewFactory.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -6,14 +6,12 @@
import org.cishell.framework.algorithm.Algorithm;
import org.cishell.framework.algorithm.AlgorithmFactory;
import org.cishell.framework.data.Data;
-import org.osgi.service.component.ComponentContext;
-import org.osgi.service.metatype.MetaTypeProvider;
-//import org.osgi.service.metatype.MetaTypeService;
public class FileViewFactory implements AlgorithmFactory {
-
- public Algorithm createAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) {
- return new FileView(data, parameters, context);
+ public Algorithm createAlgorithm(Data[] data,
+ Dictionary parameters,
+ CIShellContext ciShellContext) {
+ return new FileView(data, parameters, ciShellContext);
}
}
\ No newline at end of file
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/ViewDataChooser.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/ViewDataChooser.java 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/ViewDataChooser.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -1,51 +1,50 @@
package org.cishell.reference.gui.persistence.view;
-import java.io.File;
-
import org.cishell.framework.CIShellContext;
-import org.cishell.framework.algorithm.AlgorithmExecutionException;
import org.cishell.framework.data.Data;
-import org.cishell.framework.data.DataProperty;
-import org.cishell.reference.gui.persistence.FileUtil;
import org.cishell.reference.gui.persistence.save.SaveDataChooser;
-import org.cishell.service.conversion.ConversionException;
import org.cishell.service.conversion.Converter;
import org.eclipse.swt.widgets.Shell;
import org.osgi.service.log.LogService;
-/*
- * @author Weixia(Bonnie) Huang (hu...@in...)
- *
- */
+// TODO: Refactor this class.
public class ViewDataChooser extends SaveDataChooser {
- boolean isSaved = false;
- LogService logger;
- Data theData;
- File outputFile;
+ /*private boolean isSaved = false;
+ private LogService logger;
+ private Data theData;
+ private File outputFile;*/
+ private Converter selectedConverter = null;
- public ViewDataChooser(String title, Shell parent,
- Data data, Converter[] converters, CIShellContext context, LogService logger){
- super (data, parent, converters, title, context);
+ public ViewDataChooser(String title,
+ Shell parent,
+ Data data,
+ Converter[] converters,
+ CIShellContext ciShellContext,
+ LogService logger){
+ super (data, parent, converters, title, ciShellContext);
- this.theData = data;
- this.logger = logger;
+ /*this.theData = data;
+ this.logger = logger;*/
}
-
+ //TODO: replace this stuff with convertToFile -- specifically, just ahve this return the darn selected format.
protected void selectionMade(int selectedIndex) {
getShell().setVisible(false);
- final Converter converter = converterArray[selectedIndex];
+ this.selectedConverter = converterArray[selectedIndex];
+ close(true);
+ /*final Converter selectedConverter = converterArray[selectedIndex];
try {
- Data newData = converter.convert(theData);
+ Data newData = selectedConverter.convert(theData);
String label = (String) newData.getMetadata().get(DataProperty.LABEL);
String fileName = FileUtil.extractFileName(label);
String extension = FileUtil.extractExtension(newData.getFormat());
File tempFile = FileUtil.getTempFile(fileName, extension, logger);
try {
- isSaved = FileView.copy((File)newData.getData(), tempFile);
- } catch (AlgorithmExecutionException e) {
- logger.log(LogService.LOG_ERROR, "Error copying view for view:\n " + e.getMessage(), e);
+ FileUtilities.copyFile((File)newData.getData(), tempFile);
+ isSaved = true;
+ } catch (FileCopyingException fileCopyingException) {
+ logger.log(LogService.LOG_ERROR, "Error copying view for view:\n " + fileCopyingException.getMessage(), fileCopyingException);
return;
}
@@ -54,12 +53,18 @@
} catch (ConversionException e) {
logger.log(LogService.LOG_ERROR, "Error: Unable to view data:\n " + e.getMessage(), e);
return;
- }
+ }*/
}
- public boolean isSaved(){
- return isSaved;
+ public Converter getSelectedConverter() {
+ return this.selectedConverter;
}
-
+ /*public boolean isSaved(){
+ return this.isSaved;
+ }
+
+ public File getOutputFile() {
+ return this.outputFile;
+ }*/
}
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,488 @@
+package org.cishell.reference.gui.persistence.view.core;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.cishell.framework.CIShellContext;
+import org.cishell.framework.data.Data;
+import org.cishell.framework.data.DataProperty;
+import org.cishell.reference.gui.persistence.FileUtil;
+import org.cishell.reference.gui.persistence.view.ViewDataChooser;
+import org.cishell.reference.gui.persistence.view.core.exceptiontypes.ConvertDataForViewingException;
+import org.cishell.reference.gui.persistence.view.core.exceptiontypes.FileViewingException;
+import org.cishell.reference.gui.persistence.view.core.exceptiontypes.NoProgramFoundException;
+import org.cishell.reference.gui.persistence.view.core.exceptiontypes.UserCanceledDataViewSelectionException;
+import org.cishell.service.conversion.ConversionException;
+import org.cishell.service.conversion.Converter;
+import org.cishell.service.conversion.DataConversionService;
+import org.cishell.utilities.FileCopyingException;
+import org.cishell.utilities.FileUtilities;
+import org.eclipse.swt.program.Program;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.osgi.service.log.LogService;
+
+public class FileViewer {
+ public static final String FILE_EXTENSION_MIME_TYPE_PREFIX = "file-ext:";
+ public static final String ANY_FILE_EXTENSION_FILTER = "file-ext:*";
+ public static final String FILE_EXTENSION_PREFIX = "file-ext:";
+ public static final String ANY_MIME_TYPE = "file:";
+ public static final String CSV_FILE_EXT = "file-ext:csv";
+ public static final String CSV_MIME_TYPE = "file:text/csv";
+ public static final String TEMPORARY_CSV_FILE_NAME = "xxx-Session-";
+ public static final String CSV_FILE_EXTENSION = "csv";
+ public static final String ANY_FILE_FORMAT_PATTERN =
+ "(file:.*)|(file-ext:.*)";
+
+ public static void viewDataFile(Data data,
+ CIShellContext ciShellContext,
+ DataConversionService conversionManager)
+ throws ConversionException, FileViewingException {
+ viewDataFileWithProgram(data, null, ciShellContext, conversionManager);
+ }
+
+ public static void viewDataFileWithProgram(
+ Data data,
+ String customFileExtension,
+ CIShellContext ciShellContext,
+ DataConversionService converterManager)
+ throws FileViewingException {
+ FileWithExtension fileWithExtension =
+ convertDataForViewing(data, ciShellContext, converterManager);
+ viewFileWithExtension(fileWithExtension, customFileExtension);
+ }
+
+ private static FileWithExtension convertDataForViewing(
+ Data data,
+ CIShellContext ciShellContext,
+ DataConversionService converterManager)
+ throws FileViewingException {
+ try {
+ String dataFormat = data.getFormat();
+
+ if (isCSVFormat(data)) {
+ /*
+ * The data is already a CSV file, so it just needs to
+ * be copied.
+ */
+ try {
+ File csvFileForViewing =
+ FileUtilities.createTemporaryFileCopy(
+ (File)data.getData(),
+ TEMPORARY_CSV_FILE_NAME,
+ CSV_FILE_EXTENSION);
+
+ return new FileWithExtension(
+ csvFileForViewing, CSV_FILE_EXTENSION);
+ } catch (FileCopyingException csvFileCopyingException) {
+ throw new ConvertDataForViewingException(
+ csvFileCopyingException);
+ }
+ } else if (dataIsCSVCompatible(data, converterManager)) {
+ /*
+ * The data is either a CSV file already or CSV-convertible.
+ * This needs to be handled specially so data that can be
+ * viewed in Excel gets viewed in Excel.
+ */
+ File preparedFileForViewing = prepareFileForViewing(
+ data, CSV_FILE_EXTENSION, converterManager);
+
+ return new FileWithExtension(
+ preparedFileForViewing, CSV_FILE_EXTENSION);
+ } else if (dataIsFile(data, dataFormat)) {
+ /*
+ * The data is already a text-based file, so it just needs to
+ * be copied to a temporary file for viewing in the default
+ * text-viewing program.
+ */
+ return new FileWithExtension(
+ prepareTextFileForViewing(data), "txt");
+ } else if (convertersExist(
+ data, ANY_FILE_EXTENSION_FILTER, converterManager)) {
+ /*
+ * The data is an another type, but it can be converted to a
+ * text-based file type for viewing in the default
+ * text-viewing program.
+ */
+ return new FileWithExtension(
+ convertDataToTextFile(
+ data, converterManager, ciShellContext),
+ "txt");
+ } else {
+ String exceptionMessage =
+ "No converters exist for the data \"" +
+ data.getMetadata().get(DataProperty.LABEL) +
+ "\".";
+
+ throw new ConvertDataForViewingException(exceptionMessage);
+ }
+ } catch (ConvertDataForViewingException
+ convertDataForViewingException) {
+ String exceptionMessage =
+ "There was a problem when preparing the data \"" +
+ data.getMetadata().get(DataProperty.LABEL) +
+ "\" for viewing.";
+
+ throw new FileViewingException(
+ exceptionMessage, convertDataForViewingException);
+ }
+ }
+
+ private static void viewFileWithExtension(
+ FileWithExtension fileWithExtension, String customFileExtension)
+ throws FileViewingException {
+ try {
+ final Program program = selectProgramForFileExtension(
+ fileWithExtension.fileExtension, customFileExtension);
+
+ executeProgramWithFile(program, fileWithExtension.file);
+ } catch (NoProgramFoundException noProgramFoundException) {
+ String exceptionMessage =
+ "Could not view the file \"" +
+ fileWithExtension.file.getAbsolutePath() +
+ "\" because no viewing program could be found for it.";
+
+ throw new FileViewingException(
+ exceptionMessage, noProgramFoundException);
+ }
+ }
+
+ private static boolean isCSVFormat(Data data) {
+ String dataFormat = data.getFormat();
+
+ if (dataFormat.startsWith(CSV_MIME_TYPE) ||
+ dataFormat.startsWith(CSV_FILE_EXT)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /*private static boolean dataIsCSVCompatibleFile(
+ Data data,
+ DataConversionService converterManager) {
+ return convertersExist(data, CSV_FILE_EXT, converterManager);
+ }*/
+
+ private static boolean dataIsCSVCompatible(
+ Data data,
+ DataConversionService converterManager) {
+ if (isCSVFormat(data) ||
+ convertersExist(data, CSV_FILE_EXT, converterManager)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private static boolean dataIsFile(Data data, String dataFormat) {
+ if (data.getData() instanceof File ||
+ dataFormat.startsWith(ANY_MIME_TYPE) ||
+ dataFormat.startsWith(FILE_EXTENSION_PREFIX)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private static boolean convertersExist(
+ Data data,
+ String targetFormat,
+ DataConversionService conversionManager) {
+ final Converter[] converters =
+ conversionManager.findConverters(data, targetFormat);
+
+ if (converters.length > 0) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private static File prepareFileForViewing(
+ Data originalData,
+ String fileExtension,
+ DataConversionService converterManager)
+ throws ConvertDataForViewingException {
+ String dataLabel =
+ (String)originalData.getMetadata().get(DataProperty.LABEL);
+
+ try {
+ String fileExtensionMimeType =
+ FILE_EXTENSION_MIME_TYPE_PREFIX + fileExtension;
+ File convertedFile = convertToFile(
+ originalData, fileExtensionMimeType, converterManager);
+ String fileName = FileUtil.extractFileName(dataLabel);
+
+ return FileUtilities.createTemporaryFileCopy(
+ convertedFile, fileName, fileExtension);
+ } catch (ConversionException convertingDataToFileException) {
+ String exceptionMessage =
+ "A ConversionException occurred when converting the data \"" +
+ dataLabel +
+ "\" to " + fileExtension + ".";
+
+ throw new ConvertDataForViewingException(
+ exceptionMessage, convertingDataToFileException);
+ } catch (FileCopyingException temporaryFileCopyingException) {
+ String exceptionMessage =
+ "A FileCopyingException occurred when converting the data \"" +
+ dataLabel +
+ "\" to " + fileExtension + ".";
+
+ throw new ConvertDataForViewingException(
+ exceptionMessage, temporaryFileCopyingException);
+ }
+ }
+
+ private static File prepareTextFileForViewing(Data originalData)
+ throws ConvertDataForViewingException {
+ String dataLabel =
+ (String)originalData.getMetadata().get(DataProperty.LABEL);
+ String dataFormat = originalData.getFormat();
+ String fileName = FileUtil.extractFileName(dataLabel);
+ String fileExtension = FileUtil.extractExtension(dataFormat);
+
+ try {
+ File fileToView = FileUtilities.
+ createTemporaryFileInDefaultTemporaryDirectory(
+ fileName, fileExtension);
+ FileUtilities.copyFile((File)originalData.getData(), fileToView);
+
+ return fileToView;
+ } catch (IOException temporaryFileCreationException) {
+ String exceptionMessage =
+ "An IOException occurred when creating the temporary file \"" +
+ fileName + "." + fileExtension +
+ "\" for viewing the data \"" + dataLabel + "\".";
+
+ throw new ConvertDataForViewingException(
+ exceptionMessage, temporaryFileCreationException);
+ } catch (FileCopyingException fileCopyingException) {
+ throw new ConvertDataForViewingException(fileCopyingException);
+ }
+ }
+
+ private static File convertDataToTextFile(
+ Data originalData,
+ DataConversionService converterManager,
+ CIShellContext ciShellContext)
+ throws ConvertDataForViewingException {
+ final Converter[] converters = converterManager.findConverters(
+ originalData, ANY_FILE_EXTENSION_FILTER);
+
+ if (converters.length == 1) {
+ /*
+ * There is just one converter, so transparently do
+ * the conversion.
+ */
+ try {
+ return convertToFile(
+ originalData, converters[0]);
+ }
+ catch (ConversionException
+ convertDataToFileAndPrepareForViewingException) {
+ String exceptionMessage =
+ "A ConversionException occurred when converting the " +
+ "data \"" +
+ originalData.getMetadata().get(DataProperty.LABEL) +
+ "\" to a file format.";
+
+ throw new ConvertDataForViewingException(
+ exceptionMessage,
+ convertDataToFileAndPrepareForViewingException);
+ }
+ } else {
+ /*
+ * There are several converters available, so the user will
+ * need to select how the dataToView is to be converted.
+ */
+ try {
+ return convertDataBasedOffUserChosenConverter(
+ originalData, converters, ciShellContext);
+ } catch (ConversionException conversionException) {
+ String exceptionMessage =
+ "A ConversionException occurred when converting the " +
+ "data \"" +
+ originalData.getMetadata().get(DataProperty.LABEL) +
+ "\".";
+
+ throw new ConvertDataForViewingException(
+ exceptionMessage, conversionException);
+ } catch (UserCanceledDataViewSelectionException
+ userCanceledDataViewSelectionException) {
+ String exceptionMessage =
+ "A UserCanceledDataViewSelectionException occurred " +
+ "when the user did not choose a converter for the " +
+ "data \"" +
+ originalData.getMetadata().get(DataProperty.LABEL) +
+ "\".";
+
+ throw new ConvertDataForViewingException(
+ exceptionMessage, userCanceledDataViewSelectionException);
+ }
+ }
+ }
+
+ private static Program selectProgramForFileExtension(
+ final String fileExtension, final String customFileExtension)
+ throws NoProgramFoundException {
+ String chosenFileExtension = null;
+
+ if ((customFileExtension == null) || customFileExtension.equals("")) {
+ chosenFileExtension = fileExtension;
+ } else {
+ chosenFileExtension = customFileExtension;
+ }
+
+ final Program[] programHolder = new Program[1];
+
+ Display.getDefault().syncExec(new Runnable() {
+ public void run() {
+ programHolder[0] =
+ Program.findProgram(fileExtension);
+ }
+ });
+
+ Program program = programHolder[0];
+
+ if (program != null) {
+ return program;
+ } else {
+ String exceptionMessage =
+ "You do not have a valid viewer for the ." +
+ chosenFileExtension +
+ "file installed.";
+
+ throw new NoProgramFoundException(exceptionMessage);
+ }
+ }
+
+ private static void executeProgramWithFile(final Program program,
+ final File file) {
+ Display.getDefault().syncExec(new Runnable() {
+ public void run() {
+ program.execute(
+ file.getAbsolutePath());
+ }
+ });
+ }
+
+ private static File convertToFile(Data data,
+ String targetFormat,
+ DataConversionService conversionManager)
+ throws ConversionException {
+ if (targetFormat.matches(ANY_FILE_FORMAT_PATTERN)) {
+ Converter[] converters =
+ conversionManager.findConverters(data, targetFormat);
+
+ return convertToFile(data, converters[0]);
+ } else {
+ String exceptionMessage =
+ "The target format for conversion (\"" +
+ targetFormat +
+ "\") is not valid.";
+
+ throw new ConversionException(exceptionMessage);
+ }
+ }
+
+
+ private static File convertToFile(Data data, Converter converter)
+ throws ConversionException {
+ Data newData = converter.convert(data);
+ return (File)newData.getData();
+ }
+
+ private static File convertDataBasedOffUserChosenConverter(
+ Data originalData,
+ Converter[] converters,
+ CIShellContext ciShellContext)
+ throws ConversionException,
+ UserCanceledDataViewSelectionException {
+ /*
+ * Open the dataToView viewer, which lets the user choose
+ * which format he/she wants to see the data item in.
+ */
+ DataViewer dataViewer = new DataViewer(
+ originalData, converters, ciShellContext);
+
+ if (dataViewer.selectedConverter != null) {
+ return convertToFile(originalData, dataViewer.selectedConverter);
+ } else {
+ String exceptionMessage =
+ "The user cancelled the selection of a converter for the " +
+ "data \"" +
+ originalData.getMetadata().get(DataProperty.LABEL) +
+ "\".";
+
+ throw new UserCanceledDataViewSelectionException(
+ exceptionMessage);
+ }
+ }
+
+ private final static class DataViewer implements Runnable {
+ public static final String VIEW_DIALOG_TITLE = "View";
+ private Shell shellWindow;
+ /*private boolean isSaved;
+ private File outputFile;*/
+ private Converter selectedConverter;
+ private Data data;
+ private Converter[] converters;
+ private CIShellContext ciShellContext;
+ private LogService logger;
+
+ public DataViewer(Data data,
+ Converter[] converters,
+ CIShellContext ciShellContext) {
+ this(data,
+ converters,
+ ciShellContext,
+ (LogService)ciShellContext.getService(
+ LogService.class.getName()));
+ }
+
+ public DataViewer(Data data,
+ Converter[] converters,
+ CIShellContext ciShellContext,
+ LogService logger) {
+ IWorkbenchWindow[] windows =
+ PlatformUI.getWorkbench().getWorkbenchWindows();
+ this.shellWindow = windows[0].getShell();
+ this.data = data;
+ this.converters = converters;
+ this.ciShellContext = ciShellContext;
+ this.logger = logger;
+
+ Display display = PlatformUI.getWorkbench().getDisplay();
+ display.syncExec(this);
+ }
+
+ public void run() {
+ // Lots of persisters found, return the chooser.
+ ViewDataChooser viewDataChooser = new ViewDataChooser(
+ VIEW_DIALOG_TITLE,
+ this.shellWindow,
+ this.data,
+ this.converters,
+ this.ciShellContext,
+ this.logger);
+ viewDataChooser.open();
+ /*isSaved = viewDataChooser.isSaved();
+ outputFile = viewDataChooser.getOutputFile();*/
+ this.selectedConverter = viewDataChooser.getSelectedConverter();
+ }
+ }
+
+ private static class FileWithExtension {
+ public final File file;
+ public final String fileExtension;
+
+ public FileWithExtension(File file, String fileExtension) {
+ this.file = file;
+ this.fileExtension = fileExtension;
+ }
+ }
+}
\ No newline at end of file
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataForViewingException.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataForViewingException.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataForViewingException.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,21 @@
+package org.cishell.reference.gui.persistence.view.core.exceptiontypes;
+
+public class ConvertDataForViewingException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public ConvertDataForViewingException() {
+ super();
+ }
+
+ public ConvertDataForViewingException(String arg0) {
+ super(arg0);
+ }
+
+ public ConvertDataForViewingException(Throwable arg0) {
+ super(arg0);
+ }
+
+ public ConvertDataForViewingException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+}
\ No newline at end of file
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataToFileAndPrepareForViewingException.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataToFileAndPrepareForViewingException.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertDataToFileAndPrepareForViewingException.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,22 @@
+package org.cishell.reference.gui.persistence.view.core.exceptiontypes;
+
+public class ConvertDataToFileAndPrepareForViewingException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public ConvertDataToFileAndPrepareForViewingException() {
+ super();
+ }
+
+ public ConvertDataToFileAndPrepareForViewingException(String arg0) {
+ super(arg0);
+ }
+
+ public ConvertDataToFileAndPrepareForViewingException(Throwable arg0) {
+ super(arg0);
+ }
+
+ public ConvertDataToFileAndPrepareForViewingException(
+ String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+}
\ No newline at end of file
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertInMemoryDataToTextFileException.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertInMemoryDataToTextFileException.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/ConvertInMemoryDataToTextFileException.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,21 @@
+package org.cishell.reference.gui.persistence.view.core.exceptiontypes;
+
+public class ConvertInMemoryDataToTextFileException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public ConvertInMemoryDataToTextFileException() {
+ super();
+ }
+
+ public ConvertInMemoryDataToTextFileException(String arg0) {
+ super(arg0);
+ }
+
+ public ConvertInMemoryDataToTextFileException(Throwable arg0) {
+ super(arg0);
+ }
+
+ public ConvertInMemoryDataToTextFileException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+}
\ No newline at end of file
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/FileViewingException.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/FileViewingException.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/FileViewingException.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,21 @@
+package org.cishell.reference.gui.persistence.view.core.exceptiontypes;
+
+public class FileViewingException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public FileViewingException() {
+ super();
+ }
+
+ public FileViewingException(String arg0) {
+ super(arg0);
+ }
+
+ public FileViewingException(Throwable arg0) {
+ super(arg0);
+ }
+
+ public FileViewingException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+}
\ No newline at end of file
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/NoProgramFoundException.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/NoProgramFoundException.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/NoProgramFoundException.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,21 @@
+package org.cishell.reference.gui.persistence.view.core.exceptiontypes;
+
+public class NoProgramFoundException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public NoProgramFoundException() {
+ super();
+ }
+
+ public NoProgramFoundException(String arg0) {
+ super(arg0);
+ }
+
+ public NoProgramFoundException(Throwable arg0) {
+ super(arg0);
+ }
+
+ public NoProgramFoundException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+}
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/PrepareTextFileForViewingException.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/PrepareTextFileForViewingException.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/PrepareTextFileForViewingException.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,21 @@
+package org.cishell.reference.gui.persistence.view.core.exceptiontypes;
+
+public class PrepareTextFileForViewingException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public PrepareTextFileForViewingException() {
+ super();
+ }
+
+ public PrepareTextFileForViewingException(String arg0) {
+ super(arg0);
+ }
+
+ public PrepareTextFileForViewingException(Throwable arg0) {
+ super(arg0);
+ }
+
+ public PrepareTextFileForViewingException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+}
\ No newline at end of file
Added: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/UserCanceledDataViewSelectionException.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/UserCanceledDataViewSelectionException.java (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/exceptiontypes/UserCanceledDataViewSelectionException.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -0,0 +1,21 @@
+package org.cishell.reference.gui.persistence.view.core.exceptiontypes;
+
+public class UserCanceledDataViewSelectionException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public UserCanceledDataViewSelectionException() {
+ super();
+ }
+
+ public UserCanceledDataViewSelectionException(String arg0) {
+ super(arg0);
+ }
+
+ public UserCanceledDataViewSelectionException(Throwable arg0) {
+ super(arg0);
+ }
+
+ public UserCanceledDataViewSelectionException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+}
\ No newline at end of file
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWith.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWith.java 2009-10-12 16:29:06 UTC (rev 958)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWith.java 2009-10-12 17:37:06 UTC (rev 959)
@@ -26,20 +26,22 @@
* @author Felix Terkhorn (ter...@gm...), Weixia Huang (hu...@in...)
*/
public class FileViewWith implements Algorithm {
- Data[] data;
- Dictionary parameters;
- CIShellContext context;
- DataConversionService conversionManager;
- static GUIBuilderService guiBuilder;
- LogService logger;
- Program program;
- Program programTwo;
- Program programThree;
- Program programFour; //TC181
- File tempFile;
+ public static final String VIEW_WITH_PARAMETER_KEY = "viewWith";
+
+ private Data[] dataToView;
+ private Dictionary parameters;
+ private CIShellContext context;
+ private DataConversionService conversionManager;
+ private static GUIBuilderService guiBuilder;
+ private LogService logger;
+ private Program textProgram;
+ private Program wordProgram;
+ private Program webBrowserProgram;
+ private Program spreadsheetProgram;
+ private File temporaryFile;
public FileViewWith(Data[] data, Dictionary parameters, CIShellContext context) {
- this.data = data;
+ this.dataToView = data;
this.parameters = parameters;
this.context = context;
@@ -50,150 +52,192 @@
guiBuilder = (GUIBuilderService)context.getService(GUIBuilderService.class.getName());
}
+
public File getTempFile(){
File tempFile;
String tempPath = System.getProperty("java.io.tmpdir");
- File tempDir = new File(tempPath+File.separator+"temp");
- if(!tempDir.exists())
+ File tempDir = new File(tempPath + File.separator + "temp");
+
+ if (!tempDir.exists()) {
tempDir.mkdir();
- try{
+ }
+
+ try {
tempFile = File.createTempFile("xxx-Session-", ".txt", tempDir);
- }catch (IOException e){
- logger.log(LogService.LOG_ERROR, e.toString(), e);
- tempFile = new File (tempPath+File.separator+"temp"+File.separator+"temp.txt");
+ } catch (IOException ioException) {
+ logger.log(
+ LogService.LOG_ERROR, ioException.toString(), ioException);
+
+ String separator = File.separator;
+ String temporaryFileName =
+ tempPath + separator + "temp" + separator + "temp.txt";
+ tempFile = new File(temporaryFileName);
}
+
return tempFile;
}
public Data[] execute() throws AlgorithmExecutionException {
- boolean lastSaveSuccessful = false;
+ // TODO: Refactor this code so it and FileView use the same code.
+ boolean temporaryFileWasCreated = false;
String format;
- String viewWith = (String) parameters.get("viewWith");
+ String viewWithType = (String)parameters.get(VIEW_WITH_PARAMETER_KEY);
Display display;
IWorkbenchWindow[] windows;
final Shell parentShell;
windows = PlatformUI.getWorkbench().getWorkbenchWindows();
- if (windows.length == 0){
+
+ if (windows.length == 0) {
return null;
}
+
parentShell = windows[0].getShell();
display = PlatformUI.getWorkbench().getDisplay();
- tempFile = getTempFile();
+ temporaryFile = getTempFile();
- for (int i = 0; i < data.length; i++){
- Object theData = data[i].getData();
- format = data[i].getFormat();
+ for (int ii = 0; ii < this.dataToView.length; ii++){
+ Data data = this.dataToView[ii];
+ Object theData = data.getData();
+ format = data.getFormat();
+
if (theData instanceof File ||
format.startsWith("file:text/") ||
format.startsWith("file-ext:")){
- copy((File)data[i].getData(), tempFile);
- lastSaveSuccessful = true;
- }else{
- final Converter[] converters = conversionManager.findConverters(data[i], "file-ext:*");
+ copy((File)data.getData(), temporaryFile);
+ temporaryFileWasCreated = true;
+ } else {
+ final Converter[] converters =
+ conversionManager.findConverters(data, "file-ext:*");
- if (converters.length < 1) {
- guiBuilder.showError("No Converters",
- "No valid converters for data type: " +
- data[i].getData().getClass().getName(),
- "Please install a plugin that will save the data type to a file");
- }
- else if (converters.length == 1){
- //If length=1, use the unique path to save it directly
- //and bring the text editor.
+ if (converters.length == 1) {
+ /*
+ * If length is 1, use the unique path to save it directly
+ * and bring the text editor.
+ */
+
try {
- Data newData = converters[0].convert(data[i]);
- copy((File)newData.getData(), tempFile);
- lastSaveSuccessful = true;
- } catch (ConversionException e) {
- this.logger.log(LogService.LOG_WARNING, "Warning: Unable to convert to target save format (" + e.getMessage() + "). Will attempt to use other available converters.", e);
+ Data newData = converters[0].convert(data);
+ copy((File)newData.getData(), temporaryFile);
+ temporaryFileWasCreated = true;
+ } catch (ConversionException conversionException) {
+ String warningMessage =
+ "Warning: Unable to convert to target save " +
+ "format (" + conversionException.getMessage() +
+ "). Will attempt to use other " +
+ "available converters.";
+ this.logger.log(LogService.LOG_WARNING,
+ warningMessage,
+ conversionException);
}
- }
- else {
+ } else if (converters.length > 1) {
if (!parentShell.isDisposed()) {
try {
- DataViewer dataViewer = new DataViewer(parentShell, data[i], converters);
- display.syncExec(dataViewer);
+ DataViewer dataViewer =
+ new DataViewer(parentShell, data, converters);
+ display.syncExec(dataViewer);
- lastSaveSuccessful = dataViewer.isSaved;
- tempFile = dataViewer.theFile;
- } catch (Throwable e1) {
- throw new AlgorithmExecutionException(e1);
+ temporaryFileWasCreated = dataViewer.isSaved;
+ temporaryFile = dataViewer.theFile;
+ } catch (Throwable thrownObject) {
+ throw new AlgorithmExecutionException(
+ thrownObject);
}
}
}
+ else {
+ String errorMessage =
+ "No valid converters for data type: " +
+ data.getData().getClass().getName();
+ String errorDetail =
+ "Please install a plugin that will save the " +
+ "data type to a file";
+ guiBuilder.showError(
+ "No Converters", errorMessage, errorDetail);
+ }
}
-
+ //TODO: holy code duplication, batman!
Display.getDefault().syncExec(new Runnable() {
public void run() {
- program = Program.findProgram("txt");
+ textProgram = Program.findProgram("txt");
}});
Display.getDefault().syncExec(new Runnable() {
public void run() {
- programTwo = Program.findProgram("doc");
+ wordProgram = Program.findProgram("doc");
}});
Display.getDefault().syncExec(new Runnable() {
public void run() {
- programThree = Program.findProgram("htm");
+ webBrowserProgram = Program.findProgram("htm");
}});
- //TC181
+
Display.getDefault().syncExec(new Runnable() {
public void run() {
- programFour = Program.findProgram("csv");
+ spreadsheetProgram = Program.findProgram("csv");
}});
- //TC181
- if (program == null && programTwo == null && programThree == null && programThree == null) {
- guiBuilder.showError("No Viewers for TXT, DOC, or HTM",
- "No valid viewers for .txt, .doc, or .htm files. " +
- "The file is located at: "+tempFile.getAbsolutePath(),
- "Unable to open default text viewer. File is located at: "+
- tempFile.getAbsolutePath());
+ if ((textProgram == null) &&
+ (wordProgram == null) &&
+ (webBrowserProgram == null) &&
+ (webBrowserProgram == null)) {
+ String errorTitle = "No Viewers for TXT, DOC, or HTM";
+ String errorMessage =
+ "No valid viewers for .txt, .doc, or .htm files. " +
+ "The file is located at: " + temporaryFile.getAbsolutePath();
+ String errorDetail =
+ "Unable to open default text viewer. " +
+ "File is located at: " +
+ temporaryFile.getAbsolutePath();
+ guiBuilder.showError(
+ errorTitle, errorMessage, errorDetail);
}
else {
- if (lastSaveSuccessful == true) {
- if (viewWith.equals("txt")) {
+ if (temporaryFileWasCreated) {
+ final String filePath = temporaryFile.getAbsolutePath();
+
+ //TODO: . . . I already said "holy code duplication batman!", didn't I?
+ if (viewWithType.equals("txt")) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
- program.execute(tempFile.getAbsolutePath());
- }});
- } else if (viewWith.equals("doc")) {
+ textProgram.execute(filePath);
+ }
+ });
+ } else if (viewWithType.equals("doc")) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
- programTwo.execute(tempFile.getAbsolutePath());
- }});
- } el...
[truncated message content] |