001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.client.awt.ui;
022:
023: import java.awt.*;
024: import java.util.*;
025: import java.util.List;
026: import org.onemind.swingweb.client.core.AbstractClient;
027: import org.onemind.swingweb.client.dom.DomNode;
028:
029: public class ContainerUIHandler extends ComponentUIHandler {
030:
031: public ContainerUIHandler(AbstractClient client) {
032: super (client);
033: // TODO Auto-generated constructor stub
034: }
035:
036: public Object createComponentInstance(Object parent, DomNode element) {
037: Panel c = new Panel();
038: return c;
039: }
040:
041: protected void handleChildren(AbstractClient rootHandler,
042: Container con, Container contentPane, DomNode element) {
043: GridBagLayout gbLayout = null;
044: if (!(contentPane.getLayout() instanceof GridBagLayout)) {
045: gbLayout = new GridBagLayout();
046: contentPane.setLayout(gbLayout);
047: } else {
048: gbLayout = (GridBagLayout) contentPane.getLayout();
049: }
050: DomNode layoutNode = getFirstElementByTag(element, "layout");
051: if (layoutNode != null) {
052: int cols = Integer
053: .parseInt(layoutNode.getAttribute("cols"));
054: int rows = Integer
055: .parseInt(layoutNode.getAttribute("rows"));
056: List cells = getChildrenByTag(layoutNode, "cell");
057: Map toBeAdded = new LinkedHashMap();
058: for (int i = 0; i < cells.size(); i++) {
059: DomNode cellNode = (DomNode) cells.get(i);
060: List childElements = getComponentNodes(cellNode);
061: if (childElements.size() == 0) {
062: //just a padding cell
063: } else if (childElements.size() == 1) {
064: DomNode comNode = (DomNode) childElements.get(0);
065: Object childCom = rootHandler.handle(contentPane,
066: comNode);
067: GridBagConstraints cons = new GridBagConstraints();
068: cons.fill = GridBagConstraints.BOTH;
069: int row = Integer.parseInt(cellNode
070: .getAttribute("row"));
071: int col = Integer.parseInt(cellNode
072: .getAttribute("col"));
073: int rowSpan = Integer.parseInt(cellNode
074: .getAttribute("rowSpan"));
075: int colSpan = Integer.parseInt(cellNode
076: .getAttribute("colSpan"));
077: Dimension dim = UIHelper.getDimension(comNode);
078: cons.gridx = col;
079: cons.gridy = row;
080: cons.gridwidth = colSpan;
081: cons.gridheight = rowSpan;
082: cons.weightx = colSpan;
083: cons.weighty = rowSpan;
084: if (col + colSpan == cols) {
085: cons.gridwidth = GridBagConstraints.REMAINDER;
086: }
087: // gbLayout.setConstraints(cCom, cons);
088: // c.add(cCom);
089: toBeAdded.put(childCom, cons);
090: } else {
091: throw new IllegalStateException(
092: "Multiple component in cell node");
093: }
094: }
095: Component[] coms = contentPane.getComponents();
096: for (int i = 0; i < coms.length; i++) {
097: Component child = coms[i];
098: if (!toBeAdded.containsKey(child)) {
099: contentPane.remove(child);
100: } else {
101: GridBagConstraints newCons = (GridBagConstraints) toBeAdded
102: .get(child);
103: GridBagConstraints oldCons = (GridBagConstraints) gbLayout
104: .getConstraints(child);
105: if (false)//compare constrains is different
106: {
107: contentPane.remove(child);
108: } else {
109: toBeAdded.remove(child); //no need to add again
110: }
111: }
112: }
113: boolean validate = false;
114: Iterator it = toBeAdded.entrySet().iterator();
115: while (it.hasNext()) {
116: Map.Entry entry = (Map.Entry) it.next();
117: System.out.println("Laying out " + entry.getKey());
118: gbLayout.setConstraints((Component) entry.getKey(),
119: (GridBagConstraints) entry.getValue());
120: contentPane.add((Component) entry.getKey());
121: validate = true;
122: contentPane.invalidate();
123: }
124: } else {
125: throw new IllegalArgumentException(
126: "Expecting layout node in element " + element);
127: }
128: }
129:
130: /**
131: * {@inheritDoc}
132: */
133: public Object updateUI(Object com, DomNode element) {
134: super .updateUI(com, element);
135: handleChildren(getClient(), (Container) com, (Container) com,
136: element);
137: return com;
138: }
139: }
|