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.

LayoutAttachListenerInfo.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.vaadin.tests.components;
  2. import java.util.Arrays;
  3. import com.vaadin.data.Property.ValueChangeEvent;
  4. import com.vaadin.data.Property.ValueChangeListener;
  5. import com.vaadin.ui.AbsoluteLayout;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.Button.ClickEvent;
  8. import com.vaadin.ui.GridLayout;
  9. import com.vaadin.ui.HasComponents.ComponentAttachEvent;
  10. import com.vaadin.ui.HasComponents.ComponentAttachListener;
  11. import com.vaadin.ui.Label;
  12. import com.vaadin.ui.Notification;
  13. import com.vaadin.ui.OptionGroup;
  14. import com.vaadin.ui.VerticalLayout;
  15. public class LayoutAttachListenerInfo extends TestBase {
  16. private VerticalLayout content = new VerticalLayout();
  17. @Override
  18. protected void setup() {
  19. OptionGroup layouts = new OptionGroup("Layouts", Arrays.asList(
  20. "AbsoluteLayout", "OrderedLayout", "GridLayout"));
  21. layouts.select("AbsoluteLayout");
  22. layouts.setImmediate(true);
  23. layouts.addValueChangeListener(new ValueChangeListener() {
  24. @Override
  25. public void valueChange(ValueChangeEvent event) {
  26. if (event.getProperty().getValue().equals("AbsoluteLayout")) {
  27. testAbsoluteLayout();
  28. } else if (event.getProperty().getValue()
  29. .equals("OrderedLayout")) {
  30. testOrderedLayout();
  31. } else if (event.getProperty().getValue().equals("GridLayout")) {
  32. testGridLayout();
  33. }
  34. }
  35. });
  36. addComponent(layouts);
  37. addComponent(content);
  38. testAbsoluteLayout();
  39. }
  40. @Override
  41. protected String getDescription() {
  42. return "When pressing the attach button a Label with the value \"X\" "
  43. + "should get added to the selected layout and a notification of the position"
  44. + " of the component should be visible";
  45. }
  46. @Override
  47. protected Integer getTicketNumber() {
  48. return 6368;
  49. }
  50. private void testAbsoluteLayout() {
  51. content.removeAllComponents();
  52. final AbsoluteLayout a = new AbsoluteLayout();
  53. a.setWidth("300px");
  54. a.setHeight("300px");
  55. a.addComponentAttachListener(new ComponentAttachListener() {
  56. @Override
  57. public void componentAttachedToContainer(ComponentAttachEvent event) {
  58. AbsoluteLayout layout = (AbsoluteLayout) event.getContainer();
  59. AbsoluteLayout.ComponentPosition position = layout
  60. .getPosition(event.getAttachedComponent());
  61. getMainWindow().showNotification(
  62. "Attached to " + position.getCSSString(),
  63. Notification.TYPE_ERROR_MESSAGE);
  64. }
  65. });
  66. content.addComponent(a);
  67. content.addComponent(new Button("Attach label to layout",
  68. new Button.ClickListener() {
  69. @Override
  70. public void buttonClick(ClickEvent event) {
  71. a.addComponent(new Label("X"), "top:50px;left:50px");
  72. }
  73. }));
  74. }
  75. private void testOrderedLayout() {
  76. content.removeAllComponents();
  77. final VerticalLayout v = new VerticalLayout();
  78. v.setWidth("300px");
  79. v.setHeight("300px");
  80. v.addComponentAttachListener(new ComponentAttachListener() {
  81. @Override
  82. public void componentAttachedToContainer(ComponentAttachEvent event) {
  83. VerticalLayout layout = (VerticalLayout) event.getContainer();
  84. getMainWindow().showNotification(
  85. "Attached to index "
  86. + layout.getComponentIndex(event
  87. .getAttachedComponent()),
  88. Notification.TYPE_ERROR_MESSAGE);
  89. }
  90. });
  91. content.addComponent(v);
  92. content.addComponent(new Button("Attach label to layout",
  93. new Button.ClickListener() {
  94. @Override
  95. public void buttonClick(ClickEvent event) {
  96. v.addComponent(new Label("X"));
  97. }
  98. }));
  99. }
  100. private void testGridLayout() {
  101. content.removeAllComponents();
  102. final GridLayout g = new GridLayout(4, 4);
  103. g.setWidth("300px");
  104. g.setHeight("300px");
  105. g.setHideEmptyRowsAndColumns(true);
  106. g.addComponentAttachListener(new ComponentAttachListener() {
  107. @Override
  108. public void componentAttachedToContainer(ComponentAttachEvent event) {
  109. GridLayout layout = (GridLayout) event.getContainer();
  110. GridLayout.Area area = layout.getComponentArea(event
  111. .getAttachedComponent());
  112. getMainWindow().showNotification(
  113. "Attached to " + area.getColumn1() + ","
  114. + area.getRow1(),
  115. Notification.TYPE_ERROR_MESSAGE);
  116. }
  117. });
  118. content.addComponent(g);
  119. content.addComponent(new Button("Attach label to layout",
  120. new Button.ClickListener() {
  121. @Override
  122. public void buttonClick(ClickEvent event) {
  123. g.addComponent(new Label("X"), 2, 3);
  124. }
  125. }));
  126. }
  127. }