Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VAbstractSplitPanel.java 28KB

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