From: <mwl...@us...> - 2008-03-31 19:43:10
|
Revision: 748 http://cishell.svn.sourceforge.net/cishell/?rev=748&view=rev Author: mwlinnem Date: 2008-03-31 12:42:39 -0700 (Mon, 31 Mar 2008) Log Message: ----------- Added preference service functionality and fixed default directory for file loading on linux. Will test to see if it works on Windows as well. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/load.properties trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/load.xml trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/metatype/METADATA.XML trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadFactory.java Added Paths: ----------- trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/ Copied: trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence (from rev 745, trunk/clients/gui/org.cishell.reference.gui.persistence) Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF 2008-03-28 20:59:27 UTC (rev 745) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/META-INF/MANIFEST.MF 2008-03-31 19:42:39 UTC (rev 748) @@ -13,6 +13,7 @@ org.cishell.service.conversion;version="1.0.0", org.cishell.service.guibuilder;version="1.0.0", org.osgi.framework;version="1.3.0", + org.osgi.service.cm;version="1.2.0", org.osgi.service.component;version="1.0.0", org.osgi.service.log;version="1.3.0", org.osgi.service.metatype;version="1.1.0", Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/load.properties =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/load.properties 2008-03-28 20:59:27 UTC (rev 745) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/load.properties 2008-03-31 19:42:39 UTC (rev 748) @@ -5,3 +5,5 @@ out_data=java.lang.Object service.pid=org.cishell.reference.gui.persistence.load.FileLoad remoteable=true +prefs_published=local +receive_prefs=true Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/load.xml =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/load.xml 2008-03-28 20:59:27 UTC (rev 745) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/load.xml 2008-03-31 19:42:39 UTC (rev 748) @@ -8,5 +8,7 @@ <service> <provide interface= "org.cishell.framework.algorithm.AlgorithmFactory"/> + <provide interface= + "org.osgi.service.cm.ManagedService"/> </service> </component> \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/metatype/METADATA.XML =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/OSGI-INF/metatype/METADATA.XML 2008-03-28 20:59:27 UTC (rev 745) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/OSGI-INF/metatype/METADATA.XML 2008-03-31 19:42:39 UTC (rev 748) @@ -1,9 +1,10 @@ <?xml version="1.0" encoding="UTF-8"?> <metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0"> - <OCD name="FileView" id="org.cishell.reference.gui.persistence.view.FileView.OCD" + <OCD name="File Load" id="org.cishell.reference.gui.persistence.load.FileLoad.prefs.local.OCD" description=""> + <AD name="Default load directory" id="loadDir" type="String" default="directory:sampledata/"/> </OCD> - <Designate pid="org.cishell.reference.gui.persistence.view.FileView"> - <Object ocdref="org.cishell.reference.gui.persistence.view.FileView.OCD" /> + <Designate pid="org.cishell.reference.gui.persistence.load.FileLoad.prefs.local"> + <Object ocdref="org.cishell.reference.gui.persistence.load.FileLoad.prefs.local.OCD" /> </Designate> -</metatype:MetaData> +</metatype:MetaData> \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java 2008-03-28 20:59:27 UTC (rev 745) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java 2008-03-31 19:42:39 UTC (rev 748) @@ -2,6 +2,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.Dictionary; import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; @@ -10,8 +11,6 @@ import org.cishell.framework.data.BasicData; import org.cishell.framework.data.Data; import org.cishell.service.guibuilder.GUIBuilderService; -import org.eclipse.jface.action.IAction; -import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; @@ -24,113 +23,98 @@ /* * @author Weixia(Bonnie) Huang (hu...@in...) */ -public class FileLoad implements Algorithm{ - - private static File currentDir; - +public class FileLoad implements Algorithm { + private final LogService logger; private final GUIBuilderService guiBuilder; private BundleContext bContext; private CIShellContext ciContext; + private static String defaultLoadDirectory; - public FileLoad(CIShellContext ciContext, BundleContext bContext) { + public FileLoad(CIShellContext ciContext, BundleContext bContext, Dictionary prefProperties) { this.ciContext = ciContext; this.bContext = bContext; logger = (LogService) ciContext.getService(LogService.class.getName()); guiBuilder = (GUIBuilderService)ciContext.getService(GUIBuilderService.class.getName()); - + + //unpack preference properties + if (defaultLoadDirectory == null) { + defaultLoadDirectory = (String) prefProperties.get("loadDir"); + } } - - public void selectionChanged(IAction action, ISelection selection) { - } public Data[] execute() throws AlgorithmExecutionException { - try { -// int counter = PlatformUI.getWorkbench().getWorkbenchWindowCount(); -// System.out.println("counter is "+counter); -// ?? why getActiveWorkbenchWindow() didn't work?? - Data[] returnDM; + //prepare to run load dialog in GUI thread + + IWorkbenchWindow window = getFirstWorkbenchWindow(); + Display display = PlatformUI.getWorkbench().getDisplay(); + FileLoadRunnable fileLoader = new FileLoadRunnable (window); + + //run load dialog in gui thread. - final IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); - if (windows.length ==0){ - throw new AlgorithmExecutionException("Cannot obtain workbench window needed to open dialog."); - } - - Display display = PlatformUI.getWorkbench().getDisplay(); - DataUpdater dataUpdater = new DataUpdater (windows[0]); - if (Thread.currentThread() != display.getThread()) { - display.syncExec(dataUpdater); + display.syncExec(fileLoader); } else { - dataUpdater.run(); + fileLoader.run(); } - if (!dataUpdater.returnList.isEmpty()){ - int size = dataUpdater.returnList.size(); - returnDM = new Data[size]; - for(int index=0; index<size; index++){ - returnDM[index]=(Data)dataUpdater.returnList.get(index); - } - return returnDM; - } - else { - this.logger.log(LogService.LOG_WARNING, "File loading canceled"); - return new Data[0]; - } - } catch (AlgorithmExecutionException e1) { - throw e1; - } catch (Throwable e2) { - throw new AlgorithmExecutionException(e2); - } + //return loaded file data + + Data[] loadedFileData = extractLoadedFileData(fileLoader); + return loadedFileData; } - public static String getFileExtension(File theFile) { - String fileName = theFile.getName() ; - String extension ; - if (fileName.lastIndexOf(".") != -1) - extension = fileName.substring(fileName.lastIndexOf(".")+1) ; - else - extension = "" ; - return extension ; - } - - final class DataUpdater implements Runnable{ + final class FileLoadRunnable implements Runnable{ boolean loadFileSuccess = false; IWorkbenchWindow window; - ArrayList returnList = new ArrayList(); + //this is how we return values from the runnable + public ArrayList returnList = new ArrayList(); - DataUpdater (IWorkbenchWindow window){ + FileLoadRunnable (IWorkbenchWindow window){ this.window = window; - } + } public void run (){ - - FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN); - if (currentDir == null) { - currentDir = new File(System.getProperty("osgi.install.area").replace("file:","") - + "sampledata"); - - if (!currentDir.exists()) { - currentDir = new File(System.getProperty("osgi.install.area").replace("file:","") - + "sampledata" +File.separator + "anything"); - } - } - dialog.setFilterPath(currentDir.getPath()); + FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN); +// if (currentDir == null) { +// currentDir = new File(System.getProperty("osgi.install.area").replace("file:","") +// + "sampledata"); +// +// if (!currentDir.exists()) { +// currentDir = new File(System.getProperty("osgi.install.area").replace("file:","") +// + "sampledata" +File.separator + "anything"); +// } +// } + System.err.println("defaultLoadDirectory is " + defaultLoadDirectory); + File currentDir = new File(defaultLoadDirectory); //? good way to do this? + String absolutePath = currentDir.getAbsolutePath(); + System.err.println("absolutePath:" + absolutePath); + String name = currentDir.getName(); + System.err.println("name:" + name); + dialog.setFilterPath(absolutePath); + // dialog.setFilterPath(name); dialog.setText("Select a File"); String fileName = dialog.open(); + System.out.println("Resulting file name!:" + fileName); if (fileName == null) { return; } - File file = new File(fileName); - - if (file.isDirectory()) { - currentDir = new File(file + File.separator + "anything"); - } else { - currentDir = new File(file.getParent() + File.separator + "anything"); - } + + File file = new File(fileName); + if (file.isDirectory()) { + System.out.println("directory"); + defaultLoadDirectory = file.getAbsolutePath(); + } else { + + // File parentFile = file.getParentFile(); + // if (parentFile != null) { + System.out.println("file"); + defaultLoadDirectory = file.getParentFile().getAbsolutePath(); + // } + } String fileExtension = getFileExtension(file).toLowerCase(); String filter = "(&(type=validator)(in_data=file-ext:"+fileExtension+"))"; @@ -217,7 +201,44 @@ } }//end run() + + public String getFileExtension(File theFile) { + String fileName = theFile.getName() ; + String extension ; + if (fileName.lastIndexOf(".") != -1) + extension = fileName.substring(fileName.lastIndexOf(".")+1) ; + else + extension = "" ; + return extension ; + } } //end class - - + + private IWorkbenchWindow getFirstWorkbenchWindow() throws AlgorithmExecutionException { + final IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); + if (windows.length ==0){ + throw new AlgorithmExecutionException("Cannot obtain workbench window needed to open dialog."); + } else { + return windows[0]; + } + } + + private Data[] extractLoadedFileData(FileLoadRunnable dataUpdater) throws AlgorithmExecutionException { + Data[] loadedFileData; + try { + if (!dataUpdater.returnList.isEmpty()){ + int size = dataUpdater.returnList.size(); + loadedFileData = new Data[size]; + for(int index=0; index<size; index++){ + loadedFileData[index]=(Data)dataUpdater.returnList.get(index); + } + return loadedFileData; + } + else { + this.logger.log(LogService.LOG_WARNING, "File loading canceled"); + return new Data[0]; + } + } catch (Throwable e2) { + throw new AlgorithmExecutionException(e2); + } + } } \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadFactory.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadFactory.java 2008-03-28 20:59:27 UTC (rev 745) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoadFactory.java 2008-03-31 19:42:39 UTC (rev 748) @@ -2,29 +2,51 @@ //standard java import java.util.Dictionary; +import java.util.Enumeration; +import java.util.Hashtable; -//osgi -import org.osgi.framework.BundleContext; -import org.osgi.service.component.ComponentContext; -import org.osgi.service.metatype.MetaTypeProvider; - -//cishell 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.osgi.framework.BundleContext; +import org.osgi.service.cm.ConfigurationException; +import org.osgi.service.cm.ManagedService; +import org.osgi.service.component.ComponentContext; /* * @author Weixia(Bonnie) Huang (hu...@in...) */ -public class FileLoadFactory implements AlgorithmFactory { +public class FileLoadFactory implements AlgorithmFactory, ManagedService { private BundleContext bcontext; + private Dictionary properties = new Hashtable(); protected void activate(ComponentContext ctxt) { bcontext = ctxt.getBundleContext(); } public Algorithm createAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) { - return new FileLoad(context, bcontext); + return new FileLoad(context, bcontext, properties); } + + public void updated(Dictionary properties) throws ConfigurationException { + this.properties = properties; + printPreferences(properties); + } + + private void printPreferences(Dictionary properties) { + System.out.println(" Preferences are as follows for File Load:"); + if (properties == null) { + System.out.println(" Dictionary is null!"); + } else { + Enumeration propertiesKeys = properties.keys(); + + while (propertiesKeys.hasMoreElements()) { + String propertiesKey = (String) propertiesKeys.nextElement(); + + Object propertiesValue = properties.get(propertiesKey); + System.out.println(" " + propertiesKey + ":" + propertiesValue); + } + } + } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |