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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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.WidgetUtil;
  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. switch (orientation) {
  335. case HORIZONTAL:
  336. horizontalOrientationUpdateSizes();
  337. break;
  338. case VERTICAL:
  339. verticalOrientationUpdateSizes();
  340. break;
  341. }
  342. }
  343. private void verticalOrientationUpdateSizes() {
  344. int wholeSize = DOM.getElementPropertyInt(wrapper, "clientHeight");
  345. int pixelPosition = DOM.getElementPropertyInt(splitter, "offsetTop");
  346. // reposition splitter in case it is out of box
  347. if ((pixelPosition > 0 && pixelPosition + getSplitterSize() > wholeSize)
  348. || (positionReversed && pixelPosition < 0)) {
  349. pixelPosition = wholeSize - getSplitterSize();
  350. if (pixelPosition < 0) {
  351. pixelPosition = 0;
  352. }
  353. // Move splitter within bounds, but don't remember the new value
  354. setSplitPosition(pixelPosition + "px", false);
  355. return;
  356. }
  357. firstContainer.getStyle().setHeight(pixelPosition, Unit.PX);
  358. int secondContainerHeight = (wholeSize - pixelPosition - getSplitterSize());
  359. if (secondContainerHeight < 0) {
  360. secondContainerHeight = 0;
  361. }
  362. secondContainer.getStyle().setHeight(secondContainerHeight, Unit.PX);
  363. secondContainer.getStyle().setTop(pixelPosition + getSplitterSize(),
  364. Unit.PX);
  365. LayoutManager layoutManager = LayoutManager.get(client);
  366. ConnectorMap connectorMap = ConnectorMap.get(client);
  367. if (firstChild != null) {
  368. ComponentConnector connector = connectorMap
  369. .getConnector(firstChild);
  370. if (connector.isRelativeHeight()) {
  371. layoutManager.reportHeightAssignedToRelative(connector,
  372. pixelPosition);
  373. } else {
  374. layoutManager.setNeedsMeasure(connector);
  375. }
  376. }
  377. if (secondChild != null) {
  378. ComponentConnector connector = connectorMap
  379. .getConnector(secondChild);
  380. if (connector.isRelativeHeight()) {
  381. layoutManager.reportHeightAssignedToRelative(connector,
  382. secondContainerHeight);
  383. } else {
  384. layoutManager.setNeedsMeasure(connector);
  385. }
  386. }
  387. }
  388. private void horizontalOrientationUpdateSizes() {
  389. int wholeSize = DOM.getElementPropertyInt(wrapper, "clientWidth");
  390. int pixelPosition = DOM.getElementPropertyInt(splitter, "offsetLeft");
  391. // reposition splitter in case it is out of box
  392. if ((pixelPosition > 0 && pixelPosition + getSplitterSize() > wholeSize)
  393. || (positionReversed && pixelPosition < 0)) {
  394. pixelPosition = wholeSize - getSplitterSize();
  395. if (pixelPosition < 0) {
  396. pixelPosition = 0;
  397. }
  398. // Move splitter within bounds, but don't remember the new value
  399. setSplitPosition(pixelPosition + "px", false);
  400. return;
  401. }
  402. firstContainer.getStyle().setWidth(pixelPosition, Unit.PX);
  403. int secondContainerWidth = (wholeSize - pixelPosition - getSplitterSize());
  404. if (secondContainerWidth < 0) {
  405. secondContainerWidth = 0;
  406. }
  407. secondContainer.getStyle().setWidth(secondContainerWidth, Unit.PX);
  408. secondContainer.getStyle().setLeft(pixelPosition + getSplitterSize(),
  409. Unit.PX);
  410. LayoutManager layoutManager = LayoutManager.get(client);
  411. ConnectorMap connectorMap = ConnectorMap.get(client);
  412. if (firstChild != null) {
  413. ComponentConnector connector = connectorMap
  414. .getConnector(firstChild);
  415. if (connector.isRelativeWidth()) {
  416. layoutManager.reportWidthAssignedToRelative(connector,
  417. pixelPosition);
  418. } else {
  419. layoutManager.setNeedsMeasure(connector);
  420. }
  421. }
  422. if (secondChild != null) {
  423. ComponentConnector connector = connectorMap
  424. .getConnector(secondChild);
  425. if (connector.isRelativeWidth()) {
  426. layoutManager.reportWidthAssignedToRelative(connector,
  427. secondContainerWidth);
  428. } else {
  429. layoutManager.setNeedsMeasure(connector);
  430. }
  431. }
  432. // previous layout pass may have changed the position already, needs to
  433. // be reset before calculating which positioning should be used
  434. secondContainer.getStyle().setPosition(Position.ABSOLUTE);
  435. if (getOffsetHeight() == 0) {
  436. secondContainer.getStyle().setPosition(Position.RELATIVE);
  437. }
  438. }
  439. /** For internal use only. May be removed or replaced in the future. */
  440. public void setFirstWidget(Widget w) {
  441. if (firstChild == w) {
  442. return;
  443. }
  444. if (firstChild != null) {
  445. firstChild.removeFromParent();
  446. }
  447. if (w != null) {
  448. super.add(w, firstContainer);
  449. }
  450. firstChild = w;
  451. }
  452. public Widget getFirstWidget() {
  453. return firstChild;
  454. }
  455. /** For internal use only. May be removed or replaced in the future. */
  456. public void setSecondWidget(Widget w) {
  457. if (secondChild == w) {
  458. return;
  459. }
  460. if (secondChild != null) {
  461. secondChild.removeFromParent();
  462. }
  463. if (w != null) {
  464. super.add(w, secondContainer);
  465. }
  466. secondChild = w;
  467. }
  468. public Widget getSecondWidget() {
  469. return secondChild;
  470. }
  471. @Override
  472. public void onBrowserEvent(Event event) {
  473. switch (DOM.eventGetType(event)) {
  474. case Event.ONMOUSEMOVE:
  475. // case Event.ONTOUCHMOVE:
  476. if (resizing) {
  477. onMouseMove(event);
  478. }
  479. break;
  480. case Event.ONMOUSEDOWN:
  481. // case Event.ONTOUCHSTART:
  482. onMouseDown(event);
  483. break;
  484. case Event.ONMOUSEOUT:
  485. // Dragging curtain interferes with click events if added in
  486. // mousedown so we add it only when needed i.e., if the mouse moves
  487. // outside the splitter.
  488. if (resizing) {
  489. showDraggingCurtain();
  490. }
  491. break;
  492. case Event.ONMOUSEUP:
  493. // case Event.ONTOUCHEND:
  494. if (resizing) {
  495. onMouseUp(event);
  496. }
  497. break;
  498. case Event.ONCLICK:
  499. resizing = false;
  500. break;
  501. }
  502. // Only fire click event listeners if the splitter isn't moved
  503. if (WidgetUtil.isTouchEvent(event) || !resized) {
  504. super.onBrowserEvent(event);
  505. } else if (DOM.eventGetType(event) == Event.ONMOUSEUP) {
  506. // Reset the resized flag after a mouseup has occured so the next
  507. // mousedown/mouseup can be interpreted as a click.
  508. resized = false;
  509. }
  510. }
  511. public void onMouseDown(Event event) {
  512. if (locked || !isEnabled()) {
  513. return;
  514. }
  515. final Element trg = event.getEventTarget().cast();
  516. if (trg == splitter || trg == DOM.getChild(splitter, 0)) {
  517. resizing = true;
  518. DOM.setCapture(getElement());
  519. origX = DOM.getElementPropertyInt(splitter, "offsetLeft");
  520. origY = DOM.getElementPropertyInt(splitter, "offsetTop");
  521. origMouseX = WidgetUtil.getTouchOrMouseClientX(event);
  522. origMouseY = WidgetUtil.getTouchOrMouseClientY(event);
  523. event.stopPropagation();
  524. event.preventDefault();
  525. }
  526. }
  527. public void onMouseMove(Event event) {
  528. switch (orientation) {
  529. case HORIZONTAL:
  530. final int x = WidgetUtil.getTouchOrMouseClientX(event);
  531. onHorizontalMouseMove(x);
  532. break;
  533. case VERTICAL:
  534. default:
  535. final int y = WidgetUtil.getTouchOrMouseClientY(event);
  536. onVerticalMouseMove(y);
  537. break;
  538. }
  539. }
  540. private void onHorizontalMouseMove(int x) {
  541. int newX = origX + x - origMouseX;
  542. if (newX < 0) {
  543. newX = 0;
  544. }
  545. if (newX + getSplitterSize() > getOffsetWidth()) {
  546. newX = getOffsetWidth() - getSplitterSize();
  547. }
  548. if (position.indexOf("%") > 0) {
  549. position = convertToPositionUnits(newX + "px");
  550. } else {
  551. // Reversed position
  552. if (positionReversed) {
  553. position = (getOffsetWidth() - newX - getSplitterSize()) + "px";
  554. } else {
  555. position = newX + "px";
  556. }
  557. }
  558. if (origX != newX) {
  559. resized = true;
  560. }
  561. // Reversed position
  562. if (positionReversed) {
  563. newX = getOffsetWidth() - newX - getSplitterSize();
  564. }
  565. setSplitPosition(newX + "px");
  566. }
  567. private void onVerticalMouseMove(int y) {
  568. int newY = origY + y - origMouseY;
  569. if (newY < 0) {
  570. newY = 0;
  571. }
  572. if (newY + getSplitterSize() > getOffsetHeight()) {
  573. newY = getOffsetHeight() - getSplitterSize();
  574. }
  575. if (position.indexOf("%") > 0) {
  576. position = convertToPositionUnits(newY + "px");
  577. } else {
  578. // Reversed position
  579. if (positionReversed) {
  580. position = (getOffsetHeight() - newY - getSplitterSize())
  581. + "px";
  582. } else {
  583. position = newY + "px";
  584. }
  585. }
  586. if (origY != newY) {
  587. resized = true;
  588. }
  589. // Reversed position
  590. if (positionReversed) {
  591. newY = getOffsetHeight() - newY - getSplitterSize();
  592. }
  593. setSplitPosition(newY + "px");
  594. }
  595. public void onMouseUp(Event event) {
  596. DOM.releaseCapture(getElement());
  597. hideDraggingCurtain();
  598. resizing = false;
  599. if (!WidgetUtil.isTouchEvent(event)) {
  600. onMouseMove(event);
  601. }
  602. fireEvent(new SplitterMoveEvent(this));
  603. }
  604. public interface SplitterMoveHandler extends EventHandler {
  605. public void splitterMoved(SplitterMoveEvent event);
  606. public static class SplitterMoveEvent extends
  607. GwtEvent<SplitterMoveHandler> {
  608. public static final Type<SplitterMoveHandler> TYPE = new Type<SplitterMoveHandler>();
  609. private Widget splitPanel;
  610. public SplitterMoveEvent(Widget splitPanel) {
  611. this.splitPanel = splitPanel;
  612. }
  613. @Override
  614. public com.google.gwt.event.shared.GwtEvent.Type<SplitterMoveHandler> getAssociatedType() {
  615. return TYPE;
  616. }
  617. @Override
  618. protected void dispatch(SplitterMoveHandler handler) {
  619. handler.splitterMoved(this);
  620. }
  621. }
  622. }
  623. /** For internal use only. May be removed or replaced in the future. */
  624. public String getSplitterPosition() {
  625. return position;
  626. }
  627. /**
  628. * Used in FF to avoid losing mouse capture when pointer is moved on an
  629. * iframe.
  630. */
  631. private void showDraggingCurtain() {
  632. if (!isDraggingCurtainRequired()) {
  633. return;
  634. }
  635. if (draggingCurtain == null) {
  636. draggingCurtain = DOM.createDiv();
  637. draggingCurtain.getStyle().setPosition(Position.ABSOLUTE);
  638. draggingCurtain.getStyle().setTop(0, Unit.PX);
  639. draggingCurtain.getStyle().setLeft(0, Unit.PX);
  640. draggingCurtain.getStyle().setWidth(100, Unit.PCT);
  641. draggingCurtain.getStyle().setHeight(100, Unit.PCT);
  642. draggingCurtain.getStyle().setZIndex(VOverlay.Z_INDEX);
  643. DOM.appendChild(wrapper, draggingCurtain);
  644. }
  645. }
  646. /**
  647. * A dragging curtain is required in Gecko and Webkit.
  648. *
  649. * @return true if the browser requires a dragging curtain
  650. */
  651. private boolean isDraggingCurtainRequired() {
  652. return (BrowserInfo.get().isGecko() || BrowserInfo.get().isWebkit());
  653. }
  654. /**
  655. * Hides dragging curtain
  656. */
  657. private void hideDraggingCurtain() {
  658. if (draggingCurtain != null) {
  659. DOM.removeChild(wrapper, draggingCurtain);
  660. draggingCurtain = null;
  661. }
  662. }
  663. private int splitterSize = -1;
  664. private int getSplitterSize() {
  665. if (splitterSize < 0) {
  666. if (isAttached()) {
  667. switch (orientation) {
  668. case HORIZONTAL:
  669. splitterSize = DOM.getElementPropertyInt(splitter,
  670. "offsetWidth");
  671. break;
  672. default:
  673. splitterSize = DOM.getElementPropertyInt(splitter,
  674. "offsetHeight");
  675. break;
  676. }
  677. }
  678. }
  679. return splitterSize;
  680. }
  681. /** For internal use only. May be removed or replaced in the future. */
  682. public void setStylenames() {
  683. final String splitterClass = CLASSNAME
  684. + (orientation == Orientation.HORIZONTAL ? "-hsplitter"
  685. : "-vsplitter");
  686. final String firstContainerClass = CLASSNAME + "-first-container";
  687. final String secondContainerClass = CLASSNAME + "-second-container";
  688. final String lockedSuffix = locked ? "-locked" : "";
  689. splitter.setClassName(splitterClass + lockedSuffix);
  690. firstContainer.setClassName(firstContainerClass);
  691. secondContainer.setClassName(secondContainerClass);
  692. for (String styleName : componentStyleNames) {
  693. splitter.addClassName(splitterClass + "-" + styleName
  694. + lockedSuffix);
  695. firstContainer.addClassName(firstContainerClass + "-" + styleName);
  696. secondContainer
  697. .addClassName(secondContainerClass + "-" + styleName);
  698. }
  699. }
  700. public void setEnabled(boolean enabled) {
  701. this.enabled = enabled;
  702. }
  703. public boolean isEnabled() {
  704. return enabled;
  705. }
  706. /**
  707. * Ensures the panels are scrollable eg. after style name changes
  708. * <p>
  709. * For internal use only. May be removed or replaced in the future.
  710. */
  711. public void makeScrollable() {
  712. if (touchScrollHandler == null) {
  713. touchScrollHandler = TouchScrollDelegate.enableTouchScrolling(this);
  714. }
  715. touchScrollHandler.addElement(firstContainer);
  716. touchScrollHandler.addElement(secondContainer);
  717. }
  718. }