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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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, Connector clickedConnector) -> {
  43. fireEvent(LayoutClickEvent.createEvent(AbstractOrderedLayout.this,
  44. mouseDetails, clickedConnector));
  45. };
  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. addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
  326. LayoutClickEvent.class, listener,
  327. LayoutClickListener.clickMethod);
  328. return () -> removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
  329. LayoutClickEvent.class, listener);
  330. }
  331. @Override
  332. @Deprecated
  333. public void removeLayoutClickListener(LayoutClickListener listener) {
  334. removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER,
  335. LayoutClickEvent.class, listener);
  336. }
  337. /**
  338. * Returns the index of the given component.
  339. *
  340. * @param component
  341. * The component to look up.
  342. * @return The index of the component or -1 if the component is not a child.
  343. */
  344. public int getComponentIndex(Component component) {
  345. return components.indexOf(component);
  346. }
  347. /**
  348. * Returns the component at the given position.
  349. *
  350. * @param index
  351. * The position of the component.
  352. * @return The component at the given index.
  353. * @throws IndexOutOfBoundsException
  354. * If the index is out of range.
  355. */
  356. public Component getComponent(int index) throws IndexOutOfBoundsException {
  357. return components.get(index);
  358. }
  359. @Override
  360. public void setMargin(boolean enabled) {
  361. setMargin(new MarginInfo(enabled));
  362. }
  363. /*
  364. * (non-Javadoc)
  365. *
  366. * @see com.vaadin.ui.Layout.MarginHandler#getMargin()
  367. */
  368. @Override
  369. public MarginInfo getMargin() {
  370. return new MarginInfo(getState(false).marginsBitmask);
  371. }
  372. /*
  373. * (non-Javadoc)
  374. *
  375. * @see com.vaadin.ui.Layout.MarginHandler#setMargin(MarginInfo)
  376. */
  377. @Override
  378. public void setMargin(MarginInfo marginInfo) {
  379. getState().marginsBitmask = marginInfo.getBitMask();
  380. }
  381. /*
  382. * (non-Javadoc)
  383. *
  384. * @see com.vaadin.ui.Layout.AlignmentHandler#getDefaultComponentAlignment()
  385. */
  386. @Override
  387. public Alignment getDefaultComponentAlignment() {
  388. return defaultComponentAlignment;
  389. }
  390. /*
  391. * (non-Javadoc)
  392. *
  393. * @see
  394. * com.vaadin.ui.Layout.AlignmentHandler#setDefaultComponentAlignment(com
  395. * .vaadin.ui.Alignment)
  396. */
  397. @Override
  398. public void setDefaultComponentAlignment(Alignment defaultAlignment) {
  399. defaultComponentAlignment = defaultAlignment;
  400. }
  401. private void applyLayoutSettings(Component target, Alignment alignment,
  402. float expandRatio) {
  403. setComponentAlignment(target, alignment);
  404. setExpandRatio(target, expandRatio);
  405. }
  406. /*
  407. * (non-Javadoc)
  408. *
  409. * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element,
  410. * com.vaadin.ui.declarative.DesignContext)
  411. */
  412. @Override
  413. public void readDesign(Element design, DesignContext designContext) {
  414. // process default attributes
  415. super.readDesign(design, designContext);
  416. setMargin(readMargin(design, getMargin(), designContext));
  417. // handle children
  418. for (Element childComponent : design.children()) {
  419. Attributes attr = childComponent.attributes();
  420. Component newChild = designContext.readDesign(childComponent);
  421. addComponent(newChild);
  422. // handle alignment
  423. setComponentAlignment(newChild,
  424. DesignAttributeHandler.readAlignment(attr));
  425. // handle expand ratio
  426. if (attr.hasKey(":expand")) {
  427. String value = attr.get(":expand");
  428. if (value.length() > 0) {
  429. try {
  430. float ratio = Float.valueOf(value);
  431. setExpandRatio(newChild, ratio);
  432. } catch (NumberFormatException nfe) {
  433. getLogger()
  434. .info("Failed to parse expand ratio " + value);
  435. }
  436. } else {
  437. setExpandRatio(newChild, 1.0f);
  438. }
  439. }
  440. }
  441. }
  442. /*
  443. * (non-Javadoc)
  444. *
  445. * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element
  446. * , com.vaadin.ui.declarative.DesignContext)
  447. */
  448. @Override
  449. public void writeDesign(Element design, DesignContext designContext) {
  450. // write default attributes
  451. super.writeDesign(design, designContext);
  452. AbstractOrderedLayout def = designContext.getDefaultInstance(this);
  453. writeMargin(design, getMargin(), def.getMargin(), designContext);
  454. // handle children
  455. if (!designContext.shouldWriteChildren(this, def)) {
  456. return;
  457. }
  458. for (Component child : this) {
  459. Element childElement = designContext.createElement(child);
  460. design.appendChild(childElement);
  461. // handle alignment
  462. Alignment alignment = getComponentAlignment(child);
  463. if (alignment.isMiddle()) {
  464. childElement.attr(":middle", true);
  465. } else if (alignment.isBottom()) {
  466. childElement.attr(":bottom", true);
  467. }
  468. if (alignment.isCenter()) {
  469. childElement.attr(":center", true);
  470. } else if (alignment.isRight()) {
  471. childElement.attr(":right", true);
  472. }
  473. // handle expand ratio
  474. float expandRatio = getExpandRatio(child);
  475. if (expandRatio == 1.0f) {
  476. childElement.attr(":expand", true);
  477. } else if (expandRatio > 0) {
  478. childElement.attr(":expand", DesignAttributeHandler
  479. .getFormatter().format(expandRatio));
  480. }
  481. }
  482. }
  483. /*
  484. * (non-Javadoc)
  485. *
  486. * @see com.vaadin.ui.AbstractComponent#getCustomAttributes()
  487. */
  488. @Override
  489. protected Collection<String> getCustomAttributes() {
  490. Collection<String> customAttributes = super.getCustomAttributes();
  491. customAttributes.add("margin");
  492. customAttributes.add("margin-left");
  493. customAttributes.add("margin-right");
  494. customAttributes.add("margin-top");
  495. customAttributes.add("margin-bottom");
  496. return customAttributes;
  497. }
  498. private static Logger getLogger() {
  499. return Logger.getLogger(AbstractOrderedLayout.class.getName());
  500. }
  501. }