/** * Parses given String str and tries to evaluate it to a GeoImplicitPoly object. Returns null if * something went wrong. * * @param str * @boolean showErrors if false, only stacktraces are printed * @return implicit polygon or null */ public GeoElement evaluateToGeoElement(String str, boolean showErrors) { boolean oldMacroMode = cons.isSuppressLabelsActive(); cons.setSuppressLabelCreation(true); GeoElement geo = null; try { ValidExpression ve = parser.parseGeoGebraExpression(str); GeoElement[] temp = processValidExpression(ve); geo = temp[0]; } catch (CircularDefinitionException e) { Application.debug("CircularDefinition"); app.showError("CircularDefinition"); } catch (Exception e) { e.printStackTrace(); if (showErrors) app.showError("InvalidInput", str); } catch (MyError e) { e.printStackTrace(); if (showErrors) app.showError(e); } catch (Error e) { e.printStackTrace(); if (showErrors) app.showError("InvalidInput", str); } cons.setSuppressLabelCreation(oldMacroMode); return geo; }
/** * Parses given String str and tries to evaluate it to a GeoFunction Returns null if something * went wrong. Michael Borcherds 2008-04-04 */ public GeoFunction evaluateToFunction(String str, boolean suppressErrors) { boolean oldMacroMode = cons.isSuppressLabelsActive(); cons.setSuppressLabelCreation(true); GeoFunction func = null; try { ValidExpression ve = parser.parseGeoGebraExpression(str); GeoElement[] temp = processValidExpression(ve); if (temp[0].isGeoFunctionable()) { GeoFunctionable f = (GeoFunctionable) temp[0]; func = f.getGeoFunction(); } else if (!suppressErrors) app.showError("InvalidInput", str); } catch (CircularDefinitionException e) { Application.debug("CircularDefinition"); if (!suppressErrors) app.showError("CircularDefinition"); } catch (Exception e) { e.printStackTrace(); if (!suppressErrors) app.showError("InvalidInput", str); } catch (MyError e) { e.printStackTrace(); if (!suppressErrors) app.showError(e); } catch (Error e) { e.printStackTrace(); if (!suppressErrors) app.showError("InvalidInput", str); } cons.setSuppressLabelCreation(oldMacroMode); return func; }
// G.Sturr 2010-7-5 // added 'allowErrorDialog' flag to handle the case of unquoted text // entries in the spreadsheet public GeoElement[] processAlgebraCommandNoExceptionHandling( String cmd, boolean storeUndo, boolean allowErrorDialog, boolean throwMyError) throws Exception { ValidExpression ve; try { ve = parser.parseGeoGebraExpression(cmd); } catch (ParseException e) { // e.printStackTrace(); if (allowErrorDialog) { app.showError(app.getError("InvalidInput") + ":\n" + cmd); return null; } throw new MyException(app.getError("InvalidInput") + ":\n" + cmd, MyException.INVALID_INPUT); } catch (MyError e) { // e.printStackTrace(); if (allowErrorDialog) { app.showError(e.getLocalizedMessage()); return null; } throw new Exception(e.getLocalizedMessage()); } catch (Error e) { // e.printStackTrace(); if (allowErrorDialog) { app.showError(app.getError("InvalidInput") + ":\n" + cmd); return null; } throw new Exception(app.getError("InvalidInput") + ":\n" + cmd); } // process ValidExpression (built by parser) GeoElement[] geoElements = null; try { geoElements = processValidExpression(ve); if (storeUndo && geoElements != null) app.storeUndoInfo(); } catch (MyError e) { e.printStackTrace(); // throw new Exception(e.getLocalizedMessage()); // show error with nice "Show Online Help" box if (allowErrorDialog) { // G.Sturr 2010-7-5 app.showError(e); e.printStackTrace(); } else if (throwMyError) throw new MyError(app, e.getLocalizedMessage(), e.getcommandName()); return null; } catch (CircularDefinitionException e) { Application.debug("CircularDefinition"); throw e; } catch (Exception ex) { Application.debug("Exception"); ex.printStackTrace(); throw new Exception(app.getError("Error") + ":\n" + ex.getLocalizedMessage()); } return geoElements; }
// returns non-null GeoElement array when successful public GeoElement[] processAlgebraCommand(String cmd, boolean storeUndo) { try { return processAlgebraCommandNoExceptionHandling(cmd, storeUndo, true, false); } catch (Exception e) { e.printStackTrace(); app.showError(e.getMessage()); return null; } }
/** * for AlgebraView changes in the tree selection and redefine dialog * * @return changed geo */ public GeoElement changeGeoElement( GeoElement geo, String newValue, boolean redefineIndependent, boolean storeUndoInfo) { try { return changeGeoElementNoExceptionHandling(geo, newValue, redefineIndependent, storeUndoInfo); } catch (Exception e) { app.showError(e.getMessage()); return null; } }
/** shows dialog with syntax info cmd is the internal command name */ private void showCommandHelp(String cmd) { // show help for current command (current word) String help = app.getCommandSyntax(cmd); // show help if available if (help != null) { app.showError(new MyError(app, app.getPlain("Syntax") + ":\n" + help, cmd)); } else { app.getGuiManager().openCommandHelp(null); } }
/** * Parses given String str and tries to evaluate it to a GeoPoint. Returns null if something went * wrong. */ public GeoPointND evaluateToPoint(String str, boolean showErrors) { boolean oldMacroMode = cons.isSuppressLabelsActive(); cons.setSuppressLabelCreation(true); GeoPointND p = null; GeoElement[] temp = null; ; try { ValidExpression ve = parser.parseGeoGebraExpression(str); if (ve instanceof ExpressionNode) { ExpressionNode en = (ExpressionNode) ve; en.setForcePoint(); } temp = processValidExpression(ve); p = (GeoPointND) temp[0]; } catch (CircularDefinitionException e) { if (showErrors) { Application.debug("CircularDefinition"); app.showError("CircularDefinition"); } } catch (Exception e) { if (showErrors) { e.printStackTrace(); app.showError("InvalidInput", str); } } catch (MyError e) { if (showErrors) { e.printStackTrace(); app.showError(e); } } catch (Error e) { if (showErrors) { e.printStackTrace(); app.showError("InvalidInput", str); } } cons.setSuppressLabelCreation(oldMacroMode); return p; }
/** * Parses given String str and tries to evaluate it to a double. Returns Double.NaN if something * went wrong. */ public double evaluateToDouble(String str, boolean suppressErrors) { try { ValidExpression ve = parser.parseExpression(str); ExpressionNode en = (ExpressionNode) ve; en.resolveVariables(); NumberValue nv = (NumberValue) en.evaluate(); return nv.getDouble(); } catch (Exception e) { e.printStackTrace(); if (!suppressErrors) app.showError("InvalidInput", str); return Double.NaN; } catch (MyError e) { e.printStackTrace(); if (!suppressErrors) app.showError(e); return Double.NaN; } catch (Error e) { e.printStackTrace(); if (!suppressErrors) app.showError("InvalidInput", str); return Double.NaN; } }
/* * attempt to give Syntax help for when eg "Radius[ <Conic> ]" is typed * see CommandProcessor.argErr() for similar error */ public void showError(Exception e) { if (e instanceof MyException) { updateCurrentWord(true); int err = ((MyException) e).getErrorType(); if (err == MyException.INVALID_INPUT) { String command = app.getReverseCommand(getCurrentWord()); if (command != null) { app.showError( new MyError( app, app.getError("InvalidInput") + "\n\n" + app.getPlain("Syntax") + ":\n" + app.getCommandSyntax(command), getCurrentWord())); return; } } } // can't work out anything better, just show "Invalid Input" app.showError(e.getLocalizedMessage()); }
/* * just show syntax error (already correctly formulated by CommandProcessor.argErr()) */ public void showError(MyError e) { app.showError(e); }
/** * Processes the given casCell, i.e. compute its output depending on its input. Note that this may * create an additional twin GeoElement. */ public final void processCasCell(GeoCasCell casCell) throws MyError { // TODO: remove System.out.println("*** processCasCell: " + casCell); // check for CircularDefinition if (casCell.isCircularDefinition()) { // set twin geo to undefined casCell.computeOutput(); casCell.updateCascade(); app.showError("CircularDefinition"); return; } AlgoElement algoParent = casCell.getParentAlgorithm(); boolean prevFree = algoParent == null; boolean nowFree = casCell.getGeoElementVariables() == null; boolean needsRedefinition = false; if (prevFree) { if (nowFree) { // free -> free, e.g. m := 7 -> m := 8 cons.addToConstructionList(casCell, true); casCell.computeOutput(); // create twinGeo if necessary casCell.setLabelOfTwinGeo(); needsRedefinition = false; } else { // free -> dependent, e.g. m := 7 -> m := c+2 if (casCell.isOutputEmpty() && !casCell.hasChildren()) { // this is a new casCell cons.removeFromConstructionList(casCell); kernel.DependentCasCell(casCell); needsRedefinition = false; } else { // existing casCell with possible twinGeo needsRedefinition = true; } } } else { if (nowFree) { // dependent -> free, e.g. m := c+2 -> m := 7 // algorithm will be removed through redefinition needsRedefinition = true; } else { // dependent -> dependent, e.g. m := c+2 -> m := c+d // we already have an algorithm but need redefinition // in order to move it to the right place in construction list needsRedefinition = true; } } if (needsRedefinition) { try { // update construction order and // rebuild construction using XML cons.changeCasCell(casCell); } catch (Exception e) { casCell.setError("RedefinitionFailed"); // app.showError(e.getMessage()); } } else { casCell.updateCascade(); } }