123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
-
- package com.vaadin.terminal.gwt.client.ui;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
-
- import com.google.gwt.core.client.Scheduler;
- import com.google.gwt.core.client.Scheduler.ScheduledCommand;
- import com.google.gwt.event.dom.client.BlurEvent;
- import com.google.gwt.event.dom.client.BlurHandler;
- import com.google.gwt.event.dom.client.FocusEvent;
- import com.google.gwt.event.dom.client.FocusHandler;
- import com.google.gwt.event.dom.client.KeyDownEvent;
- import com.google.gwt.event.dom.client.KeyDownHandler;
- import com.google.gwt.event.dom.client.ScrollEvent;
- import com.google.gwt.event.dom.client.ScrollHandler;
- import com.google.gwt.user.client.Command;
- 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.Window;
- import com.google.gwt.user.client.ui.HasWidgets;
- import com.google.gwt.user.client.ui.RootPanel;
- 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.Console;
- import com.vaadin.terminal.gwt.client.EventId;
- import com.vaadin.terminal.gwt.client.Focusable;
- import com.vaadin.terminal.gwt.client.Util;
- import com.vaadin.terminal.gwt.client.ComponentConnector;
- import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
-
- /**
- * "Sub window" component.
- *
- * @author Vaadin Ltd
- */
- public class VWindow extends VOverlay implements ShortcutActionHandlerOwner,
- ScrollHandler, KeyDownHandler, FocusHandler, BlurHandler, Focusable {
-
- /**
- * Minimum allowed height of a window. This refers to the content area, not
- * the outer borders.
- */
- private static final int MIN_CONTENT_AREA_HEIGHT = 100;
-
- /**
- * Minimum allowed width of a window. This refers to the content area, not
- * the outer borders.
- */
- private static final int MIN_CONTENT_AREA_WIDTH = 150;
-
- private static ArrayList<VWindow> windowOrder = new ArrayList<VWindow>();
-
- private static boolean orderingDefered;
-
- public static final String CLASSNAME = "v-window";
-
- /**
- * Difference between offsetWidth and inner width for the content area.
- */
- private int contentAreaBorderPadding = -1;
- /**
- * Pixels used by inner borders and paddings horizontally (calculated only
- * once). This is the difference between the width of the root element and
- * the content area, such that if root element width is set to "XYZpx" the
- * inner width (width-border-padding) of the content area is
- * X-contentAreaRootDifference.
- */
- private int contentAreaToRootDifference = -1;
-
- private static final int STACKING_OFFSET_PIXELS = 15;
-
- public static final int Z_INDEX = 10000;
-
- ComponentConnector layout;
-
- Element contents;
-
- private Element header;
-
- private Element footer;
-
- private Element resizeBox;
-
- final FocusableScrollPanel contentPanel = new FocusableScrollPanel();
-
- private boolean dragging;
-
- private int startX;
-
- private int startY;
-
- private int origX;
-
- private int origY;
-
- private boolean resizing;
-
- private int origW;
-
- private int origH;
-
- Element closeBox;
-
- protected ApplicationConnection client;
-
- String id;
-
- ShortcutActionHandler shortcutHandler;
-
- /** Last known positionx read from UIDL or updated to application connection */
- private int uidlPositionX = -1;
-
- /** Last known positiony read from UIDL or updated to application connection */
- private int uidlPositionY = -1;
-
- boolean vaadinModality = false;
-
- boolean resizable = true;
-
- private boolean draggable = true;
-
- boolean resizeLazy = false;
-
- private Element modalityCurtain;
- private Element draggingCurtain;
- private Element resizingCurtain;
-
- private Element headerText;
-
- private boolean closable = true;
-
- boolean dynamicWidth = false;
- boolean dynamicHeight = false;
- boolean layoutRelativeWidth = false;
- boolean layoutRelativeHeight = false;
-
- // If centered (via UIDL), the window should stay in the centered -mode
- // until a position is received from the server, or the user moves or
- // resizes the window.
- boolean centered = false;
-
- private String width;
-
- private String height;
-
- boolean immediate;
-
- private Element wrapper, wrapper2;
-
- boolean visibilityChangesDisabled;
-
- int bringToFrontSequence = -1;
-
- private VLazyExecutor delayedContentsSizeUpdater = new VLazyExecutor(200,
- new ScheduledCommand() {
-
- public void execute() {
- updateContentsSize();
- }
- });
-
- public VWindow() {
- super(false, false, true); // no autohide, not modal, shadow
- // Different style of shadow for windows
- setShadowStyle("window");
-
- final int order = windowOrder.size();
- setWindowOrder(order);
- windowOrder.add(this);
- constructDOM();
- setPopupPosition(order * STACKING_OFFSET_PIXELS, order
- * STACKING_OFFSET_PIXELS);
- contentPanel.addScrollHandler(this);
- contentPanel.addKeyDownHandler(this);
- contentPanel.addFocusHandler(this);
- contentPanel.addBlurHandler(this);
- }
-
- public void bringToFront() {
- int curIndex = windowOrder.indexOf(this);
- if (curIndex + 1 < windowOrder.size()) {
- windowOrder.remove(this);
- windowOrder.add(this);
- for (; curIndex < windowOrder.size(); curIndex++) {
- windowOrder.get(curIndex).setWindowOrder(curIndex);
- }
- }
- }
-
- /**
- * Returns true if this window is the topmost VWindow
- *
- * @return
- */
- private boolean isActive() {
- return windowOrder.get(windowOrder.size() - 1).equals(this);
- }
-
- private void setWindowOrder(int order) {
- setZIndex(order + Z_INDEX);
- }
-
- @Override
- protected void setZIndex(int zIndex) {
- super.setZIndex(zIndex);
- if (vaadinModality) {
- DOM.setStyleAttribute(getModalityCurtain(), "zIndex", "" + zIndex);
- }
- }
-
- protected Element getModalityCurtain() {
- if (modalityCurtain == null) {
- modalityCurtain = DOM.createDiv();
- modalityCurtain.setClassName(CLASSNAME + "-modalitycurtain");
- }
- return modalityCurtain;
- }
-
- protected void constructDOM() {
- setStyleName(CLASSNAME);
-
- header = DOM.createDiv();
- DOM.setElementProperty(header, "className", CLASSNAME + "-outerheader");
- headerText = DOM.createDiv();
- DOM.setElementProperty(headerText, "className", CLASSNAME + "-header");
- contents = DOM.createDiv();
- DOM.setElementProperty(contents, "className", CLASSNAME + "-contents");
- footer = DOM.createDiv();
- DOM.setElementProperty(footer, "className", CLASSNAME + "-footer");
- resizeBox = DOM.createDiv();
- DOM.setElementProperty(resizeBox, "className", CLASSNAME + "-resizebox");
- closeBox = DOM.createDiv();
- DOM.setElementProperty(closeBox, "className", CLASSNAME + "-closebox");
- DOM.appendChild(footer, resizeBox);
-
- wrapper = DOM.createDiv();
- DOM.setElementProperty(wrapper, "className", CLASSNAME + "-wrap");
-
- wrapper2 = DOM.createDiv();
- DOM.setElementProperty(wrapper2, "className", CLASSNAME + "-wrap2");
-
- DOM.appendChild(wrapper2, closeBox);
- DOM.appendChild(wrapper2, header);
- DOM.appendChild(header, headerText);
- DOM.appendChild(wrapper2, contents);
- DOM.appendChild(wrapper2, footer);
- DOM.appendChild(wrapper, wrapper2);
- DOM.appendChild(super.getContainerElement(), wrapper);
-
- sinkEvents(Event.MOUSEEVENTS | Event.TOUCHEVENTS | Event.ONCLICK
- | Event.ONLOSECAPTURE);
-
- setWidget(contentPanel);
-
- }
-
- /**
- * Calling this method will defer ordering algorithm, to order windows based
- * on servers bringToFront and modality instructions. Non changed windows
- * will be left intact.
- */
- static void deferOrdering() {
- if (!orderingDefered) {
- orderingDefered = true;
- Scheduler.get().scheduleFinally(new Command() {
- public void execute() {
- doServerSideOrdering();
- }
- });
- }
- }
-
- private static void doServerSideOrdering() {
- orderingDefered = false;
- VWindow[] array = windowOrder.toArray(new VWindow[windowOrder.size()]);
- Arrays.sort(array, new Comparator<VWindow>() {
- public int compare(VWindow o1, VWindow o2) {
- /*
- * Order by modality, then by bringtofront sequence.
- */
-
- if (o1.vaadinModality && !o2.vaadinModality) {
- return 1;
- } else if (!o1.vaadinModality && o2.vaadinModality) {
- return -1;
- } else if (o1.bringToFrontSequence > o2.bringToFrontSequence) {
- return 1;
- } else if (o1.bringToFrontSequence < o2.bringToFrontSequence) {
- return -1;
- } else {
- return 0;
- }
- }
- });
- for (int i = 0; i < array.length; i++) {
- VWindow w = array[i];
- if (w.bringToFrontSequence != -1 || w.vaadinModality) {
- w.bringToFront();
- w.bringToFrontSequence = -1;
- }
- }
- }
-
- @Override
- public void setVisible(boolean visible) {
- /*
- * Visibility with VWindow works differently than with other Paintables
- * in Vaadin. Invisible VWindows are not attached to DOM at all. Flag is
- * used to avoid visibility call from
- * ApplicationConnection.updateComponent();
- */
- if (!visibilityChangesDisabled) {
- super.setVisible(visible);
- }
- }
-
- void setDraggable(boolean draggable) {
- if (this.draggable == draggable) {
- return;
- }
-
- this.draggable = draggable;
-
- setCursorProperties();
- }
-
- private void setCursorProperties() {
- if (!draggable) {
- header.getStyle().setProperty("cursor", "default");
- footer.getStyle().setProperty("cursor", "default");
- } else {
- header.getStyle().setProperty("cursor", "");
- footer.getStyle().setProperty("cursor", "");
- }
- }
-
- void setNaturalWidth() {
- /*
- * Use max(layout width, window width) i.e layout content width or
- * caption width. We remove the previous set width so the width is
- * allowed to shrink. All widths are measured as outer sizes, i.e. the
- * borderWidth is added to the content.
- */
-
- DOM.setStyleAttribute(getElement(), "width", "");
-
- // Content
- int contentWidth = contentPanel.getElement().getScrollWidth();
- contentWidth += getContentAreaToRootDifference();
-
- // Window width (caption)
- int windowCaptionWidth = getOffsetWidth();
-
- int naturalWidth = (contentWidth > windowCaptionWidth ? contentWidth
- : windowCaptionWidth);
-
- setWidth(naturalWidth + "px");
- }
-
- private int getContentAreaToRootDifference() {
- if (contentAreaToRootDifference < 0) {
- measure();
- }
- return contentAreaToRootDifference;
- }
-
- private void measure() {
- if (!isAttached()) {
- return;
- }
-
- contentAreaBorderPadding = Util.measureHorizontalPaddingAndBorder(
- contents, 4);
- int wrapperPaddingBorder = Util.measureHorizontalPaddingAndBorder(
- wrapper, 0)
- + Util.measureHorizontalPaddingAndBorder(wrapper2, 0);
-
- contentAreaToRootDifference = wrapperPaddingBorder
- + contentAreaBorderPadding;
-
- }
-
- /**
- * Sets the closable state of the window. Additionally hides/shows the close
- * button according to the new state.
- *
- * @param closable
- * true if the window can be closed by the user
- */
- protected void setClosable(boolean closable) {
- if (this.closable == closable) {
- return;
- }
-
- this.closable = closable;
- if (closable) {
- DOM.setStyleAttribute(closeBox, "display", "");
- } else {
- DOM.setStyleAttribute(closeBox, "display", "none");
- }
-
- }
-
- /**
- * Returns the closable state of the sub window. If the sub window is
- * closable a decoration (typically an X) is shown to the user. By clicking
- * on the X the user can close the window.
- *
- * @return true if the sub window is closable
- */
- protected boolean isClosable() {
- return closable;
- }
-
- @Override
- public void show() {
- if (vaadinModality) {
- showModalityCurtain();
- }
- super.show();
- }
-
- @Override
- public void hide() {
- if (vaadinModality) {
- hideModalityCurtain();
- }
- super.hide();
- }
-
- void setVaadinModality(boolean modality) {
- vaadinModality = modality;
- if (vaadinModality) {
- if (isAttached()) {
- showModalityCurtain();
- }
- deferOrdering();
- } else {
- if (modalityCurtain != null) {
- if (isAttached()) {
- hideModalityCurtain();
- }
- modalityCurtain = null;
- }
- }
- }
-
- private void showModalityCurtain() {
- DOM.setStyleAttribute(getModalityCurtain(), "zIndex",
- "" + (windowOrder.indexOf(this) + Z_INDEX));
- if (isShowing()) {
- RootPanel.getBodyElement().insertBefore(getModalityCurtain(),
- getElement());
- } else {
- DOM.appendChild(RootPanel.getBodyElement(), getModalityCurtain());
- }
- }
-
- private void hideModalityCurtain() {
- DOM.removeChild(RootPanel.getBodyElement(), modalityCurtain);
- }
-
- /*
- * Shows an empty div on top of all other content; used when moving, so that
- * iframes (etc) do not steal event.
- */
- private void showDraggingCurtain() {
- DOM.appendChild(RootPanel.getBodyElement(), getDraggingCurtain());
- }
-
- private void hideDraggingCurtain() {
- if (draggingCurtain != null) {
- DOM.removeChild(RootPanel.getBodyElement(), draggingCurtain);
- }
- }
-
- /*
- * Shows an empty div on top of all other content; used when resizing, so
- * that iframes (etc) do not steal event.
- */
- private void showResizingCurtain() {
- DOM.appendChild(RootPanel.getBodyElement(), getResizingCurtain());
- }
-
- private void hideResizingCurtain() {
- if (resizingCurtain != null) {
- DOM.removeChild(RootPanel.getBodyElement(), resizingCurtain);
- }
- }
-
- private Element getDraggingCurtain() {
- if (draggingCurtain == null) {
- draggingCurtain = createCurtain();
- draggingCurtain.setClassName(CLASSNAME + "-draggingCurtain");
- }
-
- return draggingCurtain;
- }
-
- private Element getResizingCurtain() {
- if (resizingCurtain == null) {
- resizingCurtain = createCurtain();
- resizingCurtain.setClassName(CLASSNAME + "-resizingCurtain");
- }
-
- return resizingCurtain;
- }
-
- private Element createCurtain() {
- Element curtain = DOM.createDiv();
-
- DOM.setStyleAttribute(curtain, "position", "absolute");
- DOM.setStyleAttribute(curtain, "top", "0px");
- DOM.setStyleAttribute(curtain, "left", "0px");
- DOM.setStyleAttribute(curtain, "width", "100%");
- DOM.setStyleAttribute(curtain, "height", "100%");
- DOM.setStyleAttribute(curtain, "zIndex", "" + VOverlay.Z_INDEX);
-
- return curtain;
- }
-
- void setResizable(boolean resizability) {
- resizable = resizability;
- if (resizability) {
- DOM.setElementProperty(footer, "className", CLASSNAME + "-footer");
- DOM.setElementProperty(resizeBox, "className", CLASSNAME
- + "-resizebox");
- } else {
- DOM.setElementProperty(footer, "className", CLASSNAME + "-footer "
- + CLASSNAME + "-footer-noresize");
- DOM.setElementProperty(resizeBox, "className", CLASSNAME
- + "-resizebox " + CLASSNAME + "-resizebox-disabled");
- }
- }
-
- @Override
- public void setPopupPosition(int left, int top) {
- if (top < 0) {
- // ensure window is not moved out of browser window from top of the
- // screen
- top = 0;
- }
- super.setPopupPosition(left, top);
- if (left != uidlPositionX && client != null) {
- client.updateVariable(id, "positionx", left, false);
- uidlPositionX = left;
- }
- if (top != uidlPositionY && client != null) {
- client.updateVariable(id, "positiony", top, false);
- uidlPositionY = top;
- }
- }
-
- public void setCaption(String c) {
- setCaption(c, null);
- }
-
- public void setCaption(String c, String icon) {
- String html = Util.escapeHTML(c);
- if (icon != null) {
- icon = client.translateVaadinUri(icon);
- html = "<img src=\"" + Util.escapeAttribute(icon)
- + "\" class=\"v-icon\" />" + html;
- }
- DOM.setInnerHTML(headerText, html);
- }
-
- @Override
- protected Element getContainerElement() {
- // in GWT 1.5 this method is used in PopupPanel constructor
- if (contents == null) {
- return super.getContainerElement();
- }
- return contents;
- }
-
- @Override
- public void onBrowserEvent(final Event event) {
- boolean bubble = true;
-
- final int type = event.getTypeInt();
-
- final Element target = DOM.eventGetTarget(event);
-
- if (client != null && header.isOrHasChild(target)) {
- // Handle window caption tooltips
- client.handleTooltipEvent(event, this);
- }
-
- if (resizing || resizeBox == target) {
- onResizeEvent(event);
- bubble = false;
- } else if (isClosable() && target == closeBox) {
- if (type == Event.ONCLICK) {
- onCloseClick();
- }
- bubble = false;
- } else if (dragging || !contents.isOrHasChild(target)) {
- onDragEvent(event);
- bubble = false;
- } else if (type == Event.ONCLICK) {
- // clicked inside window, ensure to be on top
- if (!isActive()) {
- bringToFront();
- }
- }
-
- /*
- * If clicking on other than the content, move focus to the window.
- * After that this windows e.g. gets all keyboard shortcuts.
- */
- if (type == Event.ONMOUSEDOWN
- && !contentPanel.getElement().isOrHasChild(target)
- && target != closeBox) {
- contentPanel.focus();
- }
-
- if (!bubble) {
- event.stopPropagation();
- } else {
- // Super.onBrowserEvent takes care of Handlers added by the
- // ClickEventHandler
- super.onBrowserEvent(event);
- }
- }
-
- private void onCloseClick() {
- client.updateVariable(id, "close", true, true);
- }
-
- private void onResizeEvent(Event event) {
- if (resizable && Util.isTouchEventOrLeftMouseButton(event)) {
- switch (event.getTypeInt()) {
- case Event.ONMOUSEDOWN:
- case Event.ONTOUCHSTART:
- if (!isActive()) {
- bringToFront();
- }
- showResizingCurtain();
- if (BrowserInfo.get().isIE()) {
- DOM.setStyleAttribute(resizeBox, "visibility", "hidden");
- }
- resizing = true;
- startX = Util.getTouchOrMouseClientX(event);
- startY = Util.getTouchOrMouseClientY(event);
- origW = getElement().getOffsetWidth();
- origH = getElement().getOffsetHeight();
- DOM.setCapture(getElement());
- event.preventDefault();
- break;
- case Event.ONMOUSEUP:
- case Event.ONTOUCHEND:
- setSize(event, true);
- case Event.ONTOUCHCANCEL:
- DOM.releaseCapture(getElement());
- case Event.ONLOSECAPTURE:
- hideResizingCurtain();
- if (BrowserInfo.get().isIE()) {
- DOM.setStyleAttribute(resizeBox, "visibility", "");
- }
- resizing = false;
- break;
- case Event.ONMOUSEMOVE:
- case Event.ONTOUCHMOVE:
- if (resizing) {
- centered = false;
- setSize(event, false);
- event.preventDefault();
- }
- break;
- default:
- event.preventDefault();
- break;
- }
- }
- }
-
- /**
- * TODO check if we need to support this with touch based devices.
- *
- * Checks if the cursor was inside the browser content area when the event
- * happened.
- *
- * @param event
- * The event to be checked
- * @return true, if the cursor is inside the browser content area
- *
- * false, otherwise
- */
- private boolean cursorInsideBrowserContentArea(Event event) {
- if (event.getClientX() < 0 || event.getClientY() < 0) {
- // Outside to the left or above
- return false;
- }
-
- if (event.getClientX() > Window.getClientWidth()
- || event.getClientY() > Window.getClientHeight()) {
- // Outside to the right or below
- return false;
- }
-
- return true;
- }
-
- private void setSize(Event event, boolean updateVariables) {
- if (!cursorInsideBrowserContentArea(event)) {
- // Only drag while cursor is inside the browser client area
- return;
- }
-
- int w = Util.getTouchOrMouseClientX(event) - startX + origW;
- if (w < MIN_CONTENT_AREA_WIDTH + getContentAreaToRootDifference()) {
- w = MIN_CONTENT_AREA_WIDTH + getContentAreaToRootDifference();
- }
-
- int h = Util.getTouchOrMouseClientY(event) - startY + origH;
- if (h < MIN_CONTENT_AREA_HEIGHT + getExtraHeight()) {
- h = MIN_CONTENT_AREA_HEIGHT + getExtraHeight();
- }
-
- setWidth(w + "px");
- setHeight(h + "px");
-
- if (updateVariables) {
- // sending width back always as pixels, no need for unit
- client.updateVariable(id, "width", w, false);
- client.updateVariable(id, "height", h, immediate);
- }
-
- if (updateVariables || !resizeLazy) {
- // Resize has finished or is not lazy
- updateContentsSize();
- } else {
- // Lazy resize - wait for a while before re-rendering contents
- delayedContentsSizeUpdater.trigger();
- }
- }
-
- private void updateContentsSize() {
- // Update child widget dimensions
- if (client != null) {
- client.handleComponentRelativeSize(layout.getWidget());
- client.runDescendentsLayout((HasWidgets) layout
- .getWidget());
- }
-
- Util.runWebkitOverflowAutoFix(contentPanel.getElement());
- client.doLayout(false);
- }
-
- @Override
- /**
- * Width is set to the out-most element (v-window).
- *
- * This function should never be called with percentage values (it will
- * throw an exception)
- */
- public void setWidth(String width) {
- this.width = width;
- if (!isAttached()) {
- return;
- }
- if (width != null && !"".equals(width)) {
- int rootPixelWidth = -1;
- if (width.indexOf("px") < 0) {
- /*
- * Convert non-pixel values to pixels by setting the width and
- * then measuring it. Updates the "width" variable with the
- * pixel width.
- */
- DOM.setStyleAttribute(getElement(), "width", width);
- rootPixelWidth = getElement().getOffsetWidth();
- width = rootPixelWidth + "px";
- } else {
- rootPixelWidth = Integer.parseInt(width.substring(0,
- width.indexOf("px")));
- }
-
- // "width" now contains the new width in pixels
-
- // Apply the new pixel width
- getElement().getStyle().setProperty("width", width);
-
- // Caculate the inner width of the content area
- int contentAreaInnerWidth = rootPixelWidth
- - getContentAreaToRootDifference();
- if (contentAreaInnerWidth < MIN_CONTENT_AREA_WIDTH) {
- contentAreaInnerWidth = MIN_CONTENT_AREA_WIDTH;
- int rootWidth = contentAreaInnerWidth
- + getContentAreaToRootDifference();
- DOM.setStyleAttribute(getElement(), "width", rootWidth + "px");
- }
-
- updateShadowSizeAndPosition();
- }
- }
-
- @Override
- /**
- * Height is set to the out-most element (v-window).
- *
- * This function should never be called with percentage values (it will
- * throw an exception)
- *
- * @param height A CSS string specifying the new height of the window.
- * An empty string or null clears the height and lets
- * the browser to compute it based on the window contents.
- */
- public void setHeight(String height) {
- if (!isAttached()
- || (height == null ? this.height == null : height
- .equals(this.height))) {
- return;
- }
- if (height == null || "".equals(height)) {
- getElement().getStyle().clearHeight();
- contentPanel.getElement().getStyle().clearHeight();
- } else {
- getElement().getStyle().setProperty("height", height);
- int contentHeight = getElement().getOffsetHeight()
- - getExtraHeight();
- if (contentHeight < MIN_CONTENT_AREA_HEIGHT) {
- contentHeight = MIN_CONTENT_AREA_HEIGHT;
- int rootHeight = contentHeight + getExtraHeight();
- getElement().getStyle()
- .setProperty("height", rootHeight + "px");
- }
- contentPanel.getElement().getStyle()
- .setProperty("height", contentHeight + "px");
- }
- this.height = height;
- updateShadowSizeAndPosition();
- }
-
- int getExtraHeight() {
- return header.getOffsetHeight() + footer.getOffsetHeight();
- }
-
- private void onDragEvent(Event event) {
- if (!Util.isTouchEventOrLeftMouseButton(event)) {
- return;
- }
-
- switch (DOM.eventGetType(event)) {
- case Event.ONTOUCHSTART:
- if (event.getTouches().length() > 1) {
- return;
- }
- case Event.ONMOUSEDOWN:
- if (!isActive()) {
- bringToFront();
- }
- beginMovingWindow(event);
- break;
- case Event.ONMOUSEUP:
- case Event.ONTOUCHEND:
- case Event.ONTOUCHCANCEL:
- case Event.ONLOSECAPTURE:
- stopMovingWindow();
- break;
- case Event.ONMOUSEMOVE:
- case Event.ONTOUCHMOVE:
- moveWindow(event);
- break;
- default:
- break;
- }
- }
-
- private void moveWindow(Event event) {
- if (dragging) {
- centered = false;
- if (cursorInsideBrowserContentArea(event)) {
- // Only drag while cursor is inside the browser client area
- final int x = Util.getTouchOrMouseClientX(event) - startX
- + origX;
- final int y = Util.getTouchOrMouseClientY(event) - startY
- + origY;
- setPopupPosition(x, y);
- }
- DOM.eventPreventDefault(event);
- }
- }
-
- private void beginMovingWindow(Event event) {
- if (draggable) {
- showDraggingCurtain();
- dragging = true;
- startX = Util.getTouchOrMouseClientX(event);
- startY = Util.getTouchOrMouseClientY(event);
- origX = DOM.getAbsoluteLeft(getElement());
- origY = DOM.getAbsoluteTop(getElement());
- DOM.setCapture(getElement());
- DOM.eventPreventDefault(event);
- }
- }
-
- private void stopMovingWindow() {
- dragging = false;
- hideDraggingCurtain();
- DOM.releaseCapture(getElement());
- }
-
- @Override
- public boolean onEventPreview(Event event) {
- if (dragging) {
- onDragEvent(event);
- return false;
- } else if (resizing) {
- onResizeEvent(event);
- return false;
- } else if (vaadinModality) {
- // return false when modal and outside window
- final Element target = event.getEventTarget().cast();
- if (DOM.getCaptureElement() != null) {
- // Allow events when capture is set
- return true;
- }
-
- if (!DOM.isOrHasChild(getElement(), target)) {
- // not within the modal window, but let's see if it's in the
- // debug window
- Widget w = Util.findWidget(target, null);
- while (w != null) {
- if (w instanceof Console) {
- return true; // allow debug-window clicks
- } else if (w instanceof ComponentConnector) {
- return false;
- }
- w = w.getParent();
- }
- return false;
- }
- }
- return true;
- }
-
- @Override
- public void addStyleDependentName(String styleSuffix) {
- // VWindow's getStyleElement() does not return the same element as
- // getElement(), so we need to override this.
- setStyleName(getElement(), getStylePrimaryName() + "-" + styleSuffix,
- true);
- }
-
- @Override
- protected void onAttach() {
- super.onAttach();
-
- setWidth(width);
- setHeight(height);
- }
-
- void requestLayout() {
- if (dynamicWidth && !layoutRelativeWidth) {
- setNaturalWidth();
- }
- // layout size change may affect its available space (scrollbars)
- client.handleComponentRelativeSize(layout.getWidget());
- }
-
- public ShortcutActionHandler getShortcutActionHandler() {
- return shortcutHandler;
- }
-
- public void onScroll(ScrollEvent event) {
- client.updateVariable(id, "scrollTop",
- contentPanel.getScrollPosition(), false);
- client.updateVariable(id, "scrollLeft",
- contentPanel.getHorizontalScrollPosition(), false);
-
- }
-
- public void onKeyDown(KeyDownEvent event) {
- if (shortcutHandler != null) {
- shortcutHandler
- .handleKeyboardEvent(Event.as(event.getNativeEvent()));
- return;
- }
- }
-
- public void onBlur(BlurEvent event) {
- if (client.hasEventListeners(this, EventId.BLUR)) {
- client.updateVariable(id, EventId.BLUR, "", true);
- }
- }
-
- public void onFocus(FocusEvent event) {
- if (client.hasEventListeners(this, EventId.FOCUS)) {
- client.updateVariable(id, EventId.FOCUS, "", true);
- }
- }
-
- public void focus() {
- contentPanel.focus();
- }
-
- }
|