From: <pat...@us...> - 2009-10-12 18:29:37
|
Revision: 961 http://cishell.svn.sourceforge.net/cishell/?rev=961&view=rev Author: pataphil Date: 2009-10-12 18:29:29 +0000 (Mon, 12 Oct 2009) Log Message: ----------- * Made file "View With..." use the same improved functionality as file "View". * Did a little bit of other refactoring. * Reviewed by Russell. * If time ever permits, the rest of this plugin should be refactored. Modified Paths: -------------- 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/core/FileViewer.java trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWith.java Added Paths: ----------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/ViewDataChooser.java Removed Paths: ------------- 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/ViewWithDataChooser.java 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 17:38:10 UTC (rev 960) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java 2009-10-12 18:29:29 UTC (rev 961) @@ -6,42 +6,44 @@ 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.view.core.FileViewer; -import org.cishell.service.conversion.ConversionException; +import org.cishell.reference.gui.persistence.view.core.exceptiontypes.FileViewingException; import org.cishell.service.conversion.DataConversionService; +import org.osgi.service.log.LogService; public class FileView implements Algorithm { private Data[] dataToView; private CIShellContext ciShellContext; private DataConversionService conversionManager; + private LogService logger; - public FileView(Data[] data, Dictionary parameters, CIShellContext context) { + public FileView( + Data[] data, Dictionary parameters, CIShellContext context) { this.dataToView = data; this.ciShellContext = context; - this.conversionManager = (DataConversionService) context - .getService(DataConversionService.class.getName()); + 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. public Data[] execute() throws AlgorithmExecutionException { - try { - for (int ii = 0; ii < this.dataToView.length; ii++) { + for (int ii = 0; ii < this.dataToView.length; ii++) { + try { FileViewer.viewDataFile(this.dataToView[ii], this.ciShellContext, this.conversionManager); + } catch (FileViewingException fileViewingException) { + String logMessage = + "Error: Unable to view data \"" + + this.dataToView[ii].getMetadata().get(DataProperty.LABEL) + + "\"."; + + this.logger.log(LogService.LOG_ERROR, logMessage); } - - return null; - } catch (ConversionException conversionException) { - String exceptionMessage = "Error: Unable to view data:\n " + - conversionException.getMessage(); - - throw new AlgorithmExecutionException( - exceptionMessage, conversionException); - } catch (Throwable thrownObject) { - throw new AlgorithmExecutionException(thrownObject); } + + return new Data[0]; } } \ No newline at end of file Deleted: 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 17:38:10 UTC (rev 960) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/ViewDataChooser.java 2009-10-12 18:29:29 UTC (rev 961) @@ -1,31 +0,0 @@ -package org.cishell.reference.gui.persistence.view; - -import org.cishell.framework.CIShellContext; -import org.cishell.framework.data.Data; -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; - -public class ViewDataChooser extends SaveDataChooser { - private Converter selectedConverter = null; - - public ViewDataChooser(String title, - Shell parent, - Data data, - Converter[] converters, - CIShellContext ciShellContext, - LogService logger){ - super(data, parent, converters, title, ciShellContext); - } - - protected void selectionMade(int selectedIndex) { - getShell().setVisible(false); - this.selectedConverter = converterArray[selectedIndex]; - close(true); - } - - public Converter getSelectedConverter() { - return this.selectedConverter; - } -} Modified: 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 2009-10-12 17:38:10 UTC (rev 960) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/FileViewer.java 2009-10-12 18:29:29 UTC (rev 961) @@ -7,7 +7,6 @@ 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; @@ -39,8 +38,8 @@ public static void viewDataFile(Data data, CIShellContext ciShellContext, DataConversionService conversionManager) - throws ConversionException, FileViewingException { - viewDataFileWithProgram(data, null, ciShellContext, conversionManager); + throws FileViewingException { + viewDataFileWithProgram(data, "", ciShellContext, conversionManager); } public static void viewDataFileWithProgram( @@ -134,7 +133,7 @@ FileWithExtension fileWithExtension, String customFileExtension) throws FileViewingException { try { - final Program program = selectProgramForFileExtension( + final Program program = selectChosenProgramForFileExtension( fileWithExtension.fileExtension, customFileExtension); executeProgramWithFile(program, fileWithExtension.file); @@ -326,37 +325,41 @@ } } - private static Program selectProgramForFileExtension( - final String fileExtension, final String customFileExtension) + private static Program selectChosenProgramForFileExtension( + final String defaultFileExtension, + final String customFileExtension) throws NoProgramFoundException { String chosenFileExtension = null; - if ((customFileExtension == null) || customFileExtension.equals("")) { - chosenFileExtension = fileExtension; + if (customFileExtension.equals("")) { + chosenFileExtension = defaultFileExtension; } else { chosenFileExtension = customFileExtension; } - final Program[] programHolder = new Program[1]; + Program chosenProgram = + getProgramForFileExtension(chosenFileExtension); - Display.getDefault().syncExec(new Runnable() { - public void run() { - programHolder[0] = - Program.findProgram(fileExtension); + if (chosenProgram != null) { + return chosenProgram; + } else { + /* + * The chosen program doesn't exist, so try to get the + * default viewer. + */ + Program defaultProgram = + getProgramForFileExtension(defaultFileExtension); + + if (defaultProgram != null) { + return defaultProgram; + } else { + String exceptionMessage = + "You do not have a valid viewer for the ." + + chosenFileExtension + + "file installed."; + + throw new NoProgramFoundException(exceptionMessage); } - }); - - 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); } } @@ -423,6 +426,20 @@ } } + private static Program getProgramForFileExtension( + final String fileExtension) { + final Program[] programHolder = new Program[1]; + + Display.getDefault().syncExec(new Runnable() { + public void run() { + programHolder[0] = + Program.findProgram(fileExtension); + } + }); + + return programHolder[0]; + } + private final static class DataViewer implements Runnable { public static final String VIEW_DIALOG_TITLE = "View"; private Shell shellWindow; Copied: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/ViewDataChooser.java (from rev 960, 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/core/ViewDataChooser.java (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/ViewDataChooser.java 2009-10-12 18:29:29 UTC (rev 961) @@ -0,0 +1,31 @@ +package org.cishell.reference.gui.persistence.view.core; + +import org.cishell.framework.CIShellContext; +import org.cishell.framework.data.Data; +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; + +public class ViewDataChooser extends SaveDataChooser { + private Converter selectedConverter = null; + + public ViewDataChooser(String title, + Shell parent, + Data data, + Converter[] converters, + CIShellContext ciShellContext, + LogService logger){ + super(data, parent, converters, title, ciShellContext); + } + + protected void selectionMade(int selectedIndex) { + getShell().setVisible(false); + this.selectedConverter = converterArray[selectedIndex]; + close(true); + } + + public Converter getSelectedConverter() { + return this.selectedConverter; + } +} Property changes on: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/core/ViewDataChooser.java ___________________________________________________________________ Added: svn:mergeinfo + 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 17:38:10 UTC (rev 960) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWith.java 2009-10-12 18:29:29 UTC (rev 961) @@ -1,290 +1,57 @@ package org.cishell.reference.gui.persistence.viewwith; -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.service.conversion.ConversionException; -import org.cishell.service.conversion.Converter; +import org.cishell.framework.data.DataProperty; +import org.cishell.reference.gui.persistence.view.core.FileViewer; +import org.cishell.reference.gui.persistence.view.core.exceptiontypes.FileViewingException; 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; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.PlatformUI; import org.osgi.service.log.LogService; -/* - * @author Felix Terkhorn (ter...@gm...), Weixia Huang (hu...@in...) - */ public class FileViewWith implements Algorithm { public static final String VIEW_WITH_PARAMETER_KEY = "viewWith"; private Data[] dataToView; private Dictionary parameters; - private CIShellContext context; + private CIShellContext ciShellContext; 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.dataToView = data; this.parameters = parameters; - this.context = context; + this.ciShellContext = context; - conversionManager = (DataConversionService) context.getService( - DataConversionService.class.getName()); - - logger = (LogService)context.getService(LogService.class.getName()); - guiBuilder = (GUIBuilderService)context.getService(GUIBuilderService.class.getName()); + this.conversionManager = (DataConversionService)context.getService( + DataConversionService.class.getName()); + this.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 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 { - // TODO: Refactor this code so it and FileView use the same code. - boolean temporaryFileWasCreated = false; - String format; - String viewWithType = (String)parameters.get(VIEW_WITH_PARAMETER_KEY); - - Display display; - IWorkbenchWindow[] windows; - final Shell parentShell; - - windows = PlatformUI.getWorkbench().getWorkbenchWindows(); - - if (windows.length == 0) { - return null; - } - - parentShell = windows[0].getShell(); - display = PlatformUI.getWorkbench().getDisplay(); - temporaryFile = getTempFile(); - - 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.getData(), temporaryFile); - temporaryFileWasCreated = true; - } else { - final Converter[] converters = - conversionManager.findConverters(data, "file-ext:*"); - 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); - 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 if (converters.length > 1) { - if (!parentShell.isDisposed()) { - try { - DataViewer dataViewer = - new DataViewer(parentShell, data, converters); - display.syncExec(dataViewer); - - 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() { - textProgram = Program.findProgram("txt"); - }}); - - Display.getDefault().syncExec(new Runnable() { - public void run() { - wordProgram = Program.findProgram("doc"); - }}); - - Display.getDefault().syncExec(new Runnable() { - public void run() { - webBrowserProgram = Program.findProgram("htm"); - }}); - - Display.getDefault().syncExec(new Runnable() { - public void run() { - spreadsheetProgram = Program.findProgram("csv"); - }}); - - 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 (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() { - textProgram.execute(filePath); - } - }); - } else if (viewWithType.equals("doc")) { - Display.getDefault().syncExec(new Runnable() { - public void run() { - wordProgram.execute(filePath); - } - }); - } else if (viewWithType.equals("html")) { - Display.getDefault().syncExec(new Runnable() { - public void run() { - webBrowserProgram.execute(filePath); - } - }); - } else if (viewWithType.equals("csv")) { - Display.getDefault().syncExec(new Runnable() { - public void run() { - spreadsheetProgram.execute(filePath); - } - }); - } else { - Display.getDefault().syncExec(new Runnable() { - public void run() { - textProgram.execute(filePath); - } - }); - } - } - } - } - - return null; - } - - public static boolean copy(File in, File out) { - 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) { - guiBuilder.showError("Copy Error", "IOException during copy", ioe.getMessage()); - return false; - } - } - - final class DataViewer implements Runnable { - Shell shell; - boolean isSaved; - Data theData; - File theFile = getTempFile(); - Converter[] theConverters; - - DataViewer (Shell parentShell, Data data, Converter[] converters){ - this.shell = parentShell; - this.theData = data; - this.theConverters = converters; + for (int ii = 0; ii < this.dataToView.length; ii++) { + try { + FileViewer.viewDataFileWithProgram( + this.dataToView[ii], + viewWithType, + this.ciShellContext, + this.conversionManager); + } catch (FileViewingException fileViewingException) { + String logMessage = + "Error: Unable to view data \"" + + this.dataToView[ii].getMetadata().get(DataProperty.LABEL) + + "\"."; + + this.logger.log(LogService.LOG_ERROR, logMessage); + } } - - public void run() { - // lots of persisters found, return the chooser - ViewWithDataChooser vdc = new ViewWithDataChooser("View As...", theFile, shell, - theData, theConverters, context); - vdc.open(); - isSaved = vdc.isSaved(); - } - } - - - - + + return new Data[0]; + } } \ No newline at end of file Deleted: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/ViewWithDataChooser.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/ViewWithDataChooser.java 2009-10-12 17:38:10 UTC (rev 960) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/ViewWithDataChooser.java 2009-10-12 18:29:29 UTC (rev 961) @@ -1,50 +0,0 @@ -package org.cishell.reference.gui.persistence.viewwith; - -import java.io.File; - -import org.cishell.framework.CIShellContext; -import org.cishell.framework.data.Data; -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; - -/* - * @author Felix Terkhorn (ter...@gm...) - * - */ -public class ViewWithDataChooser extends SaveDataChooser { - private File tempFile; - private boolean isSaved = false; - private Data theData; - - public ViewWithDataChooser(String title, File tempFile, Shell parent, - Data data, Converter[] converters, CIShellContext context){ - super (data, parent, converters, title, context); - - this.tempFile = tempFile; - this.theData = data; - } - - protected void selectionMade(int selectedIndex){ - try { - getShell().setVisible(false); - final Converter converter = converterArray[selectedIndex]; - Data newData = converter.convert(theData); - //TODO: hey look, yet another copy method - isSaved = FileViewWith.copy((File)newData.getData(), tempFile); - close(true); - } catch (ConversionException e) { - //TODO: RuntimeExceptioN?!?!?! - throw new RuntimeException("Error: Unable to view data:\n " + e.getMessage(), e); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public boolean isSaved(){ - return isSaved; - } - - -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |