protected void executeSelectedAlgorithmIteratively(final int iParameterToIterateOver) { try { if (m_Alg != null) { final GeoAlgorithm alg = m_Alg.getNewInstance(); final int iRet = SextanteGUI.getGUIFactory().showAlgorithmDialog(alg, m_ParentDialog.getDialog(), null); if (iRet == IGUIFactory.OK) { final ParametersSet params = m_Alg.getParameters(); final int iParamCount = params.getNumberOfParameters(); int iVectorLayers = 0; for (int i = 0; i < iParamCount; i++) { final Parameter param = m_Alg.getParameters().getParameter(i); if (param instanceof ParameterVectorLayer) { if (iVectorLayers == iParameterToIterateOver) { GeoAlgorithmExecutors.executeIterative( alg, m_ParentDialog.getDialog(), param.getParameterName()); break; } iVectorLayers++; } } } } } catch (final Exception e) { Sextante.addErrorToLog(e); } }
protected void setOutputRendering() { if (m_Alg != null) { final OutputRenderingSettingsDialog dialog = new OutputRenderingSettingsDialog(m_Alg); dialog.setVisible(true); final HashMap<String, Object> set = dialog.getSettings(); if (set != null) { SextanteGUI.getDataRenderer().setRenderingForAlgorithm(m_Alg.getCommandLineName(), set); SextanteGUI.getDataRenderer().save(); } } }
protected void executeSelectedAlgorithm() { try { if (m_Alg != null) { final GeoAlgorithm alg = m_Alg.getNewInstance(); final int iRet = SextanteGUI.getGUIFactory().showAlgorithmDialog(alg, m_ParentDialog.getDialog(), null); if (iRet == IGUIFactory.OK) { final String[] cmd = alg.getAlgorithmAsCommandLineSentences(); if (cmd != null) { History.addToHistory(cmd); } GeoAlgorithmExecutors.execute(alg, m_ParentDialog.getDialog()); updateListOfMostRecentAlgorithms(); } } else if (m_Action != null) { m_Action.execute(); } } catch (final Exception e) { Sextante.addErrorToLog(e); } }
/** * Returns true if the help file associated with an algorithm contains a given search string * * @param alg the GeoAlgorithm * @param string a string to search * @return true if the help file associated with the algorithm contains the search string */ public static boolean containsStringInHelpFile(final GeoAlgorithm alg, final String string) { String line; BufferedReader br = null; InputStreamReader is = null; FileInputStream fis = null; final String sName = alg.getName().toLowerCase(); if (sName.indexOf(string) != -1) { return true; } try { final String sFilename = SextanteGUI.getAlgorithmHelpFilename(alg, false); fis = new FileInputStream(sFilename); is = new InputStreamReader(fis); br = new BufferedReader(is); while (null != (line = br.readLine())) { line = line.toLowerCase(); if (line.indexOf(string) != -1) { br.close(); is.close(); fis.close(); return true; } } } catch (final Exception e) { // Sextante.addErrorToLog(e); } finally { try { br.close(); is.close(); fis.close(); } catch (final Exception e) { } } return false; }
/** * Returns the path where help files for a given algorithm are found * * @param alg the GeoAlgorithm * @param bForceCurrentLocale if true, returns the path to the current locale, even if it does not * exist. If false, it will return the path corresponding to the default locale (english) in * case the one corresponding to the current locale does not exist. * @return the help path for this algorithm */ public static String getHelpPath(final GeoAlgorithm alg, final boolean bForceLocale) { String sPackage = alg.getClass().getPackage().toString(); sPackage = sPackage.substring(8); String sPath = SextanteGUI.getHelpPath() + File.separator + Locale.getDefault().getLanguage() + File.separator + sPackage; final File dir = new File(sPath); if (!dir.exists() && !bForceLocale) { sPath = SextanteGUI.getHelpPath() + File.separator + Locale.ENGLISH.getLanguage() + File.separator + sPackage; } return sPath; }
/** * Fills the tree with the algorithms that match a search criteria * * @param sSearchString The search string to look for in the algorithms context help * @param bSearchInFiles true if it should search in help files. if false, it will only search in * algorithm names * @return the number of algorithms that match the given criteria */ public int fillTree(final String sSearchString, final boolean bSearchInHelpFiles) { m_sLastSearchString = sSearchString; m_bLastSearchIncludedHelpFiles = bSearchInHelpFiles; int iCount = 0; String sGroup, sSubgroup; final DefaultMutableTreeNode mainNode = new DefaultMutableTreeNode(Sextante.getText("Algorithms")); final HashMap<Object, HashMap<String, HashMap<String, DefaultMutableTreeNode>>> groups = new HashMap<Object, HashMap<String, HashMap<String, DefaultMutableTreeNode>>>(); setCursor(new Cursor(Cursor.WAIT_CURSOR)); // algorithms final HashMap<String, HashMap<String, GeoAlgorithm>> algs = Sextante.getAlgorithms(); final Set<String> groupKeys = algs.keySet(); final Iterator<String> groupIter = groupKeys.iterator(); while (groupIter.hasNext()) { final String groupKey = groupIter.next(); final HashMap<String, GeoAlgorithm> groupAlgs = algs.get(groupKey); final Set keys = groupAlgs.keySet(); final Iterator iter = keys.iterator(); while (iter.hasNext()) { final GeoAlgorithm alg = groupAlgs.get(iter.next()); if (m_Filter.accept(alg)) { if (bSearchInHelpFiles) { if (!HelpIO.containsStringInHelpFile(alg, sSearchString)) { continue; } } else { if ((sSearchString != null) && !alg.getName().toLowerCase().contains(sSearchString)) { continue; } } iCount++; final AlgorithmGroupConfiguration conf = AlgorithmGroupsOrganizer.getGroupConfiguration(alg); if (conf != null) { if (!conf.isShow()) { continue; } sGroup = conf.getGroup(); sSubgroup = conf.getSubgroup(); } else { sGroup = groupKey; sSubgroup = alg.getGroup(); } HashMap<String, HashMap<String, DefaultMutableTreeNode>> group = groups.get(sGroup); if (group == null) { group = new HashMap<String, HashMap<String, DefaultMutableTreeNode>>(); groups.put(sGroup, group); } HashMap<String, DefaultMutableTreeNode> subgroup = group.get(sSubgroup); if (subgroup == null) { subgroup = new HashMap<String, DefaultMutableTreeNode>(); group.put(sSubgroup, subgroup); } subgroup.put(alg.getName(), new DefaultMutableTreeNode(alg)); } } } // toolbox actions final HashMap<NameAndIcon, ArrayList<ToolboxAction>> allActions = SextanteGUI.getToolboxActions(); final Set<NameAndIcon> actionsKeys = allActions.keySet(); final Iterator<NameAndIcon> actionsIter = actionsKeys.iterator(); while (actionsIter.hasNext()) { final NameAndIcon nai = actionsIter.next(); final ArrayList<ToolboxAction> actions = allActions.get(nai); for (int i = 0; i < actions.size(); i++) { final ToolboxAction ita = actions.get(i); if ((sSearchString != null) && !ita.getName().toLowerCase().contains(sSearchString)) { continue; } iCount++; sSubgroup = ita.getGroup(); HashMap<String, HashMap<String, DefaultMutableTreeNode>> group = groups.get(nai.getName()); if (group == null) { group = groups.get(nai); } if (group == null) { group = new HashMap<String, HashMap<String, DefaultMutableTreeNode>>(); groups.put(nai, group); } HashMap<String, DefaultMutableTreeNode> subgroup = group.get(sSubgroup); if (subgroup == null) { subgroup = new HashMap<String, DefaultMutableTreeNode>(); group.put(sSubgroup, subgroup); } subgroup.put(ita.getName(), new DefaultMutableTreeNode(ita)); } } final Set<Object> set = groups.keySet(); final Iterator<Object> iter = set.iterator(); while (iter.hasNext()) { final Object key = iter.next(); final DefaultMutableTreeNode node = new DefaultMutableTreeNode(key); addNodeInSortedOrder(mainNode, node); final HashMap<String, HashMap<String, DefaultMutableTreeNode>> g = groups.get(key); final Set<String> set2 = g.keySet(); final Iterator<String> iter2 = set2.iterator(); while (iter2.hasNext()) { final String sKey2 = iter2.next(); final DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(sKey2); addNodeInSortedOrder(node, node2); final HashMap<String, DefaultMutableTreeNode> g2 = g.get(sKey2); final Set<String> set3 = g2.keySet(); final Iterator<String> iter3 = set3.iterator(); while (iter3.hasNext()) { final String sKey3 = iter3.next(); final DefaultMutableTreeNode node3 = g2.get(sKey3); addNodeInSortedOrder(node2, node3); } } } final boolean bShowMostRecent = new Boolean(SextanteGUI.getSettingParameterValue(SextanteGeneralSettings.SHOW_MOST_RECENT)) .booleanValue(); if (bShowMostRecent) { final GeoAlgorithm[] recent = History.getRecentlyUsedAlgs(); final DefaultMutableTreeNode recentNode = new DefaultMutableTreeNode(Sextante.getText("RecentAlgorithms")); for (int i = 0; i < recent.length; i++) { final DefaultMutableTreeNode node = new DefaultMutableTreeNode(recent[i]); recentNode.add(node); } mainNode.insert(recentNode, 0); } setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); jTree.setModel(new DefaultTreeModel(mainNode)); if (sSearchString != null) { expandAll(); } m_ParentDialog.setAlgorithmsCount(iCount); return iCount; }
private void updateSelectedAlgorithm() { m_Alg = null; m_Action = null; if (m_Path != null) { menuItemExecuteAsBatch.setVisible(false); menuItemExecuteAsBatchFromGIS.setVisible(false); menuItemExecute.setVisible(false); menuItemShowHelp.setVisible(false); for (int i = 0; i < menuItemExecuteIterative.length; i++) { menuItemExecuteIterative[i].setVisible(false); } final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent(); if (!(node.getUserObject() instanceof GeoAlgorithm)) { if ((node.getUserObject() instanceof ToolboxAction)) { m_Action = (ToolboxAction) node.getUserObject(); } return; } m_Alg = ((GeoAlgorithm) node.getUserObject()); menuItemExecuteAsBatch.setVisible(true); menuItemShowHelp.setVisible(true); menuItemExecute.setVisible(true); final Object[] objs = SextanteGUI.getInputFactory().getDataObjects(); menuItemExecuteAsBatchFromGIS.setVisible(true); final boolean bMeets = m_Alg.meetsDataRequirements(objs); menuItemExecuteAsBatchFromGIS.setEnabled(bMeets); menuItemExecute.setEnabled(bMeets); final IToolboxRightButtonAction[] actions = SextanteGUI.getToolboxRightButtonActions(); for (int i = 0; i < menuItemToolboxAction.length; i++) { menuItemToolboxAction[i].setVisible(actions[i].canBeExecutedOnAlgorithm(m_Alg)); } if (bMeets && m_Alg.requiresIndividualVectorLayers()) { m_iExecuteIterativeMenuCount = 0; final int iParams = m_Alg.getParameters().getNumberOfParameters(); for (int i = 0; i < iParams; i++) { final Parameter param = m_Alg.getParameters().getParameter(i); if (param instanceof ParameterVectorLayer) { final ParameterVectorLayer pvl = (ParameterVectorLayer) param; boolean bMandatory; try { bMandatory = ((AdditionalInfoVectorLayer) pvl.getParameterAdditionalInfo()).getIsMandatory(); if (bMandatory) { menuItemExecuteIterative[m_iExecuteIterativeMenuCount].setText( Sextante.getText("Run_iterative") + " (" + param.getParameterDescription() + ")"); menuItemExecuteIterative[m_iExecuteIterativeMenuCount].setVisible(true); m_iExecuteIterativeMenuCount++; } } catch (final NullParameterAdditionalInfoException e) { } } } } } }
/** * Returns the help associated with a given geoalgorithm as a html-formatted string * * @param alg the geoalgorithm * @param sFilename the filename where help for the passed algorithm is stored * @return a html-formatted string with help for the given algorithm */ public static String getHelpAsHTMLCode(final GeoAlgorithm alg, final String sFilename) { HelpElement element; final ArrayList list = open(sFilename); HashMap elements; final String sPath = "file:///" + sFilename.substring(0, sFilename.lastIndexOf(File.separator)) + File.separator; if (list != null) { elements = createMap(list); } else { elements = new HashMap(); } final HTMLDoc doc = new HTMLDoc(); doc.open(alg.getName()); doc.addHeader(Sextante.getText(alg.getName()), 1); doc.addHeader(Sextante.getText("Description"), 2); element = (HelpElement) elements.get("DESCRIPTION"); if (element != null) { doc.addParagraph(element.getTextAsFormattedHTML()); for (int j = 0; j < element.getImages().size(); j++) { final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j); doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription()); } } doc.addHeader(Sextante.getText("Parameters"), 2); final ParametersSet params = alg.getParameters(); doc.startUnorderedList(); for (int i = 0; i < params.getNumberOfParameters(); i++) { final Parameter param = params.getParameter(i); String sParam = param.getParameterDescription(); sParam = "<b>" + sParam + "[" + getParameterTypeName(param) + "]: </b>"; element = (HelpElement) elements.get(param.getParameterName()); if (element != null) { sParam = sParam + element.getTextAsFormattedHTML(); } doc.addListElement(sParam); if (element != null) { for (int j = 0; j < element.getImages().size(); j++) { final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j); doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription()); } } } doc.closeUnorderedList(); doc.addHeader(Sextante.getText("Outputs"), 2); element = (HelpElement) elements.get("OUTPUT_DESCRIPTION"); if (element != null) { doc.addParagraph(element.getTextAsFormattedHTML()); for (int j = 0; j < element.getImages().size(); j++) { final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j); doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription()); } } doc.startUnorderedList(); final OutputObjectsSet oo = alg.getOutputObjects(); String sOutputType = ""; for (int i = 0; i < oo.getOutputObjectsCount(); i++) { final Output out = oo.getOutput(i); String sOutput = out.getDescription(); if (out instanceof OutputRasterLayer) { sOutputType = Sextante.getText("Raster_Layer"); } else if (out instanceof Output3DRasterLayer) { sOutputType = Sextante.getText("3D_Raster_layer"); } else if (out instanceof OutputVectorLayer) { sOutputType = Sextante.getText("Vector_Layer"); final OutputVectorLayer ovl = (OutputVectorLayer) out; switch (ovl.getShapeType()) { case OutputVectorLayer.SHAPE_TYPE_UNDEFINED: default: sOutputType = sOutputType + " - " + Sextante.getText("Any_type"); break; case OutputVectorLayer.SHAPE_TYPE_LINE: sOutputType = sOutputType + " - " + Sextante.getText("Line"); break; case OutputVectorLayer.SHAPE_TYPE_POLYGON: sOutputType = sOutputType + " - " + Sextante.getText("Polygon"); break; case OutputVectorLayer.SHAPE_TYPE_POINT: sOutputType = sOutputType + " - " + Sextante.getText("Point"); break; } } else if (out instanceof OutputTable) { sOutputType = Sextante.getText("Table"); } else if (out instanceof OutputChart) { sOutputType = Sextante.getText("graph-chart"); } else if (out instanceof OutputText) { sOutputType = Sextante.getText("Text"); } else if (out instanceof OutputNumericalValue) { sOutputType = Sextante.getText("Numerical_value"); } sOutput = "<b>" + sOutput + "[" + sOutputType + "]: </b>"; element = (HelpElement) elements.get(out.getName()); if (element != null) { sOutput = sOutput + element.getTextAsFormattedHTML(); } doc.addListElement(sOutput); if (element != null) { for (int j = 0; j < element.getImages().size(); j++) { final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j); doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription()); } } } doc.closeUnorderedList(); doc.addHeader(Sextante.getText("Additional_information"), 2); element = (HelpElement) elements.get("ADDITIONAL_INFO"); if (element != null) { doc.addParagraph(element.getTextAsFormattedHTML()); for (int j = 0; j < element.getImages().size(); j++) { final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j); doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription()); } } doc.addHeader(Sextante.getText("Command_line"), 2); String sText = alg.getCommandLineHelp(); sText = sText.replaceAll("\n", "<br>"); sText = sText.replace(" ", "   "); doc.addCourierText(sText); doc.addParagraph(""); element = (HelpElement) elements.get("EXTENSION_AUTHOR"); if (element != null) { doc.addParagraph( "<i>" + Sextante.getText("Algorithm_created_by") + " " + element.getText() + "</i>"); for (int j = 0; j < element.getImages().size(); j++) { final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j); doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription()); } } element = (HelpElement) elements.get("HELP_AUTHOR"); if (element != null) { doc.addParagraph( "<i>" + Sextante.getText("Help_file_created_by") + " " + element.getText() + "</i>"); for (int j = 0; j < element.getImages().size(); j++) { final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j); doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription()); } } doc.close(); return doc.getHTMLCode(); }