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