Esempio n. 1
0
  /**
   * Returns true if a string is a standard number, i.e not in scientific notation
   *
   * @param s
   * @return
   */
  private static boolean isStandardNumber(String s) {

    // return if empty string
    if (s == null || s.length() == 0) return false;

    // test the first char for a digit, sign or decimal point.
    Character c = s.charAt(0);
    if (!(StringUtil.isDigit(c) || c == '.' || c == '-' || c == '+' || c == '\u2212')) {
      return false;
    }

    // test the remaining chars for digits or decimal point
    int decimalCount = 0;
    for (int i = 1; i < s.length(); i++) {
      c = s.charAt(i);
      if (StringUtil.isDigit(c)) {
        continue;
      }
      if (c == '.' && decimalCount == 0) {
        decimalCount++;
        continue;
      }
      return false;
    }
    return true;
  }
  /*
   * auto-insert degree symbol when appropriate
   */
  public void onKeyUp(KeyUpEvent e) {

    // return unless digit typed (instead of !Character.isDigit)
    if (e.getNativeKeyCode() < 48
        || (e.getNativeKeyCode() > 57 && e.getNativeKeyCode() < 96)
        || e.getNativeKeyCode() > 105) return;

    AutoCompleteTextFieldW tc = inputPanel.getTextComponent();
    String text = tc.getText();

    // if text already contains degree symbol or variable
    for (int i = 0; i < text.length(); i++) {
      if (!StringUtil.isDigit(text.charAt(i))) return;
    }

    int caretPos = tc.getCaretPosition();

    tc.setText(tc.getText() + Unicode.degree);

    tc.setCaretPosition(caretPos);
  }
Esempio n. 3
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];
  }