View Javadoc
1   /**
2    * This file Copyright (c) 2013-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 info.magnolia.event.EventBus;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.ui.api.context.UiContext;
40  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
41  import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnector;
42  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
43  import info.magnolia.ui.workbench.column.definition.ColumnDefinition;
44  import info.magnolia.ui.workbench.definition.ContentPresenterDefinition;
45  import info.magnolia.ui.workbench.definition.WorkbenchDefinition;
46  import info.magnolia.ui.workbench.event.ActionEvent;
47  import info.magnolia.ui.workbench.list.ListPresenter;
48  import info.magnolia.ui.workbench.tree.drop.DropConstraint;
49  
50  import java.util.ArrayList;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  import javax.inject.Inject;
55  
56  import org.apache.commons.lang3.StringUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  import com.vaadin.event.dd.DragAndDropEvent;
61  import com.vaadin.event.dd.DropHandler;
62  import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
63  import com.vaadin.v7.data.Container;
64  import com.vaadin.v7.data.Container.ItemSetChangeEvent;
65  import com.vaadin.v7.data.Property;
66  
67  /**
68   * The TreePresenter is responsible for creating, configuring and updating a tree of items according to the workbench definition.
69   */
70  public class TreePresenter extends ListPresenter implements TreeView.Listener {
71  
72      private static final String SAVE_ACTION_NAME = "saveItemProperty";
73      private static final Logger log = LoggerFactory.getLogger(TreePresenter.class);
74      private final UiContext uiContext;
75      private final SimpleTranslator i18n;
76  
77      private boolean sortable = false;
78  
79      @Inject
80      public TreePresenter(TreeView view, ComponentProvider componentProvider, UiContext uiContext, SimpleTranslator i18n) {
81          super(view, componentProvider);
82          this.uiContext = uiContext;
83          this.i18n = i18n;
84      }
85  
86      /**
87       * @deprecated since 5.4.11/5.5.3. Use {@link #TreePresenter(TreeView, ComponentProvider, UiContext, SimpleTranslator)} instead.
88       */
89      @Deprecated
90      public TreePresenter(TreeView view, ComponentProvider componentProvider) {
91          this(view, componentProvider, componentProvider.getComponent(UiContext.class), componentProvider.getComponent(SimpleTranslator.class));
92      }
93  
94      @Override
95      public TreeView start(WorkbenchDefinition workbenchDefinition, EventBus eventBus, String viewTypeName, ContentConnector contentConnector) {
96          TreeView view = (TreeView) super.start(workbenchDefinition, eventBus, viewTypeName, contentConnector);
97          for (ContentPresenterDefinition definition : workbenchDefinition.getContentViews()) {
98              if (TreePresenterDefinition.VIEW_TYPE.equals(definition.getViewType()) && definition instanceof TreePresenterDefinition) {
99                  sortable = ((TreePresenterDefinition) definition).isSortable();
100                 break;
101             }
102         }
103         if (view instanceof TreeViewImpl) {
104             ((TreeViewImpl) view).setSortable(sortable);
105         }
106 
107         // inplace-editing
108         if (workbenchDefinition.isEditable()) {
109 
110             List<Object> editableColumns = new ArrayList<Object>();
111 
112             Iterator<ColumnDefinition> it = getColumnsIterator();
113             while (it.hasNext()) {
114                 ColumnDefinition column = it.next();
115 
116                 String propertyId = column.getPropertyName();
117                 if (column.isEditable()) {
118                     editableColumns.add(propertyId);
119                 }
120             }
121             view.setEditableColumns(editableColumns.toArray());
122             view.setEditable(true);
123         }
124 
125         boolean isSorted = sortable && StringUtils.isNotBlank(((JcrContentConnector) contentConnector).getContentConnectorDefinition().getDefaultOrder());
126         enableDragAndDrop(isSorted);
127 
128         return view;
129     }
130 
131     public void disableDragAndDrop() {
132         ((TreeView) view).setDragAndDropHandler(null);
133     }
134 
135     private void enableDragAndDrop(final boolean isSorted) {
136         // Drag and Drop
137         Class<? extends DropConstraint> dropConstraintClass = workbenchDefinition.getDropConstraintClass();
138         if (workbenchDefinition.isDragAndDrop() && dropConstraintClass != null) {
139             final DropConstraint constraint = getComponentProvider().newInstance(dropConstraintClass);
140             final MoveHandler moveHandler = getComponentProvider().newInstance(MoveHandler.class, view.asVaadinComponent(), constraint);
141             if (!isSorted) {
142                 ((TreeView) view).setDragAndDropHandler(moveHandler.asDropHandler());
143             } else {
144                 ((TreeView) view).setDragAndDropHandler(new DropHandler() {
145 
146                     @Override
147                     public void drop(DragAndDropEvent event) {
148                         uiContext.openNotification(MessageStyleTypeEnum.WARNING, true, i18n.translate("ui-contentapp.actions.dragAndDrop.sortedWarning"));
149                         moveHandler.asDropHandler().drop(event);
150                     }
151 
152                     @Override
153                     public AcceptCriterion getAcceptCriterion() {
154                         return moveHandler.asDropHandler().getAcceptCriterion();
155                     }
156                 });
157             }
158             log.debug("Set following drop container {} to the treeTable", dropConstraintClass.getName());
159         }
160     }
161 
162     private boolean isSortable() {
163         return sortable;
164     }
165 
166     // TREE VIEW LISTENER IMPL
167 
168     @Override
169     public void onItemEdited(Object itemId, Object propertyId, Property<?> propertyDataSource) {
170         if (itemId != null && propertyId != null) {
171             eventBus.fireEvent(new ActionEvent(SAVE_ACTION_NAME, itemId, propertyId, propertyDataSource));
172         }
173 
174         // Clear preOrder cache of itemIds in case node was renamed
175         refresh();
176     }
177 
178     @Override
179     public void expand(Object itemId) {
180         view.expand(itemId);
181     }
182 
183     @Override
184     protected Container.Hierarchical createContainer() {
185         HierarchicalJcrContainer container = new HierarchicalJcrContainer(((JcrContentConnector) contentConnector).getContentConnectorDefinition());
186         container.addItemSetChangeListener(new Container.ItemSetChangeListener() {
187             @Override
188             public void containerItemSetChange(ItemSetChangeEvent event) {
189                 if (event instanceof HierarchicalJcrContainer.ItemsSortedEvent) {
190                     enableDragAndDrop(isSortable());
191                 }
192             }
193         });
194         return container;
195     }
196 }