From: <tan...@us...> - 2009-03-04 15:54:49
|
Revision: 864 http://cishell.svn.sourceforge.net/cishell/?rev=864&view=rev Author: tankchintan Date: 2009-03-04 15:54:38 +0000 (Wed, 04 Mar 2009) Log Message: ----------- Refactored the code for File > Load mechanism suitably.Made changes in all the input file validators to include detailed comments. Fixed the non-validation issue for the ISI file format.on trying to load a file without any extension does not give an error but displays another dialog box for selecting the File Loader e.g. Bibtex, Pajek, ISI etc. Validation is performed on selected file loader throwing either ERROR or loading the file with that loader. Code Reviewed By Micah Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java Added Paths: ----------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java Removed Paths: ------------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/LoadDataChooser.java Modified: trunk/clients/gui/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 2009-02-20 18:58:20 UTC (rev 863) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java 2009-03-04 15:54:38 UTC (rev 864) @@ -24,220 +24,302 @@ * @author Weixia(Bonnie) Huang (hu...@in...) */ 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, 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 Data[] execute() throws AlgorithmExecutionException { - //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. + private final LogService logger; + private final GUIBuilderService guiBuilder; - if (Thread.currentThread() != display.getThread()) { - display.syncExec(fileLoader); + private BundleContext bundleContext; + private CIShellContext ciShellContext; + private static String defaultLoadDirectory; + + public FileLoad(CIShellContext ciContext, BundleContext bContext, + Dictionary prefProperties) { + this.ciShellContext = ciContext; + this.bundleContext = 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 Data[] execute() throws AlgorithmExecutionException { + // 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. + + if (Thread.currentThread() != display.getThread()) { + display.syncExec(fileLoader); } else { fileLoader.run(); } - - //return loaded file data - - Data[] loadedFileData = extractLoadedFileData(fileLoader); - return loadedFileData; - } - - final class FileLoadRunnable implements Runnable{ - boolean loadFileSuccess = false; - IWorkbenchWindow window; - //this is how we return values from the runnable - public ArrayList returnList = new ArrayList(); - - 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"); -// } -// } - 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()) { - 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+"))"; - try { - // set the properties for the resource descriptor. - // note that this relies on the fact that the compression is set - // to nocompression by default. + // return loaded file data - // get all the service references of converters that can load this type of file. - ServiceReference[] serviceRefList = bContext.getAllServiceReferences( - AlgorithmFactory.class.getName(), filter); - + Data[] loadedFileData = extractLoadedFileData(fileLoader); + return loadedFileData; + } - // no converters found means the file format is not supported - if (serviceRefList == null || serviceRefList.length == 0){ - guiBuilder.showError("Unsupported File Format", "Sorry, the file format: *."+fileExtension+" is not yet supported.", - "Sorry, the file format: *."+fileExtension+" is not yet supported. \n"+ - "Please send your requests to cis...@li.... \n" - +"Thank you."); - } - - //<filename>[.<data model type>][.<index>] - // only one persister found, so load the model - else if (serviceRefList.length == 1){ - AlgorithmFactory persister = (AlgorithmFactory)bContext.getService(serviceRefList[0]); - Data[] dm = new Data[]{new BasicData(file.getPath(), String.class.getName()) }; - dm = persister.createAlgorithm(dm, null, ciContext).execute(); - if (dm != null){ - loadFileSuccess = true; - logger.log(LogService.LOG_INFO, "Loaded: "+file.getPath()); - for (int i=0; i<dm.length; i++) - returnList.add(dm[i]); - } - - - } - - // lots of persisters found, return the chooser - else if (serviceRefList.length > 1){ -// new LoadDataChooser("Load", file, window.getShell(), -// ciContext, bContext, serviceRefList, returnList).open(); - for (int index=0; index<serviceRefList.length; index++){ - AlgorithmFactory persister = (AlgorithmFactory)bContext.getService(serviceRefList[index]); - Data[] dm = new Data[]{new BasicData(file.getPath(), String.class.getName()) }; - dm = persister.createAlgorithm(dm, null, ciContext).execute(); - if (dm != null ){ - loadFileSuccess = true; - logger.log(LogService.LOG_INFO, "Loaded: "+file.getPath()); - for (int i=0; i<dm.length; i++){ - returnList.add(dm[i]); - } - break; - } - } + final class FileLoadRunnable implements Runnable { + boolean loadFileSuccess = false; + IWorkbenchWindow window; + // this is how we return values from the runnable + public ArrayList selectedServicesForLoadedFileList = new ArrayList(); - } - /* - * Bonnie: I commented out the following functions since when - * the application failed to load an nwb file, etc, the reader - * has report the error. It does not need this second error display. - * But maybe not all file readers will generate the error display if - * a failure occurs... - */ -/* if (serviceRefList != null){ - if(serviceRefList.length >0 && !loadFileSuccess){ - guiBuilder.showError("Can Not Load The File", - "Sorry, it's very possible that you have a wrong file format," + - "since the file can not be loaded to the application.", - - "Please check Data Formats that this application can support at "+ - "https://nwb.slis.indiana.edu/community/?n=Algorithms.HomePage." + - "And send your requests or report the problem to "+ - "cis...@li.... \n"+"Thank you."); - } + 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"); + // } + // } + 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()) { + System.out.println("directory"); + defaultLoadDirectory = file.getAbsolutePath(); + } else { - }catch (Exception e){ - throw new RuntimeException(e); - } + // File parentFile = file.getParentFile(); + // if (parentFile != null) { + System.out.println("file"); + defaultLoadDirectory = file.getParentFile().getAbsolutePath(); + // } + } - }//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); - } - } + String fileExtension = getFileExtension(file).toLowerCase(); + + /* + * This filter is used to filter out all the services which are not, + * 1. validators 2. validators but for output file functionality + * Hence only input file functionality validators are allowed. + */ + + String preLoadValidatorFilter = "(&(type=validator)(in_data=file-ext:" + + fileExtension + "))"; + try { + + // get all the service references of validators that can load + // this type of file. + ServiceReference[] serviceReferences = bundleContext + .getAllServiceReferences(AlgorithmFactory.class + .getName(), preLoadValidatorFilter); + + /* + * This use case is for input files selected that are, 1. + * without any file extensions 2. with file extensions that do + * not match any service "in_data" field. + */ + + if (serviceReferences == null || serviceReferences.length == 0) { + + /* + * This filter is used to accept only those services which + * are, 1. type = validators and, 2. which have non-empty + * format_name and, 3. which have non-empty in_data field. + * this is used so that all the validators for output file + * functionality are filtered out. + */ + + String validatorFilter = "(&(type=validator)(format_name=*)(in_data=file-ext:*))"; + + ServiceReference[] allValidators = bundleContext + .getAllServiceReferences(AlgorithmFactory.class + .getName(), validatorFilter); + + /* + * SelectedFileServiceSelector is used to create a GUI for + * selecting a service for the selected file from a list of + * applicable services. On selection of a service it calls + * the validator for that service. If the validator passes + * it it goes ahead and loads the file appropriately else it + * throws error message asking the user to select other + * service. + * + * This modifies the selectedServicesForLoadedFileList, + * which is the placeholder for all the verified/ applicable + * services for a selected/loaded file. + */ + + new SelectedFileServiceSelector("Load", file, window + .getShell(), ciShellContext, bundleContext, + allValidators, selectedServicesForLoadedFileList) + .open(); + } + + /* + * This use case is for input files selected that have only one + * applicable service. In this case the system goes ahead and + * loads the file with that service. + */ + + else if (serviceReferences.length == 1) { + AlgorithmFactory selectedValidatorExecutor = (AlgorithmFactory) bundleContext + .getService(serviceReferences[0]); + Data[] outputDataAfterValidation; + Data[] inputDataForValidation = new Data[] { new BasicData( + file.getPath(), String.class.getName()) }; + outputDataAfterValidation = selectedValidatorExecutor + .createAlgorithm(inputDataForValidation, null, + ciShellContext).execute(); + + /* + * outputDataAfterValidation = null implies that file was + * not loaded properly. + */ + + if (outputDataAfterValidation != null) { + loadFileSuccess = true; + logger.log(LogService.LOG_INFO, "Loaded: " + + file.getPath()); + for (int i = 0; i < outputDataAfterValidation.length; i++) + selectedServicesForLoadedFileList + .add(outputDataAfterValidation[i]); + } + + } + + /* + * This use case is for input files selected that have more than + * one applicable services. For e.g. ".xml" can be xgmml, + * graphml, treeml. TODO: Right now this is handled by going + * over all the selected services and loading the file with a + * service that did not fail the validation test. For e.g. for a + * ".xml" file which is treeml it will try out first graphml + * then treeml. This will throw error on graphml which can be + * resolved. A better approach will be to display + * SelectedFileServiceSelector with graphml, treeml & xgmml + * options. + */ + + else if (serviceReferences.length > 1) { + for (int index = 0; index < serviceReferences.length; index++) { + Data[] outputDataAfterValidation; + AlgorithmFactory selectedValidatorExecutor = (AlgorithmFactory) bundleContext + .getService(serviceReferences[index]); + Data[] inputDataForValidation = new Data[] { new BasicData( + file.getPath(), String.class.getName()) }; + outputDataAfterValidation = selectedValidatorExecutor + .createAlgorithm(inputDataForValidation, null, + ciShellContext).execute(); + if (outputDataAfterValidation != null) { + loadFileSuccess = true; + logger.log(LogService.LOG_INFO, "Loaded: " + + file.getPath()); + for (int i = 0; i < outputDataAfterValidation.length; i++) { + selectedServicesForLoadedFileList + .add(outputDataAfterValidation[i]); + } + break; + } + } + + } + /* + * Bonnie: I commented out the following functions since when + * the application failed to load an nwb file, etc, the reader + * has report the error. It does not need this second error + * display. But maybe not all file readers will generate the + * error display if a failure occurs... + */ + /* + * if (serviceRefList != null){ if(serviceRefList.length >0 && + * !loadFileSuccess){ + * guiBuilder.showError("Can Not Load The File", + * "Sorry, it's very possible that you have a wrong file format," + * + "since the file can not be loaded to the application.", + * + * "Please check Data Formats that this application can support at " + * + + * "https://nwb.slis.indiana.edu/community/?n=Algorithms.HomePage." + * + "And send your requests or report the problem to "+ + * "cis...@li.... \n"+"Thank you."); + * } + * + * } + */ + + } catch (Exception e) { + throw new RuntimeException(e); + } + + }// 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.selectedServicesForLoadedFileList.isEmpty()) { + int size = dataUpdater.selectedServicesForLoadedFileList.size(); + loadedFileData = new Data[size]; + for (int index = 0; index < size; index++) { + loadedFileData[index] = (Data) dataUpdater.selectedServicesForLoadedFileList + .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 Deleted: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/LoadDataChooser.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/LoadDataChooser.java 2009-02-20 18:58:20 UTC (rev 863) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/LoadDataChooser.java 2009-03-04 15:54:38 UTC (rev 864) @@ -1,208 +0,0 @@ -/* - * InfoVis CyberInfrastructure: A Data-Code-Compute Resource for Research - * and Education in Information Visualization (http://iv.slis.indiana.edu/). - * - * Created on Jan 24, 2005 at Indiana University. - */ -package org.cishell.reference.gui.persistence.load; - -import java.io.File; -import java.util.ArrayList; - -import org.cishell.framework.CIShellContext; -import org.cishell.framework.algorithm.AlgorithmFactory; -import org.cishell.framework.data.BasicData; -import org.cishell.framework.data.Data; -import org.cishell.reference.gui.common.AbstractDialog; -import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.StyleRange; -import org.eclipse.swt.custom.StyledText; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.layout.FillLayout; -import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Group; -import org.eclipse.swt.widgets.List; -import org.eclipse.swt.widgets.Shell; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.service.log.LogService; - -/** - * - * @author Team IVC (Weixia Huang, James Ellis) - */ -public class LoadDataChooser extends AbstractDialog { - private File theFile; - private LogService logger; - - private ServiceReference[] persisterArray; - private List persisterList; - private StyledText detailPane; - private CIShellContext ciContext; - private BundleContext bContext; - private ArrayList returnList; - - private static final String[] details_prop_names = - {"format_name", "supported_file_extension", "format_description", - "restorable_model_name", "restorable_model_description" }; - private static final String[] details_prop_name_descriptions = - {"Format name", "Supported file extension", "Format description", - "Restorable model name", "Restorable model description" }; - - public LoadDataChooser(String title, File theFile, - Shell parent, CIShellContext ciContext, BundleContext bContext, - ServiceReference[] persisterArray, ArrayList returnList){ - super(parent, title, AbstractDialog.QUESTION); - this.ciContext = ciContext; - this.bContext = bContext; - this.persisterArray = persisterArray; - this.returnList = returnList; - - this.theFile = theFile; - - this.logger = (LogService) ciContext.getService(LogService.class.getName()); -//shall this part be moved out of the code? - setDescription("The file you have selected can be loaded" - + " using the following formats.\n" - + "Please select one of them."); - setDetails("This dialog allows the user to choose among all available " + - "formats for loading the selected data model. Choose any of the formats " + - "to continue loading the dataset."); - } - - private Composite initGUI(Composite parent) { - Composite content = new Composite(parent, SWT.NONE); - - GridLayout layout = new GridLayout(); - layout.numColumns = 2; - content.setLayout(layout); - - Group persisterGroup = new Group(content, SWT.NONE); -//shall this label be moved out of the code? - persisterGroup.setText("Loaded by"); - persisterGroup.setLayout(new FillLayout()); - GridData persisterListGridData = new GridData(GridData.FILL_BOTH); - persisterListGridData.widthHint = 200; - persisterGroup.setLayoutData(persisterListGridData); - - persisterList = new List(persisterGroup, SWT.H_SCROLL |SWT.V_SCROLL | SWT.SINGLE); -// initPersisterArray(); - initPersisterList(); - persisterList.addSelectionListener(new SelectionAdapter(){ - public void widgetSelected(SelectionEvent e) { - List list = (List)e.getSource(); - int selection = list.getSelectionIndex(); - if(selection != -1){ - updateDetailPane(persisterArray[selection]); - } - } - }); - - Group detailsGroup = new Group(content, SWT.NONE); -// shall this label be moved out of the code? - detailsGroup.setText("Details"); - detailsGroup.setLayout(new FillLayout()); - GridData detailsGridData = new GridData(GridData.FILL_BOTH); - detailsGridData.widthHint = 200; - detailsGroup.setLayoutData(detailsGridData); - - detailPane = initDetailPane(detailsGroup); - - persisterList.setSelection(0); - updateDetailPane(persisterArray[0]); - - return content; - } - - private void initPersisterList(){ - for (int i = 0; i < persisterArray.length; ++i) { - - String name = (String)persisterArray[i].getProperty("converter_name"); - - // if someone was sloppy enough to not provide a name, then use the - // name of the class instead. - if (name == null || name.length() == 0) - name = persisterArray[i].getClass().getName(); - persisterList.add(name); - } - } - - - private StyledText initDetailPane(Group detailsGroup) { - StyledText detailPane = new StyledText(detailsGroup, SWT.H_SCROLL | SWT.V_SCROLL); - detailPane.setEditable(false); - detailPane.getCaret().setVisible(false); - return detailPane; - } - - private void updateDetailPane(ServiceReference persister) { - - detailPane.setText(""); - for (int i=0; i<details_prop_names.length; i++){ - String val = (String) persister.getProperty(details_prop_names[i]); - - StyleRange styleRange = new StyleRange(); - styleRange.start = detailPane.getText().length(); - detailPane.append(details_prop_name_descriptions[i] + ":\n"); - styleRange.length = details_prop_names[i].length() + 1; - styleRange.fontStyle = SWT.BOLD; - detailPane.setStyleRange(styleRange); - - detailPane.append(val + "\n"); - - } - - } - - private void selectionMade(int selectedIndex) { - AlgorithmFactory persister =(AlgorithmFactory) bContext.getService(persisterArray[selectedIndex]); - Data[] dm = null; - try { - dm = new Data[]{new BasicData(theFile.getPath(),String.class.getName())}; - dm = persister.createAlgorithm(dm, null, ciContext).execute(); - } catch (Throwable e) { - this.logger.log(LogService.LOG_ERROR, "Error occurred while executing selection", e); - e.printStackTrace(); - } - - if (dm != null) { - logger.log(LogService.LOG_INFO, "Loaded: "+theFile.getPath()); - - for(int i = 0; i<dm.length; i++){ - returnList.add(dm[i]); - } - close(true); - } else { - logger.log(LogService.LOG_ERROR, "Unable to load with selected loader"); - } - } - - public void createDialogButtons(Composite parent) { - Button select = new Button(parent, SWT.PUSH); - select.setText("Select"); - select.addSelectionListener(new SelectionAdapter(){ - public void widgetSelected(SelectionEvent e) { - int index = persisterList.getSelectionIndex(); - if(index != -1){ - selectionMade(index); - } - } - }); - - Button cancel = new Button(parent, SWT.NONE); - cancel.setText("Cancel"); - cancel.addSelectionListener(new SelectionAdapter(){ - public void widgetSelected(SelectionEvent e) { - close(false); - } - }); - } - - public Composite createContent(Composite parent) { - return initGUI(parent); - } -} Copied: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java (from rev 863, trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/LoadDataChooser.java) =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java 2009-03-04 15:54:38 UTC (rev 864) @@ -0,0 +1,212 @@ +/* + * InfoVis CyberInfrastructure: A Data-Code-Compute Resource for Research + * and Education in Information Visualization (http://iv.slis.indiana.edu/). + * + * Created on Jan 24, 2005 at Indiana University. + */ +package org.cishell.reference.gui.persistence.load; + +import java.io.File; +import java.util.ArrayList; + +import org.cishell.framework.CIShellContext; +import org.cishell.framework.algorithm.AlgorithmFactory; +import org.cishell.framework.data.BasicData; +import org.cishell.framework.data.Data; +import org.cishell.reference.gui.common.AbstractDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.List; +import org.eclipse.swt.widgets.Shell; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.log.LogService; + +/** + * + * @author Team IVC (Weixia Huang, James Ellis) + */ +public class SelectedFileServiceSelector extends AbstractDialog { + private File selectedFile; + private LogService logger; + + private ServiceReference[] persisterArray; + private List persisterList; + private StyledText detailPane; + private CIShellContext ciShellContext; + private BundleContext bundleContext; + private ArrayList returnList; + + private static final String[] details_prop_names = + {"format_name", "supported_file_extension", "format_description", + "restorable_model_name", "restorable_model_description" }; + private static final String[] details_prop_name_descriptions = + {"Format name", "Supported file extension", "Format description", + "Restorable model name", "Restorable model description" }; + + public SelectedFileServiceSelector(String title, File theFile, + Shell parent, CIShellContext ciContext, BundleContext bContext, + ServiceReference[] persisterArray, ArrayList returnList){ + super(parent, title, AbstractDialog.QUESTION); + this.ciShellContext = ciContext; + this.bundleContext = bContext; + this.persisterArray = persisterArray; + this.returnList = returnList; + + this.selectedFile = theFile; + + this.logger = (LogService) ciContext.getService(LogService.class.getName()); + //shall this part be moved out of the code? + setDescription("The file you have selected can be loaded" + + " using the following formats.\n" + + "Please select one of them."); + setDetails("This dialog allows the user to choose among all available " + + "formats for loading the selected data model. Choose any of the formats " + + "to continue loading the dataset."); + } + + private Composite initGUI(Composite parent) { + Composite content = new Composite(parent, SWT.NONE); + + GridLayout layout = new GridLayout(); + layout.numColumns = 2; + content.setLayout(layout); + + Group persisterGroup = new Group(content, SWT.NONE); + //shall this label be moved out of the code? + persisterGroup.setText("Loaded by"); + persisterGroup.setLayout(new FillLayout()); + GridData persisterListGridData = new GridData(GridData.FILL_BOTH); + persisterListGridData.widthHint = 200; + persisterGroup.setLayoutData(persisterListGridData); + + persisterList = new List(persisterGroup, SWT.H_SCROLL |SWT.V_SCROLL | SWT.SINGLE); + // initPersisterArray(); + initPersisterList(); + persisterList.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent e) { + List list = (List)e.getSource(); + int selection = list.getSelectionIndex(); + if(selection != -1){ + updateDetailPane(persisterArray[selection]); + } + } + }); + + Group detailsGroup = new Group(content, SWT.NONE); + // shall this label be moved out of the code? + detailsGroup.setText("Details"); + detailsGroup.setLayout(new FillLayout()); + GridData detailsGridData = new GridData(GridData.FILL_BOTH); + detailsGridData.widthHint = 200; + detailsGroup.setLayoutData(detailsGridData); + + detailPane = initDetailPane(detailsGroup); + + persisterList.setSelection(0); + updateDetailPane(persisterArray[0]); + + return content; + } + + private void initPersisterList(){ + for (int i = 0; i < persisterArray.length; ++i) { + + String name = (String)persisterArray[i].getProperty("converter_name"); + + // if someone was sloppy enough to not provide a name, then use the + // name of the class instead. + if (name == null || name.length() == 0) + name = persisterArray[i].getClass().getName(); + persisterList.add(name); + } + } + + + private StyledText initDetailPane(Group detailsGroup) { + StyledText detailPane = new StyledText(detailsGroup, SWT.H_SCROLL | SWT.V_SCROLL); + detailPane.setEditable(false); + detailPane.getCaret().setVisible(false); + return detailPane; + } + + private void updateDetailPane(ServiceReference persister) { + + detailPane.setText(""); + for (int i=0; i<details_prop_names.length; i++){ + String val = (String) persister.getProperty(details_prop_names[i]); + + StyleRange styleRange = new StyleRange(); + styleRange.start = detailPane.getText().length(); + detailPane.append(details_prop_name_descriptions[i] + ":\n"); + styleRange.length = details_prop_names[i].length() + 1; + styleRange.fontStyle = SWT.BOLD; + detailPane.setStyleRange(styleRange); + + detailPane.append(val + "\n"); + + } + + } + + private void selectionMade(int selectedIndex) { + AlgorithmFactory persister =(AlgorithmFactory) bundleContext.getService(persisterArray[selectedIndex]); + Data[] dataManager = null; + boolean loadSuccess = false; + + try { + dataManager = new Data[]{new BasicData(selectedFile.getPath(),String.class.getName())}; + dataManager = persister.createAlgorithm(dataManager, null, ciShellContext).execute(); + loadSuccess = true; + } catch (Throwable e) { + this.logger.log(LogService.LOG_ERROR, "Error occurred while executing selection", e); + e.printStackTrace(); + loadSuccess = false; + } + + if (dataManager != null && loadSuccess) { + logger.log(LogService.LOG_INFO, "Loaded: "+selectedFile.getPath()); + + for(int i = 0; i<dataManager.length; i++){ + returnList.add(dataManager[i]); + } + close(true); + } else { + logger.log(LogService.LOG_ERROR, "Unable to load with selected loader"); + } + } + + public void createDialogButtons(Composite parent) { + Button select = new Button(parent, SWT.PUSH); + select.setText("Select"); + select.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent e) { + int index = persisterList.getSelectionIndex(); + if(index != -1){ + selectionMade(index); + } + } + }); + + Button cancel = new Button(parent, SWT.NONE); + cancel.setText("Cancel"); + cancel.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent e) { + close(false); + } + }); + } + + public Composite createContent(Composite parent) { + return initGUI(parent); + } +} Property changes on: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java ___________________________________________________________________ Added: svn:mergeinfo + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |