View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.workbench.AbstractContentPresenter;
39  import info.magnolia.ui.workbench.column.definition.ColumnDefinition;
40  import info.magnolia.ui.workbench.container.AbstractJcrContainer;
41  import info.magnolia.ui.workbench.definition.WorkbenchDefinition;
42  
43  import java.util.Iterator;
44  import java.util.List;
45  
46  import javax.inject.Inject;
47  
48  import com.vaadin.event.Action.Container;
49  
50  /**
51   * The ListPresenter is responsible for creating, configuring and updating a list of items according to the workbench definition.
52   */
53  public class ListPresenter extends AbstractContentPresenter implements ListView.Listener {
54  
55      protected final ListView view;
56  
57      protected AbstractJcrContainer container;
58  
59      @Inject
60      public ListPresenter(final ListView view, final ComponentProvider componentProvider) {
61          super(componentProvider);
62          this.view = view;
63      }
64  
65      @Override
66      public ListView start(WorkbenchDefinition workbench, EventBus eventBus, String viewTypeName, Container shortcutActionManager) {
67          super.start(workbench, eventBus, viewTypeName, shortcutActionManager);
68  
69          this.container = createContainer(workbench);
70          view.setListener(this);
71          view.setContainer(container);
72  
73          // build columns
74          Iterator<ColumnDefinition> it = getColumnsIterator();
75  
76          while (it.hasNext()) {
77              ColumnDefinition column = it.next();
78  
79              String propertyId = column.getPropertyName() != null ? column.getPropertyName() : column.getName();
80              String title = column.getLabel();
81              container.addContainerProperty(propertyId, column.getType(), null);
82  
83              if (column.getWidth() > 0) {
84                  view.addColumn(propertyId, title, column.getWidth());
85              } else if (column.getExpandRatio() > 0) {
86                  view.addColumn(propertyId, title, column.getExpandRatio());
87              } else {
88                  view.addColumn(propertyId, column.getLabel());
89              }
90  
91              if (column.getFormatterClass() != null) {
92                  view.setColumnFormatter(propertyId, getComponentProvider().newInstance(column.getFormatterClass(), column));
93              }
94  
95              if (column.isSortable()) {
96                  container.addSortableProperty(propertyId);
97              }
98          }
99  
100         return view;
101     }
102 
103     @Override
104     public void setSelectedItemIds(List<String> itemIds) {
105         super.setSelectedItemIds(itemIds);
106     }
107 
108     @Override
109     public void select(List<String> itemIds) {
110         view.select(itemIds);
111     }
112 
113     @Override
114     public void refresh() {
115         // This will update the row count and display the newly created items.
116         container.refresh();
117         container.fireItemSetChange();
118     }
119 
120     protected AbstractJcrContainer createContainer(WorkbenchDefinition workbench) {
121         return new FlatJcrContainer(workbench);
122     }
123 
124     @Override
125     public AbstractJcrContainer getContainer() {
126         return container;
127     }
128 
129 
130 }