From: <bh...@us...> - 2008-03-27 18:35:41
|
Revision: 729 http://cishell.svn.sourceforge.net/cishell/?rev=729&view=rev Author: bh2 Date: 2008-03-27 11:35:36 -0700 (Thu, 27 Mar 2008) Log Message: ----------- Updated menumanager, cleaning up a bit, and better handling exceptions. Modified Paths: -------------- 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.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 2008-03-27 17:46:12 UTC (rev 728) +++ trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmAction.java 2008-03-27 18:35:36 UTC (rev 729) @@ -72,8 +72,6 @@ dataSelected(dataManager.getSelectedData()); } - - public void run() { //hmm... should probably change this.. maybe use the scheduler... new Thread("Menu Item Runner") { @@ -92,13 +90,9 @@ bContext.getService(bContext.getServiceReference( SchedulerService.class.getName())); - printAlgorithmInformation(); - - - - - scheduler.schedule(new AlgorithmWrapper(ref, bContext, ciContext, originalData, data, converters), ref); + + scheduler.schedule(new AlgorithmWrapper(ref, bContext, ciContext, originalData, data, converters), ref); } catch (Throwable e) { e.printStackTrace(); } @@ -110,10 +104,9 @@ StringBuffer acknowledgement = new StringBuffer(); String label = (String)ref.getProperty(LABEL); if (label != null){ - acknowledgement.append("..........\n"+ - label+" was selected.\n"); + acknowledgement.append("..........\n"+label+" was selected.\n"); } - String authors = (String)ref.getProperty("authors"); + String authors = (String)ref.getProperty(AUTHORS); if (authors != null) acknowledgement.append("Author(s): "+authors+"\n"); String implementers = (String)ref.getProperty(IMPLEMENTERS); @@ -133,8 +126,7 @@ if (docu != null) acknowledgement.append("Documentation: "+docu+"\n"); if(acknowledgement.length()>1) - logger.log(LogService.LOG_INFO, acknowledgement.toString()); - + logger.log(LogService.LOG_INFO, acknowledgement.toString()); } public void dataSelected(Data[] selectedData) { @@ -193,34 +185,6 @@ setEnabled(data != null); //&& isValid()); } - //This method will be disabled until we can find a better solution - //for extra validation beyond input/output checking -/* private boolean isValid() { - String valid = null; - String[] classes = (String[]) ref.getProperty(Constants.OBJECTCLASS); - - if (classes != null && data != null) { - for (int i=0; i < classes.length; i++) { - if (classes[i].equals(DataValidator.class.getName())) { - DataValidator validator = (DataValidator) bContext.getService(ref); - - //FIXME: Could cause concurrency problems... - for (int j=0; j < data.length; j++) { - if (converters[j] != null && converters[j].length > 0) { - //does not work for large inputs... - data[j] = converters[j][0].convert(data[j]); - converters[j] = null; - } - } - - valid = validator.validate(data); - } - } - } - - return valid == null || valid.length() == 0; - } -*/ private boolean isAsignableFrom(String type, Data datum) { Object data = datum.getData(); boolean assignable = false; @@ -247,6 +211,4 @@ public ServiceReference getServiceReference(){ return ref; } - - } \ 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 2008-03-27 17:46:12 UTC (rev 728) +++ trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/AlgorithmWrapper.java 2008-03-27 18:35:36 UTC (rev 729) @@ -24,6 +24,7 @@ import org.cishell.app.service.datamanager.DataManagerService; import org.cishell.framework.CIShellContext; import org.cishell.framework.algorithm.Algorithm; +import org.cishell.framework.algorithm.AlgorithmExecutionException; import org.cishell.framework.algorithm.AlgorithmFactory; import org.cishell.framework.algorithm.AlgorithmProperty; import org.cishell.framework.algorithm.DataValidator; @@ -34,6 +35,7 @@ import org.cishell.framework.data.DataProperty; import org.cishell.reference.gui.menumanager.Activator; import org.cishell.reference.service.metatype.BasicMetaTypeProvider; +import org.cishell.service.conversion.ConversionException; import org.cishell.service.conversion.Converter; import org.cishell.service.guibuilder.GUIBuilderService; import org.osgi.framework.BundleContext; @@ -70,128 +72,133 @@ this.converters = converters; this.idToLabelMap = new HashMap(); - this.progressMonitor = null; - - + this.progressMonitor = null; } /** * @see org.cishell.framework.algorithm.Algorithm#execute() */ public Data[] execute() { - try { - for (int i=0; i < data.length; i++) { - if (converters[i] != null) { - data[i] = converters[i][0].convert(data[i]); - - if (data[i] == null && i < (data.length - 1)) { - Exception e = - new Exception("The converter " + - converters[i].getClass().getName() + - " returned a null result where data was expected."); - throw e; - } - converters[i] = null; - } - } - - GUIBuilderService builder = (GUIBuilderService) - ciContext.getService(GUIBuilderService.class.getName()); - - AlgorithmFactory factory = (AlgorithmFactory) bContext.getService(ref); - - if (factory instanceof DataValidator) { - String validation = ((DataValidator) factory).validate(data); - - if (validation != null && validation.length() > 0) { - String label = (String) ref.getProperty(LABEL); - if (label == null) { - label = "Algorithm"; - } - - builder.showError("Invalid Data", "The data given to \""+label+"\" is incompatible for this reason: "+validation , (String) null); + for (int i=0; i < data.length; i++) { + if (converters[i] != null) { + try { + data[i] = converters[i][0].convert(data[i]); + } catch (ConversionException e) { + log(LogService.LOG_ERROR,"The conversion of data to give" + + " the algorithm failed for this reason: "+e.getMessage(), e); return null; } - } - - String pid = (String)ref.getProperty(Constants.SERVICE_PID); - - String metatype_pid = (String) ref.getProperty(PARAMETERS_PID); - if (metatype_pid == null) { - metatype_pid = pid; - } - this.provider = null; - - MetaTypeService metaTypeService = (MetaTypeService) Activator.getService(MetaTypeService.class.getName()); - if (metaTypeService != null) { - provider = metaTypeService.getMetaTypeInformation(ref.getBundle()); + if (data[i] == null && i < (data.length - 1)) { + log(LogService.LOG_ERROR, "The converter: " + + converters[i].getClass().getName() + + " returned a null result where data was expected when" + + " converting the data to give the algorithm."); + return null; + } + converters[i] = null; } + } + + GUIBuilderService builder = (GUIBuilderService) + ciContext.getService(GUIBuilderService.class.getName()); + + AlgorithmFactory factory = (AlgorithmFactory) bContext.getService(ref); + + if (factory instanceof DataValidator) { + String validation = ((DataValidator) factory).validate(data); + + if (validation != null && validation.length() > 0) { + String label = (String) ref.getProperty(LABEL); + if (label == null) { + label = "Algorithm"; + } + + builder.showError("Invalid Data", "The data given to \""+label+"\" is incompatible for this reason: "+validation , (String) null); + return null; + } + } + + String pid = (String)ref.getProperty(Constants.SERVICE_PID); + + String metatype_pid = (String) ref.getProperty(PARAMETERS_PID); + if (metatype_pid == null) { + metatype_pid = pid; + } - if (factory instanceof ParameterMutator && provider != null) { - try { - ObjectClassDefinition ocd = provider.getObjectClassDefinition(metatype_pid, null); - - ocd = ((ParameterMutator) factory).mutateParameters(data, ocd); - - if (ocd != null) { - provider = new BasicMetaTypeProvider(ocd); - } - } catch (IllegalArgumentException e) { - log(LogService.LOG_DEBUG, pid+" has an invalid metatype id: "+metatype_pid); + this.provider = null; + + MetaTypeService metaTypeService = (MetaTypeService) Activator.getService(MetaTypeService.class.getName()); + if (metaTypeService != null) { + provider = metaTypeService.getMetaTypeInformation(ref.getBundle()); + } + + if (factory instanceof ParameterMutator && provider != null) { + try { + ObjectClassDefinition ocd = provider.getObjectClassDefinition(metatype_pid, null); + + ocd = ((ParameterMutator) factory).mutateParameters(data, ocd); + + if (ocd != null) { + provider = new BasicMetaTypeProvider(ocd); } - } + } catch (IllegalArgumentException e) { + log(LogService.LOG_DEBUG, pid+" has an invalid metatype id: "+metatype_pid); + } + } + + this.parameters = new Hashtable(); + if (provider != null) { + this.parameters = builder.createGUIandWait(metatype_pid, provider); + } + + //check to see if the user cancelled the operation + if(this.parameters == null) { + return null; + } + + algorithm = factory.createAlgorithm(data, parameters, ciContext); + + printParameters(); + + if (progressMonitor != null && algorithm instanceof ProgressTrackable) { + ((ProgressTrackable)algorithm).setProgressMonitor(progressMonitor); + } + + Data[] outData = null; + try { + outData = algorithm.execute(); + } catch (AlgorithmExecutionException e) { + log(LogService.LOG_ERROR, + "The Algorithm: \""+ref.getProperty(AlgorithmProperty.LABEL)+ + "\" had an error while executing: "+e.getMessage()); + } catch (RuntimeException e) { + builder.showError("Error!", "An unexpected exception occurred while " + +"executing \""+ref.getProperty(AlgorithmProperty.LABEL)+".\"", e); + } + + if (outData != null) { + DataManagerService dataManager = (DataManagerService) + bContext.getService(bContext.getServiceReference( + DataManagerService.class.getName())); - this.parameters = new Hashtable(); - if (provider != null) { - this.parameters = builder.createGUIandWait(pid, provider); - } + doParentage(outData); - if(this.parameters == null) { - return new Data[0]; + List goodData = new ArrayList(); + for (int i=0; i < outData.length; i++) { + if (outData[i] != null) { + goodData.add(outData[i]); + } } - algorithm = factory.createAlgorithm(data, parameters, ciContext); + outData = (Data[]) goodData.toArray(new Data[0]); - printParameters(); - - if (progressMonitor != null && algorithm instanceof ProgressTrackable) { - ((ProgressTrackable)algorithm).setProgressMonitor(progressMonitor); + if (outData.length != 0) { + dataManager.setSelectedData(outData); } - - Data[] outData = algorithm.execute(); - - if (outData != null) { - DataManagerService dataManager = (DataManagerService) - bContext.getService(bContext.getServiceReference( - DataManagerService.class.getName())); - - doParentage(outData); - - List goodData = new ArrayList(); - for (int i=0; i < outData.length; i++) { - if (outData[i] != null) { - goodData.add(outData[i]); - } - } - - outData = (Data[]) goodData.toArray(new Data[0]); - - if (outData.length != 0) { - dataManager.setSelectedData(outData); - } - } - - return outData; - } catch (Throwable e) { - GUIBuilderService guiBuilder = (GUIBuilderService) - ciContext.getService(GUIBuilderService.class.getName()); - guiBuilder.showError("Error!", - "The Algorithm: \""+ref.getProperty(AlgorithmProperty.LABEL)+ - "\" had an error while executing.", e); - - return new Data[0]; } + + return outData; } protected void log(int logLevel, String message) { @@ -202,6 +209,16 @@ System.out.println(message); } } + + protected void log(int logLevel, String message, Throwable exception) { + LogService log = (LogService) Activator.getService(LogService.class.getName()); + if (log != null) { + log.log(logLevel, message, exception); + } else { + System.out.println(message); + exception.printStackTrace(); + } + } protected void printParameters() { LogService logger = getLogService(); 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 2008-03-27 17:46:12 UTC (rev 728) +++ trunk/clients/gui/org.cishell.reference.gui.menumanager/src/org/cishell/reference/gui/menumanager/menu/MenuAdapter.java 2008-03-27 18:35:36 UTC (rev 729) @@ -13,7 +13,6 @@ * ***************************************************************************/ package org.cishell.reference.gui.menumanager.menu; -//Java import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -52,10 +51,6 @@ import org.xml.sax.SAXException; -/* - * Bonnie's comments: - * weird, why implements AlgorithmProperty? It does not define any abstract interface. - */ public class MenuAdapter implements AlgorithmProperty { private IMenuManager menuBar; private Shell shell; @@ -137,13 +132,11 @@ } else{ String pid = (String)refs[i].getProperty(PRESERVED_SERVICE_PID); -// System.out.println("pid="+pid); pidToServiceReferenceMap.put(pid.toLowerCase().trim(), refs[i]); pidToServiceReferenceMapCopy.put(pid.toLowerCase().trim(), refs[i]); } } } - } /* * Parse default_menu.xml file. For each menu node, get the value of the attribut "pid" @@ -167,7 +160,6 @@ private void processTopMenu (Element topMenuNode){ MenuManager topMenuBar = null; - /* * The File and Help menus are created in ApplicationActionBarAdvisor.java @@ -217,7 +209,7 @@ processSubMenu(el, groupMenuBar); } else if (menu_type.equalsIgnoreCase(PRESERVED_BREAK)){ - //It seems that Framework automatially takes care of issues + //It seems that Framework automatically takes care of issues //such as double separators, a separator at the top or bottom parentMenuBar.add(new Separator()); } @@ -260,7 +252,6 @@ if (pid == null || pid.length()==0){ //check if the name is one of the preserved one //if so add the default action - } else{ //check if the pid has registered in pidToServiceReferenceMap @@ -292,7 +283,6 @@ //parse using builder to get DOM representation of the XML file String fullpath=System.getProperty("osgi.configuration.area") + DEFAULT_MENU_FILE_NAME; -// System.out.println(">>parse file: "+fullpath); dom = db.parse(fullpath); // printElementAttributes(dom); @@ -338,7 +328,6 @@ public void serviceChanged(ServiceEvent event) { switch (event.getType()) { case ServiceEvent.REGISTERED: -// System.out.println(">>>receive ServiceEvent.Registered"); makeMenuItem(event.getServiceReference()); break; case ServiceEvent.UNREGISTERING: @@ -367,19 +356,15 @@ menu = targetMenu.findMenuUsingPath(items[i]); if (menu == null && items[i] != null) { - menu = targetMenu.findMenuUsingPath(items[i].toLowerCase()); - + menu = targetMenu.findMenuUsingPath(items[i].toLowerCase()); } - if (menu == null) { - + if (menu == null) { menu = createMenu(items[i],items[i]); targetMenu.appendToGroup(ADDITIONS_GROUP, menu); } targetMenu = menu; - - } group = items[items.length-1]; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |