use of edu.mit.blocks.renderable.FactoryRenderableBlock in project openblocks by mikaelhg.
the class FactoryManager method workspaceEventOccurred.
public void workspaceEventOccurred(WorkspaceEvent event) {
//IT DOES SOME PREETY STRANGE THINGS
if (event.getEventType() == WorkspaceEvent.BLOCK_ADDED) {
if (event.getSourceWidget() instanceof Page) {
Page page = (Page) event.getSourceWidget();
Block block = workspace.getEnv().getBlock(event.getSourceBlockID());
//block may not be null if this is a block added event
if (block.hasStubs()) {
for (BlockStub stub : block.getFreshStubs()) {
this.addDynamicBlock(new FactoryRenderableBlock(event.getWorkspace(), this, stub.getBlockID()), page.getPageDrawer());
}
}
}
} else if (event.getEventType() == WorkspaceEvent.BLOCK_REMOVED) {
//may not be removing a null stanc eof block, so DO NOT check for it
Block block = workspace.getEnv().getBlock(event.getSourceBlockID());
if (block.hasStubs()) {
for (Long stub : BlockStub.getStubsOfParent(event.getWorkspace(), block)) {
RenderableBlock rb = workspace.getEnv().getRenderableBlock(stub);
if (rb != null && !rb.getBlockID().equals(Block.NULL) && rb.getParentWidget() != null && rb.getParentWidget().equals(this)) {
//rb.getParent() should not be null
rb.getParent().remove(rb);
rb.setParentWidget(null);
}
}
}
this.relayoutBlocks();
} else if (event.getEventType() == WorkspaceEvent.BLOCK_MOVED) {
Block block = workspace.getEnv().getBlock(event.getSourceBlockID());
if (block != null && block.hasStubs()) {
for (Long stub : BlockStub.getStubsOfParent(event.getWorkspace(), block)) {
RenderableBlock rb = workspace.getEnv().getRenderableBlock(stub);
if (rb != null && !rb.getBlockID().equals(Block.NULL) && rb.getParentWidget() != null && rb.getParentWidget().equals(this)) {
//rb.getParent() should not be null
rb.getParent().remove(rb);
rb.setParentWidget(null);
}
}
this.relayoutBlocks();
}
} else if (event.getEventType() == WorkspaceEvent.PAGE_RENAMED) {
//this.relayoutBlocks();
}
}
use of edu.mit.blocks.renderable.FactoryRenderableBlock in project openblocks by mikaelhg.
the class PageDrawerLoadingUtils method loadBlockDrawerSets.
public static void loadBlockDrawerSets(Workspace workspace, Element root, FactoryManager manager) {
Pattern attrExtractor = Pattern.compile("\"(.*)\"");
Matcher nameMatcher;
NodeList drawerSetNodes = root.getElementsByTagName("BlockDrawerSet");
Node drawerSetNode;
for (int i = 0; i < drawerSetNodes.getLength(); i++) {
drawerSetNode = drawerSetNodes.item(i);
if (drawerSetNode.getNodeName().equals("BlockDrawerSet")) {
NodeList drawerNodes = drawerSetNode.getChildNodes();
Node drawerNode;
//retreive drawer information of this bar
for (int j = 0; j < drawerNodes.getLength(); j++) {
drawerNode = drawerNodes.item(j);
if (drawerNode.getNodeName().equals("BlockDrawer")) {
String drawerName = null;
Color buttonColor = Color.blue;
StringTokenizer col;
nameMatcher = attrExtractor.matcher(drawerNode.getAttributes().getNamedItem("name").toString());
if (nameMatcher.find()) {
//will be true
drawerName = nameMatcher.group(1);
}
//get drawer's color:
Node colorNode = drawerNode.getAttributes().getNamedItem("button-color");
if (colorNode != null) {
nameMatcher = attrExtractor.matcher(colorNode.toString());
if (nameMatcher.find()) {
//will be true
col = new StringTokenizer(nameMatcher.group(1));
if (col.countTokens() == 3) {
buttonColor = new Color(Integer.parseInt(col.nextToken()), Integer.parseInt(col.nextToken()), Integer.parseInt(col.nextToken()));
} else {
buttonColor = Color.BLACK;
}
}
}
manager.addStaticDrawer(drawerName, buttonColor);
//get block genuses in drawer and create blocks
NodeList drawerBlocks = drawerNode.getChildNodes();
Node blockNode;
ArrayList<RenderableBlock> drawerRBs = new ArrayList<RenderableBlock>();
for (int k = 0; k < drawerBlocks.getLength(); k++) {
blockNode = drawerBlocks.item(k);
if (blockNode.getNodeName().equals("BlockGenusMember")) {
String genusName = blockNode.getTextContent();
assert workspace.getEnv().getGenusWithName(genusName) != null : "Unknown BlockGenus: " + genusName;
Block newBlock;
//don't link factory blocks to their stubs because they will
//forever remain inside the drawer and never be active
newBlock = new Block(workspace, genusName, false);
drawerRBs.add(new FactoryRenderableBlock(workspace, manager, newBlock.getBlockID()));
}
}
manager.addStaticBlocks(drawerRBs, drawerName);
}
}
}
}
}
use of edu.mit.blocks.renderable.FactoryRenderableBlock in project openblocks by mikaelhg.
the class PageDrawerLoadingUtils method loadPagesAndDrawers.
public static void loadPagesAndDrawers(Workspace workspace, Element root, FactoryManager manager) {
List<Page> pageList = new ArrayList<Page>();
//pagesToAdd is needed so that we can add pages all at once
//to the page bar once all the the pages been loaded
//Before adding all the pages, this method makes a check
//if there is only one page with an empty name - if so, it will just
//add the page to the workspace/block canvas but not add it to this
//LinkedHashMap<Page, PageBlockDrawer> pagesToAdd = new LinkedHashMap<Page, PageBlockDrawer>();
LinkedHashMap<String, ArrayList<RenderableBlock>> blocksForDrawers = new LinkedHashMap<String, ArrayList<RenderableBlock>>();
LinkedHashMap<Page, ArrayList<RenderableBlock>> blocksForPages = new LinkedHashMap<Page, ArrayList<RenderableBlock>>();
NodeList pagesRoot = root.getElementsByTagName("Pages");
if (pagesRoot != null) {
//isBlankPage denotes if the page being loaded is a default blank page
//in other words, the project did not specify any pages for their environment.
//EvoBeaker does this
boolean isBlankPage = false;
Node pagesNode = pagesRoot.item(0);
if (pagesNode == null) {
// short-circuit exit if there's nothing to load
return;
}
Node opt_item = pagesNode.getAttributes().getNamedItem("drawer-with-page");
if (opt_item != null) {
Matcher nameMatcher = attrExtractor.matcher(opt_item.toString());
if (nameMatcher.find()) {
Workspace.everyPageHasDrawer = nameMatcher.group(1).equals("yes") ? true : false;
}
}
opt_item = pagesNode.getAttributes().getNamedItem("is-blank-page");
if (opt_item != null) {
Matcher nameMatcher = attrExtractor.matcher(opt_item.toString());
if (nameMatcher.find()) {
isBlankPage = nameMatcher.group(1).equals("yes") ? true : false;
}
}
// whether pages should show a control to collapse them or not
boolean collapsiblePages = getBooleanValue(pagesNode, "collapsible-pages");
Page page;
NodeList pages = pagesNode.getChildNodes();
Node pageNode;
String pageName;
String pageDrawer;
Color pageColor;
boolean pageInFullView;
int pageWidth;
String pageId;
for (int i = 0; i < pages.getLength(); i++) {
//find them
pageNode = pages.item(i);
if (pageNode.getNodeName().equals("Page")) {
// a page entry
pageName = getNodeValue(pageNode, "page-name");
pageColor = getColorValue(pageNode, "page-color");
pageWidth = getIntValue(pageNode, "page-width");
pageDrawer = getNodeValue(pageNode, "page-drawer");
pageInFullView = getBooleanValue(pageNode, "page-infullview");
pageId = getNodeValue(pageNode, "page-id");
page = new Page(workspace, pageName, pageWidth, 0, pageDrawer, pageInFullView, pageColor, collapsiblePages);
page.setPageId(pageId);
NodeList pageNodes = pageNode.getChildNodes();
String drawer = null;
if (Workspace.everyPageHasDrawer) {
//create drawer instance
manager.addDynamicDrawer(page.getPageDrawer());
ArrayList<RenderableBlock> drawerBlocks = new ArrayList<RenderableBlock>();
for (int k = 0; k < pageNodes.getLength(); k++) {
Node drawerNode = pageNodes.item(k);
if (drawerNode.getNodeName().equals("PageDrawer")) {
NodeList genusMembers = drawerNode.getChildNodes();
String genusName;
for (int j = 0; j < genusMembers.getLength(); j++) {
Node genusMember = genusMembers.item(j);
if (genusMember.getNodeName().equals("BlockGenusMember")) {
genusName = genusMember.getTextContent();
assert workspace.getEnv().getGenusWithName(genusName) != null : "Unknown BlockGenus: " + genusName;
Block block = new Block(workspace, genusName);
drawerBlocks.add(new FactoryRenderableBlock(workspace, manager, block.getBlockID()));
}
}
blocksForDrawers.put(drawer, drawerBlocks);
//there can only be one drawer for this page
break;
}
}
}
if (isBlankPage) {
//place a blank page as the first page
workspace.putPage(page, 0);
//we anticipate only one page
break;
} else {
//we add to the end of the set of pages
int position = pageList.size();
//add to workspace
if (position == 0) {
//replace the blank default page
workspace.putPage(page, 0);
} else {
workspace.addPage(page, position);
}
pageList.add(position, page);
}
blocksForPages.put(page, page.loadPageFrom(pageNode, false));
}
}
//add blocks in drawers
for (String d : blocksForDrawers.keySet()) {
manager.addDynamicBlocks(blocksForDrawers.get(d), d);
}
//blocks in pages
for (Page p : blocksForPages.keySet()) {
p.addLoadedBlocks(blocksForPages.get(p), false);
}
}
}
Aggregations