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

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