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