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;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
39  import info.magnolia.ui.workbench.event.SelectionChangedEvent;
40  
41  import java.util.Set;
42  
43  import javax.inject.Inject;
44  
45  import org.apache.commons.lang3.ObjectUtils;
46  
47  import com.vaadin.ui.Alignment;
48  import com.vaadin.v7.ui.HorizontalLayout;
49  import com.vaadin.v7.ui.Label;
50  
51  /**
52   * The browser features a status bar at the bottom with selected path and item count information.
53   */
54  public class WorkbenchStatusBarPresenter {
55  
56      private final StatusBarView view;
57  
58      private ContentConnector contentConnector;
59  
60      private EventBus eventBus;
61  
62      private final Label selectionLabel = new Label();
63  
64      private ContentPresenter activeContentPresenter;
65  
66      private boolean rootIsSelected;
67  
68      private SimpleTranslator i18n;
69  
70      @Inject
71      public WorkbenchStatusBarPresenter(StatusBarView view, ContentConnector contentConnector, SimpleTranslator i18n) {
72          this.view = view;
73          this.contentConnector = contentConnector;
74          this.i18n = i18n;
75      }
76  
77      private void bindHandlers() {
78          eventBus.addHandler(SelectionChangedEvent.class, new SelectionChangedEvent.Handler() {
79  
80              @Override
81              public void onSelectionChanged(SelectionChangedEvent event) {
82                  setSelectedItems(event.getItemIds());
83              }
84          });
85      }
86  
87      public StatusBarView start(EventBus eventBus, ContentPresenter activeContentPresenter) {
88  
89          this.eventBus = eventBus;
90          this.activeContentPresenter = activeContentPresenter;
91  
92          view.addComponent(selectionLabel, Alignment.TOP_LEFT);
93          ((HorizontalLayout) view).setExpandRatio(selectionLabel, 1);
94  
95          bindHandlers();
96  
97          refresh();
98  
99          return view;
100     }
101 
102     public void setSelectedItems(Set<Object> itemIds) {
103         if (!itemIds.isEmpty()) {
104             Object id = itemIds.iterator().next();
105             rootIsSelected = ObjectUtils.equals(id, contentConnector.getDefaultItemId());
106             setSelectedItem(id, itemIds.size());
107         } else {
108             rootIsSelected = true;
109             setSelectedItem(contentConnector.getDefaultItemId(), itemIds.size());
110         }
111     }
112 
113     public void setSelectedItem(Object itemId, int totalSelected) {
114         // selection might contain the configured root path (by default '/') but we don't want to count that
115         if (rootIsSelected && totalSelected > 0) {
116             totalSelected--;
117         }
118         if (totalSelected == 1) {
119             String newValue = contentConnector.getItemUrlFragment(itemId);
120             selectionLabel.setValue(newValue);
121             selectionLabel.setDescription(newValue);
122         } else {
123             String selected = i18n.translate("ui-contentapp.statusbar.selected", totalSelected);
124             selectionLabel.setValue(selected);
125             selectionLabel.setDescription(selected);
126         }
127     }
128 
129     public void refresh() {
130         // active presenter can be null initially when there are multiple browser subapps
131         if (activeContentPresenter == null) {
132             return;
133         }
134 
135         int selected = activeContentPresenter.getSelectedItemIds().size();
136 
137         if (selected == 1) {
138             Object selectedItemId = activeContentPresenter.getSelectedItemIds().get(0);
139             if (!contentConnector.canHandleItem(selectedItemId)) {
140                 setSelectedItem(null, 0);
141             } else {
142                 setSelectedItem(selectedItemId, selected);
143             }
144         } else {
145             setSelectedItem(null, selected);
146         }
147     }
148 
149     public void setActivePresenter(ContentPresenter activePresenter) {
150         this.activeContentPresenter = activePresenter;
151     }
152 
153 }