/* Please note that Much of this code was adapted from Ben Fry's * Treemap example in chapter 7 of his book: * Visualizing Data, First Edition by Ben Fry. Copyright 2008 Ben Fry * ISBN: 9780596514556 */ /* ** ======================================================== ** MAT259 Visualizaing Data | Winter 2011 | George Legrady ** ======================================================= ** Project 2 : Top 100 Most Checked Out Items from 2005 to 2010 ** ============================================================== ** Nichole Stockman ** ================ ** ** ** ===================================================================== ** For information on how the data was parsed, how the application works ** and some key notes and observations, please see the README file.` ** Thank you! ** ===================================================================== */ class FolderItem extends Item implements MapModel { MapLayout algorithm = new PivotBySplitSize(); ArrayList items; boolean contentsVisible; boolean layoutValid; float darkness; public FolderItem(FolderItem parent, String[] contents, String name, int type, int size, int category, int level, int order) { super(parent, name, type, size, level, order); items = new ArrayList(); if(level == 1) { for (int k = 0; k < 10; k++) { String folderName = new String(k + ": " + DewCatNames[k]); int folderSize = 0; for (int i = 0; i < contents.length; i++) { String[] ParseList = split(contents[i],','); if(Integer.valueOf(ParseList[2].substring(0, 1)) == k) { folderSize = 0; for(int n = 0; n < contents.length; n++) { String[] SubParseList = split(contents[n], ','); if(Integer.valueOf(SubParseList[2].substring(0,1)) == k) { folderSize += Integer.valueOf(SubParseList[0]); } } break; } } FolderItem newItem = new FolderItem(this, contents, folderName, folder, folderSize, k, level+1, k); items.add((Mappable)(newItem)); } } else if (level == 2) { for (int k = 0; k < 10; k++) { int index = 0; for (int i = 0; i < contents.length; i++) { String[] ParseList = split(contents[i],','); if( (Integer.valueOf(ParseList[2].substring(1, 2)) == k) && (Integer.valueOf(ParseList[2].substring(0, 1)) == category) ) { String itemName = ParseList[1]; int itemSize = Integer.valueOf(ParseList[0]); String t = ParseList[4]; int itemType = book; if(t.substring(1, 3).equals("CD")) { itemType = cd; } else if (t.substring(1, 4).equals("DVD")) { itemType = dvd; } Item newItem = new Item(this, itemName, itemType, itemSize, level+1, index); items.add((Mappable)(newItem)); index++; } } } } } void updateColors() { super.updateColors(); for (int i = 0; i < items.size(); i++) { Item fi = (Item) items.get(i); fi.updateColors(); } } void checkLayout() { if (!layoutValid) { if (getItemCount() != 0) { algorithm.layout(this, bounds); } layoutValid = true; } } boolean mousePressed() { if (mouseInside()) { if (contentsVisible) { // Pass the mouse press to the child items for (int i = 0; i < items.size(); i++) { Item fi = (Item) items.get(i); if (fi.mousePressed()) { return true; } } } else { // not opened if (mouseButton == LEFT) { if (parent == zoomItem) { showContents(); } else { parent.zoomIn(); } } else if (mouseButton == RIGHT) { if (parent == zoomItem) { parent.zoomOut(); } else { parent.hideContents(); } } return true; } } return false; } // Zoom to the parent's boundary, zooming out from this item void zoomOut() { if (parent != null) { // Close contents of any opened children for (int i = 0; i < items.size(); i++) { if (items.get(i) instanceof FolderItem) { ((FolderItem)items.get(i)).hideContents(); } } parent.zoomIn(); } } void zoomIn() { zoomItem = this; zoomBounds.target(x, y, w, h); } void showContents() { contentsVisible = true; } void hideContents() { // Prevent the user from closing the root level if (parent != null) { contentsVisible = false; } } public void draw() { checkLayout(); calcBox(); if (contentsVisible) { for (int i = 0; i < items.size(); i++) { items.get(i).draw(); } } else { super.draw(); } if (contentsVisible) { if (mouseInside()) { if (parent == zoomItem) { taggedItem = this; } } } if (mouseInside()) { darkness *= 0.05; } else { darkness += (110 - darkness) * 0.05; } if (parent == zoomItem) { colorMode(RGB, 255); fill(0, darkness); rect(boxLeft, boxTop, boxRight, boxBottom); } } void drawTitle() { if (!contentsVisible) { super.drawTitle(); } } void drawTag() { float boxHeight = textAscent() + textPadding*2; if (boxBottom - boxTop > boxHeight*2) { // if the height of the box is at least twice the height of the tag, // draw the tag inside the box itself fill(0, 128); rect(boxLeft, boxTop, boxRight, boxTop+boxHeight); fill(255); textAlign(LEFT, TOP); text(name, boxLeft+textPadding, boxTop+textPadding); } else if (boxTop > boxHeight) { // if there's enough room to draw above, draw it there fill(0, 128); rect(boxLeft, boxTop-boxHeight, boxRight, boxTop); fill(255); text(name, boxLeft+textPadding, boxTop-textPadding); } else if (boxBottom + boxHeight < height) { // otherwise draw the tag below fill(0, 128); rect(boxLeft, boxBottom, boxRight, boxBottom+boxHeight); fill(255); textAlign(LEFT, TOP); text(name, boxLeft+textPadding, boxBottom+textPadding); } } public Mappable[] getItems() { Mappable[] itemsArray = new Mappable[items.size()]; for(int i = 0; i < items.size(); i++) { itemsArray[i] = items.get(i); } return itemsArray; } int getItemCount() { return items.size(); } }