Search in sources :

Example 6 with NodeException

use of expression.NodeException in project OpenNotebook by jaltekruse.

the class ExpressionObject method performSpecialObjectAction.

public void performSpecialObjectAction(String s) {
    // actions in the future
    if (((StringAttribute) getAttributeWithName(EXPRESSION)).getValue() == null || ((StringAttribute) getAttributeWithName(EXPRESSION)).getValue().equals("")) {
        JOptionPane.showMessageDialog(null, "There is no expression to work with, enter one in the box below.", WARNING, JOptionPane.WARNING_MESSAGE);
        setActionCancelled(true);
        return;
    }
    if (s.equals(SIMPLIFY)) {
        try {
            getListWithName(STEPS).addValueWithString(Expression.parseNode(getLastStep()).smartNumericSimplify().toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error with expression simplification", ERROR, JOptionPane.WARNING_MESSAGE);
        }
    }
    if (s.equals(COMBINE_LIKE_TERMS)) {
        try {
            getListWithName(STEPS).addValueWithString(Expression.parseNode(getLastStep()).collectLikeTerms().simplify().toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error with combining like terms", ERROR, JOptionPane.WARNING_MESSAGE);
        }
    }
    if (s.equals(MAKE_INTO_PROBLEM)) {
        VariableValueInsertionProblem newProblem = new VariableValueInsertionProblem(getParentContainer(), getxPos(), getyPos(), getWidth(), getHeight());
        this.getParentContainer().getParentDoc().getDocViewerPanel().setFocusedObject(newProblem);
        newProblem.addObjectFromPage(this);
        getParentContainer().addObject(newProblem);
        getParentContainer().removeObject(this);
        return;
    } else if (s.equals(UNDO_STEP)) {
        int size = getListWithName(STEPS).getValues().size();
        if (size > 0) {
            getListWithName(STEPS).getValues().remove(size - 1);
        } else {
            JOptionPane.showMessageDialog(null, "No steps to undo.", WARNING, JOptionPane.WARNING_MESSAGE);
            setActionCancelled(true);
        }
        return;
    } else if (s.equals(SUB_IN_VALUE)) {
        String variableStr = "";
        Node substitute = null;
        boolean foundVar;
        do {
            variableStr = (String) JOptionPane.showInputDialog(null, "Enter a variable to replace, variables are case sensitive 'a' is not the same as 'A'.", null, JOptionPane.PLAIN_MESSAGE, null, null, null);
            if (variableStr == null) {
                setActionCancelled(true);
                return;
            }
            if (variableStr.length() != 1 || !Character.isLetter(variableStr.charAt(0))) {
                JOptionPane.showMessageDialog(null, "Need to enter a single letter.", WARNING, JOptionPane.WARNING_MESSAGE);
            }
            foundVar = Node.parseNode(getLastStep()).containsIdentifier(variableStr);
            if (!foundVar) {
                JOptionPane.showMessageDialog(null, "Variable not found in expression.", WARNING, JOptionPane.WARNING_MESSAGE);
            }
        } while (variableStr.length() != 1 || !Character.isLetter(variableStr.charAt(0)) && !foundVar);
        substitute = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser("Enter value or expression to substitute.");
        if (substitute == null) {
            setActionCancelled(true);
            return;
        }
        substitute.setDisplayParentheses(true);
        try {
            getListWithName(STEPS).addValueWithString(Node.parseNode(getLastStep()).replace(variableStr, substitute).toStringRepresentation());
        } catch (Exception e) {
            // this should not throw an error, as both the expression and the one being
            // Substituted have both been checked for validity
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
            setActionCancelled(true);
        }
        return;
    } else if (s.equals(MODIFY_EXPRESSION)) {
        Node newNode = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser("Modify the expression.", getLastStep());
        if (newNode == null) {
            setActionCancelled(true);
            return;
        }
        try {
            getListWithName(STEPS).addValueWithString(newNode.toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        }
    } else if (s.equals(MANUALLY_TYPE_STEP)) {
        Node newNode = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser("Type the entire next line.");
        if (newNode == null) {
            setActionCancelled(true);
            return;
        }
        try {
            getListWithName(STEPS).addValueWithString(newNode.toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        }
    }
    // all of the rest of the operations require an equals sign
    Node n = null;
    try {
        String expression = ((StringAttribute) getAttributeWithName(EXPRESSION)).getValue();
        if (!expression.equals("")) {
            if (getListWithName(STEPS).getValues().isEmpty()) {
                n = Node.parseNode(((StringAttribute) getAttributeWithName(EXPRESSION)).getValue());
            } else {
                n = Node.parseNode(getLastStep());
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Previous expression has an error.", ERROR, JOptionPane.ERROR_MESSAGE);
        setActionCancelled(true);
        return;
    }
    if (!(n instanceof Expression && ((Expression) n).getOperator() instanceof Operator.Equals)) {
        //the expression does not have an equals sign
        JOptionPane.showMessageDialog(null, "Expression requires an equal sign for that operation.", ERROR, JOptionPane.ERROR_MESSAGE);
        setActionCancelled(true);
        return;
    }
    Expression ex = (Expression) n;
    Operator o = null;
    if (s.equals(OTHER_OPERATIONS)) {
        Object[] operations = { "sqrt", "sin", "cos", "tan" };
        String op = (String) JOptionPane.showInputDialog(null, "Choose an operation to apply to both sides", "Operation Selection", JOptionPane.PLAIN_MESSAGE, null, operations, "sqrt");
        if (op == null || op.equals("")) {
            setActionCancelled(true);
            return;
        }
        if (op.equals("sqrt"))
            o = new Operator.SquareRoot();
        else if (op.equals("sin"))
            o = new Operator.Sine();
        else if (op.equals("cos"))
            o = new Operator.Cosine();
        else if (op.equals("tan"))
            o = new Operator.Tangent();
        Expression newLeft = new Expression(o);
        Vector<Node> left = new Vector<>();
        Node newChild = ex.getChild(0);
        if (!op.equals("sqrt")) {
            newChild.setDisplayParentheses(true);
        }
        left.add(newChild);
        newLeft.setChildren(left);
        Expression newRight = new Expression(o);
        Vector<Node> right = new Vector<>();
        newChild = ex.getChild(1);
        if (!op.equals("sqrt")) {
            newChild.setDisplayParentheses(true);
        }
        right.add(newChild);
        newRight.setChildren(right);
        Vector<Node> exChildren = new Vector<>();
        exChildren.add(newLeft);
        exChildren.add(newRight);
        ex.setChildren(exChildren);
        try {
            getListWithName(STEPS).addValueWithString(ex.toStringRepresentation());
        } catch (NodeException e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        } catch (AttributeException e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        }
        return;
    }
    try {
        if (s.equals(ADD_TO_BOTH_SIDES) || s.equals(SUBTRACT_FROM_BOTH_SIDES) || s.equals(DIVIDE_BOTH_SIDES) || s.equals(MULTIPLY_BOTH_SIDES)) {
            String message = "";
            if (s.equals(ADD_TO_BOTH_SIDES)) {
                o = new Operator.Addition();
                message = "Add to both sides";
            } else if (s.equals(SUBTRACT_FROM_BOTH_SIDES)) {
                o = new Operator.Subtraction();
                message = "Subtract from both sides";
            } else if (s.equals(DIVIDE_BOTH_SIDES)) {
                o = new Operator.Division();
                message = "Divide both sides by";
            } else if (s.equals(MULTIPLY_BOTH_SIDES)) {
                o = new Operator.Multiplication();
                message = "Multiply both sides by";
            }
            Node newNode = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser(message);
            if (newNode == null) {
                setActionCancelled(true);
                return;
            }
            ex = ex.applyOpToBothSides(o, newNode, true);
            try {
                getListWithName(STEPS).addValueWithString(ex.toStringRepresentation());
            } catch (AttributeException e) {
                JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error with operation.", ERROR, JOptionPane.ERROR_MESSAGE);
    }
}
Also used : Operator(expression.Operator) Multiplication(expression.Operator.Multiplication) Node(expression.Node) StringAttribute(doc.attributes.StringAttribute) NodeException(expression.NodeException) AttributeException(doc.attributes.AttributeException) AttributeException(doc.attributes.AttributeException) NodeException(expression.NodeException) Expression(expression.Expression) Vector(java.util.Vector)

Example 7 with NodeException

use of expression.NodeException in project OpenNotebook by jaltekruse.

the class ExUtil method randomTerm.

public static Node randomTerm(int degree, String var, int minCoefficient, int maxCoefficient) {
    Number num = new Number(randomInt(minCoefficient, maxCoefficient, true));
    if (degree == 0) {
        return num;
    }
    Expression ex = new Expression(new Operator.Multiplication());
    ex.getChildren().add(num);
    try {
        if (degree == 1) {
            ex.getChildren().add(new Identifier(var));
            return ex;
        } else {
            ex.getChildren().add(new Expression(new Operator.Exponent(), new Identifier(var), new Number(degree)));
            return ex;
        }
    } catch (NodeException e) {
        throw new RuntimeException(e);
    }
}
Also used : Operator(expression.Operator) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) NodeException(expression.NodeException)

Example 8 with NodeException

use of expression.NodeException in project OpenNotebook by jaltekruse.

the class Graph method repaint.

public void repaint(Graphics g, int xSize, int ySize, float docZoomLevel, int xPicOrigin, int yPicOrigin, GraphObject gObj) {
    boolean hadError = false;
    DOC_ZOOM_LEVEL = docZoomLevel;
    X_PIC_ORIGIN = xPicOrigin;
    Y_PIC_ORIGIN = yPicOrigin;
    if (gObj != null) {
        pullVarsFromGraphObject(gObj, xSize, ySize);
    }
    g.setColor(Color.white);
    g.fillRect(X_PIC_ORIGIN, Y_PIC_ORIGIN, X_SIZE, Y_SIZE);
    cartAxis.draw(g);
    for (SingleGraph sg : singleGraphs) {
        try {
            sg.draw(g);
        } catch (NodeException e) {
            hadError = true;
        }
    }
    //the integrals that are drawn with the line above
    for (SingleGraph sg : singleGraphs) {
        if (sg.hasFocus()) {
            try {
                sg.draw(g);
            } catch (NodeException e) {
                // TODO Auto-generated catch block
                hadError = true;
            }
        }
    }
    lineGraph.draw(g);
    if (selectionGraphic != null) {
        selectionGraphic.draw(g);
    }
    if (dragDisk != null) {
        dragDisk.draw(g);
    }
    for (PointOnGrid p : freePoints) {
        p.draw(g);
    }
    try {
        barGraph.draw(g);
    } catch (NodeException e) {
        hadError = true;
    }
    //		graphCalcGraphics.drawInfoBoxes(g);
    g.setColor(Color.BLACK);
    ((Graphics2D) g).setStroke(new BasicStroke(2 * DOC_ZOOM_LEVEL));
    g.drawRect(X_PIC_ORIGIN, Y_PIC_ORIGIN, xSize, ySize);
    ((Graphics2D) g).setStroke(new BasicStroke(1));
    if (hadError) {
        drawErrorMessage(g, xSize, ySize, xPicOrigin, yPicOrigin);
    }
    if (gObj != null) {
        pushValsToGraphObject(gObj);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) NodeException(expression.NodeException) Graphics2D(java.awt.Graphics2D)

Example 9 with NodeException

use of expression.NodeException in project OpenNotebook by jaltekruse.

the class GraphObjectGUI method drawMathObject.

public void drawMathObject(GraphObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    ScaledSizeAndPosition sap = getSizeAndPosition(object, pageOrigin, zoomLevel);
    graph.removeAllSingleGraphs();
    graph.removeAllPoints();
    int colorIndex = 0;
    boolean hadError = false;
    for (StringAttribute ex : object.getExpressions()) {
        if (!ex.getValue().equals("")) {
            try {
                graph.AddGraph(new GraphedCartFunction(graph, "y=" + ex.getValue(), "x", "y", true, graphColors[colorIndex]));
            } catch (NodeException e) {
                // TODO Auto-generated catch block
                hadError = true;
            }
        }
        colorIndex++;
    }
    //graph.lineGraph.linePoints = object.getPoints();
    graph.lineGraph.linePoints.removeAllElements();
    graph.removeAllPoints();
    for (GridPointAttribute pt : object.getPoints()) {
        if (pt != null) {
            graph.addPoint(pt.getValue().getx(), pt.getValue().gety());
        }
    }
    graph.barGraph.values.removeAllElements();
    for (DoubleAttribute d : object.getBarGraphValues().getValues()) {
        graph.barGraph.values.add(d.getValue());
    }
    graph.barGraph.labels.removeAllElements();
    for (StringAttribute strAttr : object.getBarGraphLabels().getValues()) {
        graph.barGraph.labels.add(strAttr.getValue());
    }
    graph.barGraph.groupSize = object.getBarGraphGroupSize();
    graph.lineGraph.setColor(object.getLineGraphColor());
    synchronized (object.getLineGraphPoints().getValues()) {
        for (GridPointAttribute pt : object.getLineGraphPoints().getValues()) {
            if (pt != null) {
                graph.lineGraph.linePoints.add(new GridPoint(pt.getValue().getx(), pt.getValue().gety()));
            }
        }
    }
    graph.repaint(g, sap.getWidth(), sap.getHeight(), zoomLevel, sap.getxOrigin(), sap.getyOrigin(), object);
    if (hadError) {
        FontMetrics fm = g.getFontMetrics();
        int errorWidth = fm.stringWidth("error");
        g.setColor(Color.WHITE);
        g.fillRect((sap.getxOrigin() + sap.getWidth() / 2) - errorWidth / 2, (sap.getyOrigin() + sap.getHeight() / 2) - fm.getHeight() / 2, errorWidth + 4, fm.getHeight() + 4);
        g.setColor(Color.BLACK);
        g.drawRect((sap.getxOrigin() + sap.getWidth() / 2) - errorWidth / 2, (sap.getyOrigin() + sap.getHeight() / 2) - fm.getHeight() / 2, errorWidth + 4, fm.getHeight() + 4);
        g.setColor(Color.RED);
        g.drawString("error", (sap.getxOrigin() + sap.getWidth() / 2) - errorWidth / 2 + 2, (sap.getyOrigin() + sap.getHeight() / 2) + fm.getHeight() / 2);
    }
}
Also used : GridPoint(doc.GridPoint) FontMetrics(java.awt.FontMetrics) GridPointAttribute(doc.attributes.GridPointAttribute) StringAttribute(doc.attributes.StringAttribute) NodeException(expression.NodeException) GraphedCartFunction(doc_gui.graph.GraphedCartFunction) Point(java.awt.Point) GridPoint(doc.GridPoint) DoubleAttribute(doc.attributes.DoubleAttribute)

Example 10 with NodeException

use of expression.NodeException in project OpenNotebook by jaltekruse.

the class NotebookPanel method getExpressionFromUser.

public Node getExpressionFromUser(String message, String defaultInput) {
    String lastEx = defaultInput;
    Node newNode = null;
    while (true) {
        lastEx = createPopupBelowCurrObject(message, lastEx);
        if (lastEx == null) {
            return null;
        }
        try {
            newNode = Node.parseNode(lastEx);
            return newNode;
        } catch (NodeException e) {
            JOptionPane.showMessageDialog(null, "Error with expression.", WARNING, JOptionPane.WARNING_MESSAGE);
        }
    }
}
Also used : Node(expression.Node) NodeException(expression.NodeException)

Aggregations

NodeException (expression.NodeException)10 Node (expression.Node)7 StringAttribute (doc.attributes.StringAttribute)4 Expression (expression.Expression)4 Number (expression.Number)3 Operator (expression.Operator)3 Vector (java.util.Vector)3 AttributeException (doc.attributes.AttributeException)2 Identifier (expression.Identifier)2 BasicStroke (java.awt.BasicStroke)2 Graphics2D (java.awt.Graphics2D)2 Point (java.awt.Point)2 GridPoint (doc.GridPoint)1 DoubleAttribute (doc.attributes.DoubleAttribute)1 GridPointAttribute (doc.attributes.GridPointAttribute)1 ListAttribute (doc.attributes.ListAttribute)1 ExpressionObject (doc.mathobjects.ExpressionObject)1 GeneratedProblem (doc.mathobjects.GeneratedProblem)1 GraphedCartFunction (doc_gui.graph.GraphedCartFunction)1 Multiplication (expression.Operator.Multiplication)1