/*
* 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.gwt.ui;
import java.util.*;
import org.onemind.swingweb.client.core.AbstractClient;
import org.onemind.swingweb.client.dom.DomNode;
import org.onemind.swingweb.client.gwt.widget.*;
import com.google.gwt.user.client.ui.Widget;
public class ContainerUIHandler extends ComponentUIHandler
{
public ContainerUIHandler(AbstractClient client)
{
super(client);
}
/**
* {@inheritDoc}
*/
protected Object createComponentInstance(Object parent, DomNode element)
{
Container panel = new Container();
return panel;
}
protected void handleChildren(AbstractClient rootHandler, IContainer c, DomNode element)
{
ComponentLayout layout = c.getLayout();
DomNode layoutNode = getFirstElementByTag(element, "layout");
if (layoutNode != null)
{
List nodes = getChildrenByTag(layoutNode, "cell");
int cols = getInt(layoutNode, "cols");
int rows = getInt(layoutNode, "rows");
Map toBeAdded = new HashMap();
for (int i = 0; i < nodes.size(); i++)
{
DomNode cellElement = (DomNode) nodes.get(i);
DomNode childElement = getFirstChildElement(cellElement);
Object childCom = null;
if (childElement != null)
{
childCom = rootHandler.handle(c, childElement);
int x = getInt(childElement, "x");
int y = getInt(childElement, "y");
int row = getInt(cellElement, "row");
int col = getInt(cellElement, "col");
int rowSpan = getInt(cellElement, "rowSpan");
int colSpan = getInt(cellElement, "colSpan");
CellInfo cellInfo = new CellInfo(row, col, rowSpan, colSpan, x, y);
toBeAdded.put(childCom, cellInfo);
}
}
Map widgetInfos = new HashMap(layout.getWidgets());
Set widgets = widgetInfos.keySet();
for (Iterator it = widgets.iterator(); it.hasNext();)
{
Widget child = (Widget) it.next();
if (!toBeAdded.containsKey(child))
{
c.removeChild(child);
} else
{
CellInfo newCons = (CellInfo) toBeAdded.get(child);
CellInfo oldCons = (CellInfo) widgetInfos.get(child);
if (!newCons.equals(oldCons) && needReaddOnPositionChanged())//TODO: compare constrains is different
{
c.removeChild(child);
} else
{
toBeAdded.remove(child); //no need to add again
}
}
}
Iterator it = toBeAdded.entrySet().iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
CellInfo info = (CellInfo) entry.getValue();
addWidgetToContainer(c, (Widget) entry.getKey(), info);
}
} else
{
throw new IllegalArgumentException("Expecting layout node in element " + element.getAttribute("id"));
}
}
protected boolean needReaddOnPositionChanged()
{
return true;
}
protected void addWidgetToContainer(IContainer c, Widget widget, CellInfo info)
{
c.addChild(widget, info);
}
protected void updateLayoutStyleForContainer(Object com, DomNode element)
{
updateStyle(((IContainer) com).getLayout(), element);
}
/**
* {@inheritDoc}
*/
protected Object updateUI(Object com, DomNode element)
{
super.updateUI(com, element);
updateLayoutStyleForContainer(com, element);
handleChildren(getClient(), (IContainer) com, element);
return com;
}
}