Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AbstractOrderedLayout.java 17KB

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