|
From: <mwl...@us...> - 2008-08-15 19:01:00
|
Revision: 799
http://cishell.svn.sourceforge.net/cishell/?rev=799&view=rev
Author: mwlinnem
Date: 2008-08-15 19:00:54 +0000 (Fri, 15 Aug 2008)
Log Message:
-----------
Files opened with "View..." will now have a name based on their label in the DataManager, and should usually have the correct extension (.nwb, or .net, or whatever), instead of always being .txt
Modified Paths:
--------------
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.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/ViewDataChooser.java
Added Paths:
-----------
trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java
Added: 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 (rev 0)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/FileUtil.java 2008-08-15 19:00:54 UTC (rev 799)
@@ -0,0 +1,99 @@
+package org.cishell.reference.gui.persistence;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.osgi.service.log.LogService;
+
+public class FileUtil {
+
+ private static int uniqueIntForTempFile = 1;
+
+ public static File getTempFile(String fileName, String extension, LogService logger){
+ File tempFile;
+
+ if (fileName == null || fileName.equals("")) {
+ fileName = "unknown";
+ }
+
+ if (extension == null || extension.equals("")) {
+ extension = ".txt";
+ }
+
+ if (!extension.startsWith(".")) {
+ extension = extension + ".";
+ }
+
+ String tempPath = System.getProperty("java.io.tmpdir");
+ File tempDir = new File(tempPath+File.separator+"nwb");
+ if(!tempDir.exists())
+ tempDir.mkdir();
+ try{
+ tempFile = File.createTempFile(fileName, extension, tempDir);
+
+ }catch (IOException e1){
+ //failed with given file name and extension. Let's use a standard one.
+ logger.log(LogService.LOG_WARNING, "Failed to create temp file with provided name and extension '" + fileName + extension + "'. Trying a generic name and extension instead.", e1);
+ try {
+ tempFile = File.createTempFile("unknown", ".txt", tempDir);
+ } catch (IOException e2) {
+ //looks like it doesn't even like that. We'll have to just make a file directly.
+ tempFile = new File (tempPath+File.separator+"nwb"+File.separator+"unknown" + uniqueIntForTempFile + ".txt");
+ uniqueIntForTempFile++;
+
+ logger.log(LogService.LOG_ERROR, "Failed to create temp file twice...");
+ logger.log(LogService.LOG_ERROR, "First Try... \r\n " + e1.toString());
+ logger.log(LogService.LOG_ERROR, "Second Try... \r\n " + e2.toString());
+ }
+ }
+ return tempFile;
+ }
+
+ public static String extractExtension(String format) {
+ String extension = "";
+ //TODO: We should really have explicit piece of metadata that says what the extension is,
+ //TODO: as this method is not guaranteed to yield the correct extension
+ if (format.startsWith("file:text/")) {
+ extension = "." + format.substring("file:text/".length());
+ } else if (format.startsWith("file-ext:")) {
+ extension = "." + format.substring("file-ext:".length());
+ }
+
+ extension = extension.replace('+', '.');
+
+ return extension;
+ }
+
+ public static String extractFileName(String fileLabel) {
+
+ //index variables will be -1 if index is not found.
+ int descriptionEndIndex = fileLabel.lastIndexOf(":");
+ int filePathEndIndex = fileLabel.lastIndexOf(File.separator);
+
+ //doesn't matter if either variable is -1, since startIndex will be
+ //zero and none of the string will be cut off the front.
+ int startIndex = Math.max(descriptionEndIndex, filePathEndIndex) + 1;
+
+ String fileNameWithExtension = fileLabel.substring(startIndex);
+
+
+ //find the first character of the file name extension.
+ int extensionBeginIndex = fileNameWithExtension.lastIndexOf(".");
+
+ int endIndex;
+
+ if (extensionBeginIndex != -1) {
+ //we found a period in the file name.
+ endIndex = extensionBeginIndex; //cut off everything after
+ //first period.
+ } else {
+ //we didn't find an extension on the file name.
+ endIndex = fileNameWithExtension.length(); // don't cut any off the end.
+ }
+
+ String fileNameWithoutExtension = fileNameWithExtension.substring(0, endIndex);
+
+ String fileName = fileNameWithoutExtension;
+ return fileName;
+ }
+}
Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java
===================================================================
--- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java 2008-08-13 17:14:47 UTC (rev 798)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/save/FileSaver.java 2008-08-15 19:00:54 UTC (rev 799)
@@ -13,6 +13,7 @@
import org.cishell.framework.algorithm.AlgorithmProperty;
import org.cishell.framework.data.Data;
import org.cishell.framework.data.DataProperty;
+import org.cishell.reference.gui.persistence.FileUtil;
import org.cishell.service.conversion.ConversionException;
import org.cishell.service.conversion.Converter;
import org.cishell.service.guibuilder.GUIBuilderService;
@@ -112,7 +113,7 @@
dialog.setText("Choose File");
String fileLabel = (String)data.getMetadata().get(DataProperty.LABEL);
- String suggestedFileName = getFileName(fileLabel);
+ String suggestedFileName = FileUtil.extractFileName(fileLabel);
dialog.setFileName(suggestedFileName + "." + ext);
// if (fileLabel == null) {
@@ -185,36 +186,5 @@
}
}
- private String getFileName(String fileLabel) {
-
- //index variables will be -1 if index is not found.
- int descriptionEndIndex = fileLabel.lastIndexOf(":");
- int filePathEndIndex = fileLabel.lastIndexOf(File.separator);
-
- //doesn't matter if either variable is -1, since startIndex will be
- //zero and none of the string will be cut off the front.
- int startIndex = Math.max(descriptionEndIndex, filePathEndIndex) + 1;
-
- String fileNameWithExtension = fileLabel.substring(startIndex);
-
-
- //find the first character of the file name extension.
- int extensionBeginIndex = fileNameWithExtension.lastIndexOf(".");
-
- int endIndex;
-
- if (extensionBeginIndex != -1) {
- //we found a period in the file name.
- endIndex = extensionBeginIndex; //cut off everything after
- //first period.
- } else {
- //we didn't find an extension on the file name.
- endIndex = fileNameWithExtension.length(); // don't cut any off the end.
- }
-
- String fileNameWithoutExtension = fileNameWithExtension.substring(0, endIndex);
-
- String fileName = fileNameWithoutExtension;
- return fileName;
- }
+
}
\ 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 2008-08-13 17:14:47 UTC (rev 798)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java 2008-08-15 19:00:54 UTC (rev 799)
@@ -11,10 +11,11 @@
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.service.conversion.ConversionException;
import org.cishell.service.conversion.Converter;
import org.cishell.service.conversion.DataConversionService;
-import org.cishell.service.guibuilder.GUIBuilderService;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
@@ -45,87 +46,55 @@
logger = (LogService)context.getService(LogService.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())
- tempDir.mkdir();
- try{
- tempFile = File.createTempFile("xxx-Session-", ".txt", tempDir);
-
- }catch (IOException e){
- logger.log(LogService.LOG_ERROR, e.toString());
- tempFile = new File (tempPath+File.separator+"temp"+File.separator+"temp.txt");
- }
- return tempFile;
- }
-
- public File getTempFileCSV(){ //TC181
- 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());
- tempFile = new File (tempPath+File.separator+"temp"+File.separator+"temp.csv");
-
- }
- return tempFile;
- }
-
+ //show the contents of a file to the user
public Data[] execute() throws AlgorithmExecutionException {
try {
boolean lastSaveSuccessful = false;
- boolean isCSVFile = false;//TC181
+ boolean isCSVFile = false;
String format;
- Display display;
- IWorkbenchWindow[] windows;
- final Shell parentShell;
+ //tempFile = getTempFile(); TC181
- windows = PlatformUI.getWorkbench().getWorkbenchWindows();
- if (windows.length == 0){
- throw new AlgorithmExecutionException("Cannot get workbench window.");
- }
- parentShell = windows[0].getShell();
- display = PlatformUI.getWorkbench().getDisplay();
- //tempFile = getTempFile(); TC181
+ //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("file:text/") ||
format.startsWith("file-ext:")){
+ //if it is a csv text file...
if (format.startsWith("file:text/csv") || format.startsWith("file-ext:csv"))
{
+ //prepare to open it like a csv file
tempFile = getTempFileCSV();
isCSVFile = true;
}
- else
- {
- tempFile = getTempFile();
+ else //it is just a regular text file
+ {
+ //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{
+ }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], "file-ext:csv");
- //logger.log(LogService.LOG_ERROR, "convertersCSV's length = " + convertersCSV.length);
+
+ //if the data item can be converted to a csv file ... do it.
if (convertersCSV.length == 1)
{
Data newDataCSV = convertersCSV[0].convert(data[i]);
@@ -146,41 +115,69 @@
isCSVFile = true;
copy((File)newDataCSV.getData(), tempFile);
lastSaveSuccessful = true;
- }
- else{
+ } else { //it cannot be converted to a .csv
+
+ //try to convert it to any other file format
+
final Converter[] converters = conversionManager.findConverters(data[i], "file-ext:*");
+ //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 length=1, use the unique path to save it directly
- //and bring the text editor.
- Data newData = converters[0].convert(data[i]);
- tempFile = getTempFile();
+ 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 {
+ 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.theFile;
+ 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
- {
+ }else
+ {//it's any other file
+ //prepare to open it with the standard text editor.
Display.getDefault().syncExec(new Runnable() {
public void run() {
program = Program.findProgram("txt");
@@ -201,14 +198,18 @@
}
*/
+
+ //if we can't find any program to open the file...
if (program == null) {
+ //throw an error
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 {
+ else {//we found a program to open the file
+ //open it, for real.
if (lastSaveSuccessful == true) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
@@ -216,9 +217,6 @@
}});
}
}
-
-
-
}
return null;
} catch (ConversionException e1) {
@@ -228,6 +226,24 @@
}
}
+ public File getTempFileCSV(){ //TC181
+ 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());
+ 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);
@@ -250,8 +266,8 @@
final class DataViewer implements Runnable {
Shell shell;
boolean isSaved;
+ File outputFile;
Data theData;
- File theFile = getTempFile();
Converter[] theConverters;
DataViewer (Shell parentShell, Data data, Converter[] converters){
@@ -262,10 +278,11 @@
public void run() {
// lots of persisters found, return the chooser
- ViewDataChooser vdc = new ViewDataChooser("View", theFile, shell,
- theData, theConverters, context);
+ ViewDataChooser vdc = new ViewDataChooser("View", shell,
+ theData, theConverters, context, logger);
vdc.open();
isSaved = vdc.isSaved();
+ outputFile = vdc.outputFile;
}
}
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 2008-08-13 17:14:47 UTC (rev 798)
+++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/ViewDataChooser.java 2008-08-15 19:00:54 UTC (rev 799)
@@ -4,25 +4,29 @@
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.save.SaveDataChooser;
import org.cishell.service.conversion.Converter;
import org.eclipse.swt.widgets.Shell;
+import org.osgi.service.log.LogService;
/*
* @author Weixia(Bonnie) Huang (hu...@in...)
*
*/
public class ViewDataChooser extends SaveDataChooser {
- private File tempFile;
boolean isSaved = false;
+ LogService logger;
Data theData;
+ File outputFile;
- public ViewDataChooser(String title, File tempFile, Shell parent,
- Data data, Converter[] converters, CIShellContext context){
+ public ViewDataChooser(String title, Shell parent,
+ Data data, Converter[] converters, CIShellContext context, LogService logger){
super (data, parent, converters, title, context);
- this.tempFile = tempFile;
this.theData = data;
+ this.logger = logger;
}
protected void selectionMade(int selectedIndex){
@@ -30,7 +34,12 @@
getShell().setVisible(false);
final Converter converter = converterArray[selectedIndex];
Data newData = converter.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);
isSaved = FileView.copy((File)newData.getData(), tempFile);
+ outputFile = tempFile;
close(true);
} catch (Exception e) {
throw new RuntimeException(e);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|