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.

VAbstractSplitPanel.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.dom.client.Node;
  21. import com.google.gwt.dom.client.Style;
  22. import com.google.gwt.dom.client.Style.Position;
  23. import com.google.gwt.dom.client.Style.Unit;
  24. import com.google.gwt.event.dom.client.TouchCancelEvent;
  25. import com.google.gwt.event.dom.client.TouchCancelHandler;
  26. import com.google.gwt.event.dom.client.TouchEndEvent;
  27. import com.google.gwt.event.dom.client.TouchEndHandler;
  28. import com.google.gwt.event.dom.client.TouchMoveEvent;
  29. import com.google.gwt.event.dom.client.TouchMoveHandler;
  30. import com.google.gwt.event.dom.client.TouchStartEvent;
  31. import com.google.gwt.event.dom.client.TouchStartHandler;
  32. import com.google.gwt.event.shared.EventHandler;
  33. import com.google.gwt.event.shared.GwtEvent;
  34. import com.google.gwt.user.client.DOM;
  35. import com.google.gwt.user.client.Event;
  36. import com.google.gwt.user.client.ui.ComplexPanel;
  37. import com.google.gwt.user.client.ui.Widget;
  38. import com.vaadin.client.ApplicationConnection;
  39. import com.vaadin.client.BrowserInfo;
  40. import com.vaadin.client.ComponentConnector;
  41. import com.vaadin.client.ConnectorMap;
  42. import com.vaadin.client.LayoutManager;
  43. import com.vaadin.client.StyleConstants;
  44. import com.vaadin.client.Util;
  45. import com.vaadin.client.VConsole;
  46. import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler;
  47. import com.vaadin.client.ui.VAbstractSplitPanel.SplitterMoveHandler.SplitterMoveEvent;
  48. import com.vaadin.shared.ui.Orientation;
  49. public class VAbstractSplitPanel extends ComplexPanel {
  50. private boolean enabled = false;
  51. public static final String CLASSNAME = "v-splitpanel";
  52. private static final int MIN_SIZE = 30;
  53. private Orientation orientation = Orientation.HORIZONTAL;
  54. Widget firstChild;
  55. Widget secondChild;
  56. private final Element wrapper = DOM.createDiv();
  57. private final Element firstContainer = DOM.createDiv();
  58. private final Element secondContainer = DOM.createDiv();
  59. /** For internal use only. May be removed or replaced in the future. */
  60. public final Element splitter = DOM.createDiv();
  61. private boolean resizing;
  62. private boolean resized = false;
  63. private int origX;
  64. private int origY;
  65. private int origMouseX;
  66. private int origMouseY;
  67. private boolean locked = false;
  68. private boolean positionReversed = false;
  69. /** For internal use only. May be removed or replaced in the future. */
  70. public List<String> componentStyleNames = Collections.emptyList();
  71. private Element draggingCurtain;
  72. /** For internal use only. May be removed or replaced in the future. */
  73. public ApplicationConnection client;
  74. /** For internal use only. May be removed or replaced in the future. */
  75. public boolean immediate;
  76. /**
  77. * The current position of the split handle in either percentages or pixels
  78. * <p>
  79. * For internal use only. May be removed or replaced in the future.
  80. */
  81. public String position;
  82. /** For internal use only. May be removed or replaced in the future. */
  83. public String maximumPosition;
  84. /** For internal use only. May be removed or replaced in the future. */
  85. public String minimumPosition;
  86. private TouchScrollHandler touchScrollHandler;
  87. protected Element scrolledContainer;
  88. protected int origScrollTop;
  89. public VAbstractSplitPanel() {
  90. this(Orientation.HORIZONTAL);
  91. }
  92. public VAbstractSplitPanel(Orientation orientation) {
  93. setElement(DOM.createDiv());
  94. setStyleName(StyleConstants.UI_LAYOUT);
  95. switch (orientation) {
  96. case HORIZONTAL:
  97. addStyleName(CLASSNAME + "-horizontal");
  98. break;
  99. case VERTICAL:
  100. default:
  101. addStyleName(CLASSNAME + "-vertical");
  102. break;
  103. }
  104. // size below will be overridden in update from uidl, initial size
  105. // needed to keep IE alive
  106. setWidth(MIN_SIZE + "px");
  107. setHeight(MIN_SIZE + "px");
  108. constructDom();
  109. setOrientation(orientation);
  110. sinkEvents(Event.MOUSEEVENTS);
  111. makeScrollable();
  112. addDomHandler(new TouchCancelHandler() {
  113. @Override
  114. public void onTouchCancel(TouchCancelEvent event) {
  115. // TODO When does this actually happen??
  116. VConsole.log("TOUCH CANCEL");
  117. }
  118. }, TouchCancelEvent.getType());
  119. addDomHandler(new TouchStartHandler() {
  120. @Override
  121. public void onTouchStart(TouchStartEvent event) {
  122. Node target = event.getTouches().get(0).getTarget().cast();
  123. if (splitter.isOrHasChild(target)) {
  124. onMouseDown(Event.as(event.getNativeEvent()));
  125. }
  126. }
  127. }, TouchStartEvent.getType());
  128. addDomHandler(new TouchMoveHandler() {
  129. @Override
  130. public void onTouchMove(TouchMoveEvent event) {
  131. if (resizing) {
  132. onMouseMove(Event.as(event.getNativeEvent()));
  133. }
  134. }
  135. }, TouchMoveEvent.getType());
  136. addDomHandler(new TouchEndHandler() {
  137. @Override
  138. public void onTouchEnd(TouchEndEvent event) {
  139. if (resizing) {
  140. onMouseUp(Event.as(event.getNativeEvent()));
  141. }
  142. }
  143. }, TouchEndEvent.getType());
  144. }
  145. protected void constructDom() {
  146. DOM.appendChild(splitter, DOM.createDiv()); // for styling
  147. DOM.appendChild(getElement(), wrapper);
  148. wrapper.getStyle().setPosition(Position.RELATIVE);
  149. wrapper.getStyle().setWidth(100, Unit.PCT);
  150. wrapper.getStyle().setHeight(100, Unit.PCT);
  151. DOM.appendChild(wrapper, firstContainer);
  152. DOM.appendChild(wrapper, splitter);
  153. DOM.appendChild(wrapper, secondContainer);
  154. splitter.getStyle().setPosition(Position.ABSOLUTE);
  155. secondContainer.getStyle().setPosition(Position.ABSOLUTE);
  156. setStylenames();
  157. }
  158. private void setOrientation(Orientation orientation) {
  159. this.orientation = orientation;
  160. if (orientation == Orientation.HORIZONTAL) {
  161. splitter.getStyle().setHeight(100, Unit.PCT);
  162. splitter.getStyle().setTop(0, Unit.PX);
  163. firstContainer.getStyle().setHeight(100, Unit.PCT);
  164. secondContainer.getStyle().setTop(0, Unit.PX);
  165. secondContainer.getStyle().setHeight(100, Unit.PCT);
  166. } else {
  167. splitter.getStyle().setWidth(100, Unit.PCT);
  168. splitter.getStyle().setLeft(0, Unit.PX);
  169. firstContainer.getStyle().setWidth(100, Unit.PCT);
  170. secondContainer.getStyle().setWidth(100, Unit.PCT);
  171. }
  172. }
  173. @Override
  174. public boolean remove(Widget w) {
  175. boolean removed = super.remove(w);
  176. if (removed) {
  177. if (firstChild == w) {
  178. firstChild = null;
  179. } else {
  180. secondChild = null;
  181. }
  182. }
  183. return removed;
  184. }
  185. /** For internal use only. May be removed or replaced in the future. */
  186. public void setLocked(boolean newValue) {
  187. if (locked != newValue) {
  188. locked = newValue;
  189. splitterSize = -1;
  190. setStylenames();
  191. }
  192. }
  193. /** For internal use only. May be removed or replaced in the future. */
  194. public void setPositionReversed(boolean reversed) {
  195. if (positionReversed != reversed) {
  196. if (orientation == Orientation.HORIZONTAL) {
  197. splitter.getStyle().clearRight();
  198. splitter.getStyle().clearLeft();
  199. } else if (orientation == Orientation.VERTICAL) {
  200. splitter.getStyle().clearTop();
  201. splitter.getStyle().clearBottom();
  202. }
  203. positionReversed = reversed;
  204. }
  205. }
  206. /**
  207. * Converts given split position string (in pixels or percentage) to a
  208. * floating point pixel value.
  209. *
  210. * @param pos
  211. * @return
  212. */
  213. private float convertToPixels(String pos) {
  214. float posAsFloat;
  215. if (pos.indexOf("%") > 0) {
  216. posAsFloat = Math.round(Float.parseFloat(pos.substring(0,
  217. pos.length() - 1))
  218. / 100
  219. * (orientation == Orientation.HORIZONTAL ? getOffsetWidth()
  220. : getOffsetHeight()));
  221. } else {
  222. posAsFloat = Float.parseFloat(pos.substring(0, pos.length() - 2));
  223. }
  224. return posAsFloat;
  225. }
  226. /**
  227. * Converts given split position string (in pixels or percentage) to a float
  228. * percentage value.
  229. *
  230. * @param pos
  231. * @return
  232. */
  233. private float convertToPercentage(String pos) {
  234. if (pos.endsWith("px")) {
  235. float pixelPosition = Float.parseFloat(pos.substring(0,
  236. pos.length() - 2));
  237. int offsetLength = orientation == Orientation.HORIZONTAL ? getOffsetWidth()
  238. : getOffsetHeight();
  239. // Take splitter size into account at the edge
  240. if (pixelPosition + getSplitterSize() >= offsetLength) {
  241. return 100;
  242. }
  243. return pixelPosition / offsetLength * 100;
  244. } else {
  245. assert pos.endsWith("%");
  246. return Float.parseFloat(pos.substring(0, pos.length() - 1));
  247. }
  248. }
  249. /**
  250. * Returns the given position clamped to the range between current minimum
  251. * and maximum positions.
  252. *
  253. * TODO Should this be in the connector?
  254. *
  255. * @param pos
  256. * Position of the splitter as a CSS string, either pixels or a
  257. * percentage.
  258. * @return minimumPosition if pos is less than minimumPosition;
  259. * maximumPosition if pos is greater than maximumPosition; pos
  260. * otherwise.
  261. */
  262. private String checkSplitPositionLimits(String pos) {
  263. float positionAsFloat = convertToPixels(pos);
  264. if (maximumPosition != null
  265. && convertToPixels(maximumPosition) < positionAsFloat) {
  266. pos = maximumPosition;
  267. } else if (minimumPosition != null
  268. && convertToPixels(minimumPosition) > positionAsFloat) {
  269. pos = minimumPosition;
  270. }
  271. return pos;
  272. }
  273. /**
  274. * Converts given string to the same units as the split position is.
  275. *
  276. * @param pos
  277. * position to be converted
  278. * @return converted position string
  279. */
  280. private String convertToPositionUnits(String pos) {
  281. if (position.indexOf("%") != -1 && pos.indexOf("%") == -1) {
  282. // position is in percentage, pos in pixels
  283. pos = convertToPercentage(pos) + "%";
  284. } else if (position.indexOf("px") > 0 && pos.indexOf("px") == -1) {
  285. // position is in pixels and pos in percentage
  286. pos = convertToPixels(pos) + "px";
  287. }
  288. return pos;
  289. }
  290. public void setSplitPosition(String pos) {
  291. setSplitPosition(pos, true);
  292. }
  293. private void setSplitPosition(String pos, boolean rememberPosition) {
  294. if (pos == null) {
  295. return;
  296. }
  297. pos = checkSplitPositionLimits(pos);
  298. if (rememberPosition && !pos.equals(position)) {
  299. position = convertToPositionUnits(pos);
  300. }
  301. // Convert percentage values to pixels
  302. if (pos.indexOf("%") > 0) {
  303. int size = orientation == Orientation.HORIZONTAL ? getOffsetWidth()
  304. : getOffsetHeight();
  305. float percentage = Float.parseFloat(pos.substring(0,
  306. pos.length() - 1));
  307. pos = percentage / 100 * size + "px";
  308. }
  309. String attributeName;
  310. if (orientation == Orientation.HORIZONTAL) {
  311. if (positionReversed) {
  312. attributeName = "right";
  313. } else {
  314. attributeName = "left";
  315. }
  316. } else {
  317. if (positionReversed) {
  318. attributeName = "bottom";
  319. } else {
  320. attributeName = "top";
  321. }
  322. }
  323. Style style = splitter.getStyle();
  324. if (!pos.equals(style.getProperty(attributeName))) {
  325. style.setProperty(attributeName, pos);
  326. updateSizes();
  327. }
  328. }
  329. /** For internal use only. May be removed or replaced in the future. */
  330. public void updateSizes() {
  331. if (!isAttached()) {
  332. return;
  333. }
  334. int wholeSize;
  335. int pixelPosition;
  336. switch (orientation) {
  337. case HORIZONTAL:
  338. wholeSize = DOM.getElementPropertyInt(wrapper, "clientWidth");
  339. pixelPosition = DOM.getElementPropertyInt(splitter, "offsetLeft");
  340. // reposition splitter in case it is out of box
  341. if ((pixelPosition > 0 && pixelPosition + getSplitterSize() > wholeSize)
  342. || (positionReversed && pixelPosition < 0)) {
  343. pixelPosition = wholeSize - getSplitterSize();
  344. if (pixelPosition < 0) {
  345. pixelPosition = 0;
  346. }
  347. // Move splitter within bounds, but don't remember the new value
  348. setSplitPosition(pixelPosition + "px", false);
  349. return;
  350. }
  351. firstContainer.getStyle().setWidth(pixelPosition, Unit.PX);
  352. int secondContainerWidth = (wholeSize - pixelPosition - getSplitterSize());
  353. if (secondContainerWidth < 0) {
  354. secondContainerWidth = 0;
  355. }
  356. secondContainer.getStyle().setWidth(secondContainerWidth, Unit.PX);
  357. secondContainer.getStyle().setLeft(
  358. pixelPosition + getSplitterSize(), Unit.PX);
  359. LayoutManager layoutManager = LayoutManager.get(client);
  360. ConnectorMap connectorMap = ConnectorMap.get(client);
  361. if (firstChild != null) {
  362. ComponentConnector connector = connectorMap
  363. .getConnector(firstChild);
  364. if (connector.isRelativeWidth()) {
  365. layoutManager.reportWidthAssignedToRelative(connector,
  366. pixelPosition);
  367. } else {
  368. layoutManager.setNeedsMeasure(connector);
  369. }
  370. }
  371. if (secondChild != null) {
  372. ComponentConnector connector = connectorMap
  373. .getConnector(secondChild);
  374. if (connector.isRelativeWidth()) {
  375. layoutManager.reportWidthAssignedToRelative(connector,
  376. secondContainerWidth);
  377. } else {
  378. layoutManager.setNeedsMeasure(connector);
  379. }
  380. }
  381. break;
  382. case VERTICAL:
  383. wholeSize = DOM.getElementPropertyInt(wrapper, "clientHeight");
  384. pixelPosition = DOM.getElementPropertyInt(splitter, "offsetTop");
  385. // reposition splitter in case it is out of box
  386. if ((pixelPosition > 0 && pixelPosition + getSplitterSize() > wholeSize)
  387. || (positionReversed && pixelPosition < 0)) {
  388. pixelPosition = wholeSize - getSplitterSize();
  389. if (pixelPosition < 0) {
  390. pixelPosition = 0;
  391. }
  392. // Move splitter within bounds, but don't remember the new value
  393. setSplitPosition(pixelPosition + "px", false);
  394. return;
  395. }
  396. firstContainer.getStyle().setHeight(pixelPosition, Unit.PX);
  397. int secondContainerHeight = (wholeSize - pixelPosition - getSplitterSize());
  398. if (secondContainerHeight < 0) {
  399. secondContainerHeight = 0;
  400. }
  401. secondContainer.getStyle()
  402. .setHeight(secondContainerHeight, Unit.PX);
  403. secondContainer.getStyle().setTop(
  404. pixelPosition + getSplitterSize(), Unit.PX);
  405. layoutManager = LayoutManager.get(client);
  406. connectorMap = ConnectorMap.get(client);
  407. if (firstChild != null) {
  408. ComponentConnector connector = connectorMap
  409. .getConnector(firstChild);
  410. if (connector.isRelativeHeight()) {
  411. layoutManager.reportHeightAssignedToRelative(connector,
  412. pixelPosition);
  413. } else {
  414. layoutManager.setNeedsMeasure(connector);
  415. }
  416. }
  417. if (secondChild != null) {
  418. ComponentConnector connector = connectorMap
  419. .getConnector(secondChild);
  420. if (connector.isRelativeHeight()) {
  421. layoutManager.reportHeightAssignedToRelative(connector,
  422. secondContainerHeight);
  423. } else {
  424. layoutManager.setNeedsMeasure(connector);
  425. }
  426. }
  427. break;
  428. }
  429. }
  430. /** For internal use only. May be removed or replaced in the future. */
  431. public void setFirstWidget(Widget w) {
  432. if (firstChild == w) {
  433. return;
  434. }
  435. if (firstChild != null) {
  436. firstChild.removeFromParent();
  437. }
  438. if (w != null) {
  439. super.add(w, firstContainer);
  440. }
  441. firstChild = w;
  442. }
  443. public Widget getFirstWidget() {
  444. return firstChild;
  445. }
  446. /** For internal use only. May be removed or replaced in the future. */
  447. public void setSecondWidget(Widget w) {
  448. if (secondChild == w) {
  449. return;
  450. }
  451. if (secondChild != null) {
  452. secondChild.removeFromParent();
  453. }
  454. if (w != null) {
  455. super.add(w, secondContainer);
  456. }
  457. secondChild = w;
  458. }
  459. public Widget getSecondWidget() {
  460. return secondChild;
  461. }
  462. @Override
  463. public void onBrowserEvent(Event event) {
  464. switch (DOM.eventGetType(event)) {
  465. case Event.ONMOUSEMOVE:
  466. // case Event.ONTOUCHMOVE:
  467. if (resizing) {
  468. onMouseMove(event);
  469. }
  470. break;
  471. case Event.ONMOUSEDOWN:
  472. // case Event.ONTOUCHSTART:
  473. onMouseDown(event);
  474. break;
  475. case Event.ONMOUSEOUT:
  476. // Dragging curtain interferes with click events if added in
  477. // mousedown so we add it only when needed i.e., if the mouse moves
  478. // outside the splitter.
  479. if (resizing) {
  480. showDraggingCurtain();
  481. }
  482. break;
  483. case Event.ONMOUSEUP:
  484. // case Event.ONTOUCHEND:
  485. if (resizing) {
  486. onMouseUp(event);
  487. }
  488. break;
  489. case Event.ONCLICK:
  490. resizing = false;
  491. break;
  492. }
  493. // Only fire click event listeners if the splitter isn't moved
  494. if (Util.isTouchEvent(event) || !resized) {
  495. super.onBrowserEvent(event);
  496. } else if (DOM.eventGetType(event) == Event.ONMOUSEUP) {
  497. // Reset the resized flag after a mouseup has occured so the next
  498. // mousedown/mouseup can be interpreted as a click.
  499. resized = false;
  500. }
  501. }
  502. public void onMouseDown(Event event) {
  503. if (locked || !isEnabled()) {
  504. return;
  505. }
  506. final Element trg = event.getEventTarget().cast();
  507. if (trg == splitter || trg == DOM.getChild(splitter, 0)) {
  508. resizing = true;
  509. DOM.setCapture(getElement());
  510. origX = DOM.getElementPropertyInt(splitter, "offsetLeft");
  511. origY = DOM.getElementPropertyInt(splitter, "offsetTop");
  512. origMouseX = Util.getTouchOrMouseClientX(event);
  513. origMouseY = Util.getTouchOrMouseClientY(event);
  514. event.stopPropagation();
  515. event.preventDefault();
  516. }
  517. }
  518. public void onMouseMove(Event event) {
  519. switch (orientation) {
  520. case HORIZONTAL:
  521. final int x = Util.getTouchOrMouseClientX(event);
  522. onHorizontalMouseMove(x);
  523. break;
  524. case VERTICAL:
  525. default:
  526. final int y = Util.getTouchOrMouseClientY(event);
  527. onVerticalMouseMove(y);
  528. break;
  529. }
  530. }
  531. private void onHorizontalMouseMove(int x) {
  532. int newX = origX + x - origMouseX;
  533. if (newX < 0) {
  534. newX = 0;
  535. }
  536. if (newX + getSplitterSize() > getOffsetWidth()) {
  537. newX = getOffsetWidth() - getSplitterSize();
  538. }
  539. if (position.indexOf("%") > 0) {
  540. position = convertToPositionUnits(newX + "px");
  541. } else {
  542. // Reversed position
  543. if (positionReversed) {
  544. position = (getOffsetWidth() - newX - getSplitterSize()) + "px";
  545. } else {
  546. position = newX + "px";
  547. }
  548. }
  549. if (origX != newX) {
  550. resized = true;
  551. }
  552. // Reversed position
  553. if (positionReversed) {
  554. newX = getOffsetWidth() - newX - getSplitterSize();
  555. }
  556. setSplitPosition(newX + "px");
  557. }
  558. private void onVerticalMouseMove(int y) {
  559. int newY = origY + y - origMouseY;
  560. if (newY < 0) {
  561. newY = 0;
  562. }
  563. if (newY + getSplitterSize() > getOffsetHeight()) {
  564. newY = getOffsetHeight() - getSplitterSize();
  565. }
  566. if (position.indexOf("%") > 0) {
  567. position = convertToPositionUnits(newY + "px");
  568. } else {
  569. // Reversed position
  570. if (positionReversed) {
  571. position = (getOffsetHeight() - newY - getSplitterSize())
  572. + "px";
  573. } else {
  574. position = newY + "px";
  575. }
  576. }
  577. if (origY != newY) {
  578. resized = true;
  579. }
  580. // Reversed position
  581. if (positionReversed) {
  582. newY = getOffsetHeight() - newY - getSplitterSize();
  583. }
  584. setSplitPosition(newY + "px");
  585. }
  586. public void onMouseUp(Event event) {
  587. DOM.releaseCapture(getElement());
  588. hideDraggingCurtain();
  589. resizing = false;
  590. if (!Util.isTouchEvent(event)) {
  591. onMouseMove(event);
  592. }
  593. fireEvent(new SplitterMoveEvent(this));
  594. }
  595. public interface SplitterMoveHandler extends EventHandler {
  596. public void splitterMoved(SplitterMoveEvent event);
  597. public static class SplitterMoveEvent extends
  598. GwtEvent<SplitterMoveHandler> {
  599. public static final Type<SplitterMoveHandler> TYPE = new Type<SplitterMoveHandler>();
  600. private Widget splitPanel;
  601. public SplitterMoveEvent(Widget splitPanel) {
  602. this.splitPanel = splitPanel;
  603. }
  604. @Override
  605. public com.google.gwt.event.shared.GwtEvent.Type<SplitterMoveHandler> getAssociatedType() {
  606. return TYPE;
  607. }
  608. @Override
  609. protected void dispatch(SplitterMoveHandler handler) {
  610. handler.splitterMoved(this);
  611. }
  612. }
  613. }
  614. /** For internal use only. May be removed or replaced in the future. */
  615. public String getSplitterPosition() {
  616. return position;
  617. }
  618. /**
  619. * Used in FF to avoid losing mouse capture when pointer is moved on an
  620. * iframe.
  621. */
  622. private void showDraggingCurtain() {
  623. if (!isDraggingCurtainRequired()) {
  624. return;
  625. }
  626. if (draggingCurtain == null) {
  627. draggingCurtain = DOM.createDiv();
  628. draggingCurtain.getStyle().setPosition(Position.ABSOLUTE);
  629. draggingCurtain.getStyle().setTop(0, Unit.PX);
  630. draggingCurtain.getStyle().setLeft(0, Unit.PX);
  631. draggingCurtain.getStyle().setWidth(100, Unit.PCT);
  632. draggingCurtain.getStyle().setHeight(100, Unit.PCT);
  633. draggingCurtain.getStyle().setZIndex(VOverlay.Z_INDEX);
  634. DOM.appendChild(wrapper, draggingCurtain);
  635. }
  636. }
  637. /**
  638. * A dragging curtain is required in Gecko and Webkit.
  639. *
  640. * @return true if the browser requires a dragging curtain
  641. */
  642. private boolean isDraggingCurtainRequired() {
  643. return (BrowserInfo.get().isGecko() || BrowserInfo.get().isWebkit());
  644. }
  645. /**
  646. * Hides dragging curtain
  647. */
  648. private void hideDraggingCurtain() {
  649. if (draggingCurtain != null) {
  650. DOM.removeChild(wrapper, draggingCurtain);
  651. draggingCurtain = null;
  652. }
  653. }
  654. private int splitterSize = -1;
  655. private int getSplitterSize() {
  656. if (splitterSize < 0) {
  657. if (isAttached()) {
  658. switch (orientation) {
  659. case HORIZONTAL:
  660. splitterSize = DOM.getElementPropertyInt(splitter,
  661. "offsetWidth");
  662. break;
  663. default:
  664. splitterSize = DOM.getElementPropertyInt(splitter,
  665. "offsetHeight");
  666. break;
  667. }
  668. }
  669. }
  670. return splitterSize;
  671. }
  672. /** For internal use only. May be removed or replaced in the future. */
  673. public void setStylenames() {
  674. final String splitterClass = CLASSNAME
  675. + (orientation == Orientation.HORIZONTAL ? "-hsplitter"
  676. : "-vsplitter");
  677. final String firstContainerClass = CLASSNAME + "-first-container";
  678. final String secondContainerClass = CLASSNAME + "-second-container";
  679. final String lockedSuffix = locked ? "-locked" : "";
  680. splitter.setClassName(splitterClass + lockedSuffix);
  681. firstContainer.setClassName(firstContainerClass);
  682. secondContainer.setClassName(secondContainerClass);
  683. for (String styleName : componentStyleNames) {
  684. splitter.addClassName(splitterClass + "-" + styleName
  685. + lockedSuffix);
  686. firstContainer.addClassName(firstContainerClass + "-" + styleName);
  687. secondContainer
  688. .addClassName(secondContainerClass + "-" + styleName);
  689. }
  690. }
  691. public void setEnabled(boolean enabled) {
  692. this.enabled = enabled;
  693. }
  694. public boolean isEnabled() {
  695. return enabled;
  696. }
  697. /**
  698. * Ensures the panels are scrollable eg. after style name changes
  699. * <p>
  700. * For internal use only. May be removed or replaced in the future.
  701. */
  702. public void makeScrollable() {
  703. if (touchScrollHandler == null) {
  704. touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
  705. }
  706. touchScrollHandler.addElement(firstContainer);
  707. touchScrollHandler.addElement(secondContainer);
  708. }
  709. }