Search in sources :

Example 11 with AttributeException

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

the class NotebookPanel method viewProblemGenrator.

public void viewProblemGenrator(ProblemGenerator probGen) {
    Document newDoc = new Document("Problem Generator");
    newDoc.addBlankPage();
    Page page = newDoc.getPage(0);
    TextObject textObj = new TextObject(page, 5 + page.getyMargin(), 5 + newDoc.getPage(0).getxMargin(), page.getWidth() - 2 * page.getxMargin(), 150, 12, VIEW_PROBLEM_FORUMLA_MESSAGE);
    try {
        textObj.setAttributeValue(TextObject.SHOW_BOX, false);
    } catch (AttributeException e) {
    // should not happen
    }
    newDoc.getPage(0).addObject(textObj);
    MathObject newProb = ((MathObject) probGen).clone();
    newProb.setParentContainer(newDoc.getPage(0));
    newProb.setyPos(page.getxMargin() + 165);
    newProb.setxPos((newDoc.getPage(0).getWidth() - 2 * page.getxMargin() - newProb.getWidth()) / 2 + page.getxMargin());
    newDoc.getPage(0).addObject(newProb);
    this.addDoc(newDoc);
}
Also used : TextObject(doc.mathobjects.TextObject) MathObject(doc.mathobjects.MathObject) AttributeException(doc.attributes.AttributeException)

Example 12 with AttributeException

use of doc.attributes.AttributeException 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 13 with AttributeException

use of doc.attributes.AttributeException 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 14 with AttributeException

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

the class UnZip method unZipIt.

/**
	 * Unzip it
	 * @param zipFileName input zip file
	 */
public void unZipIt(String zipFileName, NotebookPanel notebookPanel, Document key) throws IOException {
    ZipFile zipFile;
    //get the zip file content
    zipFile = new ZipFile(zipFileName);
    //get the zipped file list entry
    Enumeration<? extends ZipEntry> zes = zipFile.entries();
    ZipEntry ze = null;
    List<String> studentDocNames = new ArrayList<>();
    List<Document> docs = new ArrayList<>();
    while (zes.hasMoreElements()) {
        ze = zes.nextElement();
        String fileName = ze.getName();
        // ZipEntries than files
        try {
            InputStream inputStream = zipFile.getInputStream(ze);
            if (inputStream.available() == 0) {
                break;
            }
            studentDocNames.add(fileName);
            docs.add(notebookPanel.openDoc(inputStream, fileName));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    Document resultDoc = new Document("Student Summary");
    resultDoc.addBlankPage();
    List<List<MathObject>> incorrectWork = new ArrayList<>();
    List<List<MathObject>> allStudentWork = new ArrayList<>();
    List<List<String>> answers = getAnswers(key);
    for (int i = 0; i < docs.get(0).getPages().size(); i++) {
        List<MathObject> allStudentWorkForOneProblem = new ArrayList<>();
        List<MathObject> allIncorrectStudentWorkForOneProblem = new ArrayList<>();
        for (Document doc : docs) {
            if (doc == null)
                continue;
            Page p = doc.getPage(i);
            List<String> studentAnswers = getStudentAnswers(p);
            boolean answerCorrect = false;
            // if there are no answers the problem must be manually graded
            if (!answers.get(i).isEmpty() && studentAnswers.size() == answers.get(i).size() && studentAnswers.containsAll(answers.get(i))) {
                answerCorrect = true;
            }
            // combine together all of the content of one page into a group, it will
            // all be shown to the teacher if the
            Grouping group = new Grouping(p);
            for (MathObject mObj : p.getObjects()) {
                group.addObject(mObj);
            }
            GridPoint gp = Document.getFirstWhiteSpaceOnPage(p);
            int numPoints = 3;
            int scoreLabelWidth = 40;
            int scoreLabelHeight = 20;
            TextObject score = new TextObject(p, (int) gp.getx(), (int) gp.gety(), scoreLabelWidth, scoreLabelHeight, 12, "Score");
            AnswerBoxObject scoreInput = new AnswerBoxObject(p, (int) gp.getx() + scoreLabelWidth, (int) gp.gety(), scoreLabelWidth, scoreLabelHeight);
            TextObject points = new TextObject(p, (int) gp.getx() + 2 * scoreLabelWidth + 10, (int) gp.gety(), scoreLabelWidth, scoreLabelHeight, 12, "of " + numPoints);
            try {
                if (answerCorrect) {
                    scoreInput.setAttributeValue(AnswerBoxObject.STUDENT_ANSWER, numPoints + "");
                }
            } catch (AttributeException e) {
                // should not happen
                e.printStackTrace();
            }
            TextObject feedback = new TextObject(p, (int) gp.getx(), (int) gp.gety() + scoreLabelHeight + 5, scoreLabelWidth * 2, scoreLabelHeight, 12, "Feedback");
            AnswerBoxObject feedbackInput = new AnswerBoxObject(p, (int) gp.getx(), (int) gp.gety() + 2 * (scoreLabelHeight + 5), scoreLabelWidth * 3, scoreLabelHeight * 3);
            group.addObject(score);
            group.addObject(scoreInput);
            group.addObject(points);
            group.addObject(feedback);
            group.addObject(feedbackInput);
            group.adjustSizeToFitChildren();
            if (!answerCorrect) {
                //					group.setHidden(true);
                allIncorrectStudentWorkForOneProblem.add(group);
            }
            allStudentWorkForOneProblem.add(group);
        }
        incorrectWork.add(allIncorrectStudentWorkForOneProblem);
        allStudentWork.add(allStudentWorkForOneProblem);
    }
    StringBuffer allAnswers = new StringBuffer();
    for (List<String> problemAnswers : answers) {
        for (String answer : problemAnswers) {
            allAnswers.append(answer).append(",");
        }
        allAnswers.append("::");
    }
    //		JOptionPane.showMessageDialog(null,
    //				allAnswers,
    //				"Error", JOptionPane.ERROR_MESSAGE);
    // TODO - let the teachers give us a list
    // of problem numbers in the assignment
    // the lists of objects will not be sparse
    int[] problemNumbers = { 5, 7, 9 };
    int problemNumber = 0;
    for (List<MathObject> problems : incorrectWork) {
        Document.layoutProblems(problems, "Problem " + problemNumber, resultDoc, false);
        problemNumber++;
    }
    notebookPanel.addDoc(resultDoc);
    notebookPanel.getCurrentDocViewer().gradePage = true;
    notebookPanel.getCurrentDocViewer().studentFeedbackDocNames = studentDocNames;
    notebookPanel.getCurrentDocViewer().allStudentWork = allStudentWork;
    zipFile.close();
}
Also used : ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) AttributeException(doc.attributes.AttributeException) ArrayList(java.util.ArrayList) List(java.util.List) ZipInputStream(java.util.zip.ZipInputStream) ZipFile(java.util.zip.ZipFile)

Example 15 with AttributeException

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

the class AdditionPropertyOfEquality method setAttributes.

@Override
protected void setAttributes() {
    setName("One Step Equations, Addition/Subtraction");
    setAuthor("Open Notebook Staff");
    setDirections("Solve for the unknown variable.");
    try {
        setAttributeValue(UUID_STR, new UUID(4190756403957524305L, 3562395472104360419L));
        addTags("Solve", "Addition", "Subtraction", "Equation");
    } catch (AttributeException e) {
        // should not be thrown
        throw new RuntimeException(e);
    }
    setDate(new Date(2, 1, 2011));
}
Also used : UUID(java.util.UUID) AttributeException(doc.attributes.AttributeException) Date(doc.attributes.Date)

Aggregations

AttributeException (doc.attributes.AttributeException)20 Date (doc.attributes.Date)9 UUID (java.util.UUID)9 StringAttribute (doc.attributes.StringAttribute)4 Node (expression.Node)3 NodeException (expression.NodeException)3 Vector (java.util.Vector)3 MathObjectAttribute (doc.attributes.MathObjectAttribute)2 GridBagConstraints (java.awt.GridBagConstraints)2 Insets (java.awt.Insets)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 JLabel (javax.swing.JLabel)2 Document (doc.Document)1 GridPoint (doc.GridPoint)1 Page (doc.Page)1 ProblemDatabase (doc.ProblemDatabase)1 BooleanAttribute (doc.attributes.BooleanAttribute)1 DoubleAttribute (doc.attributes.DoubleAttribute)1 GridPointAttribute (doc.attributes.GridPointAttribute)1