From: <pat...@us...> - 2010-08-05 19:47:49
|
Revision: 1119 http://cishell.svn.sourceforge.net/cishell/?rev=1119&view=rev Author: pataphil Date: 2010-08-05 19:47:42 +0000 (Thu, 05 Aug 2010) Log Message: ----------- * Misc. cleanup. Modified Paths: -------------- trunk/clients/gui/org.cishell.reference.gui.datamanager/src/org/cishell/reference/gui/datamanager/AbstractDataManagerView.java trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmAction.java trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmWrapper.java trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/MenuAdapter.java Modified: trunk/clients/gui/org.cishell.reference.gui.datamanager/src/org/cishell/reference/gui/datamanager/AbstractDataManagerView.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.datamanager/src/org/cishell/reference/gui/datamanager/AbstractDataManagerView.java 2010-08-05 19:44:46 UTC (rev 1118) +++ trunk/clients/gui/org.cishell.reference.gui.datamanager/src/org/cishell/reference/gui/datamanager/AbstractDataManagerView.java 2010-08-05 19:47:42 UTC (rev 1119) @@ -24,8 +24,8 @@ import org.cishell.app.service.datamanager.DataManagerListener; import org.cishell.app.service.datamanager.DataManagerService; import org.cishell.framework.algorithm.Algorithm; +import org.cishell.framework.algorithm.AlgorithmExecutionException; import org.cishell.framework.algorithm.AlgorithmFactory; -import org.cishell.framework.algorithm.AlgorithmExecutionException; import org.cishell.framework.data.Data; import org.cishell.framework.data.DataProperty; import org.cishell.reference.gui.workspace.CIShellApplication; @@ -69,13 +69,11 @@ import org.osgi.service.log.LogService; public abstract class AbstractDataManagerView - extends ViewPart - implements DataManagerListener, BundleListener { + extends ViewPart implements BundleListener, DataManagerListener { private String brandPluginID; private DataManagerService manager; private TreeViewer viewer; private TreeEditor editor; - // TODO: Finish cleaning this file up. private Text newEditor; private DataGUIItem rootItem; /* @@ -85,7 +83,7 @@ private boolean updatingTreeItem; private Tree tree; private Menu menu; - private Map dataToDataGUIItemMap; + private Map<Data, DataGUIItem> dataToDataGUIItemMap; private AlgorithmFactory saveFactory; private AlgorithmFactory viewFactory; private AlgorithmFactory viewWithFactory; @@ -93,18 +91,17 @@ private SaveListener saveListener; private ViewListener viewListener; private ViewWithListener viewWithListener; - private LogService log; + private LogService logger; public AbstractDataManagerView(String brandPluginID) { this.brandPluginID = brandPluginID; - dataToDataGUIItemMap = new HashMap(); + this.dataToDataGUIItemMap = new HashMap<Data, DataGUIItem>(); + this.manager = Activator.getDataManagerService(); + this.logger = Activator.getLogService(); - manager = Activator.getDataManagerService(); - log = Activator.getLogService(); - - if (manager == null) { - if (log != null) { - log.log(LogService.LOG_ERROR, "Data Manager Service unavailable!"); + if (this.manager == null) { + if (this.logger != null) { + this.logger.log(LogService.LOG_ERROR, "Data Manager Service unavailable!"); } } } @@ -122,41 +119,39 @@ * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) */ public void createPartControl(Composite parent) { - // Label label = new Label(parent, SWT.NONE); - // label.setText("Data Manager"); this.viewer = new TreeViewer(parent); this.viewer.setContentProvider(new DataTreeContentProvider()); this.viewer.setLabelProvider(new DataTreeLabelProvider()); - rootItem = new DataGUIItem(null, null, this.brandPluginID); - this.viewer.setInput(rootItem); + this.rootItem = new DataGUIItem(null, null, this.brandPluginID); + this.viewer.setInput(this.rootItem); this.viewer.expandAll(); - // grab the tree and add the appropriate listeners - tree = this.viewer.getTree(); - tree.addSelectionListener(new DatamodelSelectionListener()); - tree.addMouseListener(new ContextMenuListener()); + // Grab the tree and add the appropriate listeners. + this.tree = this.viewer.getTree(); + this.tree.addSelectionListener(new DatamodelSelectionListener()); + this.tree.addMouseListener(new ContextMenuListener()); - // setup the context menu for the tree - menu = new Menu(tree); - menu.setVisible(false); + // Setup the context menu for the tree. + this.menu = new Menu(tree); + this.menu.setVisible(false); - MenuItem saveItem = new MenuItem(menu, SWT.PUSH); + MenuItem saveItem = new MenuItem(this.menu, SWT.PUSH); saveItem.setText("Save"); - saveListener = new SaveListener(); - saveItem.addListener(SWT.Selection, saveListener); + this.saveListener = new SaveListener(); + saveItem.addListener(SWT.Selection, this.saveListener); - MenuItem viewItem = new MenuItem(menu, SWT.PUSH); + MenuItem viewItem = new MenuItem(this.menu, SWT.PUSH); viewItem.setText("View"); - viewListener = new ViewListener(); - viewItem.addListener(SWT.Selection, viewListener); + this.viewListener = new ViewListener(); + viewItem.addListener(SWT.Selection, this.viewListener); - MenuItem viewWithItem = new MenuItem(menu, SWT.PUSH); + MenuItem viewWithItem = new MenuItem(this.menu, SWT.PUSH); viewWithItem.setText("View With..."); - viewWithListener = new ViewWithListener(); - viewWithItem.addListener(SWT.Selection, viewWithListener); + this.viewWithListener = new ViewWithListener(); + viewWithItem.addListener(SWT.Selection, this.viewWithListener); - MenuItem renameItem = new MenuItem(menu, SWT.PUSH); + MenuItem renameItem = new MenuItem(this.menu, SWT.PUSH); renameItem.setText("Rename"); renameItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { @@ -164,19 +159,19 @@ } }); - MenuItem discardItem = new MenuItem(menu, SWT.PUSH); + MenuItem discardItem = new MenuItem(this.menu, SWT.PUSH); discardItem.setText("Discard"); - discardListener = new DiscardListener(); - discardItem.addListener(SWT.Selection, discardListener); - tree.setMenu(menu); + this.discardListener = new DiscardListener(); + discardItem.addListener(SWT.Selection, this.discardListener); + this.tree.setMenu(this.menu); // Allow cells to be edited on double click or when pressing enter on them. - this.editor = new TreeEditor(tree); + this.editor = new TreeEditor(this.tree); this.editor.horizontalAlignment = SWT.LEFT; this.editor.grabHorizontal = true; this.editor.minimumWidth = 50; - // listen to OSGi for models being added by plugins + // Listen to OSGi for models being added by plugins. if (this.manager != null) { this.manager.addDataManagerListener(this); } else { @@ -210,23 +205,22 @@ public void dataAdded(final Data newData, String label) { - //get the new data's parent GUI Item (either root or another data item) + // Get the new data's parent GUI Item (either root or another data item). DataGUIItem parentItem = getParent(newData); - // wrap the new data in a DataGUIItem - final DataGUIItem newItem = new DataGUIItem(newData, parentItem, - this.brandPluginID); + // Wrap the new data in a DataGUIItem. + final DataGUIItem newItem = new DataGUIItem(newData, parentItem, this.brandPluginID); - // notify the parent DataModelGUIItem of its new child + // Notify the parent DataModelGUIItem of its new child. parentItem.addChild(newItem); - // keep a reference to the new model in the model->TreeItem mapping so - // that - // it can be used in the future if it has a child - dataToDataGUIItemMap.put(newData, newItem); + /* Keep a reference to the new model in the model->TreeItem mapping so that it can be used + * in the future if it has a child. + */ + this.dataToDataGUIItemMap.put(newData, newItem); // update the ModelManager with the new selection - final Set selection = new HashSet(); + final Set<Data> selection = new HashSet<Data>(); selection.add(newData); guiRun(new Runnable() { @@ -246,20 +240,21 @@ } private DataGUIItem getParent(Data data) { - Dictionary modelDictionary = data.getMetadata(); + Dictionary<String, Object> modelDictionary = data.getMetadata(); Data parent = (Data) modelDictionary.get(DataProperty.PARENT); DataGUIItem parentItem; + if (parent == null) { - // if it has no parent, it is a child of the root - parentItem = rootItem; + // If it has no parent, it is a child of the root. + parentItem = this.rootItem; } else { - // otherwise find the associated DataModelGUIItem for the parent - parentItem = (DataGUIItem) dataToDataGUIItemMap.get(parent); + // Otherwise find the associated DataModelGUIItem for the parent. + parentItem = this.dataToDataGUIItemMap.get(parent); - //The parent may not be in the GUI. If its not, then use root item + // The parent may not be in the GUI. If its not, then use root item. if (parentItem == null) { - parentItem = rootItem; + parentItem = this.rootItem; } } @@ -276,7 +271,7 @@ public void dataLabelChanged(Data data, String label) { if (data != null && label != null) { - TreeItem[] treeItems = tree.getItems(); + TreeItem[] treeItems = this.tree.getItems(); for (int i = 0; i < treeItems.length; ++i) { if (((DataGUIItem)treeItems[i].getData()).getModel() == data) { updateText(label, treeItems[i]); @@ -287,20 +282,21 @@ } public void dataRemoved(Data data) { - TreeItem[] treeItems = tree.getItems(); + TreeItem[] treeItems = this.tree.getItems(); for (int i = 0; i < treeItems.length; ++i) { if (((DataGUIItem)treeItems[i].getData()).getModel() == data) { - tree.clear(tree.indexOf(treeItems[i]), false); + this.tree.clear(this.tree.indexOf(treeItems[i]), false); } } } public void dataSelected(final Data[] data) { if (data != null) { - //setFocus(); guiRun(new Runnable() { public void run() { - Set itemSet = new HashSet(); + // TODO: Abstract this? + Set<TreeItem> itemSet = new HashSet<TreeItem>(); + for (int i = 0; i < data.length; ++i) { TreeItem[] treeItems = tree.getItems(); for (int j = 0; j < treeItems.length; ++j) { @@ -310,34 +306,39 @@ } } } - tree.setSelection((TreeItem[]) itemSet - .toArray(new TreeItem[0])); - getSite().getSelectionProvider().setSelection( - new StructuredSelection(data)); + + tree.setSelection(itemSet.toArray(new TreeItem[0])); + getSite().getSelectionProvider().setSelection(new StructuredSelection(data)); } }); } } /* - * enables/disables save item in the context menu based on whether or not + * Enables/disables save item in the context menu based on whether or not * their is an available Persister for the given model */ private void updateContextMenu(Data model) { - saveFactory = enableMenuItemCheck(saveFactory, - "org.cishell.reference.gui.persistence.save.Save", model, 0); - viewFactory = enableMenuItemCheck(viewFactory, - "org.cishell.reference.gui.persistence.view.FileView", model, 1); - viewWithFactory = enableMenuItemCheck(viewWithFactory, - "org.cishell.reference.gui.persistence.viewwith.FileViewWith", model, 2); +// throws AlgorithmCreationCanceledException, AlgorithmCreationFailedException { + this.saveFactory = enableMenuItemCheck( + this.saveFactory, "org.cishell.reference.gui.persistence.save.Save", model, 0); + this.viewFactory = enableMenuItemCheck( + this.viewFactory, "org.cishell.reference.gui.persistence.view.FileView", model, 1); + this.viewWithFactory = enableMenuItemCheck( + this.viewWithFactory, + "org.cishell.reference.gui.persistence.viewwith.FileViewWith", + model, + 2); } private AlgorithmFactory enableMenuItemCheck( - AlgorithmFactory algorithmFactory, String service, Data model, - int menuNdx) { + AlgorithmFactory algorithmFactory, String service, Data model, int menuIndex) { +// throws AlgorithmCreationCanceledException, AlgorithmCreationFailedException { boolean validSaveFactory = false; + if (algorithmFactory == null) { algorithmFactory = Activator.getService(service); + if (algorithmFactory != null) { validSaveFactory = true; } @@ -349,13 +350,17 @@ if (validSaveFactory) { Algorithm algorithm = algorithmFactory.createAlgorithm( - new Data[] { model }, new Hashtable(), Activator - .getCIShellContext()); + new Data[] { model }, + new Hashtable<String, Object>(), + Activator.getCIShellContext()); + if (algorithm != null) { enabled = true; } } - menu.getItem(menuNdx).setEnabled(enabled); + + this.menu.getItem(menuIndex).setEnabled(enabled); + return algorithmFactory; } @@ -367,7 +372,7 @@ public void widgetSelected(SelectionEvent e) { Tree tree = (Tree) e.getSource(); TreeItem[] selection = tree.getSelection(); - Set models = new HashSet(); + Set<Data> models = new HashSet<Data>(); Data[] modelArray = new Data[selection.length]; for (int i = 0; i < selection.length; i++) { @@ -396,7 +401,7 @@ // Identify the selected row, only allow input if there is a single // selected row - TreeItem[] selection = tree.getSelection(); + TreeItem[] selection = this.tree.getSelection(); if (selection.length != 1) { return; @@ -409,32 +414,33 @@ } // The control that will be the editor must be a child of the Table - newEditor = new Text(tree, SWT.NONE); - newEditor.setText(item.getText()); - newEditor.addFocusListener(new FocusAdapter() { + this.newEditor = new Text(this.tree, SWT.NONE); + this.newEditor.setText(item.getText()); + this.newEditor.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent e) { if (!updatingTreeItem) { //updateText(newEditor.getText(), item); AbstractDataManagerView.this.manager.setLabel( - ((DataGUIItem)item.getData()).getModel(), newEditor.getText()); + ((DataGUIItem) item.getData()).getModel(), + AbstractDataManagerView.this.newEditor.getText()); // FELIX. This is not > stupidness. } } }); // ENTER ESC - newEditor.addKeyListener(new KeyAdapter() { + this.newEditor.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { - if ((e.character == SWT.CR) && !updatingTreeItem) { - updateText(newEditor.getText(), item); + if ((e.character == SWT.CR) && !AbstractDataManagerView.this.updatingTreeItem) { + updateText(AbstractDataManagerView.this.newEditor.getText(), item); } else if (e.keyCode == SWT.ESC) { - newEditor.dispose(); + AbstractDataManagerView.this.newEditor.dispose(); } } }); - newEditor.selectAll(); - newEditor.setFocus(); - this.editor.setEditor(newEditor, item); + this.newEditor.selectAll(); + this.newEditor.setFocus(); + this.editor.setEditor(this.newEditor, item); } /* @@ -442,7 +448,7 @@ * by the TreeEditor for renaming - only if the new name is valid though */ private void updateText(String newLabel, TreeItem item) { - updatingTreeItem = true; + this.updatingTreeItem = true; if (newLabel.startsWith(">")) newLabel = newLabel.substring(1); @@ -454,7 +460,7 @@ Data model = treeItem.getModel(); model.getMetadata().put(DataProperty.LABEL, newLabel); viewer.refresh(); - newEditor.dispose(); + this.newEditor.dispose(); updatingTreeItem = false; } @@ -466,12 +472,13 @@ private class ContextMenuListener extends MouseAdapter { public void mouseUp(MouseEvent event) { if (event.button == 3) { - TreeItem item = tree.getItem(new Point(event.x, event.y)); + TreeItem item = + AbstractDataManagerView.this.tree.getItem(new Point(event.x, event.y)); if (item != null) { - tree.getMenu().setVisible(true); + AbstractDataManagerView.this.tree.getMenu().setVisible(true); } else { - tree.getMenu().setVisible(false); + AbstractDataManagerView.this.tree.getMenu().setVisible(false); } } } @@ -480,28 +487,26 @@ private class SaveListener implements Listener { public void handleEvent(Event event) { - if (saveFactory != null) { - Data data[] = AbstractDataManagerView.this.manager - .getSelectedData(); - Algorithm algorithm = saveFactory - .createAlgorithm(data, new Hashtable(), Activator - .getCIShellContext()); - try{ + if (AbstractDataManagerView.this.saveFactory != null) { + Data data[] = AbstractDataManagerView.this.manager.getSelectedData(); + Algorithm algorithm = AbstractDataManagerView.this.saveFactory.createAlgorithm( + data, new Hashtable<String, Object>(), Activator.getCIShellContext()); + + try { algorithm.execute(); - }catch (AlgorithmExecutionException aee) { - if (log != null) { - log.log(LogService.LOG_ERROR, - aee.getMessage(), - aee); - aee.printStackTrace(); + } catch (AlgorithmExecutionException e) { + if (AbstractDataManagerView.this.logger != null) { + AbstractDataManagerView.this.logger.log( + LogService.LOG_ERROR, e.getMessage(), e); + e.printStackTrace(); + } else { + AbstractDataManagerView.this.logger = Activator.getLogService(); + AbstractDataManagerView.this.logger.log( + LogService.LOG_ERROR, + "org.cishell.framework.algorithm.AlgorithmExecutionException", + e); + e.printStackTrace(); } - else { - log = Activator.getLogService(); - log.log(LogService.LOG_ERROR, - "org.cishell.framework.algorithm.AlgorithmExecutionException", - aee); - aee.printStackTrace(); - } } } } @@ -509,27 +514,22 @@ private class ViewListener implements Listener { public void handleEvent(Event event) { - if (viewFactory != null) { - Data data[] = AbstractDataManagerView.this.manager - .getSelectedData(); - Algorithm algorithm = viewFactory - .createAlgorithm(data, new Hashtable(), Activator - .getCIShellContext()); + if (AbstractDataManagerView.this.viewFactory != null) { + Data data[] = AbstractDataManagerView.this.manager.getSelectedData(); + Algorithm algorithm = AbstractDataManagerView.this.viewFactory.createAlgorithm( + data, new Hashtable<String, Object>(), Activator.getCIShellContext()); + try { algorithm.execute(); - }catch (AlgorithmExecutionException aee) { - if (log != null) { - log.log(LogService.LOG_ERROR, - aee.getMessage(), - aee); + } catch (AlgorithmExecutionException e) { + if (logger != null) { + logger.log(LogService.LOG_ERROR, e.getMessage(), e); + } else { + logger = Activator.getLogService(); + logger.log(LogService.LOG_ERROR, e.getMessage(), e); } - else { - log = Activator.getLogService(); - log.log(LogService.LOG_ERROR, - aee.getMessage(), - aee); - } - aee.printStackTrace(); + + e.printStackTrace(); } } } @@ -539,14 +539,16 @@ public void handleEvent(Event event) { IMenuManager topLevelMenu = CIShellApplication.getMenuManager(); IMenuManager fileMenu = topLevelMenu.findMenuUsingPath("File"); - BundleContext bContext = Activator.getBundleContext(); + BundleContext bundleContext = Activator.getBundleContext(); try { - ServiceReference[] ref = bContext.getAllServiceReferences(AlgorithmFactory.class.getName(), - "(service.pid=org.cishell.reference.gui.persistence.viewwith.FileViewWith)"); + ServiceReference[] serviceReference = bundleContext.getAllServiceReferences( + AlgorithmFactory.class.getName(), + "(service.pid=org.cishell.reference.gui.persistence.viewwith.FileViewWith)"); - if (ref != null && ref.length > 0) { - ActionContributionItem action = (ActionContributionItem)fileMenu.find(getItemID(ref[0])); + if ((serviceReference != null) && (serviceReference.length > 0)) { + ActionContributionItem action = + (ActionContributionItem) fileMenu.find(getItemID(serviceReference[0])); action.getAction().run(); } } catch (InvalidSyntaxException e) { @@ -557,18 +559,19 @@ private class DiscardListener implements Listener { public void handleEvent(Event event) { - TreeItem[] selection = AbstractDataManagerView.this.tree - .getSelection(); + TreeItem[] selections = AbstractDataManagerView.this.tree.getSelection(); - for (int i = 0; i < selection.length; i++) { - DataGUIItem item = (DataGUIItem) selection[i].getData(); + for (TreeItem selection : selections) { +// for (int i = 0; i < selections.length; i++) { +// DataGUIItem item = (DataGUIItem) selections[i].getData(); + DataGUIItem item = (DataGUIItem) selection.getData(); DataGUIItem parent = item.getParent(); if (parent != null) { parent.removeChild(item); } - dataToDataGUIItemMap.remove(item.getModel()); + AbstractDataManagerView.this.dataToDataGUIItemMap.remove(item.getModel()); AbstractDataManagerView.this.manager.removeData(item.getModel()); } @@ -579,13 +582,11 @@ private class DataModelSelectionProvider implements ISelectionProvider { - - private Set listeners = new HashSet(); - + private Set<ISelectionChangedListener> listeners = + new HashSet<ISelectionChangedListener>(); private ISelection selection; - public void addSelectionChangedListener( - ISelectionChangedListener listener) { + public void addSelectionChangedListener(ISelectionChangedListener listener) { listeners.add(listener); } @@ -593,17 +594,19 @@ return selection; } - public void removeSelectionChangedListener( - ISelectionChangedListener listener) { + public void removeSelectionChangedListener(ISelectionChangedListener listener) { listeners.remove(listener); } private TreeItem getTreeItem(Data model, TreeItem[] items) { TreeItem result = null; int i = 0; - while (i < items.length && result == null) { + + while ((i < items.length) && (result == null)) { DataGUIItem data = ((DataGUIItem) items[i].getData()); - if (data != null) { // not sure why this happens.. + + // TODO: Not sure why this happens... + if (data != null) { Data item = data.getModel(); if (item == model) @@ -628,37 +631,37 @@ this.selection = selection; AbstractDataManagerView.this.viewer.refresh(true); - if (selection != null - && selection instanceof IStructuredSelection) { - IStructuredSelection ss = (IStructuredSelection) selection; - Iterator iterator = ss.iterator(); - TreeItem[] newTreeSelection = new TreeItem[ss.size()]; + if ((selection != null) && (selection instanceof IStructuredSelection)) { + IStructuredSelection selections = (IStructuredSelection) selection; + Iterator<?> iterator = selections.iterator(); + TreeItem[] newTreeSelection = new TreeItem[selections.size()]; int i = 0; + while (iterator.hasNext()) { Object next = iterator.next(); + if (next instanceof Data) { - TreeItem result = getTreeItem((Data) next, tree - .getItems()); + TreeItem result = getTreeItem( + (Data) next, AbstractDataManagerView.this.tree.getItems()); newTreeSelection[i] = result; AbstractDataManagerView.this.viewer.expandToLevel( - dataToDataGUIItemMap.get(next), 0); + AbstractDataManagerView.this.dataToDataGUIItemMap.get(next), 0); } + i++; } - tree.setSelection(newTreeSelection); + AbstractDataManagerView.this.tree.setSelection(newTreeSelection); } - Iterator listenerIterator = listeners.iterator(); + Iterator<ISelectionChangedListener> listenerIterator = listeners.iterator(); + while (listenerIterator.hasNext()) { - ISelectionChangedListener listener = (ISelectionChangedListener) listenerIterator - .next(); - SelectionChangedEvent event = new SelectionChangedEvent( - this, selection); + ISelectionChangedListener listener = listenerIterator.next(); + SelectionChangedEvent event = new SelectionChangedEvent(this, selection); listener.selectionChanged(event); } } } - } } Modified: trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmAction.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmAction.java 2010-08-05 19:44:46 UTC (rev 1118) +++ trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmAction.java 2010-08-05 19:47:42 UTC (rev 1119) @@ -33,29 +33,38 @@ public class AlgorithmAction extends Action implements AlgorithmProperty, DataManagerListener { - protected CIShellContext ciContext; - protected BundleContext bContext; - protected ServiceReference ref; + protected CIShellContext ciShellContext; + protected BundleContext bundleContext; + protected ServiceReference serviceReference; protected Data[] data; protected Data[] originalData; protected Converter[][] converters; public AlgorithmAction( - ServiceReference ref, BundleContext bContext, CIShellContext ciContext) { - this((String)ref.getProperty(LABEL), ref, bContext, ciContext); + ServiceReference serviceReference, + BundleContext bundleContext, + CIShellContext ciShellContext) { + this( + (String) serviceReference.getProperty(LABEL), + serviceReference, + bundleContext, + ciShellContext); } public AlgorithmAction( - String label, ServiceReference ref, BundleContext bContext, CIShellContext ciContext) { - this.ref = ref; - this.ciContext = ciContext; - this.bContext = bContext; + String label, + ServiceReference serviceReference, + BundleContext bundleContext, + CIShellContext ciShellContext) { + this.serviceReference = serviceReference; + this.ciShellContext = ciShellContext; + this.bundleContext = bundleContext; setText(label); - setToolTipText((String)ref.getProperty(AlgorithmProperty.DESCRIPTION)); + setToolTipText((String)serviceReference.getProperty(AlgorithmProperty.DESCRIPTION)); DataManagerService dataManager = (DataManagerService) - bContext.getService(bContext.getServiceReference( + bundleContext.getService(bundleContext.getServiceReference( DataManagerService.class.getName())); dataManager.addDataManagerListener(this); @@ -64,12 +73,18 @@ public void run() { try { - printAlgorithmInformation(ref, ciContext); - - Algorithm algorithm = new AlgorithmWrapper(ref, bContext, ciContext, originalData, data, converters); + printAlgorithmInformation(this.serviceReference, this.ciShellContext); + + Algorithm algorithm = new AlgorithmWrapper( + this.serviceReference, + this.bundleContext, + this.ciShellContext, + this.originalData, + this.data, + this.converters); SchedulerService scheduler = (SchedulerService) getService(SchedulerService.class); - scheduler.schedule(algorithm, ref); + scheduler.schedule(algorithm, this.serviceReference); } catch (Throwable exception) { // Just in case an uncaught exception occurs. Eclipse will swallow errors thrown here. exception.printStackTrace(); @@ -134,76 +149,77 @@ return inData; } - + public void dataSelected(Data[] selectedData) { - String inDataString = (String)ref.getProperty(IN_DATA); + String inDataString = (String) this.serviceReference.getProperty(IN_DATA); String[] inData = separateInData(inDataString); - if ((inData.length == 1 && inData[0].equalsIgnoreCase(NULL_DATA))) { - data = new Data[0]; + if ((inData.length == 1) && inData[0].equalsIgnoreCase(NULL_DATA)) { + this.data = new Data[0]; } else if (selectedData == null) { - data = null; + this.data = null; } else { - DataConversionService converter = (DataConversionService) - ciContext.getService(DataConversionService.class.getName()); + DataConversionService converter = + (DataConversionService) this.ciShellContext.getService( + DataConversionService.class.getName()); - List dataSet = new ArrayList(Arrays.asList(selectedData)); - data = new Data[inData.length]; - converters = new Converter[inData.length][]; + List<Data> dataSet = new ArrayList<Data>(Arrays.asList(selectedData)); + this.data = new Data[inData.length]; + this.converters = new Converter[inData.length][]; - for (int i=0; i < inData.length; i++) { - for (int j=0; j < dataSet.size(); j++) { - Data datum = (Data) dataSet.get(j); + for (int ii = 0; ii < inData.length; ii++) { + for (int jj = 0; jj < dataSet.size(); jj++) { + Data datum = (Data) dataSet.get(jj); if (datum != null) { - if (isAssignableFrom(inData[i], datum)) { - dataSet.remove(j); - data[i] = datum; - converters[i] = null; + if (isAssignableFrom(inData[ii], datum)) { + dataSet.remove(jj); + this.data[ii] = datum; + this.converters[ii] = null; } else { - Converter[] conversion = converter.findConverters(datum, inData[i]); + Converter[] conversion = converter.findConverters(datum, inData[ii]); if (conversion.length > 0) { - dataSet.remove(j); - data[i] = datum; - converters[i] = conversion; + dataSet.remove(jj); + this.data[ii] = datum; + this.converters[ii] = conversion; } } } } - //if there isn't a converter for one of the inputs - //then this data isn't useful - if (data[i] == null) { - data = null; + // If there isn't a converter for one of the inputs then this data isn't useful. + if (this.data[ii] == null) { + this.data = null; + break; } } } - if (data != null) { - originalData = (Data[]) data.clone(); + if (this.data != null) { + this.originalData = (Data[]) this.data.clone(); } else { - originalData = null; + this.originalData = null; } - setEnabled(data != null); + setEnabled(this.data != null); } private boolean isAssignableFrom(String type, Data datum) { Object data = datum.getData(); boolean assignable = false; - if (type != null && type.equalsIgnoreCase(datum.getFormat())) { + if ((type != null) && type.equalsIgnoreCase(datum.getFormat())) { assignable = true; } else if (data != null) { try { - Class c = Class.forName(type, false, data.getClass().getClassLoader()); + Class<?> clazz = Class.forName(type, false, data.getClass().getClassLoader()); - if (c != null && c.isInstance(data)) { + if (clazz != null && clazz.isInstance(data)) { assignable = true; } - } catch (ClassNotFoundException e) { /*ignore*/ } + } catch (ClassNotFoundException e) { /* Ignore. */ } } return assignable; @@ -213,16 +229,17 @@ public void dataLabelChanged(Data data, String label) {} public void dataRemoved(Data data) {} - private Object getService(Class clas) { - ServiceReference ref = bContext.getServiceReference(clas.getName()); - if (ref != null) { - return bContext.getService(ref); + private Object getService(Class<?> clazz) { + ServiceReference serviceReference = bundleContext.getServiceReference(clazz.getName()); + + if (serviceReference != null) { + return bundleContext.getService(serviceReference); } return null; } public ServiceReference getServiceReference(){ - return ref; + return this.serviceReference; } } \ No newline at end of file Modified: trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmWrapper.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmWrapper.java 2010-08-05 19:44:46 UTC (rev 1118) +++ trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmWrapper.java 2010-08-05 19:47:42 UTC (rev 1119) @@ -52,26 +52,26 @@ import org.osgi.service.metatype.MetaTypeService; import org.osgi.service.metatype.ObjectClassDefinition; -public class AlgorithmWrapper - implements Algorithm, AlgorithmProperty, ProgressTrackable { - protected ServiceReference ref; - protected BundleContext bContext; - protected CIShellContext ciContext; +public class AlgorithmWrapper implements Algorithm, AlgorithmProperty, ProgressTrackable { + protected ServiceReference serviceReference; + protected BundleContext bundleContext; + protected CIShellContext ciShellContext; protected Data[] originalData; protected Data[] data; protected Converter[][] converters; protected ProgressMonitor progressMonitor; protected Algorithm algorithm; - public AlgorithmWrapper(ServiceReference ref, - BundleContext bContext, - CIShellContext ciContext, - Data[] originalData, - Data[] data, - Converter[][] converters) { - this.ref = ref; - this.bContext = bContext; - this.ciContext = ciContext; + public AlgorithmWrapper( + ServiceReference serviceReference, + BundleContext bundleContext, + CIShellContext ciShellContext, + Data[] originalData, + Data[] data, + Converter[][] converters) { + this.serviceReference = serviceReference; + this.bundleContext = bundleContext; + this.ciShellContext = ciShellContext; this.originalData = originalData; this.data = data; this.converters = converters; @@ -83,45 +83,45 @@ */ public Data[] execute() { try { - AlgorithmFactory factory = getAlgorithmFactory(bContext, ref); - + AlgorithmFactory factory = getAlgorithmFactory(bundleContext, serviceReference); + if (factory == null) { return null; } - - String pid = (String)ref.getProperty(Constants.SERVICE_PID); + String pid = (String) serviceReference.getProperty(Constants.SERVICE_PID); + // Convert input data to the correct format. - boolean conversionSuccessful = - tryConvertingDataToRequiredFormat(data, converters); - + boolean conversionSuccessful = tryConvertingDataToRequiredFormat(data, converters); + if (!conversionSuccessful) { return null; } boolean inputIsValid = testDataValidityIfPossible(factory, data); - if (!inputIsValid) + if (!inputIsValid) { return null; + } // Create algorithm parameters. - String metatype_pid = getMetaTypeID(ref); + String metatypePID = getMetaTypeID(serviceReference); MetaTypeProvider provider = - getPossiblyMutatedMetaTypeProvider(metatype_pid, pid, factory); + getPossiblyMutatedMetaTypeProvider(metatypePID, pid, factory); - Dictionary parameters = - getUserEnteredParameters(metatype_pid, provider); + Dictionary<String, Object> parameters = + getUserEnteredParameters(metatypePID, provider); // Check to see if the user cancelled the operation. if (parameters == null) { return null; } - printParameters(metatype_pid, provider, parameters); + printParameters(metatypePID, provider, parameters); // Create the algorithm. - algorithm = createAlgorithm(factory, data, parameters, ciContext); + algorithm = createAlgorithm(factory, data, parameters, ciShellContext); if (algorithm == null) { return null; @@ -143,11 +143,11 @@ return outData; } catch (Exception e) { - GUIBuilderService builder = (GUIBuilderService)ciContext.getService + GUIBuilderService builder = (GUIBuilderService)ciShellContext.getService (GUIBuilderService.class.getName()); String errorMessage = "An error occurred while preparing to run " - + "the algorithm \"" + ref.getProperty(AlgorithmProperty.LABEL) + + "the algorithm \"" + serviceReference.getProperty(AlgorithmProperty.LABEL) + ".\""; builder.showError("Error!", errorMessage, e); @@ -157,50 +157,56 @@ return null; } - protected AlgorithmFactory getAlgorithmFactory(BundleContext bContext, - ServiceReference ref) { + protected AlgorithmFactory getAlgorithmFactory( + BundleContext bundleContext, ServiceReference serviceReference) { AlgorithmFactory algorithmFactory = - (AlgorithmFactory) bContext.getService(ref); + (AlgorithmFactory) bundleContext.getService(serviceReference); + if (algorithmFactory == null) { String errorMessage = "Could not create AlgorithmFactory for the algorithm " - + "\"" + ref.getProperty(AlgorithmProperty.LABEL) + "\"."; + + "\"" + serviceReference.getProperty(AlgorithmProperty.LABEL) + "\"."; String details = "The algorithm's pid was \"" - + ref.getProperty(Constants.SERVICE_PID) + + serviceReference.getProperty(Constants.SERVICE_PID) + "\" (potentially useful for debugging purposes)."; - GUIBuilderService builder = (GUIBuilderService) - ciContext.getService(GUIBuilderService.class.getName()); + GUIBuilderService builder = + (GUIBuilderService) ciShellContext.getService(GUIBuilderService.class.getName()); builder.showError("Error!", errorMessage, details); - log(LogService.LOG_ERROR, errorMessage); + this.logger(LogService.LOG_ERROR, errorMessage); } return algorithmFactory; } - protected Algorithm createAlgorithm(AlgorithmFactory factory, - Data[] data, - Dictionary parameters, - CIShellContext ciContext) { + protected Algorithm createAlgorithm( + AlgorithmFactory factory, + Data[] data, + Dictionary<String, Object> parameters, + CIShellContext ciContext) { + // TODO: Call on algorithm invocation service here. try { return factory.createAlgorithm(data, parameters, ciContext); } catch (Exception e) { String errorMessage = - "Unexpected error occurred while creating algorithm " + " \"" - + ref.getProperty(AlgorithmProperty.LABEL) + ".\""; - GUIBuilderService builder = (GUIBuilderService) - ciContext.getService(GUIBuilderService.class.getName()); + "Unexpected error occurred while creating algorithm " + " \"" + + serviceReference.getProperty(AlgorithmProperty.LABEL) + ".\""; + GUIBuilderService builder = + (GUIBuilderService) ciContext.getService(GUIBuilderService.class.getName()); + // TODO: This is where uncaught exceptions are displayed. builder.showError("Error!", errorMessage, e); log(LogService.LOG_ERROR, errorMessage, e); + return null; } } protected Data[] removeNullData(Data[] outData) { if (outData != null) { - List goodData = new ArrayList(); - for (int i = 0; i < outData.length; i++) { - if (outData[i] != null) { - goodData.add(outData[i]); + List<Data> goodData = new ArrayList<Data>(); + + for (Data data : outData) { + if (data != null) { + goodData.add(data); } } @@ -213,8 +219,8 @@ protected void addDataToDataManager(Data[] outData) { if (outData != null) { DataManagerService dataManager = (DataManagerService) - bContext.getService( - bContext.getServiceReference( + bundleContext.getService( + bundleContext.getServiceReference( DataManagerService.class.getName())); if (outData.length != 0) { @@ -234,15 +240,15 @@ outData = algorithm.execute(); } catch (AlgorithmExecutionException e) { log(LogService.LOG_ERROR, "The Algorithm: \"" - + ref.getProperty(AlgorithmProperty.LABEL) + + serviceReference.getProperty(AlgorithmProperty.LABEL) + "\" had an error while executing: " + e.getMessage(), e); } catch (RuntimeException e) { GUIBuilderService builder = (GUIBuilderService) - ciContext.getService(GUIBuilderService.class.getName()); + ciShellContext.getService(GUIBuilderService.class.getName()); builder.showError("Error!", "An unexpected exception occurred while " + "executing \"" - + ref.getProperty(AlgorithmProperty.LABEL) + ".\"", e); + + serviceReference.getProperty(AlgorithmProperty.LABEL) + ".\"", e); } return outData; @@ -262,7 +268,7 @@ } if (data[i] == null && i < (data.length - 1)) { - log(LogService.LOG_ERROR, "The converter: " + logger(LogService.LOG_ERROR, "The converter: " + converters[i].getClass().getName() + " returned a null result where data was " + "expected when converting the data to give " @@ -282,12 +288,12 @@ String validation = ((DataValidator) factory).validate(data); if (validation != null && validation.length() > 0) { - String label = (String) ref.getProperty(LABEL); + String label = (String) serviceReference.getProperty(LABEL); if (label == null) { label = "Algorithm"; } - log(LogService.LOG_ERROR, + logger(LogService.LOG_ERROR, "INVALID DATA: The data given to \"" + label + "\" is incompatible for this reason: " + validation); return false; @@ -314,7 +320,7 @@ MetaTypeService metaTypeService = (MetaTypeService) Activator.getService(MetaTypeService.class.getName()); if (metaTypeService != null) { - provider = metaTypeService.getMetaTypeInformation(ref.getBundle()); + provider = metaTypeService.getMetaTypeInformation(serviceReference.getBundle()); } if (factory instanceof ParameterMutator && provider != null) { @@ -333,7 +339,7 @@ } if (provider != null) { - provider = wrapProvider(ref, provider); + provider = wrapProvider(serviceReference, provider); } return provider; @@ -345,14 +351,16 @@ } } - protected Dictionary getUserEnteredParameters(String metatype_pid, - MetaTypeProvider provider) { - Dictionary parameters = new Hashtable(); + protected Dictionary<String, Object> getUserEnteredParameters( + String metatypePID, MetaTypeProvider provider) { + Dictionary<String, Object> parameters = new Hashtable<String, Object>(); + if (provider != null) { - GUIBuilderService builder = (GUIBuilderService) - ciContext.getService(GUIBuilderService.class.getName()); + GUIBuilderService builder = + (GUIBuilderService) ciShellContext.getService(GUIBuilderService.class.getName()); - parameters = builder.createGUIandWait(metatype_pid, provider); + // TODO: Make builder.createGUIAndWait return a Dictionary<String, Object>. + parameters = builder.createGUIandWait(metatypePID, provider); } return parameters; @@ -399,9 +407,9 @@ UserPrefsProperty.PUBLISH_PARAM_DEFAULT_PREFS_VALUE); } - protected void log(int logLevel, String message) { + protected void logger(int logLevel, String message) { LogService log = - (LogService) ciContext.getService(LogService.class.getName()); + (LogService) ciShellContext.getService(LogService.class.getName()); if (log != null) { log.log(logLevel, message); } else { @@ -411,7 +419,7 @@ protected void log(int logLevel, String message, Throwable exception) { LogService log = - (LogService) ciContext.getService(LogService.class.getName()); + (LogService) ciShellContext.getService(LogService.class.getName()); if (log != null) { log.log(logLevel, message, exception); } else { @@ -496,7 +504,7 @@ } // Check and act on parentage settings - String parentage = (String) ref.getProperty("parentage"); + String parentage = (String) serviceReference.getProperty("parentage"); if (parentage != null) { parentage = parentage.trim(); if (parentage.equalsIgnoreCase("default")) { @@ -519,12 +527,12 @@ private LogService getLogService() { ServiceReference serviceReference = - bContext.getServiceReference(DataManagerService.class.getName()); + bundleContext.getServiceReference(DataManagerService.class.getName()); LogService log = null; if (serviceReference != null) { - log = (LogService) bContext.getService( - bContext.getServiceReference(LogService.class.getName())); + log = (LogService) bundleContext.getService( + bundleContext.getServiceReference(LogService.class.getName())); } return log; @@ -532,12 +540,12 @@ private ConfigurationAdmin getConfigurationAdmin() { ServiceReference serviceReference = - bContext.getServiceReference(ConfigurationAdmin.class.getName()); + bundleContext.getServiceReference(ConfigurationAdmin.class.getName()); ConfigurationAdmin ca = null; if (serviceReference != null) { - ca = (ConfigurationAdmin) bContext.getService( - bContext.getServiceReference( + ca = (ConfigurationAdmin) bundleContext.getService( + bundleContext.getServiceReference( ConfigurationAdmin.class.getName())); } @@ -545,7 +553,7 @@ } private void logNullOCDWarning(String pid, String metatype_pid) { - this.log(LogService.LOG_WARNING, + this.logger(LogService.LOG_WARNING, "Warning: could not get object class definition '" + metatype_pid + "' from the algorithm '" + pid + "'"); } Modified: trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/MenuAdapter.java =================================================================== --- trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/MenuAdapter.java 2010-08-05 19:44:46 UTC (rev 1118) +++ trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/MenuAdapter.java 2010-08-05 19:47:42 UTC (rev 1119) @@ -89,7 +89,7 @@ /* * This is the exactly same copy of pidsToServiceReferences. * Since some plug-ins could display on menu more than once, it provides a map between a pid - * and a ref while in pidsToServiceReferences that pid has been removed. + * and a serviceReference while in pidsToServiceReferences that pid has been removed. */ private Map pidsToServiceReferencesCopy; private Document documentObjectModel; @@ -462,7 +462,7 @@ } } - private void initializeMenu() throws InvalidSyntaxException{ + private void initializeMenu() throws InvalidSyntaxException { ServiceReference[] serviceReferences = this.bundleContext.getAllServiceReferences( AlgorithmFactory.class.getName(), null); @@ -493,7 +493,7 @@ } private void makeMenuItem(ServiceReference serviceReference) { - String path = (String)serviceReference.getProperty(MENU_PATH); + String path = (String) serviceReference.getProperty(MENU_PATH); String[] items = null; if (path != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |