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 12KB

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