1
|
package eu.etaxonomy.cdm.vaadin.component.distributionStatus;
|
2
|
|
3
|
import java.io.Serializable;
|
4
|
|
5
|
import javax.annotation.PostConstruct;
|
6
|
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.beans.factory.annotation.Qualifier;
|
9
|
import org.vaadin.spring.events.EventBus;
|
10
|
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
|
11
|
|
12
|
import com.vaadin.server.FontAwesome;
|
13
|
import com.vaadin.server.ThemeResource;
|
14
|
import com.vaadin.spring.annotation.SpringComponent;
|
15
|
import com.vaadin.spring.annotation.ViewScope;
|
16
|
import com.vaadin.ui.Alignment;
|
17
|
import com.vaadin.ui.Button;
|
18
|
import com.vaadin.ui.HorizontalLayout;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
21
|
import eu.etaxonomy.cdm.i18n.Messages;
|
22
|
import eu.etaxonomy.cdm.service.CdmUserHelper;
|
23
|
import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
|
24
|
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
|
25
|
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
|
26
|
|
27
|
@SpringComponent("distributionToolbar")
|
28
|
@ViewScope
|
29
|
public class DistributionToolbar extends HorizontalLayout implements Serializable{
|
30
|
|
31
|
/**
|
32
|
* automatically generated ID
|
33
|
*/
|
34
|
private static final long serialVersionUID = 5344340511582993289L;
|
35
|
|
36
|
private EventBus.UIEventBus uiEventBus;
|
37
|
|
38
|
@Autowired
|
39
|
private final void setViewEventBus(EventBus.UIEventBus viewEventBus){
|
40
|
this.uiEventBus = viewEventBus;
|
41
|
viewEventBus.subscribe(AuthenticationSuccessEvent.class);
|
42
|
}
|
43
|
|
44
|
@Autowired
|
45
|
@Qualifier("cdmRepository")
|
46
|
private CdmRepository repo;
|
47
|
|
48
|
@Autowired
|
49
|
private NavigationManager navigationManager;
|
50
|
|
51
|
@Autowired
|
52
|
private CdmUserHelper userHelper;
|
53
|
|
54
|
private final Button loginButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_LOGIN));
|
55
|
|
56
|
private final Button logoutButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_LOGOUT));
|
57
|
|
58
|
private final Button userButton = new Button(FontAwesome.USER);
|
59
|
|
60
|
private final Button editButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_EDIT));
|
61
|
|
62
|
private final Button saveButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_SAVE));
|
63
|
|
64
|
private final Button detailButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_DETAIL));
|
65
|
|
66
|
private final Button distributionSettingsButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_AREAS_AND_TAXA));
|
67
|
|
68
|
private final Button settingsButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_STATUS));
|
69
|
|
70
|
private final Button helpButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_HELP));
|
71
|
|
72
|
// private final Authentication authentication;
|
73
|
// private ExcelExporter exporter = new ExcelExporter();
|
74
|
|
75
|
@PostConstruct
|
76
|
public void init() {
|
77
|
setMargin(true);
|
78
|
setSpacing(true);
|
79
|
setStyleName("toolbar"); //$NON-NLS-1$
|
80
|
setWidth("100%"); //$NON-NLS-1$
|
81
|
setHeight("75px"); //$NON-NLS-1$
|
82
|
|
83
|
// exporter.setCaption("Export");
|
84
|
// exporter.setIcon(new ThemeResource("icons/32/document-xsl.png"));
|
85
|
loginButton.addClickListener(e -> performLogin());
|
86
|
logoutButton.addClickListener(e -> performLogout());
|
87
|
saveButton.setIcon(new ThemeResource("icons/32/document-save.png")); //$NON-NLS-1$
|
88
|
editButton.setIcon(new ThemeResource("icons/32/document-edit.png")); //$NON-NLS-1$
|
89
|
detailButton.setIcon(new ThemeResource("icons/32/document-txt.png")); //$NON-NLS-1$
|
90
|
settingsButton.setIcon(new ThemeResource("icons/32/settings_1.png")); //$NON-NLS-1$
|
91
|
distributionSettingsButton.setIcon(new ThemeResource("icons/32/settings_1.png")); //$NON-NLS-1$
|
92
|
|
93
|
HorizontalLayout leftLayout = new HorizontalLayout();
|
94
|
leftLayout.addComponent(distributionSettingsButton);
|
95
|
leftLayout.addComponent(settingsButton);
|
96
|
leftLayout.addComponent(detailButton);
|
97
|
|
98
|
HorizontalLayout rightLayout = new HorizontalLayout();
|
99
|
rightLayout.addComponent(helpButton);
|
100
|
rightLayout.addComponent(loginButton);
|
101
|
rightLayout.addComponent(logoutButton);
|
102
|
rightLayout.addComponent(userButton);
|
103
|
|
104
|
addComponent(leftLayout);
|
105
|
setComponentAlignment(leftLayout, Alignment.MIDDLE_LEFT);
|
106
|
addComponent(rightLayout);
|
107
|
setComponentAlignment(rightLayout, Alignment.MIDDLE_RIGHT);
|
108
|
setExpandRatio(rightLayout, 1);
|
109
|
updateAuthenticationButtons();
|
110
|
}
|
111
|
|
112
|
@EventBusListenerMethod
|
113
|
public void onAuthenticationSuccessEvent(org.vaadin.spring.events.Event<AuthenticationSuccessEvent> event){
|
114
|
boolean isInitialized = userButton != null;
|
115
|
// The RegistrationToolbar is being initialize even if not needed only because it is a EventListener
|
116
|
// which causes Spring to initialize it.
|
117
|
// TODO After switching to an other event bus this check can be removed
|
118
|
if(isInitialized){
|
119
|
updateAuthenticationButtons();
|
120
|
}
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* @param event
|
125
|
*/
|
126
|
protected void updateAuthenticationButtons() {
|
127
|
if(userHelper.userIsAutheticated() && !userHelper.userIsAnnonymous()){
|
128
|
userButton.setCaption(userHelper.userName());
|
129
|
userButton.setVisible(true);
|
130
|
logoutButton.setVisible(true);
|
131
|
loginButton.setVisible(false);
|
132
|
saveButton.setVisible(true);
|
133
|
editButton.setVisible(true);
|
134
|
detailButton.setVisible(true);
|
135
|
settingsButton.setVisible(true);
|
136
|
distributionSettingsButton.setVisible(true);
|
137
|
} else {
|
138
|
userButton.setCaption(null);
|
139
|
userButton.setVisible(false);
|
140
|
logoutButton.setVisible(false);
|
141
|
loginButton.setVisible(true);
|
142
|
saveButton.setVisible(false);
|
143
|
editButton.setVisible(false);
|
144
|
detailButton.setVisible(false);
|
145
|
settingsButton.setVisible(false);
|
146
|
distributionSettingsButton.setVisible(false);
|
147
|
}
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* @return
|
152
|
*/
|
153
|
private void performLogin() {
|
154
|
uiEventBus.publish(this, new NavigationEvent("login", navigationManager.getCurrentViewName())); //$NON-NLS-1$
|
155
|
}
|
156
|
|
157
|
|
158
|
private void performLogout() {
|
159
|
userHelper.logout();
|
160
|
updateAuthenticationButtons();
|
161
|
navigationManager.reloadCurrentView();
|
162
|
}
|
163
|
|
164
|
public Button getSettingsButton(){
|
165
|
return settingsButton;
|
166
|
}
|
167
|
|
168
|
public Button getDistributionSettingsButton() {
|
169
|
return distributionSettingsButton;
|
170
|
}
|
171
|
|
172
|
public Button getEditButton() {
|
173
|
return editButton;
|
174
|
}
|
175
|
|
176
|
public Button getSaveButton() {
|
177
|
return saveButton;
|
178
|
}
|
179
|
|
180
|
public Button getDetailButton() {
|
181
|
return detailButton;
|
182
|
}
|
183
|
|
184
|
public Button getHelpButton() {
|
185
|
return helpButton;
|
186
|
}
|
187
|
}
|