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