123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
-
- package com.vaadin.terminal.gwt.client.ui;
-
- import java.util.Set;
-
- import com.google.gwt.dom.client.DivElement;
- import com.google.gwt.dom.client.Document;
- import com.google.gwt.event.dom.client.DomEvent.Type;
- import com.google.gwt.event.dom.client.TouchStartEvent;
- import com.google.gwt.event.dom.client.TouchStartHandler;
- import com.google.gwt.event.shared.EventHandler;
- import com.google.gwt.event.shared.HandlerRegistration;
- 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.vaadin.terminal.gwt.client.ApplicationConnection;
- import com.vaadin.terminal.gwt.client.BrowserInfo;
- import com.vaadin.terminal.gwt.client.Container;
- import com.vaadin.terminal.gwt.client.Focusable;
- import com.vaadin.terminal.gwt.client.RenderInformation;
- import com.vaadin.terminal.gwt.client.RenderSpace;
- import com.vaadin.terminal.gwt.client.UIDL;
- import com.vaadin.terminal.gwt.client.Util;
- import com.vaadin.terminal.gwt.client.VPaintableMap;
- import com.vaadin.terminal.gwt.client.VPaintableWidget;
- import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
-
- public class VPanel extends SimplePanel implements Container,
- ShortcutActionHandlerOwner, Focusable {
-
- public static final String CLICK_EVENT_IDENTIFIER = "click";
- public static final String CLASSNAME = "v-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 String height;
-
- private VPaintableWidget layout;
-
- ShortcutActionHandler shortcutHandler;
-
- private String width = "";
-
- private Element geckoCaptionMeter;
-
- private int scrollTop;
-
- private int scrollLeft;
-
- private RenderInformation renderInformation = new RenderInformation();
-
- private int borderPaddingHorizontal = -1;
-
- private int borderPaddingVertical = -1;
-
- private int captionPaddingHorizontal = -1;
-
- private int captionMarginLeft = -1;
-
- private boolean rendering;
-
- private int contentMarginLeft = -1;
-
- private String previousStyleName;
-
- private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
- CLICK_EVENT_IDENTIFIER) {
-
- @Override
- protected <H extends EventHandler> HandlerRegistration registerHandler(
- H handler, Type<H> type) {
- return addDomHandler(handler, type);
- }
- };
- private TouchScrollDelegate touchScrollDelegate;
-
- public VPanel() {
- super();
- DivElement captionWrap = Document.get().createDivElement();
- captionWrap.appendChild(captionNode);
- captionNode.appendChild(captionText);
-
- captionWrap.setClassName(CLASSNAME + "-captionwrap");
- captionNode.setClassName(CLASSNAME + "-caption");
- contentNode.setClassName(CLASSNAME + "-content");
- bottomDecoration.setClassName(CLASSNAME + "-deco");
-
- getElement().appendChild(captionWrap);
-
- /*
- * Make contentNode focusable only by using the setFocus() method. This
- * behaviour can be changed by invoking setTabIndex() in the serverside
- * implementation
- */
- contentNode.setTabIndex(-1);
-
- getElement().appendChild(contentNode);
-
- getElement().appendChild(bottomDecoration);
- setStyleName(CLASSNAME);
- DOM.sinkEvents(getElement(), Event.ONKEYDOWN);
- DOM.sinkEvents(contentNode, Event.ONSCROLL | Event.TOUCHEVENTS);
- contentNode.getStyle().setProperty("position", "relative");
- getElement().getStyle().setProperty("overflow", "hidden");
- addHandler(new TouchStartHandler() {
- public void onTouchStart(TouchStartEvent event) {
- getTouchScrollDelegate().onTouchStart(event);
- }
- }, TouchStartEvent.getType());
- }
-
- /**
- * Sets the keyboard focus on the Panel
- *
- * @param focus
- * Should the panel have focus or not.
- */
- public void setFocus(boolean focus) {
- if (focus) {
- getContainerElement().focus();
- } else {
- getContainerElement().blur();
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.terminal.gwt.client.Focusable#focus()
- */
- public void focus() {
- setFocus(true);
-
- }
-
- @Override
- protected Element getContainerElement() {
- return contentNode;
- }
-
- private void setCaption(String text) {
- DOM.setInnerHTML(captionText, text);
- }
-
- public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
- rendering = true;
- if (!uidl.hasAttribute("cached")) {
-
- // Handle caption displaying and style names, prior generics.
- // Affects size
- // calculations
-
- // Restore default stylenames
- contentNode.setClassName(CLASSNAME + "-content");
- bottomDecoration.setClassName(CLASSNAME + "-deco");
- captionNode.setClassName(CLASSNAME + "-caption");
- boolean hasCaption = false;
- if (uidl.hasAttribute("caption")
- && !uidl.getStringAttribute("caption").equals("")) {
- setCaption(uidl.getStringAttribute("caption"));
- hasCaption = true;
- } else {
- setCaption("");
- captionNode.setClassName(CLASSNAME + "-nocaption");
- }
-
- // 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];
- }
- captionNode.setClassName(captionClass);
- contentNode.setClassName(contentClass);
- bottomDecoration.setClassName(decoClass);
-
- }
- }
- // Ensure correct implementation
- if (client.updateComponent(this, uidl, false)) {
- rendering = false;
- return;
- }
-
- clickEventHandler.handleEventHandlerRegistration(client);
-
- this.client = client;
- id = uidl.getId();
-
- setIconUri(uidl, client);
-
- handleError(uidl);
-
- // Render content
- final UIDL layoutUidl = uidl.getChildUIDL(0);
- final VPaintableWidget newLayout = client.getPaintable(layoutUidl);
- if (newLayout != layout) {
- if (layout != null) {
- client.unregisterPaintable(layout);
- }
- setWidget(newLayout.getWidgetForPaintable());
- 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");
- contentNode.setScrollTop(scrollTop);
- // re-read the actual scrollTop in case invalid value was set
- // (scrollTop != 0 when no scrollbar exists, other values would be
- // caught by scroll listener), see #3784
- scrollTop = contentNode.getScrollTop();
- }
-
- if (uidl.hasVariable("scrollLeft")
- && uidl.getIntVariable("scrollLeft") != scrollLeft) {
- scrollLeft = uidl.getIntVariable("scrollLeft");
- contentNode.setScrollLeft(scrollLeft);
- // re-read the actual scrollTop in case invalid value was set
- // (scrollTop != 0 when no scrollbar exists, other values would be
- // caught by scroll listener), see #3784
- scrollLeft = contentNode.getScrollLeft();
- }
-
- // Must be run after scrollTop is set as Webkit overflow fix re-sets the
- // scrollTop
- runHacks(false);
-
- // And apply tab index
- if (uidl.hasVariable("tabindex")) {
- contentNode.setTabIndex(uidl.getIntVariable("tabindex"));
- }
-
- rendering = false;
-
- }
-
- @Override
- public void setStyleName(String style) {
- if (!style.equals(previousStyleName)) {
- super.setStyleName(style);
- detectContainerBorders();
- previousStyleName = style;
- }
- }
-
- private void handleError(UIDL uidl) {
- if (uidl.hasAttribute("error")) {
- if (errorIndicatorElement == null) {
- errorIndicatorElement = DOM.createSpan();
- DOM.setElementProperty(errorIndicatorElement, "className",
- "v-errorindicator");
- DOM.sinkEvents(errorIndicatorElement, Event.MOUSEEVENTS);
- sinkEvents(Event.MOUSEEVENTS);
- }
- DOM.insertBefore(captionNode, errorIndicatorElement, captionText);
- } else if (errorIndicatorElement != null) {
- DOM.removeChild(captionNode, errorIndicatorElement);
- errorIndicatorElement = null;
- }
- }
-
- 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 runHacks(boolean runGeckoFix) {
- if ((BrowserInfo.get().isIE()) && (width == null || width.equals(""))) {
- /*
- * IE (what version??) needs width to be specified for the root DIV
- * so we calculate that from the sizes of the caption and layout
- */
- int captionWidth = captionText.getOffsetWidth()
- + getCaptionMarginLeft() + getCaptionPaddingHorizontal();
- int layoutWidth = layout.getWidgetForPaintable().getOffsetWidth()
- + getContainerBorderWidth();
- int width = layoutWidth;
- if (captionWidth > width) {
- width = captionWidth;
- }
-
- super.setWidth(width + "px");
- }
-
- 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", "");
- }
- }
- }
-
- client.runDescendentsLayout(this);
-
- Util.runWebkitOverflowAutoFix(contentNode);
-
- }
-
- @Override
- public void onBrowserEvent(Event event) {
- super.onBrowserEvent(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)) {
- scrollLeft = newscrollLeft;
- scrollTop = newscrollTop;
- client.updateVariable(id, "scrollTop", scrollTop, false);
- client.updateVariable(id, "scrollLeft", scrollLeft, false);
- }
- } else if (captionNode.isOrHasChild(target)) {
- if (client != null) {
- client.handleTooltipEvent(event, this);
- }
- }
- }
-
- protected TouchScrollDelegate getTouchScrollDelegate() {
- if (touchScrollDelegate == null) {
- touchScrollDelegate = new TouchScrollDelegate(contentNode);
- }
- return touchScrollDelegate;
-
- }
-
- @Override
- public void setHeight(String height) {
- this.height = height;
- super.setHeight(height);
- if (height != null && !"".equals(height)) {
- final int targetHeight = getOffsetHeight();
- int containerHeight = targetHeight
- - captionNode.getParentElement().getOffsetHeight()
- - bottomDecoration.getOffsetHeight()
- - getContainerBorderHeight();
- if (containerHeight < 0) {
- containerHeight = 0;
- }
- DOM.setStyleAttribute(contentNode, "height", containerHeight + "px");
- } else {
- DOM.setStyleAttribute(contentNode, "height", "");
- }
- if (!rendering) {
- runHacks(true);
- }
- }
-
- private int getCaptionMarginLeft() {
- if (captionMarginLeft < 0) {
- detectContainerBorders();
- }
- return captionMarginLeft;
- }
-
- private int getContentMarginLeft() {
- if (contentMarginLeft < 0) {
- detectContainerBorders();
- }
- return contentMarginLeft;
- }
-
- private int getCaptionPaddingHorizontal() {
- if (captionPaddingHorizontal < 0) {
- detectContainerBorders();
- }
- return captionPaddingHorizontal;
- }
-
- private int getContainerBorderHeight() {
- if (borderPaddingVertical < 0) {
- detectContainerBorders();
- }
- return borderPaddingVertical;
- }
-
- @Override
- public void setWidth(String width) {
- if (this.width.equals(width)) {
- return;
- }
-
- this.width = width;
- super.setWidth(width);
- if (!rendering) {
- runHacks(true);
-
- if (height.equals("")) {
- // Width change may affect height
- Util.updateRelativeChildrenAndSendSizeUpdateEvent(client, this,
- this);
- }
-
- }
- }
-
- private int getContainerBorderWidth() {
- if (borderPaddingHorizontal < 0) {
- detectContainerBorders();
- }
- return borderPaddingHorizontal;
- }
-
- private void detectContainerBorders() {
- DOM.setStyleAttribute(contentNode, "overflow", "hidden");
-
- borderPaddingHorizontal = Util.measureHorizontalBorder(contentNode);
- borderPaddingVertical = Util.measureVerticalBorder(contentNode);
-
- DOM.setStyleAttribute(contentNode, "overflow", "auto");
-
- captionPaddingHorizontal = Util.measureHorizontalPaddingAndBorder(
- captionNode, 26);
-
- captionMarginLeft = Util.measureMarginLeft(captionNode);
- contentMarginLeft = Util.measureMarginLeft(contentNode);
-
- }
-
- public boolean hasChildComponent(Widget component) {
- if (component != null && component == layout) {
- return true;
- } else {
- return false;
- }
- }
-
- public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
- // TODO This is untested as no layouts require this
- if (oldComponent != layout.getWidgetForPaintable()) {
- return;
- }
-
- setWidget(newComponent);
- layout = VPaintableMap.get(client).getPaintable(newComponent);
- }
-
- public RenderSpace getAllocatedSpace(Widget child) {
- int w = 0;
- int h = 0;
-
- if (width != null && !width.equals("")) {
- w = getOffsetWidth() - getContainerBorderWidth();
- if (w < 0) {
- w = 0;
- }
- }
-
- if (height != null && !height.equals("")) {
- h = contentNode.getOffsetHeight() - getContainerBorderHeight();
- if (h < 0) {
- h = 0;
- }
- }
-
- return new RenderSpace(w, h, true);
- }
-
- public boolean requestLayout(Set<Widget> children) {
- // content size change might cause change to its available space
- // (scrollbars)
- client.handleComponentRelativeSize(layout.getWidgetForPaintable());
- if (height != null && height != "" && width != null && width != "") {
- /*
- * If the height and width has been specified the child components
- * cannot make the size of the layout change
- */
- return true;
- }
- runHacks(false);
- return !renderInformation.updateSize(getElement());
- }
-
- public void updateCaption(VPaintableWidget component, UIDL uidl) {
- // NOP: layouts caption, errors etc not rendered in Panel
- }
-
- @Override
- protected void onAttach() {
- super.onAttach();
- detectContainerBorders();
- }
-
- public ShortcutActionHandler getShortcutActionHandler() {
- return shortcutHandler;
- }
-
- public Widget getWidgetForPaintable() {
- return this;
- }
-
- }
|