View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.incub;
35  
36  import info.magnolia.icons.MagnoliaIcons;
37  import info.magnolia.ui.theme.ResurfaceTheme;
38  
39  import java.util.Collection;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import com.vaadin.ui.AbstractOrderedLayout;
45  import com.vaadin.ui.Alignment;
46  import com.vaadin.ui.Button;
47  import com.vaadin.ui.Button.ClickListener;
48  import com.vaadin.ui.Component;
49  import com.vaadin.ui.CustomField;
50  import com.vaadin.ui.HorizontalLayout;
51  import com.vaadin.ui.NativeButton;
52  import com.vaadin.ui.VerticalLayout;
53  
54  /**
55   * Generic Multi Field.
56   *
57   * @param <T> the multifield's generic entity type
58   */
59  public class MultiField<T> extends CustomField<Collection<T>> {
60  
61      private static final Logger log = LoggerFactory.getLogger(MultiField.class);
62  
63      protected AbstractOrderedLayout root;
64  
65      private final Button addButton = new NativeButton();
66      //FIXME: Get from definition
67      private String buttonCaptionAdd = "Add";
68      private String buttonCaptionRemove = "Remove";
69      private String buttonCaptionMoveUp = "Move Up";
70      private String buttonCaptionMoveDown = "Move Down";
71      private boolean isOrderable = true;
72  
73      @Override
74      protected Component initContent() {
75          // Init root layout
76          root = new VerticalLayout();
77          root.setStyleName("multifield");
78          root.setSpacing(true);
79          root.setMargin(false);
80          root.setWidth(100, Unit.PERCENTAGE);
81          root.setHeight(-1, Unit.PIXELS);
82  
83          // Init addButton
84          addButton.setCaption(buttonCaptionAdd);
85          addButton.addStyleName("magnoliabutton");
86          addButton.addClickListener((ClickListener) event -> root.addComponent(createEntryComponent(), root.getComponentCount() - 1));
87          root.addComponent(addButton);
88          return root;
89      }
90  
91      /**
92       * Create a single element.<br>
93       * This single element is composed of:<br>
94       * - a configured field <br>
95       * - a remove Button<br>
96       */
97      private Component createEntryComponent() {
98          final HorizontalLayout layout = new HorizontalLayout();
99          layout.setStyleName("controls-wrapper");
100 
101         // FIXME: Create field from definition
102         final LinkFieldld.html#LinkField">LinkField field = new LinkField();
103         field.setButtonCaptionNew("Select new");
104         field.setButtonCaptionOther("Select another");
105         field.getSelectButton().setDisableOnClick(true);
106         field.setFieldEditable(true);
107         field.setWidth(100, Unit.PERCENTAGE);
108 
109         layout.addComponent(field);
110 
111         // bind the field's property to the item
112         // set layout to full width
113         layout.setWidth(100, Unit.PERCENTAGE);
114 
115         // distribute space in favour of field over delete button
116         layout.setExpandRatio(field, 1);
117 
118         if (isOrderable()) {
119             // move up Button
120             final Button moveUpButton = new Button(MagnoliaIcons.ARROW2_N);
121             moveUpButton.addStyleNames(ResurfaceTheme.BUTTON_ICON, "inline");
122             moveUpButton.setDescription(buttonCaptionMoveUp);
123             //FIXME: Add action to move up
124             moveUpButton.addClickListener((ClickListener) event -> moveUpButton.focus());
125 
126             // move down Button
127             final Button moveDownButton = new Button(MagnoliaIcons.ARROW2_S);
128             moveDownButton.addStyleNames(ResurfaceTheme.BUTTON_ICON, "inline");
129             moveDownButton.setDescription(buttonCaptionMoveDown);
130             //FIXME: Add action to move down
131             moveDownButton.addClickListener((ClickListener) event -> moveDownButton.focus());
132             layout.addComponents(moveUpButton, moveDownButton);
133             // make sure button stays aligned with the field and not with the optional field label when used
134             layout.setComponentAlignment(moveUpButton, Alignment.MIDDLE_CENTER);
135             layout.setComponentAlignment(moveDownButton, Alignment.MIDDLE_CENTER);
136         }
137 
138         // Delete Button
139         Button deleteButton = new Button(MagnoliaIcons.CLOSE);
140         deleteButton.addStyleNames(ResurfaceTheme.BUTTON_ICON, "inline");
141         deleteButton.setDescription(buttonCaptionRemove);
142         //FIXME: Add action to move delete
143 
144         layout.addComponents(deleteButton);
145         layout.setComponentAlignment(deleteButton, Alignment.MIDDLE_CENTER);
146 
147         return layout;
148     }
149 
150     public boolean isOrderable() {
151         return isOrderable;
152     }
153 
154     @Override
155     protected void doSetValue(Collection<T> value) {
156 
157     }
158 
159     @Override
160     public Collection<T> getValue() {
161         return null;
162     }
163 }