/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.client.awt.ui;
import java.awt.*;
import java.util.*;
import java.util.List;
import org.onemind.swingweb.client.core.AbstractClient;
import org.onemind.swingweb.client.dom.DomNode;
public class ContainerUIHandler extends ComponentUIHandler
{
public ContainerUIHandler(AbstractClient client)
{
super(client);
// TODO Auto-generated constructor stub
}
public Object createComponentInstance(Object parent, DomNode element)
{
Panel c = new Panel();
return c;
}
protected void handleChildren(AbstractClient rootHandler, Container con, Container contentPane, DomNode element)
{
GridBagLayout gbLayout = null;
if (!(contentPane.getLayout() instanceof GridBagLayout))
{
gbLayout = new GridBagLayout();
contentPane.setLayout(gbLayout);
} else
{
gbLayout = (GridBagLayout) contentPane.getLayout();
}
DomNode layoutNode = getFirstElementByTag(element, "layout");
if (layoutNode != null)
{
int cols = Integer.parseInt(layoutNode.getAttribute("cols"));
int rows = Integer.parseInt(layoutNode.getAttribute("rows"));
List cells = getChildrenByTag(layoutNode, "cell");
Map toBeAdded = new LinkedHashMap();
for (int i = 0; i < cells.size(); i++)
{
DomNode cellNode = (DomNode) cells.get(i);
List childElements = getComponentNodes(cellNode);
if (childElements.size() == 0)
{
//just a padding cell
} else if (childElements.size() == 1)
{
DomNode comNode = (DomNode) childElements.get(0);
Object childCom = rootHandler.handle(contentPane, comNode);
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.BOTH;
int row = Integer.parseInt(cellNode.getAttribute("row"));
int col = Integer.parseInt(cellNode.getAttribute("col"));
int rowSpan = Integer.parseInt(cellNode.getAttribute("rowSpan"));
int colSpan = Integer.parseInt(cellNode.getAttribute("colSpan"));
Dimension dim = UIHelper.getDimension(comNode);
cons.gridx = col;
cons.gridy = row;
cons.gridwidth = colSpan;
cons.gridheight = rowSpan;
cons.weightx = colSpan;
cons.weighty = rowSpan;
if (col + colSpan == cols)
{
cons.gridwidth = GridBagConstraints.REMAINDER;
}
// gbLayout.setConstraints(cCom, cons);
// c.add(cCom);
toBeAdded.put(childCom, cons);
} else
{
throw new IllegalStateException("Multiple component in cell node");
}
}
Component[] coms = contentPane.getComponents();
for (int i = 0; i < coms.length; i++)
{
Component child = coms[i];
if (!toBeAdded.containsKey(child))
{
contentPane.remove(child);
} else
{
GridBagConstraints newCons = (GridBagConstraints) toBeAdded.get(child);
GridBagConstraints oldCons = (GridBagConstraints) gbLayout.getConstraints(child);
if (false)//compare constrains is different
{
contentPane.remove(child);
} else
{
toBeAdded.remove(child); //no need to add again
}
}
}
boolean validate = false;
Iterator it = toBeAdded.entrySet().iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
System.out.println("Laying out " + entry.getKey());
gbLayout.setConstraints((Component) entry.getKey(), (GridBagConstraints) entry.getValue());
contentPane.add((Component) entry.getKey());
validate = true;
contentPane.invalidate();
}
} else
{
throw new IllegalArgumentException("Expecting layout node in element " + element);
}
}
/**
* {@inheritDoc}
*/
public Object updateUI(Object com, DomNode element)
{
super.updateUI(com, element);
handleChildren(getClient(), (Container) com, (Container) com, element);
return com;
}
}