aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2012-04-04 22:56:51 +0300
committerArtur Signell <artur@vaadin.com>2012-04-05 00:09:44 +0300
commit2fe2ec13c6be4ca318a7b74f26b9986ca401f5e2 (patch)
treed8b79770d93171a2712c39e2f50fc34943f3ad5e
parentd4a4cc148bb3097c0db85577b7ad2ed255f13697 (diff)
downloadvaadin-framework-2fe2ec13c6be4ca318a7b74f26b9986ca401f5e2.tar.gz
vaadin-framework-2fe2ec13c6be4ca318a7b74f26b9986ca401f5e2.zip
Updated CustomLayout to use shared state and new events
-rw-r--r--src/com/vaadin/terminal/gwt/client/ui/CustomLayoutConnector.java142
-rw-r--r--src/com/vaadin/terminal/gwt/client/ui/VCustomLayout.java54
-rw-r--r--src/com/vaadin/ui/CustomLayout.java77
3 files changed, 128 insertions, 145 deletions
diff --git a/src/com/vaadin/terminal/gwt/client/ui/CustomLayoutConnector.java b/src/com/vaadin/terminal/gwt/client/ui/CustomLayoutConnector.java
index a8196f6705..f0fb1b7e6f 100644
--- a/src/com/vaadin/terminal/gwt/client/ui/CustomLayoutConnector.java
+++ b/src/com/vaadin/terminal/gwt/client/ui/CustomLayoutConnector.java
@@ -3,74 +3,131 @@
*/
package com.vaadin.terminal.gwt.client.ui;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;
-import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.ComponentConnector;
-import com.vaadin.terminal.gwt.client.Paintable;
-import com.vaadin.terminal.gwt.client.UIDL;
+import com.vaadin.terminal.gwt.client.Connector;
+import com.vaadin.terminal.gwt.client.ConnectorHierarchyChangeEvent;
+import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
import com.vaadin.ui.CustomLayout;
@Component(CustomLayout.class)
-public class CustomLayoutConnector extends AbstractComponentContainerConnector
- implements Paintable, SimpleManagedLayout {
+public class CustomLayoutConnector extends AbstractLayoutConnector implements
+ SimpleManagedLayout {
- public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
- getWidget().client = client;
- if (!isRealUpdate(uidl)) {
- return;
+ public static class CustomLayoutState extends AbstractLayoutState {
+ Map<Connector, String> childLocations = new HashMap<Connector, String>();
+ private String templateContents;
+ private String templateName;
+
+ public String getTemplateContents() {
+ return templateContents;
+ }
+
+ public void setTemplateContents(String templateContents) {
+ this.templateContents = templateContents;
+ }
+
+ public String getTemplateName() {
+ return templateName;
+ }
+
+ public void setTemplateName(String templateName) {
+ this.templateName = templateName;
+ }
+
+ public Map<Connector, String> getChildLocations() {
+ return childLocations;
}
- getWidget().pid = uidl.getId();
- if (!getWidget().hasTemplate()) {
- // Update HTML template only once
- getWidget().initializeHTML(uidl, client);
+ public void setChildLocations(Map<Connector, String> childLocations) {
+ this.childLocations = childLocations;
}
+ }
+
+ @Override
+ public CustomLayoutState getState() {
+ return (CustomLayoutState) super.getState();
+ }
+
+ @Override
+ protected void init() {
+ super.init();
+ getWidget().client = getConnection();
+ getWidget().pid = getConnectorId();
+
+ }
+
+ @Override
+ public void onStateChanged(StateChangeEvent stateChangeEvent) {
+ super.onStateChanged(stateChangeEvent);
+
// Evaluate scripts
VCustomLayout.eval(getWidget().scripts);
getWidget().scripts = null;
- // TODO Check if this is needed
- client.runDescendentsLayout(getWidget());
+ }
- Set<Widget> oldWidgets = new HashSet<Widget>();
- oldWidgets.addAll(getWidget().locationToWidget.values());
+ private void updateHtmlTemplate() {
+ if (getWidget().hasTemplate()) {
+ // We (currently) only do this once. You can't change the template
+ // later on.
+ return;
+ }
+ String templateName = getState().getTemplateName();
+ String templateContents = getState().getTemplateContents();
+
+ if (templateName != null) {
+ // Get the HTML-template from client. Overrides templateContents
+ // (even though both can never be given at the same time)
+ templateContents = getConnection().getResource(
+ "layouts/" + templateName + ".html");
+ if (templateContents == null) {
+ templateContents = "<em>Layout file layouts/"
+ + templateName
+ + ".html is missing. Components will be drawn for debug purposes.</em>";
+ }
+ }
+
+ getWidget().initializeHTML(templateContents,
+ getConnection().getThemeUri());
+ }
+
+ @Override
+ public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
+ super.onConnectorHierarchyChange(event);
+
+ // Must do this once here so the HTML has been set up before we start
+ // adding child widgets.
+
+ updateHtmlTemplate();
// For all contained widgets
- for (final Iterator<?> i = uidl.getChildIterator(); i.hasNext();) {
- final UIDL uidlForChild = (UIDL) i.next();
- if (uidlForChild.getTag().equals("location")) {
- final String location = uidlForChild.getStringAttribute("name");
- UIDL childUIDL = uidlForChild.getChildUIDL(0);
- final ComponentConnector childPaintable = client
- .getPaintable(childUIDL);
- Widget childWidget = childPaintable.getWidget();
- try {
- getWidget().setWidget(childWidget, location);
- } catch (final IllegalArgumentException e) {
- // If no location is found, this component is not visible
- }
- oldWidgets.remove(childWidget);
+ for (ComponentConnector child : getChildren()) {
+ String location = getState().getChildLocations().get(child);
+ try {
+ getWidget().setWidget(child.getWidget(), location);
+ } catch (final IllegalArgumentException e) {
+ // If no location is found, this component is not visible
}
}
- for (Iterator<Widget> iterator = oldWidgets.iterator(); iterator
- .hasNext();) {
- Widget oldWidget = iterator.next();
- if (oldWidget.isAttached()) {
+ for (ComponentConnector oldChild : event.getOldChildren()) {
+ if (oldChild.getParent() == this) {
+ // Connector still a child of this
+ continue;
+ }
+ Widget oldChildWidget = oldChild.getWidget();
+ if (oldChildWidget.isAttached()) {
// slot of this widget is emptied, remove it
- getWidget().remove(oldWidget);
+ getWidget().remove(oldChildWidget);
}
}
- // TODO Check if this is needed
- client.runDescendentsLayout(getWidget());
-
}
@Override
@@ -85,7 +142,6 @@ public class CustomLayoutConnector extends AbstractComponentContainerConnector
public void updateCaption(ComponentConnector paintable) {
getWidget().updateCaption(paintable);
-
}
public void layout() {
diff --git a/src/com/vaadin/terminal/gwt/client/ui/VCustomLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VCustomLayout.java
index 8861a9bf24..aa23119964 100644
--- a/src/com/vaadin/terminal/gwt/client/ui/VCustomLayout.java
+++ b/src/com/vaadin/terminal/gwt/client/ui/VCustomLayout.java
@@ -17,8 +17,6 @@ import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.BrowserInfo;
import com.vaadin.terminal.gwt.client.ComponentConnector;
-import com.vaadin.terminal.gwt.client.ConnectorMap;
-import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VCaption;
import com.vaadin.terminal.gwt.client.VCaptionWrapper;
@@ -40,7 +38,7 @@ public class VCustomLayout extends ComplexPanel {
final HashMap<String, Widget> locationToWidget = new HashMap<String, Widget>();
/** Widget to captionwrapper map */
- private final HashMap<ComponentConnector, VCaptionWrapper> paintableToCaptionWrapper = new HashMap<ComponentConnector, VCaptionWrapper>();
+ private final HashMap<Widget, VCaptionWrapper> childWidgetToCaptionWrapper = new HashMap<Widget, VCaptionWrapper>();
/** Name of the currently rendered style */
String currentTemplateName;
@@ -53,8 +51,7 @@ public class VCustomLayout extends ComplexPanel {
ApplicationConnection client;
- /** Has the template been loaded from contents passed in UIDL **/
- private boolean hasTemplateContents = false;
+ private boolean htmlInitialized = false;
private Element elementWithNativeResizeFunction;
@@ -124,37 +121,13 @@ public class VCustomLayout extends ComplexPanel {
}
/** Initialize HTML-layout. */
- void initializeHTML(UIDL uidl, ApplicationConnection client) {
-
- final String newTemplateContents = uidl
- .getStringAttribute("templateContents");
- final String newTemplate = uidl.getStringAttribute("template");
-
- currentTemplateName = null;
- hasTemplateContents = false;
-
- String template = "";
- if (newTemplate != null) {
- // Get the HTML-template from client
- template = client.getResource("layouts/" + newTemplate + ".html");
- if (template == null) {
- template = "<em>Layout file layouts/"
- + newTemplate
- + ".html is missing. Components will be drawn for debug purposes.</em>";
- } else {
- currentTemplateName = newTemplate;
- }
- } else {
- hasTemplateContents = true;
- template = newTemplateContents;
- }
+ public void initializeHTML(String template, String themeUri) {
// Connect body of the template to DOM
template = extractBodyAndScriptsFromTemplate(template);
// TODO prefix img src:s here with a regeps, cannot work further with IE
- String themeUri = client.getThemeUri();
String relImgPrefix = themeUri + "/layouts/";
// prefix all relative image elements to point to theme dir with a
@@ -187,6 +160,7 @@ public class VCustomLayout extends ComplexPanel {
}
publishResizedFunction(elementWithNativeResizeFunction);
+ htmlInitialized = true;
}
private native boolean uriEndsWithSlash()
@@ -198,11 +172,7 @@ public class VCustomLayout extends ComplexPanel {
}-*/;
boolean hasTemplate() {
- if (currentTemplateName == null && !hasTemplateContents) {
- return false;
- } else {
- return true;
- }
+ return htmlInitialized;
}
/** Collect locations from template */
@@ -301,8 +271,8 @@ public class VCustomLayout extends ComplexPanel {
/** Update caption for given widget */
public void updateCaption(ComponentConnector paintable) {
- VCaptionWrapper wrapper = paintableToCaptionWrapper.get(paintable);
Widget widget = paintable.getWidget();
+ VCaptionWrapper wrapper = childWidgetToCaptionWrapper.get(widget);
if (VCaption.isNeeded(paintable.getState())) {
if (wrapper == null) {
// Add a wrapper between the layout and the child widget
@@ -310,7 +280,7 @@ public class VCustomLayout extends ComplexPanel {
super.remove(widget);
wrapper = new VCaptionWrapper(paintable, client);
super.add(wrapper, locationToElement.get(loc));
- paintableToCaptionWrapper.put(paintable, wrapper);
+ childWidgetToCaptionWrapper.put(widget, wrapper);
}
wrapper.updateCaption();
} else {
@@ -319,7 +289,7 @@ public class VCustomLayout extends ComplexPanel {
final String loc = getLocation(widget);
super.remove(wrapper);
super.add(widget, locationToElement.get(loc));
- paintableToCaptionWrapper.remove(paintable);
+ childWidgetToCaptionWrapper.remove(widget);
}
}
}
@@ -339,15 +309,13 @@ public class VCustomLayout extends ComplexPanel {
/** Removes given widget from the layout */
@Override
public boolean remove(Widget w) {
- ComponentConnector paintable = ConnectorMap.get(client).getConnector(w);
- client.unregisterPaintable(paintable);
final String location = getLocation(w);
if (location != null) {
locationToWidget.remove(location);
}
- final VCaptionWrapper cw = paintableToCaptionWrapper.get(paintable);
+ final VCaptionWrapper cw = childWidgetToCaptionWrapper.get(w);
if (cw != null) {
- paintableToCaptionWrapper.remove(paintable);
+ childWidgetToCaptionWrapper.remove(w);
return super.remove(cw);
} else if (w != null) {
return super.remove(w);
@@ -366,7 +334,7 @@ public class VCustomLayout extends ComplexPanel {
public void clear() {
super.clear();
locationToWidget.clear();
- paintableToCaptionWrapper.clear();
+ childWidgetToCaptionWrapper.clear();
}
/**
diff --git a/src/com/vaadin/ui/CustomLayout.java b/src/com/vaadin/ui/CustomLayout.java
index cb0cf25e6f..e1c6bd595e 100644
--- a/src/com/vaadin/ui/CustomLayout.java
+++ b/src/com/vaadin/ui/CustomLayout.java
@@ -10,8 +10,7 @@ import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
-import com.vaadin.terminal.PaintException;
-import com.vaadin.terminal.PaintTarget;
+import com.vaadin.terminal.gwt.client.ui.CustomLayoutConnector.CustomLayoutState;
/**
* <p>
@@ -52,10 +51,6 @@ public class CustomLayout extends AbstractLayout {
*/
private final HashMap<String, Component> slots = new HashMap<String, Component>();
- private String templateContents = null;
-
- private String templateName = null;
-
/**
* Default constructor only used by subclasses. Subclasses are responsible
* for setting the appropriate fields. Either
@@ -88,7 +83,7 @@ public class CustomLayout extends AbstractLayout {
*/
public CustomLayout(String template) {
this();
- templateName = template;
+ setTemplateName(template);
}
protected void initTemplateContentsFromInputStream(
@@ -108,7 +103,12 @@ public class CustomLayout extends AbstractLayout {
}
}
- templateContents = b.toString();
+ setTemplateContents(b.toString());
+ }
+
+ @Override
+ public CustomLayoutState getState() {
+ return (CustomLayoutState) super.getState();
}
/**
@@ -126,6 +126,7 @@ public class CustomLayout extends AbstractLayout {
removeComponent(old);
}
slots.put(location, c);
+ getState().getChildLocations().put(c, location);
c.setParent(this);
fireComponentAttachEvent(c);
requestRepaint();
@@ -157,6 +158,7 @@ public class CustomLayout extends AbstractLayout {
return;
}
slots.values().remove(c);
+ getState().getChildLocations().remove(c);
super.removeComponent(c);
requestRepaint();
}
@@ -203,37 +205,6 @@ public class CustomLayout extends AbstractLayout {
return slots.get(location);
}
- /**
- * Paints the content of this component.
- *
- * @param target
- * @throws PaintException
- * if the paint operation failed.
- */
- @Override
- public void paintContent(PaintTarget target) throws PaintException {
- super.paintContent(target);
-
- if (templateName != null) {
- target.addAttribute("template", templateName);
- } else {
- target.addAttribute("templateContents", templateContents);
- }
- // Adds all items in all the locations
- for (final Iterator<String> i = slots.keySet().iterator(); i.hasNext();) {
- // Gets the (location,component)
- final String location = i.next();
- final Component c = slots.get(location);
- if (c != null) {
- // Writes the item
- target.startTag("location");
- target.addAttribute("name", location);
- c.paint(target);
- target.endTag("location");
- }
- }
- }
-
/* Documented in superclass */
public void replaceComponent(Component oldComponent, Component newComponent) {
@@ -259,32 +230,20 @@ public class CustomLayout extends AbstractLayout {
} else {
slots.put(newLocation, oldComponent);
slots.put(oldLocation, newComponent);
+ getState().getChildLocations().put(newComponent, oldLocation);
+ getState().getChildLocations().put(oldComponent, newLocation);
requestRepaint();
}
}
- /**
- * CustomLayout's template selecting was previously implemented with
- * setStyle. Overriding to improve backwards compatibility.
- *
- * @param name
- * template name
- * @deprecated Use {@link #setTemplateName(String)} instead
- */
- @Deprecated
- @Override
- public void setStyle(String name) {
- setTemplateName(name);
- }
-
/** Get the name of the template */
public String getTemplateName() {
- return templateName;
+ return getState().getTemplateName();
}
/** Get the contents of the template */
public String getTemplateContents() {
- return templateContents;
+ return getState().getTemplateContents();
}
/**
@@ -297,8 +256,8 @@ public class CustomLayout extends AbstractLayout {
* @param templateName
*/
public void setTemplateName(String templateName) {
- this.templateName = templateName;
- templateContents = null;
+ getState().setTemplateName(templateName);
+ getState().setTemplateContents(null);
requestRepaint();
}
@@ -308,8 +267,8 @@ public class CustomLayout extends AbstractLayout {
* @param templateContents
*/
public void setTemplateContents(String templateContents) {
- this.templateContents = templateContents;
- templateName = null;
+ getState().setTemplateContents(templateContents);
+ getState().setTemplateName(null);
requestRepaint();
}