Search in sources :

Example 1 with Page

use of doc.Page in project OpenNotebook by jaltekruse.

the class DocReader method startElement.

@Override
public void startElement(String uri, String name, String qName, Attributes atts) {
    if (qName.equals(Document.OPEN_NOTEBOOK_DOC)) {
        // the name of the file in the filesystem
        // will allow documents that are renamed outside of
        // the application to take their new name
        doc = new Document(fileName);
        doc.setAuthor(atts.getValue(Document.AUTHOR));
        if (atts.getValue(Document.LAST_PROBLEM_NUMBER) != null) {
            doc.setLastProblemNumber(Integer.parseInt(atts.getValue(Document.LAST_PROBLEM_NUMBER)));
        }
        if (atts.getValue(Document.X_MARGIN) != null) {
            doc.setxMargin(Double.parseDouble(atts.getValue(Document.X_MARGIN)));
        }
        if (atts.getValue(Document.Y_MARGIN) != null) {
            doc.setyMargin(Double.parseDouble(atts.getValue(Document.Y_MARGIN)));
        }
        return;
    } else if (qName.equals(ProblemDatabase.NAME)) {
        database = new ProblemDatabase();
        readingProblemDatabase = true;
        return;
    } else if (qName.equals(Document.GENERATORS)) {
        readingGenerators = true;
        return;
    } else if (qName.equals(ListAttribute.LIST)) {
        if (mObj != null) {
            // grab the list from the current object with the specified name
            list = mObj.getListWithName(atts.getValue(NAME));
            if (list != null && !overridenLists.contains(atts.getValue(ListAttribute.NAME))) {
                // the object had a list with the given name
                list.removeAll();
                readingList = true;
            }
            if (DEBUG) {
                System.out.println("list added: " + list);
            }
            return;
        }
    } else if (qName.equals(ListAttribute.ENTRY)) {
        if (!readingList) {
            // this is an entry for a list that is currently not in use
            if (DEBUG) {
                System.out.println("  found entry tag, but not reading a list");
            }
            return;
        }
        try {
            list.addValueWithString(atts.getValue(ListAttribute.VAL));
            return;
        } catch (AttributeException e) {
            hadAttributeError = true;
            attributeNameInError = atts.getValue(NAME);
            attributeValueInError = atts.getValue(VALUE);
            objectWithError = mObj.getClass().getSimpleName();
            return;
        }
    } else if (doc != null || readingProblemDatabase) {
        if (qName.equals("Page")) {
            page = new Page(doc);
            doc.addPage(page);
            return;
        }
        if (page != null || readingGenerators || readingProblemDatabase) {
            if (mObj != null) {
                if (DEBUG) {
                    System.out.println("in object, should be finding attributes, or other objects if in group");
                }
                if (readAttribute(uri, name, qName, atts)) {
                    // if the current tag was an attribute
                    if (DEBUG) {
                        System.out.println("return found attribute");
                    }
                    return;
                }
            }
            if (DEBUG) {
                System.out.println("tag was not attribute " + qName);
            }
            if (qName.equals("survey")) {
                int j = 1;
            }
            mObj = MathObject.newInstanceWithType(qName);
            if (mObj == null) {
                mObj = readOldObjectName(qName);
            }
            if (mObj != null) {
                if (DEBUG) {
                    System.out.println();
                    System.out.println("added Object: " + mObj.getClass().getSimpleName());
                }
                if (page != null) {
                    mObj.setParentContainer(page);
                }
            } else {
                foundBadTag = true;
                badTag = qName;
                return;
            }
            if (!containerStack.isEmpty()) {
                // if an object tag was found in a group, there is an object in a group
                if (mObj != null) {
                    objectsInGroup.get(objectsInGroup.size() - 1).add(mObj);
                    if (mObj instanceof Grouping) {
                        containerStack.add((Grouping) mObj);
                        objectsInGroup.add(new Vector<MathObject>());
                    }
                }
            } else {
                // in the document, or in the background of the application
                if (readingGenerators || readingProblemDatabase) {
                    if (DEBUG) {
                        System.out.println("wait until attributes are read before adding to database");
                    }
                } else {
                    page.addObject(mObj);
                }
                if (mObj instanceof Grouping) {
                    containerStack.add((Grouping) mObj);
                    objectsInGroup.add(new Vector<MathObject>());
                }
            }
        }
    }
}
Also used : Page(doc.Page) Grouping(doc.mathobjects.Grouping) ProblemDatabase(doc.ProblemDatabase) Document(doc.Document) AttributeException(doc.attributes.AttributeException) Vector(java.util.Vector)

Example 2 with Page

use of doc.Page in project OpenNotebook by jaltekruse.

the class DocMouseListener method mouseClicked.

public void mouseClicked(MouseEvent e) {
    docPanel.requestFocus();
    boolean clickHandled = false, requiresRedraw = false;
    if (selectionRectRequiresMouseDrag || selectionRectBeingResized) {
        selectionRectRequiresMouseDrag = false;
        selectionRectBeingResized = false;
        docPanel.setSelectionRect(null);
    }
    PointInDocument clickedPt = docPanel.panelPt2DocPt(e.getX(), e.getY());
    if (!clickedPt.isOutSidePage()) {
        Rectangle objRect;
        // gives extra space around object to allow selection,
        // most useful for when objects are very thin
        int clickBuffer = 3;
        Vector<MathObject> currPageObjects = docPanel.getDoc().getPage(clickedPt.getPage()).getObjects();
        MathObject mObj, oldFocused;
        oldFocused = docPanel.getFocusedObject();
        Page page = docPanel.getDoc().getPage(clickedPt.getPage());
        for (int i = (currPageObjects.size() - 1); i >= 0; i--) {
            // cycle through all of the objects belonging the page that was clicked on
            mObj = currPageObjects.get(i);
            objRect = mObj.getBounds();
            if (objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos())) && mObj != docPanel.getFocusedObject()) {
                // the click occurred within an object, that was already selected
                if (mObj instanceof Grouping && docPanel.isInStudentMode()) {
                    for (MathObject subObj : ((Grouping) mObj).getObjects()) {
                        objRect = subObj.getBounds();
                        if (objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos())) && mObj != docPanel.getFocusedObject()) {
                            docPanel.setFocusedObject(subObj);
                            docPanel.repaintDoc();
                            return;
                        }
                    }
                } else {
                    if (e.isShiftDown()) {
                        docPanel.addObjectToSelection(mObj);
                    } else {
                        docPanel.setFocusedObject(mObj);
                    }
                    docPanel.repaintDoc();
                    docPanel.updateObjectToolFrame();
                    return;
                }
            }
            // without ungrouping
            if (e.getClickCount() == 2 && objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos())) && mObj == docPanel.getFocusedObject() && docPanel.getFocusedObject() instanceof Grouping) {
                for (MathObject subObj : ((Grouping) mObj).getObjects()) {
                    objRect = new Rectangle(subObj.getxPos(), subObj.getyPos(), subObj.getWidth(), subObj.getHeight());
                    if (objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos()))) {
                        docPanel.setFocusedObject(subObj);
                        docPanel.repaintDoc();
                        return;
                    }
                }
            }
        }
        if (docPanel.getFocusedObject() == oldFocused) {
            if (oldFocused != null) {
                objRect = new Rectangle(oldFocused.getxPos() - clickBuffer, oldFocused.getyPos() - clickBuffer, oldFocused.getWidth() + 2 * clickBuffer, oldFocused.getHeight() + 2 * clickBuffer);
                if (!objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos()))) {
                    // send event to object
                    docPanel.setFocusedObject(null);
                    docPanel.repaintDoc();
                    return;
                }
            }
        }
    } else {
        // click was outside of page
        docPanel.setSelectedPage(null);
        docPanel.setFocusedObject(null);
        docPanel.repaintDoc();
        return;
    }
    // objects can be off of the page, so this check must happen out here
    if (docPanel.getFocusedObject() != null && !clickHandled) {
        Point objPos = null;
        objPos = docPanel.getObjectPos(docPanel.getFocusedObject());
        Rectangle focusedRect = new Rectangle(objPos.x, objPos.y, (int) (docPanel.getFocusedObject().getWidth() * docPanel.getZoomLevel()), (int) (docPanel.getFocusedObject().getHeight() * docPanel.getZoomLevel()));
        if (focusedRect.contains(new Point(e.getX(), e.getY()))) {
            // user clicked on the object that already had focus, send an event to the objet so it
            // can handle it for (for drag and drop, moving graphs etc.)
            docPanel.getPageGUI().handleMouseAction(docPanel.getFocusedObject(), (int) (e.getX() - objPos.getX()), (int) (e.getY() - objPos.getY()), PageGUI.MOUSE_LEFT_CLICK);
            clickHandled = true;
            requiresRedraw = true;
        }
    // throw click down to focused object
    }
    if (!clickHandled && !clickedPt.isOutSidePage()) {
        // the click hit a page, but missed all of its objects, select the
        // page
        docPanel.setSelectedPage(clickedPt.getPage());
        clickHandled = true;
        requiresRedraw = true;
    }
    if (!clickHandled) {
        // click occurred on a non-active part of screen,
        // unfocus current object
        docPanel.setFocusedObject(null);
        requiresRedraw = true;
    }
    if (requiresRedraw) {
        docPanel.repaintDoc();
    }
}
Also used : Rectangle(java.awt.Rectangle) Page(doc.Page) Grouping(doc.mathobjects.Grouping) Point(java.awt.Point) MathObject(doc.mathobjects.MathObject) Point(java.awt.Point) PointInDocument(doc.PointInDocument)

Example 3 with Page

use of doc.Page in project OpenNotebook by jaltekruse.

the class VariableValueInsertionProblem method performSpecialObjectAction.

@Override
public void performSpecialObjectAction(String s) {
    if (s.equals(REMOVE_PROBLEM)) {
        Object[] options = { "Continue", "Cancel" };
        int n = JOptionPane.showOptionDialog(null, "This operation will ungroup the objects in this problem and\n" + "place them back on the page. It will also delete the problem's\n" + "script data.", "Revert a Problem", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
        if (n == 0) {
            this.unGroup();
            this.getParentContainer().removeObject(this);
            this.getParentContainer().getParentDoc().getDocViewerPanel().setFocusedObject(null);
        } else {
            this.setActionCancelled(true);
        }
    } else if (s.equals(STORE_IN_DATABASE)) {
        getParentContainer().getParentDoc().getDocViewerPanel().getNotebookPanel().addProbelmToDatabase((ProblemGenerator) clone());
    } else if (s.equals(GENERATE_NEW)) {
        int number = 0;
        do {
            String num = (String) JOptionPane.showInputDialog(null, "Number of problems", "Number of problems.", JOptionPane.PLAIN_MESSAGE, null, null, null);
            if (num == null) {
                // the user hit cancel or the exit button on the dialog
                this.setActionCancelled(true);
                return;
            }
            try {
                number = Integer.parseInt(num);
                if (number < 1 || number > 50) {
                    JOptionPane.showMessageDialog(null, "Input must be an integer between 1 and 50", ERROR, JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Input must be an integer between 1 and 50", ERROR, JOptionPane.ERROR_MESSAGE);
            }
        } while (number < 1 || number > 50);
        int greatestWidth = 0, greatestHeight = 0;
        Vector<GeneratedProblem> newProblems = new Vector<>();
        int difficulty;
        for (int i = 0; i < number; i++) {
            if (i < number / 3) {
                difficulty = EASY;
            } else if (i < number * (2 / 3)) {
                difficulty = MEDIUM;
            } else {
                difficulty = HARD;
            }
            GeneratedProblem newProb = generateProblem(difficulty);
            newProblems.add(newProb);
            if (newProb.getWidth() > greatestWidth) {
                greatestWidth = newProb.getWidth();
            }
            if (newProb.getHeight() > greatestHeight) {
                greatestHeight = newProb.getHeight();
            }
        }
        // reference by the children
        try {
            getParentContainer().getParentDoc().addGenerator(this);
        } catch (Exception e1) {
        // TODO - What to do if generator ID collides with another, or if
        // the same generator tries to get added twice
        }
        int numColumns = ((getParentContainer().getWidth() - 2 * getParentPage().getxMargin() - bufferSpace) / (greatestWidth + bufferSpace));
        int totalExtraSpace = ((getParentContainer().getWidth() - 2 * getParentPage().getxMargin() - bufferSpace) % (greatestWidth + bufferSpace));
        int extraColumnSpace = totalExtraSpace / (numColumns + 1);
        int currColumn = 0;
        int curryPos = getyPos() + getHeight() + bufferSpace;
        Page currentPage = getParentPage();
        for (MathObject mObj : newProblems) {
            mObj.setxPos(currentPage.getxMargin() + bufferSpace + extraColumnSpace + currColumn * (greatestWidth + bufferSpace + extraColumnSpace));
            mObj.setyPos(curryPos);
            if (!mObj.isOnPage()) {
                if (currentPage.getParentDoc().getNumPages() < currentPage.getParentDoc().getPageIndex(currentPage) + 2) {
                    // a new page must be added to add the objects
                    currentPage.getParentDoc().addBlankPage();
                    currentPage = currentPage.getParentDoc().getPage(currentPage.getParentDoc().getNumPages() - 1);
                    currentPage.getParentDoc().getDocViewerPanel().resizeViewWindow();
                } else {
                    // there is a next page on the document that the new objects can be added to
                    currentPage = currentPage.getParentDoc().getPage(currentPage.getParentDoc().getPageIndex(currentPage) + 1);
                }
                curryPos = currentPage.getyMargin() + bufferSpace;
                mObj.setyPos(curryPos);
            }
            currentPage.addObject(mObj);
            mObj.setParentContainer(currentPage);
            currColumn++;
            if (currColumn > numColumns - 1) {
                curryPos += greatestHeight + bufferSpace;
                currColumn = 0;
            }
        }
    }
}
Also used : Page(doc.Page) Vector(java.util.Vector) AttributeException(doc.attributes.AttributeException) NodeException(expression.NodeException)

Example 4 with Page

use of doc.Page in project OpenNotebook by jaltekruse.

the class DocPrinter method print.

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    Page p;
    p = doc.getPage(page);
    if (p == null) {
        return NO_SUCH_PAGE;
    }
    PageGUI pageDrawer = new PageGUI();
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    pageDrawer.drawPage(g, p, new Point(0, 0), new Rectangle(p.getWidth(), p.getHeight()), 1);
    return PAGE_EXISTS;
}
Also used : Rectangle(java.awt.Rectangle) Page(doc.Page) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D)

Example 5 with Page

use of doc.Page in project OpenNotebook by jaltekruse.

the class OldReader method startElement.

@Override
public void startElement(String uri, String name, String qName, Attributes atts) {
    boolean justAddedObject = false;
    if (qName.equals("OpenNotebookDoc")) {
        doc = new Document(atts.getValue(Document.FILENAME));
        doc.setAuthor(atts.getValue(Document.AUTHOR));
    }
    if (doc != null) {
        if (qName.equals("Page")) {
            page = new Page(doc);
            doc.addPage(page);
            return;
        }
        if (page != null) {
            if (mObj != null) {
                readAttribute(uri, name, qName, atts);
            }
            if (qName.equals("AnswerBox")) {
                mObj = new AnswerBoxObject(page);
                justAddedObject = true;
            }
            if (qName.equals("CubeObject")) {
                mObj = new CubeObject(page);
                justAddedObject = true;
            } else if (qName.equals("ExpressionObject")) {
                mObj = new ExpressionObject(page);
                justAddedObject = true;
            } else if (qName.equals("GraphObject")) {
                mObj = new GraphObject(page);
                justAddedObject = true;
            } else if (qName.equals("NumberLineObject")) {
                mObj = new NumberLineObject(page);
                justAddedObject = true;
            } else if (qName.equals("OvalObject")) {
                mObj = new OvalObject(page);
                justAddedObject = true;
            } else if (qName.equals("ParallelogramObject")) {
                mObj = new ParallelogramObject(page);
                justAddedObject = true;
            } else if (qName.equals("RectangleObject")) {
                mObj = new RectangleObject(page);
                justAddedObject = true;
            } else if (qName.equals("TextObject")) {
                mObj = new TextObject(page);
                justAddedObject = true;
            } else if (qName.equals("TrapezoidObject")) {
                mObj = new TrapezoidObject(page);
                justAddedObject = true;
            } else if (qName.equals("TriangleObject")) {
                mObj = new TriangleObject(page);
                justAddedObject = true;
            }
            if (justAddedObject) {
                if (page != null) {
                    page.addObject(mObj);
                }
            }
        }
    }
}
Also used : NumberLineObject(doc.mathobjects.NumberLineObject) RectangleObject(doc.mathobjects.RectangleObject) Page(doc.Page) Document(doc.Document) GraphObject(doc.mathobjects.GraphObject) TrapezoidObject(doc.mathobjects.TrapezoidObject) TextObject(doc.mathobjects.TextObject) TriangleObject(doc.mathobjects.TriangleObject) ExpressionObject(doc.mathobjects.ExpressionObject) AnswerBoxObject(doc.mathobjects.AnswerBoxObject) CubeObject(doc.mathobjects.CubeObject) OvalObject(doc.mathobjects.OvalObject) ParallelogramObject(doc.mathobjects.ParallelogramObject)

Aggregations

Page (doc.Page)6 Grouping (doc.mathobjects.Grouping)3 Point (java.awt.Point)3 Document (doc.Document)2 PointInDocument (doc.PointInDocument)2 AttributeException (doc.attributes.AttributeException)2 MathObject (doc.mathobjects.MathObject)2 Rectangle (java.awt.Rectangle)2 Vector (java.util.Vector)2 ProblemDatabase (doc.ProblemDatabase)1 AnswerBoxObject (doc.mathobjects.AnswerBoxObject)1 CubeObject (doc.mathobjects.CubeObject)1 ExpressionObject (doc.mathobjects.ExpressionObject)1 GraphObject (doc.mathobjects.GraphObject)1 NumberLineObject (doc.mathobjects.NumberLineObject)1 OvalObject (doc.mathobjects.OvalObject)1 ParallelogramObject (doc.mathobjects.ParallelogramObject)1 RectangleObject (doc.mathobjects.RectangleObject)1 TextObject (doc.mathobjects.TextObject)1 TrapezoidObject (doc.mathobjects.TrapezoidObject)1