use of doc.mathobjects.Grouping 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>());
}
}
}
}
}
use of doc.mathobjects.Grouping 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();
}
}
use of doc.mathobjects.Grouping in project OpenNotebook by jaltekruse.
the class Document method getProblemNumbersInOrder.
private Vector<ObjectAndPosition> getProblemNumbersInOrder() {
Vector<ObjectAndPosition> allNumbers = new Vector<>();
for (Page p : getPages()) {
for (MathObject mObj : p.getObjects()) {
if (mObj instanceof ProblemNumberObject) {
allNumbers.add(new ObjectAndPosition(mObj.getPositionInDoc(), mObj));
} else if (mObj instanceof Grouping) {
allNumbers.addAll(0, findAllProblemNumbersInGroup(((Grouping) mObj)));
}
}
}
Collections.sort(allNumbers);
return allNumbers;
}
use of doc.mathobjects.Grouping in project OpenNotebook by jaltekruse.
the class DocMouseListener method adjustTempGroupSelection.
/**
* Method to change the temporary group selection based on the current
* selection rectangle. Removes objects no longer contacted, and adds new
* objects that are not yet group members.
*
* @return - whether or not a redraw event is needed
*/
private boolean adjustTempGroupSelection() {
// need to modify this code to take into account the current stacking of
// the objects
// large objects automatically cover up smaller ones using this system
Rectangle selectRect = docPanel.getSelectionRect().getBounds();
Grouping tempGroup = docPanel.getTempGroup();
Vector<MathObject> pageObjects = docPanel.getSelectionRect().getParentPage().getObjects();
// temporary storage of objects that collide with the selection
// rectangle
Vector<MathObject> collisionObjects = new Vector<>();
for (MathObject mObj : pageObjects) {
// selection rectangle
if (selectRect.intersects(mObj.getBounds())) {
collisionObjects.add(mObj);
}
}
if (collisionObjects.isEmpty()) {
// no objects were contacted
docPanel.ungroupTempGroup();
docPanel.setFocusedObject(null);
return true;
}
if (collisionObjects.size() == 1) {
// only one object was contacted
MathObject contactedObj = collisionObjects.get(0);
if (contactedObj.equals(docPanel.getFocusedObject())) {
// do nothing
;
if (contactedObj != tempGroup) {
collisionObjects.remove(contactedObj);
return true;
}
} else {
docPanel.setFocusedObject(contactedObj);
collisionObjects.remove(contactedObj);
return true;
}
}
if (collisionObjects.contains(tempGroup)) {
MathObject mObj;
// otherwise remove them
for (int i = 0; i < tempGroup.getObjects().size(); i++) {
mObj = tempGroup.getObjects().get(i);
if (!selectRect.intersects(mObj.getBounds())) {
tempGroup.removeObject(mObj);
tempGroup.getParentContainer().addObject(mObj);
mObj.setParentContainer(tempGroup.getParentContainer());
i--;
}
}
// remove the temporary group from the contacted list, it now
// contains only elements that were
// in it before and were contacted by the current selection
// rectangle
collisionObjects.remove(tempGroup);
}
if (!collisionObjects.isEmpty()) {
tempGroup.setParentContainer(collisionObjects.get(0).getParentContainer());
collisionObjects.get(0).getParentContainer().addObject(tempGroup);
for (MathObject mObj : collisionObjects) {
mObj.getParentContainer().removeObject(mObj);
mObj.setParentContainer(null);
tempGroup.addObjectFromPage(mObj);
mObj.setParentContainer(tempGroup);
}
docPanel.getDoc().refactorPageNumbers();
docPanel.setFocusedObject(tempGroup);
}
if (tempGroup.getParentContainer() != null) {
if (tempGroup.getObjects().size() == 1) {
// there is one one object
// left in the temporary group
docPanel.setFocusedObject(tempGroup.getObjects().get(0));
docPanel.ungroupTempGroup();
}
}
return false;
}
use of doc.mathobjects.Grouping in project OpenNotebook by jaltekruse.
the class DocViewerPanel method ungroupTempGroup.
public void ungroupTempGroup() {
tempGroup.unGroup();
if (tempGroup.getParentContainer() != null)
tempGroup.getParentContainer().removeObject(tempGroup);
tempGroup = new Grouping();
}
Aggregations