1
|
package eu.etaxonomy.cdm.vaadin.component;
|
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.springframework.context.ApplicationEventPublisher;
|
10
|
import org.springframework.context.event.EventListener;
|
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.i10n.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
|
@Autowired
|
37
|
protected ApplicationEventPublisher eventBus;
|
38
|
|
39
|
@Autowired
|
40
|
@Qualifier("cdmRepository")
|
41
|
private CdmRepository repo;
|
42
|
|
43
|
@Autowired
|
44
|
private NavigationManager navigationManager;
|
45
|
|
46
|
@Autowired
|
47
|
private CdmUserHelper userHelper;
|
48
|
|
49
|
private final Button loginButton = new Button(Messages.DistributionToolbar_LOGIN);
|
50
|
|
51
|
private final Button logoutButton = new Button(Messages.DistributionToolbar_LOGOUT);
|
52
|
|
53
|
private final Button userButton = new Button(FontAwesome.USER);
|
54
|
|
55
|
private final Button editButton = new Button(Messages.DistributionToolbar_EDIT);
|
56
|
|
57
|
private final Button saveButton = new Button(Messages.DistributionToolbar_SAVE);
|
58
|
|
59
|
private final Button detailButton = new Button(Messages.DistributionToolbar_DETAIL);
|
60
|
|
61
|
private final Button distributionSettingsButton = new Button(Messages.DistributionToolbar_AREAS_AND_TAXA);
|
62
|
|
63
|
private final Button settingsButton = new Button(Messages.DistributionToolbar_STATUS);
|
64
|
|
65
|
// private final Authentication authentication;
|
66
|
// private ExcelExporter exporter = new ExcelExporter();
|
67
|
|
68
|
@PostConstruct
|
69
|
public void init() {
|
70
|
setMargin(true);
|
71
|
setSpacing(true);
|
72
|
setStyleName("toolbar"); //$NON-NLS-1$
|
73
|
setWidth("100%"); //$NON-NLS-1$
|
74
|
setHeight("75px"); //$NON-NLS-1$
|
75
|
|
76
|
// exporter.setCaption("Export");
|
77
|
// exporter.setIcon(new ThemeResource("icons/32/document-xsl.png"));
|
78
|
loginButton.addClickListener(e -> performLogin());
|
79
|
logoutButton.addClickListener(e -> performLogout());
|
80
|
saveButton.setIcon(new ThemeResource("icons/32/document-save.png")); //$NON-NLS-1$
|
81
|
editButton.setIcon(new ThemeResource("icons/32/document-edit.png")); //$NON-NLS-1$
|
82
|
detailButton.setIcon(new ThemeResource("icons/32/document-txt.png")); //$NON-NLS-1$
|
83
|
settingsButton.setIcon(new ThemeResource("icons/32/settings_1.png")); //$NON-NLS-1$
|
84
|
distributionSettingsButton.setIcon(new ThemeResource("icons/32/settings_1.png")); //$NON-NLS-1$
|
85
|
|
86
|
HorizontalLayout leftLayout = new HorizontalLayout();
|
87
|
leftLayout.addComponent(detailButton);
|
88
|
leftLayout.addComponent(settingsButton);
|
89
|
leftLayout.addComponent(distributionSettingsButton);
|
90
|
|
91
|
HorizontalLayout rightLayout = new HorizontalLayout();
|
92
|
rightLayout.addComponent(loginButton);
|
93
|
rightLayout.addComponent(logoutButton);
|
94
|
rightLayout.addComponent(userButton);
|
95
|
|
96
|
addComponent(leftLayout);
|
97
|
setComponentAlignment(leftLayout, Alignment.MIDDLE_LEFT);
|
98
|
addComponent(rightLayout);
|
99
|
setComponentAlignment(rightLayout, Alignment.MIDDLE_RIGHT);
|
100
|
setExpandRatio(rightLayout, 1);
|
101
|
updateAuthenticationButtons();
|
102
|
}
|
103
|
|
104
|
@EventListener
|
105
|
public void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event){
|
106
|
boolean isInitialized = userButton != null;
|
107
|
// The RegistrationToolbar is being initialize even if not needed only because it is a EventListener
|
108
|
// which causes Spring to initialize it.
|
109
|
// TODO After switching to an other event bus this check can be removed
|
110
|
if(isInitialized){
|
111
|
updateAuthenticationButtons();
|
112
|
}
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* @param event
|
117
|
*/
|
118
|
protected void updateAuthenticationButtons() {
|
119
|
if(userHelper.userIsAutheticated() && !userHelper.userIsAnnonymous()){
|
120
|
userButton.setCaption(userHelper.userName());
|
121
|
userButton.setVisible(true);
|
122
|
logoutButton.setVisible(true);
|
123
|
loginButton.setVisible(false);
|
124
|
saveButton.setVisible(true);
|
125
|
editButton.setVisible(true);
|
126
|
detailButton.setVisible(true);
|
127
|
settingsButton.setVisible(true);
|
128
|
distributionSettingsButton.setVisible(true);
|
129
|
} else {
|
130
|
userButton.setCaption(null);
|
131
|
userButton.setVisible(false);
|
132
|
logoutButton.setVisible(false);
|
133
|
loginButton.setVisible(true);
|
134
|
saveButton.setVisible(false);
|
135
|
editButton.setVisible(false);
|
136
|
detailButton.setVisible(false);
|
137
|
settingsButton.setVisible(false);
|
138
|
distributionSettingsButton.setVisible(false);
|
139
|
}
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* @return
|
144
|
*/
|
145
|
private void performLogin() {
|
146
|
eventBus.publishEvent(new NavigationEvent("login", navigationManager.getCurrentViewName())); //$NON-NLS-1$
|
147
|
}
|
148
|
|
149
|
|
150
|
private void performLogout() {
|
151
|
userHelper.logout();
|
152
|
updateAuthenticationButtons();
|
153
|
navigationManager.reloadCurrentView();
|
154
|
}
|
155
|
|
156
|
public Button getSettingsButton(){
|
157
|
return settingsButton;
|
158
|
}
|
159
|
|
160
|
public Button getDistributionSettingsButton() {
|
161
|
return distributionSettingsButton;
|
162
|
}
|
163
|
|
164
|
public Button getEditButton() {
|
165
|
return editButton;
|
166
|
}
|
167
|
|
168
|
public Button getSaveButton() {
|
169
|
return saveButton;
|
170
|
}
|
171
|
|
172
|
public Button getDetailButton() {
|
173
|
return detailButton;
|
174
|
}
|
175
|
}
|