|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.itmill.toolkit.terminal.gwt.client.ui;
-
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Element;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.user.client.ui.SimplePanel;
- import com.google.gwt.user.client.ui.Widget;
- import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
- import com.itmill.toolkit.terminal.gwt.client.BrowserInfo;
- import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
- import com.itmill.toolkit.terminal.gwt.client.IErrorMessage;
- import com.itmill.toolkit.terminal.gwt.client.Paintable;
- import com.itmill.toolkit.terminal.gwt.client.UIDL;
- import com.itmill.toolkit.terminal.gwt.client.Util;
-
- public class IPanel extends SimplePanel implements Paintable,
- ContainerResizedListener {
-
- public static final String CLASSNAME = "i-panel";
-
- ApplicationConnection client;
-
- String id;
-
- private final Element captionNode = DOM.createDiv();
-
- private final Element captionText = DOM.createSpan();
-
- private Icon icon;
-
- private final Element bottomDecoration = DOM.createDiv();
-
- private final Element contentNode = DOM.createDiv();
-
- private Element errorIndicatorElement;
-
- private IErrorMessage errorMessage;
-
- private String height;
-
- private Paintable layout;
-
- ShortcutActionHandler shortcutHandler;
-
- private String width;
-
- private Element geckoCaptionMeter;
-
- private int scrollTop;
-
- private int scrollLeft;
-
- public IPanel() {
- super();
- DOM.appendChild(getElement(), captionNode);
- DOM.appendChild(captionNode, captionText);
- DOM.appendChild(getElement(), contentNode);
- DOM.appendChild(getElement(), bottomDecoration);
- setStyleName(CLASSNAME);
- DOM
- .setElementProperty(captionNode, "className", CLASSNAME
- + "-caption");
- DOM
- .setElementProperty(contentNode, "className", CLASSNAME
- + "-content");
- DOM.setElementProperty(bottomDecoration, "className", CLASSNAME
- + "-deco");
- DOM.sinkEvents(getElement(), Event.ONKEYDOWN);
- DOM.sinkEvents(contentNode, Event.ONSCROLL);
- }
-
- protected Element getContainerElement() {
- return contentNode;
- }
-
- private void setCaption(String text) {
- DOM.setInnerText(captionText, text);
- }
-
- public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
- // Ensure correct implementation
- if (client.updateComponent(this, uidl, false)) {
- return;
- }
-
- this.client = client;
- id = uidl.getId();
-
- // Panel size. Height needs to be saved for later use
- height = uidl.hasVariable("height") ? uidl.getStringVariable("height")
- : null;
- setWidth(uidl.hasVariable("width") ? uidl.getStringVariable("width")
- : "");
-
- // Restore default stylenames
- DOM
- .setElementProperty(captionNode, "className", CLASSNAME
- + "-caption");
- DOM
- .setElementProperty(contentNode, "className", CLASSNAME
- + "-content");
- DOM.setElementProperty(bottomDecoration, "className", CLASSNAME
- + "-deco");
-
- // Handle caption displaying
- boolean hasCaption = false;
- if (uidl.hasAttribute("caption")
- && !uidl.getStringAttribute("caption").equals("")) {
- setCaption(uidl.getStringAttribute("caption"));
- hasCaption = true;
- } else {
- setCaption("");
- DOM.setElementProperty(captionNode, "className", CLASSNAME
- + "-nocaption");
- }
-
- setIconUri(uidl, client);
-
- handleDescription(uidl);
-
- handleError(uidl);
-
- // Add proper stylenames for all elements. This way we can prevent
- // unwanted CSS selector inheritance.
- if (uidl.hasAttribute("style")) {
- final String[] styles = uidl.getStringAttribute("style").split(" ");
- final String captionBaseClass = CLASSNAME
- + (hasCaption ? "-caption" : "-nocaption");
- final String contentBaseClass = CLASSNAME + "-content";
- final String decoBaseClass = CLASSNAME + "-deco";
- String captionClass = captionBaseClass;
- String contentClass = contentBaseClass;
- String decoClass = decoBaseClass;
- for (int i = 0; i < styles.length; i++) {
- captionClass += " " + captionBaseClass + "-" + styles[i];
- contentClass += " " + contentBaseClass + "-" + styles[i];
- decoClass += " " + decoBaseClass + "-" + styles[i];
- }
- DOM.setElementProperty(captionNode, "className", captionClass);
- DOM.setElementProperty(contentNode, "className", contentClass);
- DOM.setElementProperty(bottomDecoration, "className", decoClass);
- }
-
- // Height adjustment
- iLayout(false);
-
- // Render content
- final UIDL layoutUidl = uidl.getChildUIDL(0);
- final Paintable newLayout = client.getPaintable(layoutUidl);
- if (newLayout != layout) {
- if (layout != null) {
- client.unregisterPaintable(layout);
- }
- setWidget((Widget) newLayout);
- layout = newLayout;
- }
- (layout).updateFromUIDL(layoutUidl, client);
-
- // We may have actions attached to this panel
- if (uidl.getChildCount() > 1) {
- final int cnt = uidl.getChildCount();
- for (int i = 1; i < cnt; i++) {
- UIDL childUidl = uidl.getChildUIDL(i);
- if (childUidl.getTag().equals("actions")) {
- if (shortcutHandler == null) {
- shortcutHandler = new ShortcutActionHandler(id, client);
- }
- shortcutHandler.updateActionMap(childUidl);
- }
- }
- }
-
- if (uidl.hasVariable("scrollTop")
- && uidl.getIntVariable("scrollTop") != scrollTop) {
- scrollTop = uidl.getIntVariable("scrollTop");
- DOM.setElementPropertyInt(contentNode, "scrollTop", scrollTop);
- }
-
- if (uidl.hasVariable("scrollLeft")
- && uidl.getIntVariable("scrollLeft") != scrollLeft) {
- scrollLeft = uidl.getIntVariable("scrollLeft");
- DOM.setElementPropertyInt(contentNode, "scrollLeft", scrollLeft);
- }
-
- }
-
- private void handleError(UIDL uidl) {
- if (uidl.hasAttribute("error")) {
- final UIDL errorUidl = uidl.getErrors();
- if (errorIndicatorElement == null) {
- errorIndicatorElement = DOM.createDiv();
- DOM.setElementProperty(errorIndicatorElement, "className",
- "i-errorindicator");
- DOM.sinkEvents(errorIndicatorElement, Event.MOUSEEVENTS);
- sinkEvents(Event.MOUSEEVENTS);
- }
- DOM.insertBefore(captionNode, errorIndicatorElement, captionText);
- if (errorMessage == null) {
- errorMessage = new IErrorMessage();
- }
- errorMessage.updateFromUIDL(errorUidl);
-
- } else if (errorIndicatorElement != null) {
- DOM.removeChild(captionNode, errorIndicatorElement);
- errorIndicatorElement = null;
- }
- }
-
- private void handleDescription(UIDL uidl) {
- DOM.setElementProperty(captionText, "title", uidl
- .hasAttribute("description") ? uidl
- .getStringAttribute("description") : "");
- }
-
- private void setIconUri(UIDL uidl, ApplicationConnection client) {
- final String iconUri = uidl.hasAttribute("icon") ? uidl
- .getStringAttribute("icon") : null;
- if (iconUri == null) {
- if (icon != null) {
- DOM.removeChild(captionNode, icon.getElement());
- icon = null;
- }
- } else {
- if (icon == null) {
- icon = new Icon(client);
- DOM.insertChild(captionNode, icon.getElement(), 0);
- }
- icon.setUri(iconUri);
- }
- }
-
- public void iLayout() {
- iLayout(true);
- }
-
- public void iLayout(boolean runGeckoFix) {
- if (height != null && height != "") {
- final boolean hasChildren = getWidget() != null;
- Element contentEl = null;
- String origPositioning = null;
- // save scroll position
- int scrollTop = DOM.getElementPropertyInt(contentNode, "scrollTop");
- int scrollLeft = DOM.getElementPropertyInt(contentNode,
- "scrollLeft");
- if (hasChildren) {
- // Remove children temporary form normal flow to detect proper
- // size
- contentEl = getWidget().getElement();
- origPositioning = DOM.getStyleAttribute(contentEl, "position");
- DOM.setStyleAttribute(contentEl, "position", "absolute");
- }
-
- // Set defaults
- DOM.setStyleAttribute(contentNode, "overflow", "hidden");
- DOM.setStyleAttribute(contentNode, "height", "");
-
- // Calculate target height
- super.setHeight(height);
- final int targetHeight = getOffsetHeight();
-
- // Calculate used height
- super.setHeight("");
- final int usedHeight = DOM.getElementPropertyInt(bottomDecoration,
- "offsetTop")
- + DOM.getElementPropertyInt(bottomDecoration,
- "offsetHeight")
- - DOM.getElementPropertyInt(getElement(), "offsetTop");
-
- // Calculate content area height (don't allow negative values)
- int h = targetHeight - usedHeight;
- if (h < 0) {
- h = 0;
- }
-
- // Set proper values for content element
- DOM.setStyleAttribute(contentNode, "height", h + "px");
- DOM.setStyleAttribute(contentNode, "overflow", "auto");
-
- // Restore content to flow
- if (hasChildren) {
- ApplicationConnection.getConsole().log(
- "positioning:" + origPositioning);
- DOM.setStyleAttribute(contentEl, "position", origPositioning);
- }
- // restore scroll position
- DOM.setElementPropertyInt(contentNode, "scrollTop", scrollTop);
- DOM.setElementPropertyInt(contentNode, "scrollLeft", scrollLeft);
-
- } else {
- DOM.setStyleAttribute(contentNode, "height", "");
- }
-
- if (runGeckoFix && BrowserInfo.get().isGecko()) {
- // workaround for #1764
- if (width == null || width.equals("")) {
- if (geckoCaptionMeter == null) {
- geckoCaptionMeter = DOM.createDiv();
- DOM.appendChild(captionNode, geckoCaptionMeter);
- }
- int captionWidth = DOM.getElementPropertyInt(captionText,
- "offsetWidth");
- int availWidth = DOM.getElementPropertyInt(geckoCaptionMeter,
- "offsetWidth");
- if (captionWidth == availWidth) {
- /*
- * Caption width defines panel width -> Gecko based browsers
- * somehow fails to float things right, without the
- * "noncode" below
- */
- setWidth(getOffsetWidth() + "px");
- } else {
- DOM.setStyleAttribute(captionNode, "width", "");
- }
- }
- }
- Util.runDescendentsLayout(this);
- }
-
- public void onBrowserEvent(Event event) {
- final Element target = DOM.eventGetTarget(event);
- final int type = DOM.eventGetType(event);
- if (type == Event.ONKEYDOWN && shortcutHandler != null) {
- shortcutHandler.handleKeyboardEvent(event);
- return;
- }
- if (type == Event.ONSCROLL) {
- int newscrollTop = DOM.getElementPropertyInt(contentNode,
- "scrollTop");
- int newscrollLeft = DOM.getElementPropertyInt(contentNode,
- "scrollLeft");
- if (client != null
- && (newscrollLeft != scrollLeft || newscrollTop != scrollTop)) {
- ApplicationConnection.getConsole().log("scrollded panel");
- scrollLeft = newscrollLeft;
- scrollTop = newscrollTop;
- client.updateVariable(id, "scrollTop", scrollTop, false);
- client.updateVariable(id, "scrollLeft", scrollLeft, false);
- }
- } else if (errorIndicatorElement != null
- && DOM.compare(target, errorIndicatorElement)) {
- switch (type) {
- case Event.ONMOUSEOVER:
- if (errorMessage != null) {
- errorMessage.showAt(errorIndicatorElement);
- }
- break;
- case Event.ONMOUSEOUT:
- if (errorMessage != null) {
- errorMessage.hide();
- }
- break;
- case Event.ONCLICK:
- ApplicationConnection.getConsole().log(
- DOM.getInnerHTML(errorMessage.getElement()));
- return;
- default:
- break;
- }
- }
- }
-
- /**
- * Panel handles dimensions by itself.
- */
- public void setHeight(String height) {
- // NOP
- }
-
- /**
- * Panel handles dimensions by itself.
- */
- public void setWidth(String width) {
- this.width = width;
- // Let browser handle 100% width (DIV element takes all size by
- // default).
- // This way we can specify borders for Panel's outer element.
- if (width.equals("100%")) {
- super.setWidth("");
- } else {
- super.setWidth(width);
- }
- }
-
- }
|