View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.workbench.tree;
35  
36  import com.vaadin.event.FieldEvents.BlurListener;
37  import com.vaadin.ui.Component;
38  import com.vaadin.v7.data.Container;
39  import com.vaadin.v7.data.Property;
40  import com.vaadin.v7.data.Property.ValueChangeListener;
41  import com.vaadin.v7.ui.AbstractTextField;
42  import com.vaadin.v7.ui.DefaultFieldFactory;
43  import com.vaadin.v7.ui.Field;
44  import com.vaadin.v7.ui.TableFieldFactory;
45  import com.vaadin.v7.ui.TextField;
46  
47  /**
48   * The InplaceEditingFieldFactory is responsible for creating input fields in table cells for inplace-editing.
49   * Compared to a standard Vaadin {@link TableFieldFactory}, this one only creates an input field in one cell at a time,
50   * i.e. corresponding to a single combination of itemId and container's propertyId.<br />
51   * <br/>
52   * It also expects a field {@link BlurListener} to be set as a hook to persist changes.
53   */
54  public class InplaceEditingFieldFactory implements TableFieldFactory {
55  
56      private Object editingItemId;
57      private Object editingPropertyId;
58  
59      private Field<?> field;
60      private BlurListener fieldBlurListener;
61  
62      /**
63       * @return the id of the item currently being edited
64       */
65      public Object getEditingItemId() {
66          return editingItemId;
67      }
68  
69      /**
70       * @return the id of the item's property currently being edited
71       */
72      public Object getEditingPropertyId() {
73          return editingPropertyId;
74      }
75  
76      public Field<?> getField() {
77          return field;
78      }
79  
80      /**
81       * Sets the item and property to edit.
82       */
83      public void setEditing(Object editingItemId, Object editingPropertyId) {
84          this.editingItemId = editingItemId;
85          this.editingPropertyId = editingPropertyId;
86          if (editingItemId == null || editingPropertyId == null) {
87              field = null;
88          }
89      }
90  
91      /**
92       * Sets the blur listener that should react when leaving the inplace-editing field.
93       */
94      public void setFieldBlurListener(BlurListener fieldBlurListener) {
95          this.fieldBlurListener = fieldBlurListener;
96      }
97  
98      @Override
99      public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) {
100         // add field only for selected row/column.
101         if (itemId.equals(editingItemId) && propertyId.equals(editingPropertyId)) {
102             Field<?> field = createFieldAndRegister(container, itemId, propertyId);
103             // register component on the table (only for partial updates)
104             // uiContext.registerComponent(field);
105             this.field = field;
106             return field;
107         }
108         return null;
109     }
110 
111     /**
112      * For partial updates to work, we need to perform a dry run to attach the component to the table beforehand,
113      * i.e. before it is actually requested at paint phase by the table.
114      */
115     private Field<?> createFieldAndRegister(Container container, Object itemId, Object propertyId) {
116 
117         Property<?> containerProperty = container.getContainerProperty(itemId, propertyId);
118         // the previous call can return null, i.e. when clicking on an empty cell of a node row (i.e. /config/server and then the "value" cell)
119         if (containerProperty == null) {
120             return null;
121         }
122 
123         Class<?> type = containerProperty.getType();
124         Field<?> field = createFieldByPropertyType(type);
125         if (field != null) {
126             field.setCaption(DefaultFieldFactory.createCaptionByPropertyId(propertyId));
127             field.setSizeFull();
128         }
129 
130         // set TextField listeners
131         if (field instanceof AbstractTextField) {
132             final AbstractTextField tf = (AbstractTextField) field;
133             tf.addBlurListener(fieldBlurListener);
134             tf.focus();
135 
136             tf.addValueChangeListener(new ValueChangeListener() {
137 
138                 @Override
139                 public void valueChange(Property.ValueChangeEvent event) {
140                     final Object text = event.getProperty().getValue();
141                     if (text instanceof String) {
142                         tf.selectAll();
143                     }
144                     tf.removeValueChangeListener(this);
145                 }
146             });
147         }
148         return field;
149     }
150 
151     private Field<?> createFieldByPropertyType(Class<?> type) {
152         if (type == null) {
153             return null;
154         }
155         Field<?> field = new TextField();
156         return field;
157     }
158 
159 }