View Javadoc

1   /**
2    * This file Copyright (c) 2011-2014 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.form.field.factory;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.ui.api.app.AppController;
38  import info.magnolia.ui.api.app.ChooseDialogCallback;
39  import info.magnolia.ui.api.context.UiContext;
40  import info.magnolia.ui.form.field.LinkField;
41  import info.magnolia.ui.form.field.definition.FieldDefinition;
42  import info.magnolia.ui.form.field.definition.LinkFieldDefinition;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
45  
46  import javax.inject.Inject;
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  
50  import org.apache.commons.lang3.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  import com.vaadin.data.Item;
55  import com.vaadin.ui.Button;
56  import com.vaadin.ui.Button.ClickEvent;
57  import com.vaadin.ui.Field;
58  
59  /**
60   * Creates and initializes a LinkField field based on a field definition.
61   *
62   * @param <D> definition type
63   */
64  public class LinkFieldFactory<D extends FieldDefinition> extends AbstractFieldFactory<LinkFieldDefinition, String> {
65      private static final Logger log = LoggerFactory.getLogger(LinkFieldFactory.class);
66      public static final String PATH_PROPERTY_NAME = "transientPathProperty";
67  
68      private LinkField linkField;
69  
70      private final AppController appController;
71      private final UiContext uiContext;
72      private ComponentProvider componentProvider;
73  
74      @Inject
75      public LinkFieldFactory(LinkFieldDefinition definition, Item relatedFieldItem, AppController appController, UiContext uiContext, ComponentProvider componentProvider) {
76          super(definition, relatedFieldItem);
77          this.appController = appController;
78          this.uiContext = uiContext;
79          this.componentProvider = componentProvider;
80      }
81  
82      @Override
83      public void setComponentProvider(ComponentProvider componentProvider) {
84          super.setComponentProvider(componentProvider);
85          this.componentProvider = componentProvider;
86      }
87  
88      @Override
89      protected Field<String> createFieldComponent() {
90          linkField = new LinkField(definition, componentProvider);
91          // Set Caption
92          linkField.setButtonCaptionNew(getMessage(definition.getButtonSelectNewLabel()));
93          linkField.setButtonCaptionOther(getMessage(definition.getButtonSelectOtherLabel()));
94          // Add a callback listener on the select button
95          linkField.getSelectButton().addClickListener(createButtonClickListener());
96          return linkField;
97      }
98  
99      /**
100      * Create the Button click Listener. On click: Create a Dialog and
101      * Initialize callback handling.
102      */
103     private Button.ClickListener createButtonClickListener() {
104         return new Button.ClickListener() {
105             @Override
106             public void buttonClick(ClickEvent event) {
107                 ChooseDialogCallback callback = createChooseDialogCallback();
108                 String value = linkField.getTextField().getValue();
109                 if (StringUtils.isNotBlank(definition.getTargetTreeRootPath())) {
110                     appController.openChooseDialog(definition.getAppName(), uiContext, definition.getTargetTreeRootPath(), value, callback);
111                 } else {
112                     appController.openChooseDialog(definition.getAppName(), uiContext, value, callback);
113                 }
114             }
115         };
116     }
117 
118     /**
119      * @return specific {@link ChooseDialogCallback} implementation used to process the selected value.
120      */
121     protected ChooseDialogCallback createChooseDialogCallback() {
122         return new ChooseDialogCallback() {
123 
124             @Override
125             public void onItemChosen(String actionName, final Object chosenValue) {
126                 String propertyName = definition.getTargetPropertyToPopulate();
127                 String newValue = null;
128                 if (chosenValue instanceof JcrItemId) {
129                     try {
130                         javax.jcr.Item jcrItem = JcrItemUtil.getJcrItem((JcrItemId) chosenValue);
131                         if (jcrItem.isNode()) {
132                             final Node selected = (Node) jcrItem;
133                             boolean isPropertyExisting = StringUtils.isNotBlank(propertyName) && selected.hasProperty(propertyName);
134                             newValue = isPropertyExisting ? selected.getProperty(propertyName).getString() : selected.getPath();
135                         }
136                     } catch (RepositoryException e) {
137                         log.error("Not able to access the configured property. Value will not be set.", e);
138                     }
139                 }
140                 linkField.setValue(newValue);
141             }
142 
143             @Override
144             public void onCancel() {
145             }
146         };
147     }
148 
149 }