View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.contentapp.field;
35  
36  import info.magnolia.ui.workbench.WorkbenchView;
37  
38  import com.vaadin.ui.Component;
39  import com.vaadin.v7.data.Property;
40  import com.vaadin.v7.ui.CustomField;
41  import com.vaadin.v7.ui.TextField;
42  import com.vaadin.v7.ui.VerticalLayout;
43  
44  /**
45   * A base custom field allowing to display a {@link WorkbenchView} and a TextField.
46   * <ul>
47   * <li>Text field can be hidden or placed on top or button.
48   * <li>This field is mainly used to perform some selection in a list and to put the selected value into the text input field.
49   * </ul>
50   */
51  public class TextAndContentViewField extends CustomField<String> {
52  
53      private WorkbenchView contentView;
54  
55      private VerticalLayout layout;
56  
57      private TextField textField;
58  
59      private boolean displayTextFieldOnTop;
60  
61      private boolean isTextFieldVisible;
62  
63      public TextAndContentViewField(boolean displayTextField, boolean displayTextFieldOnTop) {
64          this.displayTextFieldOnTop = displayTextFieldOnTop;
65          this.isTextFieldVisible = displayTextField;
66          this.textField = new TextField();
67          this.layout = new VerticalLayout();
68      }
69  
70      @Override
71      protected Component initContent() {
72          addTextFieldToLayout(isTextFieldVisible);
73          addStyleName("text-and-content");
74          return layout;
75      }
76  
77      /**
78       * Set textField visible or not.
79       */
80      private void addTextFieldToLayout(boolean displayTextField) {
81          if (!displayTextField) {
82              textField.setVisible(false);
83              return;
84          }
85          layout.addComponent(textField);
86      }
87  
88      public TextField getTextField() {
89          return this.textField;
90      }
91  
92      public WorkbenchView getContentView() {
93          return this.contentView;
94      }
95  
96      /**
97       * Set contentView, and Add it to the Layout.
98       * Based on displayTextFieldOnTop, put it before or after the TextField.
99       */
100     public void setContentView(WorkbenchView contentView) {
101         if (this.contentView != null) {
102             layout.removeComponent(this.contentView.asVaadinComponent());
103         }
104         this.contentView = contentView;
105         if (!displayTextFieldOnTop) {
106             layout.addComponentAsFirst(this.contentView.asVaadinComponent());
107         } else {
108             layout.addComponent(this.contentView.asVaadinComponent());
109         }
110     }
111 
112     @Override
113     protected void setInternalValue(String newValue) {
114         super.setInternalValue(newValue);
115         textField.setValue(newValue);
116     }
117 
118     @Override
119     public void setReadOnly(boolean readOnly) {
120         super.setReadOnly(readOnly);
121         textField.setReadOnly(readOnly);
122     }
123 
124     @Override
125     @SuppressWarnings("rawtypes")
126     public void setPropertyDataSource(Property newDataSource) {
127         textField.setPropertyDataSource(newDataSource);
128     }
129 
130     @Override
131     @SuppressWarnings("rawtypes")
132     public Property getPropertyDataSource() {
133         return textField.getPropertyDataSource();
134     }
135 
136     @Override
137     public Class<String> getType() {
138         return String.class;
139     }
140 
141     @Override
142     public void setCaption(String caption) {
143         super.setCaption(null);
144     }
145 }