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.

AbstractOrderedLayout.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.ui;
  17. import java.util.Iterator;
  18. import java.util.LinkedList;
  19. import com.vaadin.event.LayoutEvents.LayoutClickEvent;
  20. import com.vaadin.event.LayoutEvents.LayoutClickListener;
  21. import com.vaadin.event.LayoutEvents.LayoutClickNotifier;
  22. import com.vaadin.server.Sizeable;
  23. import com.vaadin.shared.Connector;
  24. import com.vaadin.shared.EventId;
  25. import com.vaadin.shared.MouseEventDetails;
  26. import com.vaadin.shared.ui.MarginInfo;
  27. import com.vaadin.shared.ui.orderedlayout.AbstractOrderedLayoutServerRpc;
  28. import com.vaadin.shared.ui.orderedlayout.AbstractOrderedLayoutState;
  29. import com.vaadin.shared.ui.orderedlayout.AbstractOrderedLayoutState.ChildComponentData;
  30. @SuppressWarnings("serial")
  31. public abstract class AbstractOrderedLayout extends AbstractLayout implements
  32. Layout.AlignmentHandler, Layout.SpacingHandler, LayoutClickNotifier,
  33. Layout.MarginHandler {
  34. private AbstractOrderedLayoutServerRpc rpc = new AbstractOrderedLayoutServerRpc() {
  35. @Override
  36. public void layoutClick(MouseEventDetails mouseDetails,
  37. Connector clickedConnector) {
  38. fireEvent(LayoutClickEvent.createEvent(AbstractOrderedLayout.this,
  39. mouseDetails, clickedConnector));
  40. }
  41. };
  42. public static final Alignment ALIGNMENT_DEFAULT = Alignment.TOP_LEFT;
  43. /**
  44. * Custom layout slots containing the components.
  45. */
  46. protected LinkedList<Component> components = new LinkedList<Component>();
  47. private Alignment defaultComponentAlignment = Alignment.TOP_LEFT;
  48. /* Child component alignments */
  49. /**
  50. * Constructs an empty AbstractOrderedLayout.
  51. */
  52. public AbstractOrderedLayout() {
  53. registerRpc(rpc);
  54. }
  55. @Override
  56. protected AbstractOrderedLayoutState getState() {
  57. return (AbstractOrderedLayoutState) super.getState();
  58. }
  59. @Override
  60. protected AbstractOrderedLayoutState getState(boolean markAsDirty) {
  61. return (AbstractOrderedLayoutState) super.getState(markAsDirty);
  62. }
  63. /**
  64. * Add a component into this container. The component is added to the right
  65. * or under the previous component.
  66. *
  67. * @param c
  68. * the component to be added.
  69. */
  70. @Override
  71. public void addComponent(Component c) {
  72. // Add to components before calling super.addComponent
  73. // so that it is available to AttachListeners
  74. components.add(c);
  75. try {
  76. super.addComponent(c);
  77. } catch (IllegalArgumentException e) {
  78. components.remove(c);
  79. throw e;
  80. }
  81. componentAdded(c);
  82. }
  83. /**
  84. * Adds a component into this container. The component is added to the left
  85. * or on top of the other components.
  86. *
  87. * @param c
  88. * the component to be added.
  89. */
  90. public void addComponentAsFirst(Component c) {
  91. // If c is already in this, we must remove it before proceeding
  92. // see ticket #7668
  93. if (c.getParent() == this) {
  94. removeComponent(c);
  95. }
  96. components.addFirst(c);
  97. try {
  98. super.addComponent(c);
  99. } catch (IllegalArgumentException e) {
  100. components.remove(c);
  101. throw e;
  102. }
  103. componentAdded(c);
  104. }
  105. /**
  106. * Adds a component into indexed position in this container.
  107. *
  108. * @param c
  109. * the component to be added.
  110. * @param index
  111. * the index of the component position. The components currently
  112. * in and after the position are shifted forwards.
  113. */
  114. public void addComponent(Component c, int index) {
  115. // If c is already in this, we must remove it before proceeding
  116. // see ticket #7668
  117. if (c.getParent() == this) {
  118. // When c is removed, all components after it are shifted down
  119. if (index > getComponentIndex(c)) {
  120. index--;
  121. }
  122. removeComponent(c);
  123. }
  124. components.add(index, c);
  125. try {
  126. super.addComponent(c);
  127. } catch (IllegalArgumentException e) {
  128. components.remove(c);
  129. throw e;
  130. }
  131. componentAdded(c);
  132. }
  133. private void componentRemoved(Component c) {
  134. getState().childData.remove(c);
  135. }
  136. private void componentAdded(Component c) {
  137. ChildComponentData ccd = new ChildComponentData();
  138. ccd.alignmentBitmask = getDefaultComponentAlignment().getBitMask();
  139. getState().childData.put(c, ccd);
  140. }
  141. /**
  142. * Removes the component from this container.
  143. *
  144. * @param c
  145. * the component to be removed.
  146. */
  147. @Override
  148. public void removeComponent(Component c) {
  149. components.remove(c);
  150. super.removeComponent(c);
  151. componentRemoved(c);
  152. }
  153. /**
  154. * Gets the component container iterator for going trough all the components
  155. * in the container.
  156. *
  157. * @return the Iterator of the components inside the container.
  158. */
  159. @Override
  160. public Iterator<Component> iterator() {
  161. return components.iterator();
  162. }
  163. /**
  164. * Gets the number of contained components. Consistent with the iterator
  165. * returned by {@link #getComponentIterator()}.
  166. *
  167. * @return the number of contained components
  168. */
  169. @Override
  170. public int getComponentCount() {
  171. return components.size();
  172. }
  173. /* Documented in superclass */
  174. @Override
  175. public void replaceComponent(Component oldComponent, Component newComponent) {
  176. // Gets the locations
  177. int oldLocation = -1;
  178. int newLocation = -1;
  179. int location = 0;
  180. for (final Iterator<Component> i = components.iterator(); i.hasNext();) {
  181. final Component component = i.next();
  182. if (component == oldComponent) {
  183. oldLocation = location;
  184. }
  185. if (component == newComponent) {
  186. newLocation = location;
  187. }
  188. location++;
  189. }
  190. if (oldLocation == -1) {
  191. addComponent(newComponent);
  192. } else if (newLocation == -1) {
  193. Alignment alignment = getComponentAlignment(oldComponent);
  194. float expandRatio = getExpandRatio(oldComponent);
  195. removeComponent(oldComponent);
  196. addComponent(newComponent, oldLocation);
  197. applyLayoutSettings(newComponent, alignment, expandRatio);
  198. } else {
  199. // Both old and new are in the layout
  200. if (oldLocation > newLocation) {
  201. components.remove(oldComponent);
  202. components.add(newLocation, oldComponent);
  203. components.remove(newComponent);
  204. components.add(oldLocation, newComponent);
  205. } else {
  206. components.remove(newComponent);
  207. components.add(oldLocation, newComponent);
  208. components.remove(oldComponent);
  209. components.add(newLocation, oldComponent);
  210. }
  211. markAsDirty();
  212. }
  213. }
  214. @Override
  215. public void setComponentAlignment(Component childComponent,
  216. Alignment alignment) {
  217. ChildComponentData childData = getState().childData.get(childComponent);
  218. if (childData != null) {
  219. // Alignments are bit masks
  220. childData.alignmentBitmask = alignment.getBitMask();
  221. } else {
  222. throw new IllegalArgumentException(
  223. "Component must be added to layout before using setComponentAlignment()");
  224. }
  225. }
  226. /*
  227. * (non-Javadoc)
  228. *
  229. * @see com.vaadin.ui.Layout.AlignmentHandler#getComponentAlignment(com
  230. * .vaadin.ui.Component)
  231. */
  232. @Override
  233. public Alignment getComponentAlignment(Component childComponent) {
  234. ChildComponentData childData = getState().childData.get(childComponent);
  235. if (childData == null) {
  236. throw new IllegalArgumentException(
  237. "The given component is not a child of this layout");
  238. }
  239. return new Alignment(childData.alignmentBitmask);
  240. }
  241. /*
  242. * (non-Javadoc)
  243. *
  244. * @see com.vaadin.ui.Layout.SpacingHandler#setSpacing(boolean)
  245. */
  246. @Override
  247. public void setSpacing(boolean spacing) {
  248. getState().spacing = spacing;
  249. }
  250. /*
  251. * (non-Javadoc)
  252. *
  253. * @see com.vaadin.ui.Layout.SpacingHandler#isSpacing()
  254. */
  255. @Override
  256. public boolean isSpacing() {
  257. return getState(false).spacing;
  258. }
  259. /**
  260. * <p>
  261. * This method is used to control how excess space in layout is distributed
  262. * among components. Excess space may exist if layout is sized and contained
  263. * non relatively sized components don't consume all available space.
  264. *
  265. * <p>
  266. * Example how to distribute 1:3 (33%) for component1 and 2:3 (67%) for
  267. * component2 :
  268. *
  269. * <code>
  270. * layout.setExpandRatio(component1, 1);<br>
  271. * layout.setExpandRatio(component2, 2);
  272. * </code>
  273. *
  274. * <p>
  275. * If no ratios have been set, the excess space is distributed evenly among
  276. * all components.
  277. *
  278. * <p>
  279. * Note, that width or height (depending on orientation) needs to be defined
  280. * for this method to have any effect.
  281. *
  282. * @see Sizeable
  283. *
  284. * @param component
  285. * the component in this layout which expand ratio is to be set
  286. * @param ratio
  287. */
  288. public void setExpandRatio(Component component, float ratio) {
  289. ChildComponentData childData = getState().childData.get(component);
  290. if (childData == null) {
  291. throw new IllegalArgumentException(
  292. "The given component is not a child of this layout");
  293. }
  294. childData.expandRatio = ratio;
  295. }
  296. /**
  297. * Returns the expand ratio of given component.
  298. *
  299. * @param component
  300. * which expand ratios is requested
  301. * @return expand ratio of given component, 0.0f by default.
  302. */
  303. public float getExpandRatio(Component component) {
  304. ChildComponentData childData = getState(false).childData.get(component);
  305. if (childData == null) {
  306. throw new IllegalArgumentException(
  307. "The given component is not a child of this layout");
  308. }
  309. return childData.expandRatio;
  310. }
  311. @Override
  312. public void addLayoutClickListener(LayoutClickListener listener) {
  313. addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
  314. LayoutClickEvent.class, listener,
  315. LayoutClickListener.clickMethod);
  316. }
  317. /**
  318. * @deprecated As of 7.0, replaced by
  319. * {@link #addLayoutClickListener(LayoutClickListener)}
  320. **/
  321. @Override
  322. @Deprecated
  323. public void addListener(LayoutClickListener listener) {
  324. addLayoutClickListener(listener);
  325. }
  326. @Override
  327. public void removeLayoutClickListener(LayoutClickListener listener) {
  328. removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
  329. LayoutClickEvent.class, listener);
  330. }
  331. /**
  332. * @deprecated As of 7.0, replaced by
  333. * {@link #removeLayoutClickListener(LayoutClickListener)}
  334. **/
  335. @Override
  336. @Deprecated
  337. public void removeListener(LayoutClickListener listener) {
  338. removeLayoutClickListener(listener);
  339. }
  340. /**
  341. * Returns the index of the given component.
  342. *
  343. * @param component
  344. * The component to look up.
  345. * @return The index of the component or -1 if the component is not a child.
  346. */
  347. public int getComponentIndex(Component component) {
  348. return components.indexOf(component);
  349. }
  350. /**
  351. * Returns the component at the given position.
  352. *
  353. * @param index
  354. * The position of the component.
  355. * @return The component at the given index.
  356. * @throws IndexOutOfBoundsException
  357. * If the index is out of range.
  358. */
  359. public Component getComponent(int index) throws IndexOutOfBoundsException {
  360. return components.get(index);
  361. }
  362. @Override
  363. public void setMargin(boolean enabled) {
  364. setMargin(new MarginInfo(enabled));
  365. }
  366. /*
  367. * (non-Javadoc)
  368. *
  369. * @see com.vaadin.ui.Layout.MarginHandler#getMargin()
  370. */
  371. @Override
  372. public MarginInfo getMargin() {
  373. return new MarginInfo(getState(false).marginsBitmask);
  374. }
  375. /*
  376. * (non-Javadoc)
  377. *
  378. * @see com.vaadin.ui.Layout.MarginHandler#setMargin(MarginInfo)
  379. */
  380. @Override
  381. public void setMargin(MarginInfo marginInfo) {
  382. getState().marginsBitmask = marginInfo.getBitMask();
  383. }
  384. /*
  385. * (non-Javadoc)
  386. *
  387. * @see com.vaadin.ui.Layout.AlignmentHandler#getDefaultComponentAlignment()
  388. */
  389. @Override
  390. public Alignment getDefaultComponentAlignment() {
  391. return defaultComponentAlignment;
  392. }
  393. /*
  394. * (non-Javadoc)
  395. *
  396. * @see
  397. * com.vaadin.ui.Layout.AlignmentHandler#setDefaultComponentAlignment(com
  398. * .vaadin.ui.Alignment)
  399. */
  400. @Override
  401. public void setDefaultComponentAlignment(Alignment defaultAlignment) {
  402. defaultComponentAlignment = defaultAlignment;
  403. }
  404. private void applyLayoutSettings(Component target, Alignment alignment,
  405. float expandRatio) {
  406. setComponentAlignment(target, alignment);
  407. setExpandRatio(target, expandRatio);
  408. }
  409. }