Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

VPanel.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Set;
  6. import com.google.gwt.dom.client.DivElement;
  7. import com.google.gwt.dom.client.Document;
  8. import com.google.gwt.event.dom.client.DomEvent.Type;
  9. import com.google.gwt.event.shared.EventHandler;
  10. import com.google.gwt.event.shared.HandlerRegistration;
  11. import com.google.gwt.user.client.DOM;
  12. import com.google.gwt.user.client.Element;
  13. import com.google.gwt.user.client.Event;
  14. import com.google.gwt.user.client.ui.SimplePanel;
  15. import com.google.gwt.user.client.ui.Widget;
  16. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  17. import com.vaadin.terminal.gwt.client.BrowserInfo;
  18. import com.vaadin.terminal.gwt.client.Container;
  19. import com.vaadin.terminal.gwt.client.Paintable;
  20. import com.vaadin.terminal.gwt.client.RenderInformation;
  21. import com.vaadin.terminal.gwt.client.RenderSpace;
  22. import com.vaadin.terminal.gwt.client.UIDL;
  23. import com.vaadin.terminal.gwt.client.Util;
  24. import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner;
  25. public class VPanel extends SimplePanel implements Container,
  26. ShortcutActionHandlerOwner {
  27. public static final String CLICK_EVENT_IDENTIFIER = "click";
  28. public static final String CLASSNAME = "v-panel";
  29. ApplicationConnection client;
  30. String id;
  31. private final Element captionNode = DOM.createDiv();
  32. private final Element captionText = DOM.createSpan();
  33. private Icon icon;
  34. private final Element bottomDecoration = DOM.createDiv();
  35. private final Element contentNode = DOM.createDiv();
  36. private Element errorIndicatorElement;
  37. private String height;
  38. private Paintable layout;
  39. ShortcutActionHandler shortcutHandler;
  40. private String width = "";
  41. private Element geckoCaptionMeter;
  42. private int scrollTop;
  43. private int scrollLeft;
  44. private RenderInformation renderInformation = new RenderInformation();
  45. private int borderPaddingHorizontal = -1;
  46. private int borderPaddingVertical = -1;
  47. private int captionPaddingHorizontal = -1;
  48. private int captionMarginLeft = -1;
  49. private boolean rendering;
  50. private int contentMarginLeft = -1;
  51. private String previousStyleName;
  52. private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
  53. CLICK_EVENT_IDENTIFIER) {
  54. @Override
  55. protected <H extends EventHandler> HandlerRegistration registerHandler(
  56. H handler, Type<H> type) {
  57. return addDomHandler(handler, type);
  58. }
  59. };
  60. public VPanel() {
  61. super();
  62. DivElement captionWrap = Document.get().createDivElement();
  63. captionWrap.appendChild(captionNode);
  64. captionNode.appendChild(captionText);
  65. captionWrap.setClassName(CLASSNAME + "-captionwrap");
  66. captionNode.setClassName(CLASSNAME + "-caption");
  67. contentNode.setClassName(CLASSNAME + "-content");
  68. bottomDecoration.setClassName(CLASSNAME + "-deco");
  69. getElement().appendChild(captionWrap);
  70. getElement().appendChild(contentNode);
  71. getElement().appendChild(bottomDecoration);
  72. setStyleName(CLASSNAME);
  73. DOM.sinkEvents(getElement(), Event.ONKEYDOWN);
  74. DOM.sinkEvents(contentNode, Event.ONSCROLL);
  75. contentNode.getStyle().setProperty("position", "relative");
  76. getElement().getStyle().setProperty("overflow", "hidden");
  77. }
  78. @Override
  79. protected Element getContainerElement() {
  80. return contentNode;
  81. }
  82. private void setCaption(String text) {
  83. DOM.setInnerHTML(captionText, text);
  84. }
  85. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  86. rendering = true;
  87. if (!uidl.hasAttribute("cached")) {
  88. // Handle caption displaying and style names, prior generics.
  89. // Affects size
  90. // calculations
  91. // Restore default stylenames
  92. contentNode.setClassName(CLASSNAME + "-content");
  93. bottomDecoration.setClassName(CLASSNAME + "-deco");
  94. captionNode.setClassName(CLASSNAME + "-caption");
  95. boolean hasCaption = false;
  96. if (uidl.hasAttribute("caption")
  97. && !uidl.getStringAttribute("caption").equals("")) {
  98. setCaption(uidl.getStringAttribute("caption"));
  99. hasCaption = true;
  100. } else {
  101. setCaption("");
  102. captionNode.setClassName(CLASSNAME + "-nocaption");
  103. }
  104. // Add proper stylenames for all elements. This way we can prevent
  105. // unwanted CSS selector inheritance.
  106. if (uidl.hasAttribute("style")) {
  107. final String[] styles = uidl.getStringAttribute("style").split(
  108. " ");
  109. final String captionBaseClass = CLASSNAME
  110. + (hasCaption ? "-caption" : "-nocaption");
  111. final String contentBaseClass = CLASSNAME + "-content";
  112. final String decoBaseClass = CLASSNAME + "-deco";
  113. String captionClass = captionBaseClass;
  114. String contentClass = contentBaseClass;
  115. String decoClass = decoBaseClass;
  116. for (int i = 0; i < styles.length; i++) {
  117. captionClass += " " + captionBaseClass + "-" + styles[i];
  118. contentClass += " " + contentBaseClass + "-" + styles[i];
  119. decoClass += " " + decoBaseClass + "-" + styles[i];
  120. }
  121. captionNode.setClassName(captionClass);
  122. contentNode.setClassName(contentClass);
  123. bottomDecoration.setClassName(decoClass);
  124. }
  125. }
  126. // Ensure correct implementation
  127. if (client.updateComponent(this, uidl, false)) {
  128. rendering = false;
  129. return;
  130. }
  131. clickEventHandler.handleEventHandlerRegistration(client);
  132. this.client = client;
  133. id = uidl.getId();
  134. setIconUri(uidl, client);
  135. handleError(uidl);
  136. // Render content
  137. final UIDL layoutUidl = uidl.getChildUIDL(0);
  138. final Paintable newLayout = client.getPaintable(layoutUidl);
  139. if (newLayout != layout) {
  140. if (layout != null) {
  141. client.unregisterPaintable(layout);
  142. }
  143. setWidget((Widget) newLayout);
  144. layout = newLayout;
  145. }
  146. layout.updateFromUIDL(layoutUidl, client);
  147. // We may have actions attached to this panel
  148. if (uidl.getChildCount() > 1) {
  149. final int cnt = uidl.getChildCount();
  150. for (int i = 1; i < cnt; i++) {
  151. UIDL childUidl = uidl.getChildUIDL(i);
  152. if (childUidl.getTag().equals("actions")) {
  153. if (shortcutHandler == null) {
  154. shortcutHandler = new ShortcutActionHandler(id, client);
  155. }
  156. shortcutHandler.updateActionMap(childUidl);
  157. }
  158. }
  159. }
  160. if (uidl.hasVariable("scrollTop")
  161. && uidl.getIntVariable("scrollTop") != scrollTop) {
  162. scrollTop = uidl.getIntVariable("scrollTop");
  163. contentNode.setScrollTop(scrollTop);
  164. // re-read the actual scrollTop in case invalid value was set
  165. // (scrollTop != 0 when no scrollbar exists, other values would be
  166. // caught by scroll listener), see #3784
  167. scrollTop = contentNode.getScrollTop();
  168. }
  169. if (uidl.hasVariable("scrollLeft")
  170. && uidl.getIntVariable("scrollLeft") != scrollLeft) {
  171. scrollLeft = uidl.getIntVariable("scrollLeft");
  172. contentNode.setScrollLeft(scrollLeft);
  173. // re-read the actual scrollTop in case invalid value was set
  174. // (scrollTop != 0 when no scrollbar exists, other values would be
  175. // caught by scroll listener), see #3784
  176. scrollLeft = contentNode.getScrollLeft();
  177. }
  178. // Must be run after scrollTop is set as Webkit overflow fix re-sets the
  179. // scrollTop
  180. runHacks(false);
  181. rendering = false;
  182. }
  183. @Override
  184. public void setStyleName(String style) {
  185. if (!style.equals(previousStyleName)) {
  186. super.setStyleName(style);
  187. detectContainerBorders();
  188. previousStyleName = style;
  189. }
  190. }
  191. private void handleError(UIDL uidl) {
  192. if (uidl.hasAttribute("error")) {
  193. if (errorIndicatorElement == null) {
  194. errorIndicatorElement = DOM.createSpan();
  195. DOM.setElementProperty(errorIndicatorElement, "className",
  196. "v-errorindicator");
  197. DOM.sinkEvents(errorIndicatorElement, Event.MOUSEEVENTS);
  198. sinkEvents(Event.MOUSEEVENTS);
  199. }
  200. DOM.insertBefore(captionNode, errorIndicatorElement, captionText);
  201. } else if (errorIndicatorElement != null) {
  202. DOM.removeChild(captionNode, errorIndicatorElement);
  203. errorIndicatorElement = null;
  204. }
  205. }
  206. private void setIconUri(UIDL uidl, ApplicationConnection client) {
  207. final String iconUri = uidl.hasAttribute("icon") ? uidl
  208. .getStringAttribute("icon") : null;
  209. if (iconUri == null) {
  210. if (icon != null) {
  211. DOM.removeChild(captionNode, icon.getElement());
  212. icon = null;
  213. }
  214. } else {
  215. if (icon == null) {
  216. icon = new Icon(client);
  217. DOM.insertChild(captionNode, icon.getElement(), 0);
  218. }
  219. icon.setUri(iconUri);
  220. }
  221. }
  222. public void runHacks(boolean runGeckoFix) {
  223. if (BrowserInfo.get().isIE6() && width != null && !width.equals("")) {
  224. /*
  225. * IE6 requires overflow-hidden elements to have a width specified
  226. * so we calculate the width of the content and caption nodes when
  227. * no width has been specified.
  228. */
  229. /*
  230. * Fixes #1923 VPanel: Horizontal scrollbar does not appear in IE6
  231. * with wide content
  232. */
  233. /*
  234. * Caption must be shrunk for parent measurements to return correct
  235. * result in IE6
  236. */
  237. DOM.setStyleAttribute(captionNode, "width", "1px");
  238. int parentPadding = Util.measureHorizontalPaddingAndBorder(
  239. getElement(), 0);
  240. int parentWidthExcludingPadding = getElement().getOffsetWidth()
  241. - parentPadding;
  242. Util.setWidthExcludingPaddingAndBorder(captionNode,
  243. parentWidthExcludingPadding - getCaptionMarginLeft(), 26,
  244. false);
  245. int contentMarginLeft = getContentMarginLeft();
  246. Util.setWidthExcludingPaddingAndBorder(contentNode,
  247. parentWidthExcludingPadding - contentMarginLeft, 2, false);
  248. }
  249. if ((BrowserInfo.get().isIE() || BrowserInfo.get().isFF2())
  250. && (width == null || width.equals(""))) {
  251. /*
  252. * IE and FF2 needs width to be specified for the root DIV so we
  253. * calculate that from the sizes of the caption and layout
  254. */
  255. int captionWidth = captionText.getOffsetWidth()
  256. + getCaptionMarginLeft() + getCaptionPaddingHorizontal();
  257. int layoutWidth = ((Widget) layout).getOffsetWidth()
  258. + getContainerBorderWidth();
  259. int width = layoutWidth;
  260. if (captionWidth > width) {
  261. width = captionWidth;
  262. }
  263. if (BrowserInfo.get().isIE7()) {
  264. Util.setWidthExcludingPaddingAndBorder(captionNode, width
  265. - getCaptionMarginLeft(), 26, false);
  266. }
  267. super.setWidth(width + "px");
  268. }
  269. if (runGeckoFix && BrowserInfo.get().isGecko()) {
  270. // workaround for #1764
  271. if (width == null || width.equals("")) {
  272. if (geckoCaptionMeter == null) {
  273. geckoCaptionMeter = DOM.createDiv();
  274. DOM.appendChild(captionNode, geckoCaptionMeter);
  275. }
  276. int captionWidth = DOM.getElementPropertyInt(captionText,
  277. "offsetWidth");
  278. int availWidth = DOM.getElementPropertyInt(geckoCaptionMeter,
  279. "offsetWidth");
  280. if (captionWidth == availWidth) {
  281. /*
  282. * Caption width defines panel width -> Gecko based browsers
  283. * somehow fails to float things right, without the
  284. * "noncode" below
  285. */
  286. setWidth(getOffsetWidth() + "px");
  287. } else {
  288. DOM.setStyleAttribute(captionNode, "width", "");
  289. }
  290. }
  291. }
  292. client.runDescendentsLayout(this);
  293. Util.runWebkitOverflowAutoFix(contentNode);
  294. }
  295. @Override
  296. public void onBrowserEvent(Event event) {
  297. super.onBrowserEvent(event);
  298. final Element target = DOM.eventGetTarget(event);
  299. final int type = DOM.eventGetType(event);
  300. if (type == Event.ONKEYDOWN && shortcutHandler != null) {
  301. shortcutHandler.handleKeyboardEvent(event);
  302. return;
  303. }
  304. if (type == Event.ONSCROLL) {
  305. int newscrollTop = DOM.getElementPropertyInt(contentNode,
  306. "scrollTop");
  307. int newscrollLeft = DOM.getElementPropertyInt(contentNode,
  308. "scrollLeft");
  309. if (client != null
  310. && (newscrollLeft != scrollLeft || newscrollTop != scrollTop)) {
  311. scrollLeft = newscrollLeft;
  312. scrollTop = newscrollTop;
  313. client.updateVariable(id, "scrollTop", scrollTop, false);
  314. client.updateVariable(id, "scrollLeft", scrollLeft, false);
  315. }
  316. } else if (captionNode.isOrHasChild(target)) {
  317. if (client != null) {
  318. client.handleTooltipEvent(event, this);
  319. }
  320. }
  321. }
  322. @Override
  323. public void setHeight(String height) {
  324. this.height = height;
  325. super.setHeight(height);
  326. if (height != null && !"".equals(height)) {
  327. final int targetHeight = getOffsetHeight();
  328. int containerHeight = targetHeight
  329. - captionNode.getParentElement().getOffsetHeight()
  330. - bottomDecoration.getOffsetHeight()
  331. - getContainerBorderHeight();
  332. if (containerHeight < 0) {
  333. containerHeight = 0;
  334. }
  335. DOM.setStyleAttribute(contentNode, "height", containerHeight + "px");
  336. } else {
  337. DOM.setStyleAttribute(contentNode, "height", "");
  338. }
  339. if (!rendering) {
  340. runHacks(true);
  341. }
  342. }
  343. private int getCaptionMarginLeft() {
  344. if (captionMarginLeft < 0) {
  345. detectContainerBorders();
  346. }
  347. return captionMarginLeft;
  348. }
  349. private int getContentMarginLeft() {
  350. if (contentMarginLeft < 0) {
  351. detectContainerBorders();
  352. }
  353. return contentMarginLeft;
  354. }
  355. private int getCaptionPaddingHorizontal() {
  356. if (captionPaddingHorizontal < 0) {
  357. detectContainerBorders();
  358. }
  359. return captionPaddingHorizontal;
  360. }
  361. private int getContainerBorderHeight() {
  362. if (borderPaddingVertical < 0) {
  363. detectContainerBorders();
  364. }
  365. return borderPaddingVertical;
  366. }
  367. @Override
  368. public void setWidth(String width) {
  369. if (this.width.equals(width)) {
  370. return;
  371. }
  372. this.width = width;
  373. super.setWidth(width);
  374. if (!rendering) {
  375. runHacks(true);
  376. if (height.equals("")) {
  377. // Width change may affect height
  378. Util.updateRelativeChildrenAndSendSizeUpdateEvent(client, this);
  379. }
  380. }
  381. }
  382. private int getContainerBorderWidth() {
  383. if (borderPaddingHorizontal < 0) {
  384. detectContainerBorders();
  385. }
  386. return borderPaddingHorizontal;
  387. }
  388. private void detectContainerBorders() {
  389. DOM.setStyleAttribute(contentNode, "overflow", "hidden");
  390. borderPaddingHorizontal = Util.measureHorizontalBorder(contentNode);
  391. borderPaddingVertical = Util.measureVerticalBorder(contentNode);
  392. DOM.setStyleAttribute(contentNode, "overflow", "auto");
  393. captionPaddingHorizontal = Util.measureHorizontalPaddingAndBorder(
  394. captionNode, 26);
  395. captionMarginLeft = Util.measureMarginLeft(captionNode);
  396. contentMarginLeft = Util.measureMarginLeft(contentNode);
  397. }
  398. public boolean hasChildComponent(Widget component) {
  399. if (component != null && component == layout) {
  400. return true;
  401. } else {
  402. return false;
  403. }
  404. }
  405. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  406. // TODO This is untested as no layouts require this
  407. if (oldComponent != layout) {
  408. return;
  409. }
  410. setWidget(newComponent);
  411. layout = (Paintable) newComponent;
  412. }
  413. public RenderSpace getAllocatedSpace(Widget child) {
  414. int w = 0;
  415. int h = 0;
  416. if (width != null && !width.equals("")) {
  417. w = getOffsetWidth() - getContainerBorderWidth();
  418. if (w < 0) {
  419. w = 0;
  420. }
  421. }
  422. if (height != null && !height.equals("")) {
  423. h = contentNode.getOffsetHeight() - getContainerBorderHeight();
  424. if (h < 0) {
  425. h = 0;
  426. }
  427. }
  428. return new RenderSpace(w, h, true);
  429. }
  430. public boolean requestLayout(Set<Paintable> child) {
  431. // content size change might cause change to its available space
  432. // (scrollbars)
  433. client.handleComponentRelativeSize((Widget) layout);
  434. if (height != null && height != "" && width != null && width != "") {
  435. /*
  436. * If the height and width has been specified the child components
  437. * cannot make the size of the layout change
  438. */
  439. return true;
  440. }
  441. runHacks(false);
  442. return !renderInformation.updateSize(getElement());
  443. }
  444. public void updateCaption(Paintable component, UIDL uidl) {
  445. // NOP: layouts caption, errors etc not rendered in Panel
  446. }
  447. @Override
  448. protected void onAttach() {
  449. super.onAttach();
  450. detectContainerBorders();
  451. }
  452. public ShortcutActionHandler getShortcutActionHandler() {
  453. return shortcutHandler;
  454. }
  455. }