Beispiel #1
0
  /** adds a new node to the tree */
  public void add(GeoElement geo) {
    cancelEditing();

    if (geo.isLabelSet() && geo.showInAlgebraView() && geo.isSetAlgebraVisible()) {
      // don't add auxiliary objects if the tree is categorized by type
      if (!getTreeMode().equals(SortMode.DEPENDENCY)
          && !showAuxiliaryObjects()
          && geo.isAuxiliaryObject()) {
        return;
      }

      DefaultMutableTreeNode parent, node;
      node = new DefaultMutableTreeNode(geo);
      parent = getParentNode(geo);

      // add node to model (alphabetically ordered)
      int pos = getInsertPosition(parent, geo, treeMode);

      model.insertNodeInto(node, parent, pos);
      nodeTable.put(geo, node);

      // ensure that the leaf with the new object is visible
      expandPath(new TreePath(new Object[] {model.getRoot(), parent}));
    }
  }
  private GeoElement[] processList(ExpressionNode n, MyList evalList) {
    String label = n.getLabel();

    GeoElement[] ret = new GeoElement[1];

    // no operations or no variables are present, e.g.
    // { a, b, 7 } or  { 2, 3, 5 } + {1, 2, 4}
    if (!n.hasOperations() || n.isConstant()) {

      // PROCESS list items to generate a list of geoElements
      ArrayList<GeoElement> geoElements = new ArrayList<GeoElement>();
      boolean isIndependent = true;

      // make sure we don't create any labels for the list elements
      boolean oldMacroMode = cons.isSuppressLabelsActive();
      cons.setSuppressLabelCreation(true);

      int size = evalList.size();
      for (int i = 0; i < size; i++) {
        ExpressionNode en = (ExpressionNode) evalList.getListElement(i);
        // we only take one resulting object
        GeoElement[] results = processExpressionNode(en);
        GeoElement geo = results[0];

        // add to list
        geoElements.add(geo);
        if (geo.isLabelSet() || !geo.isIndependent()) isIndependent = false;
      }
      cons.setSuppressLabelCreation(oldMacroMode);

      // Create GeoList object
      ret[0] = kernel.List(label, geoElements, isIndependent);
    }

    // operations and variables are present
    // e.g. {3, 2, 1} + {a, b, 2}
    else {
      ret[0] = kernel.ListExpression(label, n);
    }

    return ret;
  }
    /** adds a new element to the list */
    public final void add(GeoElement geo) {
      if (!geo.isLabelSet() || !geo.hasProperties()) return;

      // get type node
      String typeString = geo.getObjectType();
      DefaultMutableTreeNode typeNode = typeNodesMap.get(typeString);

      // init type node
      boolean initing = typeNode == null;
      if (initing) {
        String transTypeString = geo.translatedTypeString();
        typeNode = new DefaultMutableTreeNode(transTypeString);
        typeNodesMap.put(typeString, typeNode);

        // find insert pos
        int pos = root.getChildCount();
        for (int i = 0; i < pos; i++) {
          DefaultMutableTreeNode child = (DefaultMutableTreeNode) root.getChildAt(i);
          if (transTypeString.compareTo(child.toString()) < 0) {
            pos = i;
            break;
          }
        }

        treeModel.insertNodeInto(typeNode, root, pos);
      }

      // check if already present in type node
      int pos = AlgebraView.binarySearchGeo(typeNode, geo.getLabel());
      if (pos >= 0) return;

      // add geo to type node
      DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(geo);
      pos = AlgebraView.getInsertPosition(typeNode, geo, AlgebraView.SortMode.DEPENDENCY);
      treeModel.insertNodeInto(newNode, typeNode, pos);

      // make sure something is selected
      if (getSelectionModel().isSelectionEmpty()) {
        selectFirstElement();
      }
    }
  /**
   * 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;
  }