1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
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 }