From: <mwl...@us...> - 2009-05-27 17:45:00
|
Revision: 867 http://cishell.svn.sourceforge.net/cishell/?rev=867&view=rev Author: mwlinnem Date: 2009-05-27 17:44:50 +0000 (Wed, 27 May 2009) Log Message: ----------- Reworked code that handles loading files for ambiguous file extensions. Reviewed by Patrick. 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/FileFormatSelector.java Removed Paths: ------------- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java Copied: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileFormatSelector.java (from rev 866, trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java) =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileFormatSelector.java (rev 0) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileFormatSelector.java 2009-05-27 17:44:50 UTC (rev 867) @@ -0,0 +1,219 @@ +/* + * 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 FileFormatSelector 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_ITEM_KEY = +// {"format_name", "supported_file_extension", "format_description" }; + + /* + * Other possible keys could be restorable_model_name, restorable_model_description + * */ + +// private static final String[] DETAILS_ITEM_KEY_DISPLAY_VALUE = +// {"Format name", "Supported file extension", "Format description"}; + + /* + * Other possible keys display values could be "Restorable model name", "Restorable model description" + * */ + + public FileFormatSelector(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 one or more of the following formats.\n" + + "Please select the format you would like to try."); + 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 = 1; + content.setLayout(layout); + + Group persisterGroup = new Group(content, SWT.NONE); + //shall this label be moved out of the code? + persisterGroup.setText("Load as..."); + 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("label"); + + // 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_ITEM_KEY.length; i++){ +// String val = (String) persister.getProperty(DETAILS_ITEM_KEY[i]); +// +// StyleRange styleRange = new StyleRange(); +// styleRange.start = detailPane.getText().length(); +// detailPane.append(DETAILS_ITEM_KEY_DISPLAY_VALUE[i] + ":\n"); +// styleRange.length = DETAILS_ITEM_KEY[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); + } +} 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-04-24 22:00:50 UTC (rev 866) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/FileLoad.java 2009-05-27 17:44:50 UTC (rev 867) @@ -21,29 +21,19 @@ import org.osgi.framework.ServiceReference; import org.osgi.service.log.LogService; -/* - * @author Weixia(Bonnie) Huang (hu...@in...) - */ public class FileLoad implements Algorithm { private final LogService logger; - private final GUIBuilderService guiBuilder; private BundleContext bundleContext; private CIShellContext ciShellContext; private static String defaultLoadDirectory; - private final static String FILTER_EXTENSION_ALL = "*"; - private final static String FILTER_AMBIGUOUS = "&(type=validator)(format_name=*)(in_data=file-ext:*)"; - private final static String FILTER_IN_DATA = "&(type=validator)(format_name=*)"; - 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) { @@ -75,291 +65,158 @@ final class FileLoadRunnable implements Runnable { boolean loadFileSuccess = false; IWorkbenchWindow window; + // this is how we return values from the runnable - public ArrayList selectedServicesForLoadedFileList = new ArrayList(); + public ArrayList loadedFiles_ReturnParameter = new ArrayList(); FileLoadRunnable(IWorkbenchWindow window) { this.window = window; } + /* + * Let the user chose which file to load, + * Let the user choose the file type (if it is ambiguous), + * and then actually load and validate the file. + */ 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"); - // } - // } - File currentDir = new File(defaultLoadDirectory); // ? good way to - // do this? - String absolutePath = currentDir.getAbsolutePath(); - String name = currentDir.getName(); - dialog.setFilterPath(absolutePath); - // dialog.setFilterPath(name); - dialog.setText("Select a File"); - String fileName = dialog.open(); - if (fileName == null) { - return; - } + try { + // Prepare to ask the user which file to load. - File file = new File(fileName); - if (file.isDirectory()) { - defaultLoadDirectory = file.getAbsolutePath(); - } else { + FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN); + File currentDir = new File(defaultLoadDirectory); + String absolutePath = currentDir.getAbsolutePath(); + dialog.setFilterPath(absolutePath); + dialog.setText("Select a File"); - // File parentFile = file.getParentFile(); - // if (parentFile != null) { - defaultLoadDirectory = file.getParentFile().getAbsolutePath(); - // } - } + // Determine which file to load. - String fileExtension = getFileExtension(file).toLowerCase(); + String nameOfFileToLoad = dialog.open(); + if (nameOfFileToLoad == null) { + return; + } - /* - * 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. - */ + // Actually load the file. - try { + File file = new File(nameOfFileToLoad); - // get all the service references of validators that can load - // this type of file. + if (file.isDirectory()) { + defaultLoadDirectory = file.getAbsolutePath(); + } else { + defaultLoadDirectory = file.getParentFile().getAbsolutePath(); + } - ServiceReference[] selectedFileServiceReferences = null; + //Validate the loaded file, "casting" it to a certain MIME type. - if(fileExtension != null && fileExtension.length() > 0) { - selectedFileServiceReferences = getApplicableServiceReferences( - FILTER_IN_DATA, fileExtension); - } - - /* - * 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. - */ + // Extract the file's file extension. - if ((selectedFileServiceReferences == null || selectedFileServiceReferences.length == 0)) { + String fileExtension = getFileExtension(file).toLowerCase(); - /* - * 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. - * or, 3. which have empty in_data field and non-empty - * ambiguous_extension field. This is used so that all the - * validators for output file functionality are filtered - * out. - */ + // Get all the validators which support this file extension... - ServiceReference[] potentialValidators = null; + ServiceReference[] supportingValidators = getSupportingValidators(fileExtension); - /* - * This is used to find validators that support ambiguous - * extensions for the provided file extension. There are - * good chances that the file selected does not have any - * file extension, this is handled by below case. - */ + // If there are no supporting validators... + if (supportingValidators.length == 0) { + // Let the user choose from all the validators available. - if (fileExtension != null && fileExtension.length() > 0) { + ServiceReference[] allValidators = getAllValidators(); - potentialValidators = getApplicableServiceReferences( - FILTER_AMBIGUOUS, fileExtension); - - } - - /* - * If no services are found then provide for all the - * validators list. - */ - - if (potentialValidators == null - || potentialValidators.length == 0) { - - potentialValidators = getApplicableServiceReferences( - FILTER_IN_DATA, FILTER_EXTENSION_ALL); - - } - - /* - * 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, - potentialValidators, - selectedServicesForLoadedFileList).open(); + new FileFormatSelector("Load", file, window.getShell(), + ciShellContext, bundleContext, allValidators, + loadedFiles_ReturnParameter).open(); } - /* - * This use case is for input files selected that have only one - * applicable service. Special case where the file extension - * belongs to ambiguous file extension group like csv is also - * handled. In the simple case system goes ahead and loads the - * file with that service. - */ + // If there is just one supporting validator... + if (supportingValidators.length == 1) { + // Just use that validator to validate the file. + + ServiceReference onlyPossibleValidator = supportingValidators[0]; + AlgorithmFactory selectedValidatorExecutor = (AlgorithmFactory) bundleContext + .getService(onlyPossibleValidator); + Data[] outputDataAfterValidation; + Data[] inputDataForValidation = new Data[] { new BasicData( + file.getPath(), String.class.getName()) }; + outputDataAfterValidation = selectedValidatorExecutor + .createAlgorithm(inputDataForValidation, null, + ciShellContext).execute(); - else if (selectedFileServiceReferences.length == 1) { - /* - * To check for the files with ambiguous file extension a - * seperate list of service references is created. + * outputDataAfterValidation = null implies that file + * was not loaded properly. */ - ServiceReference[] selectedFileAmbiguousValidators = getApplicableServiceReferences( - FILTER_AMBIGUOUS, fileExtension); - - /* - * If allAmbiguousValidators is not empty then the system - * provides a dialog box to select from the available - * validators. In this case CSV, NSF & SCOPUS - */ - - if ((selectedFileAmbiguousValidators != null && selectedFileAmbiguousValidators.length > 0)) { - new SelectedFileServiceSelector("Load", file, window - .getShell(), ciShellContext, bundleContext, - selectedFileAmbiguousValidators, - selectedServicesForLoadedFileList).open(); + if (outputDataAfterValidation != null) { + loadFileSuccess = true; + logger.log(LogService.LOG_INFO, "Loaded: " + + file.getPath()); + for (int i = 0; i < outputDataAfterValidation.length; i++) + loadedFiles_ReturnParameter. + add(outputDataAfterValidation[i]); } - - /* - * If allAmbiguousValidators is not empty we go forward with - * normal work flow of loading the file with selected file - * format. - */ - - else { - - AlgorithmFactory selectedValidatorExecutor = (AlgorithmFactory) bundleContext - .getService(selectedFileServiceReferences[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. This now triggers the - * SelectedFileServiceSelector dialog & can be used to select - * any one of the available services for that particular file - * extension. - */ + // If there is more than one supporting validator... + if (supportingValidators.length > 1) { + // Let the user choose which validator they want to use. - else if (selectedFileServiceReferences.length > 1) { - - new SelectedFileServiceSelector("Load", file, window - .getShell(), ciShellContext, bundleContext, - selectedFileServiceReferences, - selectedServicesForLoadedFileList).open(); - + new FileFormatSelector("Load", file, window.getShell(), + ciShellContext, bundleContext, supportingValidators, + loadedFiles_ReturnParameter).open(); } - /* - * 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() - - /** - * @param selectedFileServiceReferencesFilter - * @param fileExtensionManipulations - * @return - * @throws InvalidSyntaxException - */ - private ServiceReference[] getApplicableServiceReferences( - String selectedFileServiceReferencesFilter, - String fileExtensionManipulations) - throws InvalidSyntaxException { - - String appliedValidatorFilter = getValidatorFilter( - selectedFileServiceReferencesFilter, - fileExtensionManipulations); - - ServiceReference[] selectedFileServiceReferences = bundleContext - .getAllServiceReferences(AlgorithmFactory.class.getName(), - appliedValidatorFilter); - - return selectedFileServiceReferences; - } - - private String getValidatorFilter( - String selectedFileServiceReferencesFilter, - String fileExtensionManipulations) { - - if (selectedFileServiceReferencesFilter.equals(FILTER_IN_DATA)) { - return "(" + selectedFileServiceReferencesFilter - + "(in_data=file-ext:" + fileExtensionManipulations - + ")" + - ")"; - - } else if (selectedFileServiceReferencesFilter.equals(FILTER_AMBIGUOUS)) { - return "(" + selectedFileServiceReferencesFilter - + "(ambiguous_extension=" + fileExtensionManipulations - + ")" + - ")"; + + private ServiceReference[] getSupportingValidators(String fileExtension) { + try { + String ldapQuery = "(& (type=validator)" + + "(|" + + "(in_data=file-ext:" + fileExtension + ")" + + "(also_validates=" + fileExtension + ")" + + "))"; + + ServiceReference[] supportingValidators = + bundleContext.getAllServiceReferences( + AlgorithmFactory.class.getName(), + ldapQuery); + + + if (supportingValidators == null) { + //(better to return a list of length zero than null) + supportingValidators = new ServiceReference[]{}; + } + + return supportingValidators; + + + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + return new ServiceReference[]{}; } - - return null; } - + + private ServiceReference[] getAllValidators() { + try { + ServiceReference[] allValidators = + bundleContext.getAllServiceReferences( + AlgorithmFactory.class.getName(), + "(&(type=validator)(in_data=file-ext:*))"); + + if (allValidators == null) { + //(better to return a list of length zero than null) + allValidators = new ServiceReference[]{}; + } + + return allValidators; + + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + return new ServiceReference[]{}; + } + } + public String getFileExtension(File theFile) { String fileName = theFile.getName(); String extension; @@ -369,7 +226,7 @@ extension = ""; return extension; } - } // end class + } private IWorkbenchWindow getFirstWorkbenchWindow() throws AlgorithmExecutionException { @@ -387,11 +244,11 @@ throws AlgorithmExecutionException { Data[] loadedFileData; try { - if (!dataUpdater.selectedServicesForLoadedFileList.isEmpty()) { - int size = dataUpdater.selectedServicesForLoadedFileList.size(); + if (!dataUpdater.loadedFiles_ReturnParameter.isEmpty()) { + int size = dataUpdater.loadedFiles_ReturnParameter.size(); loadedFileData = new Data[size]; for (int index = 0; index < size; index++) { - loadedFileData[index] = (Data) dataUpdater.selectedServicesForLoadedFileList + loadedFileData[index] = (Data) dataUpdater.loadedFiles_ReturnParameter .get(index); } return loadedFileData; Deleted: trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java 2009-04-24 22:00:50 UTC (rev 866) +++ trunk/clients/gui/org.cishell.reference.gui.persistence/src/org/cishell/reference/gui/persistence/load/SelectedFileServiceSelector.java 2009-05-27 17:44:50 UTC (rev 867) @@ -1,219 +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 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_ITEM_KEY = - {"format_name", "supported_file_extension", "format_description" }; - - /* - * Other possible keys could be restorable_model_name, restorable_model_description - * */ - - private static final String[] DETAILS_ITEM_KEY_DISPLAY_VALUE = - {"Format name", "Supported file extension", "Format description"}; - - /* - * Other possible keys display values could be "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_ITEM_KEY.length; i++){ - String val = (String) persister.getProperty(DETAILS_ITEM_KEY[i]); - - StyleRange styleRange = new StyleRange(); - styleRange.start = detailPane.getText().length(); - detailPane.append(DETAILS_ITEM_KEY_DISPLAY_VALUE[i] + ":\n"); - styleRange.length = DETAILS_ITEM_KEY[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); - } -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |