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.list;
35  
36  import static java.util.stream.Collectors.toList;
37  
38  import info.magnolia.event.EventBus;
39  import info.magnolia.objectfactory.ComponentProvider;
40  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
41  import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnector;
42  import info.magnolia.ui.workbench.AbstractContentPresenter;
43  import info.magnolia.ui.workbench.column.definition.ColumnDefinition;
44  import info.magnolia.ui.workbench.container.AbstractJcrContainer;
45  import info.magnolia.ui.workbench.container.Refreshable;
46  import info.magnolia.ui.workbench.definition.ContentPresenterDefinition;
47  import info.magnolia.ui.workbench.definition.WorkbenchDefinition;
48  
49  import java.util.Iterator;
50  import java.util.List;
51  
52  import javax.inject.Inject;
53  
54  import com.vaadin.v7.data.Container;
55  
56  /**
57   * The ListPresenter is responsible for creating, configuring and updating a list of items according to the workbench definition.
58   */
59  public class ListPresenter extends AbstractContentPresenter implements ListView.Listener {
60  
61      protected final ListView view;
62  
63      @Inject
64      public ListPresenter(final ListView view, final ComponentProvider componentProvider) {
65          super(componentProvider);
66          this.view = view;
67      }
68  
69      @Override
70      public ListView start(WorkbenchDefinition workbenchDefinition, EventBus eventBus, String viewTypeName, ContentConnector contentConnector) {
71          super.start(workbenchDefinition, eventBus, viewTypeName, contentConnector);
72          this.container = initializeContainer();
73          view.setListener(this);
74          view.setContainer(container);
75          view.clearColumns();
76  
77          // build columns
78          Iterator<ColumnDefinition> it = getColumnsIterator();
79  
80          while (it.hasNext()) {
81              ColumnDefinition column = it.next();
82  
83              if (workbenchDefinition.isDialogWorkbench() && !column.isDisplayInChooseDialog()) {
84                  continue;
85              }
86  
87              String propertyId = column.getPropertyName();
88              String title = column.getLabel();
89  
90              if (column.getWidth() > 0) {
91                  view.addColumn(propertyId, title, column.getWidth());
92              } else if (column.getExpandRatio() > 0) {
93                  view.addColumn(propertyId, title, column.getExpandRatio());
94              } else {
95                  view.addColumn(propertyId, column.getLabel());
96              }
97  
98              if (column.getFormatterClass() != null) {
99                  view.setColumnFormatter(propertyId, getComponentProvider().newInstance(column.getFormatterClass(), column));
100             }
101         }
102 
103         return view;
104     }
105 
106     @Override
107     public void select(List<Object> itemIds) {
108         List<Object> itemIdsExcludingDefault = itemIds.stream()
109                 .filter(itemId -> !itemId.equals(contentConnector.getDefaultItemId()))
110                 .collect(toList());
111         view.select(itemIdsExcludingDefault);
112     }
113 
114     @Override
115     public void refresh() {
116         super.refresh();
117         // This will update the row count and display the newly created items.
118         if (container instanceof Refreshable) {
119             ((Refreshable) container).refresh();
120         }
121     }
122 
123     @Override
124     protected Container initializeContainer() {
125         Container container = createContainer();
126         configureContainer(getPresenterDefinition(), container);
127         return container;
128     }
129 
130     protected Container createContainer() {
131         return new FlatJcrContainer(((JcrContentConnector) contentConnector).getContentConnectorDefinition());
132     }
133 
134     protected void configureContainer(ContentPresenterDefinition presenterDefinition, Container container) {
135         for (ColumnDefinition column : presenterDefinition.getColumns()) {
136             String propertyId = column.getPropertyName();
137             container.addContainerProperty(propertyId, column.getType(), null);
138             // TODO Rely on Container.Sortable instead.
139             // Containers thus need to be created with their propertyIds and sortablePropertyIds (Container.Sortable only exposes #getSortableContainerPropertyIds())
140             if (container instanceof AbstractJcrContainer) {
141                 ((AbstractJcrContainer) container).addSortableProperty(propertyId);
142             }
143         }
144     }
145 }