You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ISplitPanel.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import java.util.Set;
  6. import com.google.gwt.user.client.Command;
  7. import com.google.gwt.user.client.DOM;
  8. import com.google.gwt.user.client.DeferredCommand;
  9. import com.google.gwt.user.client.Element;
  10. import com.google.gwt.user.client.Event;
  11. import com.google.gwt.user.client.ui.ComplexPanel;
  12. import com.google.gwt.user.client.ui.RootPanel;
  13. import com.google.gwt.user.client.ui.Widget;
  14. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  15. import com.itmill.toolkit.terminal.gwt.client.BrowserInfo;
  16. import com.itmill.toolkit.terminal.gwt.client.Container;
  17. import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
  18. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  19. import com.itmill.toolkit.terminal.gwt.client.RenderInformation;
  20. import com.itmill.toolkit.terminal.gwt.client.RenderSpace;
  21. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  22. import com.itmill.toolkit.terminal.gwt.client.Util;
  23. public class ISplitPanel extends ComplexPanel implements Container,
  24. ContainerResizedListener {
  25. public static final String CLASSNAME = "i-splitpanel";
  26. public static final int ORIENTATION_HORIZONTAL = 0;
  27. public static final int ORIENTATION_VERTICAL = 1;
  28. private static final int MIN_SIZE = 30;
  29. private int orientation = ORIENTATION_HORIZONTAL;
  30. private Widget firstChild;
  31. private Widget secondChild;
  32. private final Element wrapper = DOM.createDiv();
  33. private final Element firstContainer = DOM.createDiv();
  34. private final Element secondContainer = DOM.createDiv();
  35. private final Element splitter = DOM.createDiv();
  36. private boolean resizing;
  37. private int origX;
  38. private int origY;
  39. private int origMouseX;
  40. private int origMouseY;
  41. private boolean locked;
  42. private String splitterStyleName;
  43. private Element draggingCurtain;
  44. private ApplicationConnection client;
  45. private String width = null;
  46. private String height = null;
  47. private RenderSpace firstRenderSpace = new RenderSpace(0, 0, true);
  48. private RenderSpace secondRenderSpace = new RenderSpace(0, 0, true);
  49. RenderInformation renderInformation = new RenderInformation();
  50. public ISplitPanel() {
  51. this(ORIENTATION_HORIZONTAL);
  52. }
  53. public ISplitPanel(int orientation) {
  54. setElement(DOM.createDiv());
  55. switch (orientation) {
  56. case ORIENTATION_HORIZONTAL:
  57. setStyleName(CLASSNAME + "-horizontal");
  58. break;
  59. case ORIENTATION_VERTICAL:
  60. default:
  61. setStyleName(CLASSNAME + "-vertical");
  62. break;
  63. }
  64. // size below will be overridden in update from uidl, initial size
  65. // needed to keep IE alive
  66. setWidth(MIN_SIZE + "px");
  67. setHeight(MIN_SIZE + "px");
  68. constructDom();
  69. setOrientation(orientation);
  70. DOM.sinkEvents(splitter, (Event.MOUSEEVENTS));
  71. DOM.sinkEvents(getElement(), (Event.MOUSEEVENTS));
  72. }
  73. protected void constructDom() {
  74. DOM.appendChild(splitter, DOM.createDiv()); // for styling
  75. DOM.appendChild(getElement(), wrapper);
  76. DOM.setStyleAttribute(wrapper, "position", "relative");
  77. DOM.setStyleAttribute(wrapper, "width", "100%");
  78. DOM.setStyleAttribute(wrapper, "height", "100%");
  79. DOM.appendChild(wrapper, splitter);
  80. DOM.appendChild(wrapper, secondContainer);
  81. DOM.appendChild(wrapper, firstContainer);
  82. DOM.setStyleAttribute(splitter, "position", "absolute");
  83. DOM.setStyleAttribute(secondContainer, "position", "absolute");
  84. DOM.setStyleAttribute(firstContainer, "overflow", "auto");
  85. DOM.setStyleAttribute(secondContainer, "overflow", "auto");
  86. if (BrowserInfo.get().isIE7()) {
  87. /*
  88. * Part I of IE7 weirdness hack, will be set to auto in layout phase
  89. *
  90. * With IE7 one will sometimes get scrollbars with overflow auto
  91. * even though there is nothing to scroll (content fits into area).
  92. */
  93. DOM.setStyleAttribute(firstContainer, "overflow", "hidden");
  94. DOM.setStyleAttribute(secondContainer, "overflow", "hidden");
  95. }
  96. }
  97. private void setOrientation(int orientation) {
  98. this.orientation = orientation;
  99. if (orientation == ORIENTATION_HORIZONTAL) {
  100. DOM.setStyleAttribute(splitter, "height", "100%");
  101. DOM.setStyleAttribute(firstContainer, "height", "100%");
  102. DOM.setStyleAttribute(secondContainer, "height", "100%");
  103. } else {
  104. DOM.setStyleAttribute(splitter, "width", "100%");
  105. DOM.setStyleAttribute(firstContainer, "width", "100%");
  106. DOM.setStyleAttribute(secondContainer, "width", "100%");
  107. }
  108. splitterStyleName = CLASSNAME
  109. + (orientation == ORIENTATION_HORIZONTAL ? "-hsplitter"
  110. : "-vsplitter");
  111. DOM.setElementProperty(splitter, "className", splitterStyleName);
  112. }
  113. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  114. this.client = client;
  115. if (client.updateComponent(this, uidl, true)) {
  116. return;
  117. }
  118. renderInformation.updateSize(getElement());
  119. setSplitPosition(uidl.getStringAttribute("position"));
  120. locked = uidl.hasAttribute("locked");
  121. if (locked) {
  122. DOM.setElementProperty(splitter, "className", splitterStyleName
  123. + "-locked");
  124. } else {
  125. DOM.setElementProperty(splitter, "className", splitterStyleName);
  126. }
  127. final Paintable newFirstChild = client.getPaintable(uidl
  128. .getChildUIDL(0));
  129. final Paintable newSecondChild = client.getPaintable(uidl
  130. .getChildUIDL(1));
  131. if (firstChild != newFirstChild) {
  132. if (firstChild != null) {
  133. client.unregisterPaintable((Paintable) firstChild);
  134. }
  135. setFirstWidget((Widget) newFirstChild);
  136. }
  137. if (secondChild != newSecondChild) {
  138. if (secondChild != null) {
  139. client.unregisterPaintable((Paintable) secondChild);
  140. }
  141. setSecondWidget((Widget) newSecondChild);
  142. }
  143. newFirstChild.updateFromUIDL(uidl.getChildUIDL(0), client);
  144. newSecondChild.updateFromUIDL(uidl.getChildUIDL(1), client);
  145. if (Util.isIE7()) {
  146. // Part III of IE7 hack
  147. DeferredCommand.addCommand(new Command() {
  148. public void execute() {
  149. iLayout();
  150. }
  151. });
  152. }
  153. }
  154. private void setSplitPosition(String pos) {
  155. if (orientation == ORIENTATION_HORIZONTAL) {
  156. DOM.setStyleAttribute(splitter, "left", pos);
  157. } else {
  158. DOM.setStyleAttribute(splitter, "top", pos);
  159. }
  160. iLayout();
  161. }
  162. /*
  163. * Calculates absolutely positioned container places/sizes (non-Javadoc)
  164. *
  165. * @see com.itmill.toolkit.terminal.gwt.client.NeedsLayout#layout()
  166. */
  167. public void iLayout() {
  168. if (!isAttached()) {
  169. return;
  170. }
  171. renderInformation.updateSize(getElement());
  172. int wholeSize;
  173. int pixelPosition;
  174. if (!(resizing && BrowserInfo.get().isGecko())) {
  175. DOM.setStyleAttribute(firstContainer, "overflow", "hidden");
  176. DOM.setStyleAttribute(secondContainer, "overflow", "hidden");
  177. }
  178. switch (orientation) {
  179. case ORIENTATION_HORIZONTAL:
  180. wholeSize = DOM.getElementPropertyInt(wrapper, "clientWidth");
  181. pixelPosition = DOM.getElementPropertyInt(splitter, "offsetLeft");
  182. // reposition splitter in case it is out of box
  183. if (pixelPosition > 0
  184. && pixelPosition + getSplitterSize() > wholeSize) {
  185. pixelPosition = wholeSize - getSplitterSize();
  186. if (pixelPosition < 0) {
  187. pixelPosition = 0;
  188. }
  189. setSplitPosition(pixelPosition + "px");
  190. return;
  191. }
  192. DOM
  193. .setStyleAttribute(firstContainer, "width", pixelPosition
  194. + "px");
  195. int secondContainerWidth = (wholeSize - pixelPosition - getSplitterSize());
  196. if (secondContainerWidth < 0) {
  197. secondContainerWidth = 0;
  198. }
  199. DOM.setStyleAttribute(secondContainer, "width",
  200. secondContainerWidth + "px");
  201. DOM.setStyleAttribute(secondContainer, "left",
  202. (pixelPosition + getSplitterSize()) + "px");
  203. int contentHeight = renderInformation.getRenderedSize().getHeight();
  204. firstRenderSpace.setHeight(contentHeight);
  205. firstRenderSpace.setWidth(pixelPosition);
  206. secondRenderSpace.setHeight(contentHeight);
  207. secondRenderSpace.setWidth(secondContainerWidth);
  208. break;
  209. case ORIENTATION_VERTICAL:
  210. wholeSize = DOM.getElementPropertyInt(wrapper, "clientHeight");
  211. pixelPosition = DOM.getElementPropertyInt(splitter, "offsetTop");
  212. // reposition splitter in case it is out of box
  213. if (pixelPosition > 0
  214. && pixelPosition + getSplitterSize() > wholeSize) {
  215. pixelPosition = wholeSize - getSplitterSize();
  216. if (pixelPosition < 0) {
  217. pixelPosition = 0;
  218. }
  219. setSplitPosition(pixelPosition + "px");
  220. return;
  221. }
  222. DOM.setStyleAttribute(firstContainer, "height", pixelPosition
  223. + "px");
  224. int secondContainerHeight = (wholeSize - pixelPosition - getSplitterSize());
  225. if (secondContainerHeight < 0) {
  226. secondContainerHeight = 0;
  227. }
  228. DOM.setStyleAttribute(secondContainer, "height",
  229. secondContainerHeight + "px");
  230. DOM.setStyleAttribute(secondContainer, "top",
  231. (pixelPosition + getSplitterSize()) + "px");
  232. int contentWidth = renderInformation.getRenderedSize().getWidth();
  233. firstRenderSpace.setHeight(pixelPosition);
  234. firstRenderSpace.setWidth(contentWidth);
  235. secondRenderSpace.setHeight(secondContainerHeight);
  236. secondRenderSpace.setWidth(contentWidth);
  237. break;
  238. }
  239. if (Util.isIE7()) {
  240. // Part I of IE7 weirdness hack, will be set to auto in layout phase
  241. client.runDescendentsLayout(this);
  242. DeferredCommand.addCommand(new Command() {
  243. public void execute() {
  244. DOM.setStyleAttribute(firstContainer, "overflow", "auto");
  245. DOM.setStyleAttribute(secondContainer, "overflow", "auto");
  246. }
  247. });
  248. } else {
  249. client.runDescendentsLayout(this);
  250. if (!(resizing && BrowserInfo.get().isGecko())) {
  251. DOM.setStyleAttribute(firstContainer, "overflow", "auto");
  252. DOM.setStyleAttribute(secondContainer, "overflow", "auto");
  253. }
  254. }
  255. renderInformation.updateSize(getElement());
  256. // fixes scrollbars sometimes seen on webkit 528.5, but not in Safari
  257. // 3.1
  258. Util.runWebkitOverflowAutoFix(secondContainer);
  259. }
  260. private void setFirstWidget(Widget w) {
  261. if (firstChild != null) {
  262. firstChild.removeFromParent();
  263. }
  264. super.add(w, firstContainer);
  265. firstChild = w;
  266. }
  267. private void setSecondWidget(Widget w) {
  268. if (secondChild != null) {
  269. secondChild.removeFromParent();
  270. }
  271. super.add(w, secondContainer);
  272. secondChild = w;
  273. }
  274. public void onBrowserEvent(Event event) {
  275. switch (DOM.eventGetType(event)) {
  276. case Event.ONMOUSEMOVE:
  277. if (resizing) {
  278. onMouseMove(event);
  279. }
  280. break;
  281. case Event.ONMOUSEDOWN:
  282. onMouseDown(event);
  283. break;
  284. case Event.ONMOUSEUP:
  285. if (resizing) {
  286. onMouseUp(event);
  287. }
  288. break;
  289. case Event.ONCLICK:
  290. resizing = false;
  291. break;
  292. }
  293. }
  294. public void onMouseDown(Event event) {
  295. if (locked) {
  296. return;
  297. }
  298. final Element trg = DOM.eventGetTarget(event);
  299. if (DOM.compare(trg, splitter)
  300. || DOM.compare(trg, DOM.getChild(splitter, 0))) {
  301. resizing = true;
  302. if (BrowserInfo.get().isGecko()) {
  303. showDraggingCurtain();
  304. }
  305. DOM.setCapture(getElement());
  306. origX = DOM.getElementPropertyInt(splitter, "offsetLeft");
  307. origY = DOM.getElementPropertyInt(splitter, "offsetTop");
  308. origMouseX = DOM.eventGetClientX(event);
  309. origMouseY = DOM.eventGetClientY(event);
  310. DOM.eventCancelBubble(event, true);
  311. DOM.eventPreventDefault(event);
  312. }
  313. }
  314. public void onMouseMove(Event event) {
  315. switch (orientation) {
  316. case ORIENTATION_HORIZONTAL:
  317. final int x = DOM.eventGetClientX(event);
  318. onHorizontalMouseMove(x);
  319. break;
  320. case ORIENTATION_VERTICAL:
  321. default:
  322. final int y = DOM.eventGetClientY(event);
  323. onVerticalMouseMove(y);
  324. break;
  325. }
  326. iLayout();
  327. }
  328. private void onHorizontalMouseMove(int x) {
  329. int newX = origX + x - origMouseX;
  330. if (newX < 0) {
  331. newX = 0;
  332. }
  333. if (newX + getSplitterSize() > getOffsetWidth()) {
  334. newX = getOffsetWidth() - getSplitterSize();
  335. }
  336. DOM.setStyleAttribute(splitter, "left", newX + "px");
  337. }
  338. private void onVerticalMouseMove(int y) {
  339. int newY = origY + y - origMouseY;
  340. if (newY < 0) {
  341. newY = 0;
  342. }
  343. if (newY + getSplitterSize() > getOffsetHeight()) {
  344. newY = getOffsetHeight() - getSplitterSize();
  345. }
  346. DOM.setStyleAttribute(splitter, "top", newY + "px");
  347. }
  348. public void onMouseUp(Event event) {
  349. DOM.releaseCapture(getElement());
  350. if (BrowserInfo.get().isGecko()) {
  351. hideDraggingCurtain();
  352. }
  353. resizing = false;
  354. onMouseMove(event);
  355. }
  356. /**
  357. * Used in FF to avoid losing mouse capture when pointer is moved on an
  358. * iframe.
  359. */
  360. private void showDraggingCurtain() {
  361. if (draggingCurtain == null) {
  362. draggingCurtain = DOM.createDiv();
  363. DOM.setStyleAttribute(draggingCurtain, "position", "absolute");
  364. DOM.setStyleAttribute(draggingCurtain, "top", "0px");
  365. DOM.setStyleAttribute(draggingCurtain, "left", "0px");
  366. DOM.setStyleAttribute(draggingCurtain, "width", "100%");
  367. DOM.setStyleAttribute(draggingCurtain, "height", "100%");
  368. DOM.setStyleAttribute(draggingCurtain, "zIndex", ""
  369. + IToolkitOverlay.Z_INDEX);
  370. DOM.appendChild(RootPanel.getBodyElement(), draggingCurtain);
  371. }
  372. }
  373. /**
  374. * Hides dragging curtain
  375. */
  376. private void hideDraggingCurtain() {
  377. if (draggingCurtain != null) {
  378. DOM.removeChild(RootPanel.getBodyElement(), draggingCurtain);
  379. draggingCurtain = null;
  380. }
  381. }
  382. private static int splitterSize = -1;
  383. private int getSplitterSize() {
  384. if (splitterSize < 0) {
  385. if (isAttached()) {
  386. switch (orientation) {
  387. case ORIENTATION_HORIZONTAL:
  388. splitterSize = DOM.getElementPropertyInt(splitter,
  389. "offsetWidth");
  390. break;
  391. default:
  392. splitterSize = DOM.getElementPropertyInt(splitter,
  393. "offsetHeight");
  394. break;
  395. }
  396. }
  397. }
  398. return splitterSize;
  399. }
  400. @Override
  401. public void setHeight(String height) {
  402. this.height = height;
  403. super.setHeight(height);
  404. }
  405. @Override
  406. public void setWidth(String width) {
  407. this.width = width;
  408. super.setWidth(width);
  409. }
  410. public RenderSpace getAllocatedSpace(Widget child) {
  411. if (child == firstChild) {
  412. return firstRenderSpace;
  413. } else if (child == secondChild) {
  414. return secondRenderSpace;
  415. }
  416. return null;
  417. }
  418. public boolean hasChildComponent(Widget component) {
  419. return (component != null && (component == firstChild || component == secondChild));
  420. }
  421. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  422. // TODO Auto-generated method stub
  423. }
  424. public boolean requestLayout(Set<Paintable> child) {
  425. if (height != null && width != null) {
  426. /*
  427. * If the height and width has been specified the child components
  428. * cannot make the size of the layout change
  429. */
  430. return true;
  431. }
  432. if (renderInformation.updateSize(getElement())) {
  433. return false;
  434. } else {
  435. return true;
  436. }
  437. }
  438. public void updateCaption(Paintable component, UIDL uidl) {
  439. // TODO Auto-generated method stub
  440. }
  441. }