From: <ter...@us...> - 2007-05-14 18:58:04
|
Revision: 399 http://svn.sourceforge.net/cishell/?rev=399&view=rev Author: terkhorn Date: 2007-05-14 11:57:45 -0700 (Mon, 14 May 2007) Log Message: ----------- Added VERY BASIC support for "View as..." option for files seen in the data manager window. Currently you can only use this option by selecting it from the "File" menu. You cannot right-click the file and select view as yet, but I will add that ASAP. Most changes are seen in org.cishell.reference.gui.persistence. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java Added Paths: ----------- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.properties trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.xml trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/ 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 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 2007-05-14 18:55:13 UTC (rev 398) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF 2007-05-14 18:57:45 UTC (rev 399) @@ -10,6 +10,7 @@ org.cishell.framework.algorithm, org.cishell.framework.data, org.cishell.reference.gui.common, + org.cishell.reference.service.metatype, org.cishell.service.conversion, org.cishell.service.guibuilder, org.osgi.framework;version="1.3.0", @@ -18,6 +19,6 @@ org.osgi.service.metatype;version="1.1.0", org.osgi.service.prefs;version="1.1.0" X-AutoStart: true -Service-Component: OSGI-INF/load.xml, OSGI-INF/save.xml, OSGI-INF/view.xml +Service-Component: OSGI-INF/load.xml, OSGI-INF/save.xml, OSGI-INF/view.xml, OSGI-INF/viewwith.xml Require-Bundle: org.eclipse.swt, org.eclipse.ui Added: trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.properties =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.properties (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.properties 2007-05-14 18:57:45 UTC (rev 399) @@ -0,0 +1,7 @@ +menu_path=File/additions +label=View with... +description=This algorithm does this and this... +in_data=java.lang.Object +out_data=null +service.pid=org.cishell.reference.gui.persistence.viewwith.FileViewWith +remoteable=true Added: trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.xml =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.xml (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/viewwith.xml 2007-05-14 18:57:45 UTC (rev 399) @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<component name="org.cishell.reference.gui.persistence.viewwith.FileViewWith.component" immediate="false"> + <implementation class="org.cishell.reference.gui.persistence.viewwith.FileViewWithFactory"/> + <properties entry="OSGI-INF/viewwith.properties"/> + <reference name="LOG" interface="org.osgi.service.log.LogService"/> + <reference name="MTS" interface="org.osgi.service.metatype.MetaTypeService"/> + + <service> + <provide interface= + "org.cishell.framework.algorithm.AlgorithmFactory"/> + </service> +</component> \ 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 2007-05-14 18:55:13 UTC (rev 398) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/view/FileView.java 2007-05-14 18:57:45 UTC (rev 399) @@ -31,6 +31,7 @@ static GUIBuilderService guiBuilder; LogService logger; Program program; + // Program programTwo; File tempFile; public FileView(Data[] data, Dictionary parameters, CIShellContext context) { @@ -118,7 +119,19 @@ public void run() { program = Program.findProgram("txt"); }}); + /* + Display.getDefault().syncExec(new Runnable() { + public void run() { + programTwo = Program.findProgram("doc"); + }}); + if (programTwo == null) { + System.out.println("***\nYO!\nNo doc viewer\n"); + } else { + System.out.println("!!!\nHEY!\nDoc viewer found\n"); + + } + */ if (program == null) { guiBuilder.showError("No Text Viewer", "No valid text viewer for the .txt file. " + Added: 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 (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWith.java 2007-05-14 18:57:45 UTC (rev 399) @@ -0,0 +1,222 @@ +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.data.Data; +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; +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 { + Data[] data; + Dictionary parameters; + CIShellContext context; + DataConversionService conversionManager; + static GUIBuilderService guiBuilder; + LogService logger; + Program program; + Program programTwo; + Program programThree; + File tempFile; + + public FileViewWith(Data[] data, Dictionary parameters, CIShellContext context) { + this.data = data; + this.parameters = parameters; + this.context = context; + + conversionManager = (DataConversionService) context.getService( + DataConversionService.class.getName()); + + logger = (LogService)context.getService(LogService.class.getName()); + 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()) + 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 Data[] execute() { + boolean lastSaveSuccessful = false; + String format; + + String viewWith = (String) parameters.get("viewWith"); + + 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(); + tempFile = getTempFile(); + + for (int i = 0; i < data.length; i++){ + Object theData = data[i].getData(); + format = data[i].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:*"); + + 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. + Data newData = converters[0].convert(data[i]); + copy((File)newData.getData(), tempFile); + lastSaveSuccessful = true; + } + else { + if (!parentShell.isDisposed()) { + DataViewer dataViewer = new DataViewer(parentShell, data[i], converters); + display.syncExec(dataViewer); + lastSaveSuccessful = dataViewer.isSaved; + tempFile = dataViewer.theFile; + } + } + } + + + Display.getDefault().syncExec(new Runnable() { + public void run() { + program = Program.findProgram("txt"); + }}); + + Display.getDefault().syncExec(new Runnable() { + public void run() { + programTwo = Program.findProgram("doc"); + }}); + + Display.getDefault().syncExec(new Runnable() { + public void run() { + programThree = Program.findProgram("htm"); + }}); + + + if (program == null && programTwo == 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()); + } + else { + if (lastSaveSuccessful == true) { + if (viewWith.equals("txt")) { + Display.getDefault().syncExec(new Runnable() { + public void run() { + program.execute(tempFile.getAbsolutePath()); + }}); + } else if (viewWith.equals("doc")) { + Display.getDefault().syncExec(new Runnable() { + public void run() { + programTwo.execute(tempFile.getAbsolutePath()); + }}); + } else if (viewWith.equals("html")) { + Display.getDefault().syncExec(new Runnable() { + public void run() { + programThree.execute(tempFile.getAbsolutePath()); + }}); + } else { + // Try to run it with txt viewer... + Display.getDefault().syncExec(new Runnable() { + public void run() { + program.execute(tempFile.getAbsolutePath()); + }}); + } + } + } + + + + } + 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; + } + + 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(); + } + } + + + + +} \ No newline at end of file Added: 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/FileViewWithFactory.java (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/FileViewWithFactory.java 2007-05-14 18:57:45 UTC (rev 399) @@ -0,0 +1,127 @@ +package org.cishell.reference.gui.persistence.viewwith; + +import java.io.IOException; +import java.util.Dictionary; + +import org.cishell.framework.CIShellContext; +import org.cishell.framework.algorithm.Algorithm; +import org.cishell.framework.algorithm.AlgorithmFactory; +import org.cishell.framework.data.Data; +import org.cishell.reference.service.metatype.BasicAttributeDefinition; +import org.cishell.reference.service.metatype.BasicMetaTypeProvider; +import org.cishell.reference.service.metatype.BasicObjectClassDefinition; +import org.eclipse.swt.program.Program; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.PlatformUI; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.metatype.AttributeDefinition; +import org.osgi.service.metatype.MetaTypeInformation; +import org.osgi.service.metatype.MetaTypeProvider; +import org.osgi.service.metatype.MetaTypeService; +import org.osgi.service.metatype.ObjectClassDefinition; + + +// Felix: +// See edu.iu.nwb.visualization.prefuse.beta.common +// It implements AlgorithmFactory and adds dropbox boxes... +// You will need to do something similar, but much less complicated, here, I believe. + +public class FileViewWithFactory implements AlgorithmFactory { + Program programTxt; + Program programDoc; + Program programHtml; + private MetaTypeProvider provider; +// private MetaTypeInformation originalProvider; // taken from PrefuseBetaAlgorithmFactory +// private String pid; // taken from PrefuseBetaAlgFactory + + protected void activate(ComponentContext ctxt) { + //You may delete all references to metatype service if + //your algorithm does not require parameters and return + //null in the createParameters() method + //MetaTypeService mts = (MetaTypeService)ctxt.locateService("MTS"); + //provider = mts.getMetaTypeInformation(ctxt.getBundleContext().getBundle()); + // bruce says these should be commented out... + } + protected void deactivate(ComponentContext ctxt) { + provider = null; + } + + public Algorithm createAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) { + return new FileViewWith(data, parameters, context); + } + + + private String[] createKeyArray() { + String[] keys = new String[1]; + keys[0] = "Viewer"; + /* + for(int ii = 1; ii <= schema.getColumnCount(); ii++) { + keys[ii] = schema.getColumnName(ii - 1); + System.out.println("keys["+ii+"] = " + schema.getColumnName(ii - 1) + "; "); + } + */ + + return keys; + } + + public MetaTypeProvider createParameters(Data[] data) { + //ObjectClassDefinition oldDefinition = provider.getObjectClassDefinition(this.pid, null); + // read the API for creating on ObjectClassDefinition + // and create a basic one ... + // BasicObjectClassDefinition oldDefinition = new ObjectClassDefinition(); + + BasicObjectClassDefinition definition; + //try { + definition = new BasicObjectClassDefinition("fileViewWithDefinition", "Application viewer type", "Please choose an application viewer to read this file.\nThe application associated with the chosen extension will be called.", null); + //} catch (IOException e) { + //definition = new BasicObjectClassDefinition("fileViewWithDef", "fileViewWithName", "This is an OCD for fileViewWith", null); + //} + +// String[] dialogAttributesArray = createKeyArray(); + + + Display display; + + + display = PlatformUI.getWorkbench().getDisplay(); + + Display.getDefault().syncExec(new Runnable() { + public void run() { + programTxt = Program.findProgram("txt"); + }}); + + Display.getDefault().syncExec(new Runnable() { + public void run() { + programDoc = Program.findProgram("doc"); + }}); + + Display.getDefault().syncExec(new Runnable() { + public void run() { + programHtml = Program.findProgram("htm"); + }}); + + System.err.println(definition.getID()); + + String[] defValStringArray = null; //doesn't actually work yet... + String[] myOptionLabels = new String[] {"TXT","DOC","HTML"}; + String[] myOptionValues = new String[] {"txt","doc","html"}; + + if (programTxt == null) { + myOptionLabels[0] = ""; + } + if (programDoc == null) { + myOptionLabels[1] = ""; + } + if (programHtml == null) { + myOptionLabels[2] = ""; + } + AttributeDefinition ad = new BasicAttributeDefinition("viewWith", "View file as", "Type of viewer", AttributeDefinition.STRING /*string*/, 0, defValStringArray/*String[] defaultValue*/, null /*validator*/, myOptionLabels, myOptionValues); + definition.addAttributeDefinition(ObjectClassDefinition.REQUIRED, ad); + + AttributeDefinition[] definitions = definition.getAttributeDefinitions(ObjectClassDefinition.ALL); + + + MetaTypeProvider provider = new BasicMetaTypeProvider(definition); + return provider; + } +} \ No newline at end of file Added: 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 (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/viewwith/ViewWithDataChooser.java 2007-05-14 18:57:45 UTC (rev 399) @@ -0,0 +1,44 @@ +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.Converter; + +import org.eclipse.swt.widgets.Shell; + +/* + * @author Felix Terkhorn (ter...@gm...) + * + */ +public class ViewWithDataChooser extends SaveDataChooser { + private File tempFile; + boolean isSaved = false; + Converter[] converters; + 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.converters = converters; + this.theData = data; + } + + protected void selectionMade(int selectedIndex) { + getShell().setVisible(false); + final Converter converter = converterArray[selectedIndex]; + Data newData = converter.convert(theData); + isSaved = FileViewWith.copy((File)newData.getData(), tempFile); + close(true); + } + + public boolean isSaved(){ + return isSaved; + } + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |