Esempio n. 1
0
  // 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;
  }
Esempio n. 2
0
  /**
   * for AlgebraView changes in the tree selection and redefine dialog
   *
   * @return changed geo
   */
  public GeoElement changeGeoElementNoExceptionHandling(
      GeoElement geo, ValidExpression newValue, boolean redefineIndependent, boolean storeUndoInfo)
      throws Exception {
    String oldLabel, newLabel;
    GeoElement[] result;

    try {
      oldLabel = geo.getLabel();
      newLabel = newValue.getLabel();

      if (newLabel == null) {
        newLabel = oldLabel;
        newValue.setLabel(newLabel);
      }

      // make sure that points stay points and vectors stay vectors
      if (newValue instanceof ExpressionNode) {
        ExpressionNode n = (ExpressionNode) newValue;
        if (geo.isGeoPoint()) n.setForcePoint();
        else if (geo.isGeoVector()) n.setForceVector();
        else if (geo.isGeoFunction()) n.setForceFunction();
      }

      if (newLabel.equals(oldLabel)) {
        // try to overwrite
        result = processValidExpression(newValue, redefineIndependent);
        if (result != null && storeUndoInfo) app.storeUndoInfo();
        return result[0];
      } else if (cons.isFreeLabel(newLabel)) {
        newValue.setLabel(oldLabel);
        // rename to oldLabel to enable overwriting
        result = processValidExpression(newValue, redefineIndependent);
        result[0].setLabel(newLabel); // now we rename
        if (storeUndoInfo) app.storeUndoInfo();
        return result[0];
      } else {
        String str[] = {"NameUsed", newLabel};
        throw new MyError(app, str);
      }
    } catch (CircularDefinitionException e) {
      Application.debug("CircularDefinition");
      throw e;
    } catch (Exception e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    } catch (MyError e) {
      e.printStackTrace();
      throw new Exception(e.getLocalizedMessage());
    } catch (Error e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    }
  }
Esempio n. 3
0
  /**
   * for AlgebraView changes in the tree selection and redefine dialog
   *
   * @return changed geo
   */
  public GeoElement changeGeoElementNoExceptionHandling(
      GeoElement geo, String newValue, boolean redefineIndependent, boolean storeUndoInfo)
      throws Exception {

    try {
      ValidExpression ve = parser.parseGeoGebraExpression(newValue);
      return changeGeoElementNoExceptionHandling(geo, ve, redefineIndependent, storeUndoInfo);
    } catch (Exception e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    } catch (MyError e) {
      e.printStackTrace();
      throw new Exception(e.getLocalizedMessage());
    } catch (Error e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    }
  }
  /*
   * 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());
  }
Esempio n. 5
0
  /**
   * processes valid expression.
   *
   * @param ve
   * @param redefineIndependent == true: independent objects are redefined too
   * @throws MyError
   * @throws Exception
   * @return
   */
  public GeoElement[] processValidExpression(ValidExpression ve, boolean redefineIndependent)
      throws MyError, Exception {

    // check for existing labels
    String[] labels = ve.getLabels();
    GeoElement replaceable = null;
    if (labels != null && labels.length > 0) {
      boolean firstTime = true;
      for (int i = 0; i < labels.length; i++) {
        GeoElement geo = kernel.lookupLabel(labels[i]);
        if (geo != null) {
          if (geo.isFixed()) {
            String[] strs = {
              "IllegalAssignment", "AssignmentToFixed", ":\n", geo.getLongDescription()
            };
            throw new MyError(app, strs);
          } else {
            // replace (overwrite or redefine) geo
            if (firstTime) { // only one geo can be replaced
              replaceable = geo;
              firstTime = false;
            }
          }
        }
      }
    }

    GeoElement[] ret;
    boolean oldMacroMode = cons.isSuppressLabelsActive();
    if (replaceable != null) cons.setSuppressLabelCreation(true);

    // we have to make sure that the macro mode is
    // set back at the end
    try {
      ret = doProcessValidExpression(ve);

      if (ret == null) { // eg (1,2,3) running in 2D
        Application.debug("Unhandled ValidExpression : " + ve);
        throw new MyError(app, app.getError("InvalidInput") + ":\n" + ve);
      }
    } finally {
      cons.setSuppressLabelCreation(oldMacroMode);
    }

    //	try to replace replaceable geo by ret[0]
    if (replaceable != null && ret != null && ret.length > 0) {
      // a changeable replaceable is not redefined:
      // it gets the value of ret[0]
      // (note: texts are always redefined)
      if (!redefineIndependent && replaceable.isChangeable() && !(replaceable.isGeoText())) {
        try {
          replaceable.set(ret[0]);
          replaceable.updateRepaint();
          ret[0] = replaceable;
        } catch (Exception e) {
          String errStr =
              app.getError("IllegalAssignment")
                  + "\n"
                  + replaceable.getLongDescription()
                  + "     =     "
                  + ret[0].getLongDescription();
          throw new MyError(app, errStr);
        }
      }
      // redefine
      else {
        try {
          // SPECIAL CASE: set value
          // new and old object are both independent and have same type:
          // simply assign value and don't redefine
          if (replaceable.isIndependent()
              && ret[0].isIndependent()
              && replaceable.getGeoClassType() == ret[0].getGeoClassType()) {
            replaceable.set(ret[0]);
            replaceable.updateRepaint();
            ret[0] = replaceable;
          }

          // STANDARD CASE: REDFINED
          else {
            GeoElement newGeo = ret[0];
            cons.replace(replaceable, newGeo);

            // now all objects have changed
            // get the new object with same label as our result
            String newLabel = newGeo.isLabelSet() ? newGeo.getLabel() : replaceable.getLabel();
            ret[0] = kernel.lookupLabel(newLabel, false);
          }
        } catch (CircularDefinitionException e) {
          throw e;
        } catch (Exception e) {
          e.printStackTrace();
          throw new MyError(app, "ReplaceFailed");
        } catch (MyError e) {
          e.printStackTrace();
          throw new MyError(app, "ReplaceFailed");
        }
      }
    }

    return ret;
  }