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

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