示例#1
0
  /**
   * Prepares a spreadsheet cell editor string for processing in the kernel and returns either (1) a
   * new GeoElement for the cell or (2) null.
   *
   * @param kernel kernel
   * @param app application
   * @param inputText string representation of the new GeoElement
   * @param oldValue current cell GeoElement
   * @param column cell column
   * @param row cell row
   * @return either (1) a new GeoElement for the cell or (2) null
   * @throws Exception
   */
  public static GeoElement prepareAddingValueToTableNoStoringUndoInfo(
      Kernel kernel, App app, String inputText, GeoElement oldValue, int column, int row)
      throws Exception {
    String text = inputText;
    // get the cell name
    String name = GeoElementSpreadsheet.getSpreadsheetCellName(column, row);

    // trim the text
    if (text != null) {
      text = text.trim();
      if (text.length() == 0) {
        text = null;
      }
    }

    // if "=" is required before commands and text is not a number
    // or does not begin with "=" then surround it with quotes.
    // This will force the cell to become GeoText.
    if (app.getSettings().getSpreadsheet().equalsRequired() && text != null) {

      if (!((text.charAt(0) == '=') || isNumber(text))) {
        text = "\"" + text + "\"";
      }
    }

    // if the cell is currently GeoText then prepare it for changes
    // make sure it can be changed to something else
    // eg (2,3 can be overwritten as (2,3)
    // if (oldValue != null && oldValue.isGeoText() &&
    // !oldValue.hasChildren()) {
    // oldValue.remove();
    // oldValue = null;
    // }

    // if the text is null then remove the current cell geo and return null
    if (text == null) {
      if (oldValue != null) {
        oldValue.remove();
      }
      return null;

      // else if the target cell is empty, try to create a new GeoElement
      // for this cell
    } else if (oldValue == null) {
      try {
        // this will be a new geo
        return prepareNewValue(kernel, name, text);
      } catch (Throwable t) {
        return prepareNewValue(kernel, name, "");
      }

      // else the target cell is an existing GeoElement, so redefine it
    } else {
      return updateOldValue(kernel, oldValue, name, text);
    }
  }
 @SuppressWarnings("unused")
 private void closeDialog() {
   // either remove our geo or keep it and make it visible
   if (keepNewGeo) {
     addNewGeoToConstruction();
   } else {
     newGeo.remove();
   }
   setVisible(false);
 }
  /** Handles button clicks for dialog. */
  @Override
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    try {
      btnValue.removeActionListener(this);
      btnObject.removeActionListener(this);

      if (source instanceof JTextField) {
        doTextFieldActionPerformed((JTextField) source);
      }

      // btCancel acts as create for now
      else if (source == btCancel) {

        keepNewGeo = true;
        setVisible(false);

      } else if (source == btApply) {
        // processInput();

        // btOK acts as cancel for now
      } else if (source == btOK) {
        newGeo.remove();
        setVisible(false);
      } else if (source == btnObject) {
        btnValue.setSelected(!btnObject.isSelected());
        createNewGeo();
      } else if (source == btnValue) {
        btnObject.setSelected(!btnValue.isSelected());
        createNewGeo();
      } else if (source == cbScanOrder || source == cbLeftRightOrder || source == ckTranspose) {
        createNewGeo();
      }

      btnValue.addActionListener(this);
      btnObject.addActionListener(this);

    } catch (Exception ex) {
      // do nothing on uninitializedValue
      setVisible(false);
    }
  }
  @Override
  public void setVisible(boolean isVisible) {
    if (!wrappedDialog.isModal()) {
      if (isVisible) { // set old mode again
        wrappedDialog.addWindowFocusListener(this);
      } else {
        wrappedDialog.removeWindowFocusListener(this);
        app.setSelectionListenerMode(null);
      }
    }

    // clean up on exit: either remove our geo or keep it and make it
    // visible
    if (!isVisible) {
      if (keepNewGeo) {
        addNewGeoToConstruction();
      } else {
        newGeo.remove();
      }
    }
    super.setVisible(isVisible);
  }
示例#5
0
  private static GeoElement updateOldValue(
      Kernel kernel, GeoElement oldValue, String name, String text) throws Exception {
    String text0 = text;
    if (text.charAt(0) == '=') {
      text = text.substring(1);
    }
    GeoElement newValue = null;
    try {
      // always redefine objects in spreadsheet, don't store undo info
      // here
      newValue =
          kernel
              .getAlgebraProcessor()
              .changeGeoElementNoExceptionHandling(oldValue, text, true, false);

      // newValue.setConstructionDefaults();
      newValue.setAllVisualProperties(oldValue, true);
      if (oldValue.isAuxiliaryObject()) {
        newValue.setAuxiliaryObject(true);
      }

      // Application.debug("GeoClassType = " +
      // newValue.getGeoClassType()+" " + newValue.getGeoClassType());
      if (newValue.getGeoClassType() == oldValue.getGeoClassType()) {
        // newValue.setVisualStyle(oldValue);
      } else {
        kernel.getApplication().refreshViews();
      }
    } catch (CircularDefinitionException cde) {
      kernel.getApplication().showError("CircularDefinition");
      return null;
    } catch (Throwable e) {
      // if exception is thrown treat the input as text and try to update
      // the cell as a GeoText
      {
        // reset the text string if old value is GeoText
        if (oldValue.isGeoText()) {
          ((GeoText) oldValue).setTextString(text0);
          oldValue.updateCascade();
        }

        // if not currently a GeoText and no children, redefine the cell
        // as new GeoText
        else if (!oldValue.hasChildren()) {
          oldValue.remove();

          // add input as text
          try {
            newValue = prepareNewValue(kernel, name, "\"" + text0 + "\"");
          } catch (Throwable t) {
            newValue = prepareNewValue(kernel, name, "");
          }
          newValue.setEuclidianVisible(false);
          newValue.update();
        }

        // otherwise throw an exception and let the cell revert to the
        // old value
        else {
          throw new Exception(e);
        }
      }
    }
    return newValue;
  }
示例#6
0
  private static GeoElement prepareNewValue(Kernel kernel, String name, String inputText)
      throws Exception {
    String text = inputText;
    if (text == null) {
      return null;
    }

    // remove leading equal sign, e.g. "= A1 + A2"
    if (text.length() > 0 && text.charAt(0) == '=') {
      text = text.substring(1);
    }
    text = text.trim();

    // no equal sign in input
    GeoElement[] newValues = null;
    try {
      // check if input is same as name: circular definition
      if (text.equals(name)) {
        // circular definition
        throw new CircularDefinitionException();
      }

      // evaluate input text without an error dialog in case of unquoted
      // text
      newValues =
          kernel
              .getAlgebraProcessor()
              .processAlgebraCommandNoExceptionHandling(text, false, false, false);

      // check if text was the label of an existing geo
      // toUpperCase() added to fix bug A1=1, enter just 'a1' or 'A1' into
      // cell B1 -> A1 disappears
      if (StringUtil.toLowerCase(text).equals(newValues[0].getLabel(StringTemplate.defaultTemplate))
          // also need eg =a to work
          || text.equals(newValues[0].getLabel(StringTemplate.defaultTemplate))) {
        // make sure we create a copy of this existing or auto-created
        // geo
        // by providing the new cell name in the beginning
        text = name + " = " + text;
        newValues = kernel.getAlgebraProcessor().processAlgebraCommandNoExceptions(text, false);
      }

      // check if name was auto-created: if yes we could have a circular
      // definition
      GeoElement autoCreateGeo = kernel.lookupLabel(name);
      if (autoCreateGeo != null) {
        // check for circular definition: if newValue depends on
        // autoCreateGeo
        boolean circularDefinition = false;
        for (int i = 0; i < newValues.length; i++) {
          if (newValues[i].isChildOf(autoCreateGeo)) {
            circularDefinition = true;
            break;
          }
        }

        if (circularDefinition) {
          // remove the auto-created object and the result
          autoCreateGeo.remove();
          newValues[0].remove();

          // circular definition
          throw new CircularDefinitionException();
        }
      }

      for (int i = 0; i < newValues.length; i++) {
        newValues[i].setAuxiliaryObject(true);
        if (newValues[i].isGeoText()) {
          newValues[i].setEuclidianVisible(false);
        }
      }

      GeoElement.setLabels(name, newValues); // set names to be D1,
      // E1,
      // F1, etc for multiple
      // objects
    } catch (CircularDefinitionException ce) {
      // circular definition
      kernel.getApplication().showError("CircularDefinition");
      return null;
    } catch (Exception e) {
      // create text if something went wrong
      if (text.startsWith("\"")) text = text.substring(1, text.length() - 2);
      text = "\"" + (text.replace("\"", "\"+UnicodeToLetter[34]+\"")) + "\"";
      newValues = kernel.getAlgebraProcessor().processAlgebraCommandNoExceptions(text, false);
      newValues[0].setLabel(name);
      newValues[0].setEuclidianVisible(false);
      newValues[0].update();
    }
    return newValues[0];
  }
示例#7
0
  public static GeoElement doCopyNoStoringUndoInfo0(
      Kernel kernel, App app, GeoElement value, GeoElement oldValue, int dx, int dy)
      throws Exception {
    if (value == null) {
      if (oldValue != null) {
        MatchResult matcher =
            GeoElementSpreadsheet.spreadsheetPatternPart.exec(
                oldValue.getLabel(StringTemplate.defaultTemplate));
        int column = GeoElementSpreadsheet.getSpreadsheetColumn(matcher);
        int row = GeoElementSpreadsheet.getSpreadsheetRow(matcher);

        prepareAddingValueToTableNoStoringUndoInfo(kernel, app, null, oldValue, column, row);
      }
      return null;
    }
    String text = null;

    // make sure a/0.001 doesn't become a/0

    StringTemplate highPrecision = StringTemplate.maxPrecision;
    if (value.isPointOnPath() || value.isPointInRegion()) {
      text = value.getCommandDescription(highPrecision);
    } else if (value.isChangeable()) {
      text = value.toValueString(highPrecision);
    } else {
      text = value.getCommandDescription(highPrecision);
    }

    // handle GeoText source value
    if (value.isGeoText() && !((GeoText) value).isTextCommand()) {
      // enclose text in quotes if we are copying an independent GeoText,
      // e.g. "2+3"
      if (value.isIndependent()) {
        text = "\"" + text + "\"";
      } else {

        // check if 'text' parses to a GeoText
        GeoText testGeoText = kernel.getAlgebraProcessor().evaluateToText(text, false, false);

        // if it doesn't then force it to by adding +"" on the end
        if (testGeoText == null) {
          text = text + "+\"\"";
        }
      }
    }

    // for E1 = Polynomial[D1] we need value.getCommandDescription();
    // even though it's a GeoFunction
    if (value.isGeoFunction() && text.equals("")) {
      // we need the definition without A1(x)= on the front
      text = ((GeoFunction) value).toSymbolicString(highPrecision);
    }

    boolean freeImage = false;

    if (value.isGeoImage()) {
      GeoImage image = (GeoImage) value;
      if (image.getParentAlgorithm() == null) {
        freeImage = true;
      }
    }

    // Application.debug("before:"+text);
    text = updateCellReferences(value, text, dx, dy);
    // Application.debug("after:"+text);

    // condition to show object
    GeoBoolean bool = value.getShowObjectCondition();
    String boolText = null, oldBoolText = null;
    if (bool != null) {
      if (bool.isChangeable()) {
        oldBoolText = bool.toValueString(highPrecision);
      } else {
        oldBoolText = bool.getCommandDescription(highPrecision);
      }
    }

    if (oldBoolText != null) {
      boolText = updateCellReferences(bool, oldBoolText, dx, dy);
    }

    String startPoints[] = null;
    if (value instanceof Locateable) {
      Locateable loc = (Locateable) value;

      GeoPointND[] pts = loc.getStartPoints();

      startPoints = new String[pts.length];

      for (int i = 0; i < pts.length; i++) {
        startPoints[i] = ((GeoElement) pts[i]).getLabel(highPrecision);
        startPoints[i] = updateCellReferences((GeoElement) pts[i], startPoints[i], dx, dy);
      }
    }

    // dynamic color function
    GeoList dynamicColorList = value.getColorFunction();
    String colorText = null, oldColorText = null;
    if (dynamicColorList != null) {
      if (dynamicColorList.isChangeable()) {
        oldColorText = dynamicColorList.toValueString(highPrecision);
      } else {
        oldColorText = dynamicColorList.getCommandDescription(highPrecision);
      }
    }

    if (oldColorText != null) {
      colorText = updateCellReferences(dynamicColorList, oldColorText, dx, dy);
    }

    // allow pasting blank strings
    if (text.equals("")) {
      text = "\"\"";
    }

    // make sure that non-GeoText elements are copied when the
    // equalsRequired option is set
    if (!value.isGeoText() && app.getSettings().getSpreadsheet().equalsRequired()) {
      text = "=" + text;
    }

    // Application.debug("add text = " + text + ", name = " + (char)('A' +
    // column + dx) + (row + dy + 1));

    // create the new cell geo
    MatchResult matcher =
        GeoElementSpreadsheet.spreadsheetPatternPart.exec(
            value.getLabel(StringTemplate.defaultTemplate));
    int column0 = GeoElementSpreadsheet.getSpreadsheetColumn(matcher);
    int row0 = GeoElementSpreadsheet.getSpreadsheetRow(matcher);
    GeoElement value2;
    if (freeImage || value.isGeoButton()) {
      value2 = value.copy();
      if (oldValue != null) {
        oldValue.remove();
      }
      // value2.setLabel(table.getModel().getColumnName(column0 + dx)
      //		+ (row0 + dy + 1));
      value2.setLabel(GeoElementSpreadsheet.getSpreadsheetCellName(column0 + dx, row0 + dy + 1));
      value2.updateRepaint();
    } else {
      value2 =
          prepareAddingValueToTableNoStoringUndoInfo(
              kernel, app, text, oldValue, column0 + dx, row0 + dy);
    }
    value2.setAllVisualProperties(value, false);

    value2.setAuxiliaryObject(true);

    // attempt to set updated condition to show object (if it's changed)
    if ((boolText != null)) {
      // removed as doesn't work for eg "random()<0.5" #388
      // && !boolText.equals(oldBoolText)) {
      try {
        // Application.debug("new condition to show object: "+boolText);
        GeoBoolean newConditionToShowObject =
            kernel.getAlgebraProcessor().evaluateToBoolean(boolText);
        value2.setShowObjectCondition(newConditionToShowObject);
        value2.update(); // needed to hide/show object as appropriate
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }

    // attempt to set updated dynamic color function (if it's changed)
    if ((colorText != null)) {
      // removed as doesn't work for eg "random()" #388
      // && !colorText.equals(oldColorText)) {
      try {
        // Application.debug("new color function: "+colorText);
        GeoList newColorFunction = kernel.getAlgebraProcessor().evaluateToList(colorText);
        value2.setColorFunction(newColorFunction);
        // value2.update();
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }

    if (startPoints != null) {
      for (int i = 0; i < startPoints.length; i++) {
        ((Locateable) value2)
            .setStartPoint(
                kernel.getAlgebraProcessor().evaluateToPoint(startPoints[i], false, true), i);
      }

      value2.update();
    }

    // Application.debug((row + dy) + "," + column);
    // Application.debug("isGeoFunction()=" + value2.isGeoFunction());
    // Application.debug("row0 ="+row0+" dy="+dy+" column0= "+column0+" dx="+dx);

    return value2;
  }
  private void createNewGeo() {

    boolean nullGeo = newGeo == null;

    if (!nullGeo) {
      if (objectType == TYPE_LISTOFPOINTS) {
        GeoList gl = (GeoList) newGeo;
        for (int i = 0; i < gl.size(); i++) gl.get(i).remove();
      }

      if (objectType == TYPE_POLYLINE) {
        GeoPoint[] pts = ((AlgoPolyLine) newGeo.getParentAlgorithm()).getPoints();
        for (int i = 0; i < pts.length; i++) pts[i].remove();
      }
      newGeo.remove();
    }

    int column1 = table.selectedCellRanges.get(0).getMinColumn();
    int column2 = table.selectedCellRanges.get(0).getMaxColumn();
    int row1 = table.selectedCellRanges.get(0).getMinRow();
    int row2 = table.selectedCellRanges.get(0).getMaxRow();

    boolean copyByValue = btnValue.isSelected();
    boolean scanByColumn = cbScanOrder.getSelectedIndex() == 1;
    boolean leftToRight = cbLeftRightOrder.getSelectedIndex() == 0;
    boolean transpose = ckTranspose.isSelected();
    boolean doCreateFreePoints = true;
    boolean doStoreUndo = true;
    boolean isSorted = false;

    try {
      switch (objectType) {
        case TYPE_LIST:
          newGeo = cp.createList(selectedCellRanges, scanByColumn, copyByValue);
          break;

        case TYPE_LISTOFPOINTS:
          newGeo =
              cp.createPointGeoList(
                  selectedCellRanges,
                  copyByValue,
                  leftToRight,
                  isSorted,
                  doStoreUndo,
                  doCreateFreePoints);
          newGeo.setLabel(null);
          for (int i = 0; i < ((GeoList) newGeo).size(); i++) {
            ((GeoList) newGeo).get(i).setAuxiliaryObject(true);
            ((GeoList) newGeo).get(i).setEuclidianVisible(false);
          }
          newGeo.updateRepaint();
          break;

        case TYPE_MATRIX:
          newGeo = cp.createMatrix(column1, column2, row1, row2, copyByValue, transpose);
          break;

        case TYPE_TABLETEXT:
          newGeo = cp.createTableText(column1, column2, row1, row2, copyByValue, transpose);
          break;

        case TYPE_POLYLINE:
          newGeo = cp.createPolyLine(selectedCellRanges, copyByValue, leftToRight);
          newGeo.setLabel(null);
          GeoPoint[] pts = ((AlgoPolyLine) newGeo.getParentAlgorithm()).getPoints();
          for (int i = 0; i < pts.length; i++) {
            pts[i].setAuxiliaryObject(true);
            pts[i].setEuclidianVisible(false);
          }
          newGeo.updateRepaint();
          break;
      }

      ImageIcon latexIcon = new ImageIcon();
      // String latexStr = newGeo.getLaTeXAlgebraDescription(true);

      String latexStr = newGeo.getFormulaString(StringTemplate.latexTemplate, true);

      // System.out.println(latexStr);

      Font latexFont =
          new Font(
              app.getPlainFont().getName(),
              app.getPlainFont().getStyle(),
              app.getPlainFont().getSize() - 1);

      if (latexStr != null && newGeo.isLaTeXDrawableGeo()) {
        app.getDrawEquation()
            .drawLatexImageIcon(app, latexIcon, latexStr, latexFont, false, Color.black, null);
        lblPreview.setText(" ");
      } else {
        lblPreview.setText(newGeo.getAlgebraDescriptionTextOrHTMLDefault());
      }
      lblPreview.setIcon(latexIcon);

      if (!nullGeo) {
        newGeo.setLabel(fldName.getText());
        newGeo.setAuxiliaryObject(true);
        newGeo.setEuclidianVisible(false);
      }

      updateGUI();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }