Search in sources :

Example 6 with StringAttribute

use of doc.attributes.StringAttribute in project OpenNotebook by jaltekruse.

the class ExpressionObjectGUI method drawInteractiveComponents.

public void drawInteractiveComponents(ExpressionObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    g.setColor(Color.BLACK);
    ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
    int outerBufferSpace = (int) (5 * zoomLevel);
    int stepBufferSpace = (int) (10 * zoomLevel);
    Graphics2D g2d = (Graphics2D) g;
    RootNodeGraphic rootGraphic;
    if (!(object.getExpression().equals("") && object.allAnswersBlank())) {
        // if any of the steps cannot be rendered, this information will allow
        // space to be left for printing an error message in its place
        g.setFont(g.getFont().deriveFont(sap.getFontSize()));
        int errorMessageHeight = g.getFontMetrics().getHeight();
        int errorMessageWidth = g.getFontMetrics().stringWidth(EX_ERROR);
        Node n = null;
        int totalHeight = 0;
        int greatestWidth = 0;
        Vector<Integer> indeciesInError = new Vector<>();
        Vector<Integer> yPosOfSteps = new Vector<>();
        int currentIndex = 0;
        Vector<RootNodeGraphic> expressions = new Vector<>();
        // add the expression
        try {
            n = parser.parseNode(object.getExpression());
            boolean objectChanged = currentSelectionStep || current == null || currentExObj != object || !object.getDecimalRectangleBounds().equals(currentPosSize) || !currentEx.equals(object.getExpression());
            if (objectChanged) {
                rootGraphic = new RootNodeGraphic(n);
                rootGraphic.generateExpressionGraphic(g, outerBufferSpace + sap.getxOrigin(), outerBufferSpace + sap.getyOrigin(), (int) sap.getFontSize(), zoomLevel);
            } else if (currentZoom != zoomLevel) {
                rootGraphic = current;
                rootGraphic.requestSize(g, outerBufferSpace + sap.getxOrigin(), outerBufferSpace + sap.getyOrigin(), (int) sap.getFontSize(), zoomLevel);
            } else {
                rootGraphic = current;
                rootGraphic.setGraphics((Graphics2D) g);
            }
            // keep these next three lines in the try catch block, they should only happen
            // if they line above does not throw an error
            expressions.add(rootGraphic);
            yPosOfSteps.add(rootGraphic.yPos);
            totalHeight = rootGraphic.getHeight();
            greatestWidth = rootGraphic.getWidth();
            currentSelectionStep = false;
            current = rootGraphic;
            currentExObj = object;
            currentEx = object.getExpression();
            currentPosSize = object.getDecimalRectangleBounds();
            currentZoom = zoomLevel;
        } catch (Exception e) {
            indeciesInError.add(currentIndex);
            yPosOfSteps.add(outerBufferSpace + sap.getyOrigin());
            expressions.add(null);
            totalHeight += errorMessageHeight;
            greatestWidth = errorMessageWidth;
        }
        Vector<StringAttribute> steps = (Vector<StringAttribute>) object.getListWithName(ExpressionObject.STEPS).getValues();
        // TODO - confirm if this works with multiple correct answers
        for (StringAttribute strAtt : object.getCorrectAnswers()) {
            if (!strAtt.getValue().equals("")) {
                steps.add(strAtt);
            }
        }
        // add the steps to the list of expressions to render
        String s;
        int i = 0;
        for (StringAttribute mAtt : steps) {
            s = mAtt.getValue();
            currentIndex++;
            totalHeight += stepBufferSpace;
            try {
                n = parser.parseNode(s);
                rootGraphic = new RootNodeGraphic(n);
                //                    current = rootGraphic;
                rootGraphic.generateExpressionGraphic(g, sap.getxOrigin() + outerBufferSpace, outerBufferSpace + sap.getyOrigin() + totalHeight, (int) sap.getFontSize(), zoomLevel);
                expressions.add(rootGraphic);
                yPosOfSteps.add(rootGraphic.yPos);
                if (rootGraphic.getWidth() > greatestWidth) {
                    greatestWidth = rootGraphic.getWidth();
                }
                totalHeight += rootGraphic.getHeight();
                i++;
            } catch (Exception e) {
                indeciesInError.add(currentIndex);
                totalHeight += errorMessageHeight;
                if (errorMessageWidth > greatestWidth) {
                    greatestWidth = errorMessageWidth;
                }
                yPosOfSteps.add(outerBufferSpace + sap.getyOrigin() + totalHeight);
            }
        }
        // remove the correct answers, so they are not permanently added as steps
        steps.removeAll(object.getCorrectAnswers());
        if (object.getColor() != null) {
            g.setColor(object.getColor());
        } else {
            g.setColor(Color.white);
        }
        g.fillRect(sap.getxOrigin(), sap.getyOrigin(), greatestWidth + 2 * outerBufferSpace, totalHeight + 2 * outerBufferSpace);
        g.setColor(Color.BLACK);
        int index = 0;
        int numberOfSteps = steps.size() + 1;
        for (RootNodeGraphic r : expressions) {
            try {
                if (indeciesInError.contains(index)) {
                    g.setFont(g.getFont().deriveFont(sap.getFontSize()));
                    g.setColor(Color.RED);
                    g.drawString(EX_ERROR, sap.getxOrigin() + outerBufferSpace, yPosOfSteps.get(index) + errorMessageHeight);
                    g.setColor(Color.BLACK);
                    index++;
                    continue;
                } else {
                    if (index >= numberOfSteps) {
                        // draw the answers with a highlight
                        g.setColor(new Color(180, 255, 100));
                        g.fillRect(r.xPos - 4, r.yPos - 4, r.getWidth() + 8, r.getHeight() + 8);
                        g.setColor(Color.BLACK);
                    }
                    if (index != 0)
                        r.setCursor(new Cursor(null, 0));
                    r.draw();
                }
            } catch (NodeException e) {
                e.printStackTrace();
                g.setFont(g.getFont().deriveFont(sap.getFontSize()));
                g.setColor(Color.RED);
                g.drawString(EX_ERROR, r.xPos, r.yPos + errorMessageHeight);
            }
            index++;
        }
        if (greatestWidth > 0 && totalHeight > 0) {
            object.setWidth((int) ((greatestWidth + 2 * outerBufferSpace) / zoomLevel));
            object.setHeight((int) ((totalHeight + 2 * outerBufferSpace) / zoomLevel));
        }
    } else {
        g2d.setStroke(new BasicStroke());
        g2d.setPaint(Color.BLACK);
        g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Node(expression.Node) Color(java.awt.Color) StringAttribute(doc.attributes.StringAttribute) NodeException(expression.NodeException) RootNodeGraphic(math_rendering.RootNodeGraphic) Cursor(math_rendering.Cursor) Point(java.awt.Point) NodeException(expression.NodeException) Graphics2D(java.awt.Graphics2D) Vector(java.util.Vector)

Example 7 with StringAttribute

use of doc.attributes.StringAttribute in project OpenNotebook by jaltekruse.

the class AnswerBoxObject method getObjectWithAnswer.

public AnswerBoxObject getObjectWithAnswer() {
    AnswerBoxObject o = (AnswerBoxObject) this.clone();
    try {
        String str = "";
        for (StringAttribute strAtt : o.getCorrectAnswers().getValues()) {
            str += strAtt.getValue() + "; ";
        }
        o.setStudentAnswer(str);
    } catch (AttributeException e) {
        // should not throw an error, just setting a string attribute
        e.printStackTrace();
    }
    return o;
}
Also used : StringAttribute(doc.attributes.StringAttribute) AttributeException(doc.attributes.AttributeException)

Example 8 with StringAttribute

use of doc.attributes.StringAttribute in project OpenNotebook by jaltekruse.

the class AnswerBoxObject method addDefaultAttributes.

@Override
public void addDefaultAttributes() {
    //this.addAction(GRADE);
    addAttribute(new StringAttribute(STUDENT_ANSWER, "", true, true));
    addList(new ListAttribute<>(CORRECT_ANSWERS, new StringAttribute(""), 20, true, false));
    addAttribute(new IntegerAttribute(FONT_SIZE, 12, 1, 50, true, false));
    addAttribute(new IntegerAttribute(TOTAL_POINTS, 5, 1, 100, true, false));
    addAttribute(new IntegerAttribute(STUDENT_SCORE, 5, 0, 100, true, false));
    addAttribute(new EnumeratedAttribute(ANSWER_TYPE, PLAIN_TEXT, ANSWER_TYPES));
}
Also used : StringAttribute(doc.attributes.StringAttribute) EnumeratedAttribute(doc.attributes.EnumeratedAttribute) IntegerAttribute(doc.attributes.IntegerAttribute)

Example 9 with StringAttribute

use of doc.attributes.StringAttribute 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 10 with StringAttribute

use of doc.attributes.StringAttribute in project OpenNotebook by jaltekruse.

the class GraphObject method setDefaults.

public void setDefaults() {
    try {
        addList(new ListAttribute<>(EXPRESSIONS, new StringAttribute(EXPRESSION), 6, true, false));
        addList(new ListAttribute<>(POINTS, new GridPointAttribute("", -7E8, 7E8, -7E8, 7E8), 1000000, true, false));
        addList(new ListAttribute<>(LINE_GRAPH, new GridPointAttribute("", -7E8, 7E8, -7E8, 7E8), 1000000, true, false));
        addList(new ListAttribute<>(BAR_GRAPH_VALUES, new DoubleAttribute("", -7E8, 7E8), 50, false, true));
        addAttribute(new IntegerAttribute(BAR_GRAPH_GROUP_SIZE, 1, 1, 100, false));
        addList(new ListAttribute<>(BAR_GRAPH_LABELS, new StringAttribute(""), 100, false, true));
        addAttribute(new SelectionAttribute(SELECTION, new Selection(), false));
        addAttribute(new DoubleAttribute(X_MIN, -7E8, 7E8, true, true));
        getAttributeWithName(X_MIN).setValueWithString("-5");
        addAttribute(new DoubleAttribute(X_MAX, -7E8, 7E8, true, true));
        getAttributeWithName(X_MAX).setValueWithString("5");
        addAttribute(new DoubleAttribute(Y_MIN, -7E8, 7E8, true, true));
        getAttributeWithName(Y_MIN).setValueWithString("-5");
        addAttribute(new DoubleAttribute(Y_MAX, -7E8, 7E8, true, true));
        getAttributeWithName(Y_MAX).setValueWithString("5");
        addAttribute(new DoubleAttribute(X_STEP, -3E8, 3E8, true, true));
        getAttributeWithName(X_STEP).setValueWithString("1");
        addAttribute(new DoubleAttribute(Y_STEP, -3E8, 3E8, true, true));
        getAttributeWithName(Y_STEP).setValueWithString("1");
        addAttribute(new IntegerAttribute(FONT_SIZE, 1, 20));
        getAttributeWithName(FONT_SIZE).setValueWithString("8");
        addAttribute(new BooleanAttribute(SHOW_AXIS));
        getAttributeWithName(SHOW_AXIS).setValue(true);
        addAttribute(new BooleanAttribute(SHOW_NUMBERS));
        getAttributeWithName(SHOW_NUMBERS).setValue(true);
        addAttribute(new BooleanAttribute(SHOW_GRID));
        getAttributeWithName(SHOW_GRID).setValue(true);
        addAttribute(new ColorAttribute(LINE_GRAPH_COLOR));
        getAttributeWithName(LINE_GRAPH_COLOR).setValue(Color.BLUE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BooleanAttribute(doc.attributes.BooleanAttribute) Selection(doc_gui.graph.Selection) GridPointAttribute(doc.attributes.GridPointAttribute) StringAttribute(doc.attributes.StringAttribute) ColorAttribute(doc.attributes.ColorAttribute) IntegerAttribute(doc.attributes.IntegerAttribute) SelectionAttribute(doc.attributes.SelectionAttribute) AttributeException(doc.attributes.AttributeException) DoubleAttribute(doc.attributes.DoubleAttribute)

Aggregations

StringAttribute (doc.attributes.StringAttribute)14 IntegerAttribute (doc.attributes.IntegerAttribute)6 AttributeException (doc.attributes.AttributeException)5 NodeException (expression.NodeException)5 BooleanAttribute (doc.attributes.BooleanAttribute)4 DoubleAttribute (doc.attributes.DoubleAttribute)4 Node (expression.Node)4 GridPointAttribute (doc.attributes.GridPointAttribute)3 Vector (java.util.Vector)3 ColorAttribute (doc.attributes.ColorAttribute)2 DateAttribute (doc.attributes.DateAttribute)2 EnumeratedAttribute (doc.attributes.EnumeratedAttribute)2 MathObjectAttribute (doc.attributes.MathObjectAttribute)2 Expression (expression.Expression)2 Point (java.awt.Point)2 GridPoint (doc.GridPoint)1 ListAttribute (doc.attributes.ListAttribute)1 SelectionAttribute (doc.attributes.SelectionAttribute)1 UUIDAttribute (doc.attributes.UUIDAttribute)1 ExpressionObject (doc.mathobjects.ExpressionObject)1