Project

General

Profile

Download (4.04 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.toolbar;
10

    
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Qualifier;
13
import org.springframework.context.ApplicationEventPublisher;
14
import org.springframework.context.event.EventListener;
15

    
16
import com.vaadin.server.FontAwesome;
17
import com.vaadin.spring.annotation.SpringComponent;
18
import com.vaadin.spring.annotation.UIScope;
19
import com.vaadin.ui.Alignment;
20
import com.vaadin.ui.Button;
21
import com.vaadin.ui.CssLayout;
22
import com.vaadin.ui.HorizontalLayout;
23
import com.vaadin.ui.themes.ValoTheme;
24

    
25
import eu.etaxonomy.cdm.api.application.CdmRepository;
26
import eu.etaxonomy.cdm.service.CdmUserHelper;
27
import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
28
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
29
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
30

    
31
/**
32
 *
33
 * @author a.kohlbecker
34
 * @since Sep 28, 2017
35
 *
36
 */
37
@SpringComponent("registrationToolbar")
38
@UIScope
39
public class RegistrationToolbar extends HorizontalLayout implements Toolbar {
40

    
41
    private static final long serialVersionUID = 2594781255088231474L;
42

    
43
    @Autowired
44
    protected ApplicationEventPublisher eventBus;
45

    
46
    @Autowired
47
    @Qualifier("cdmRepository")
48
    private CdmRepository repo;
49

    
50
    @Autowired
51
    protected NavigationManager navigationManager;
52

    
53
    @Autowired
54
    CdmUserHelper userHelper;
55

    
56
    CssLayout buttonGroup = new CssLayout();
57
    Button messageButton;
58
    Button loginButton;
59
    Button logoutButton;
60
    Button userButton;
61

    
62
    public RegistrationToolbar() {
63
    }
64

    
65
    @Override
66
    public void initialize(){
67

    
68
        setWidth("100%");
69
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
70
        messageButton = new Button(FontAwesome.COMMENT);
71
        loginButton = new Button("login");
72
        userButton = new Button(FontAwesome.USER);
73
        logoutButton = new Button("logout");
74

    
75
        messageButton.setEnabled(false);
76
        logoutButton.addClickListener(e -> performLogut());
77
        loginButton.addClickListener(e -> performLogin());
78
        buttonGroup.addComponent(messageButton);
79
        buttonGroup.addComponent(loginButton);
80
        buttonGroup.addComponent(logoutButton);
81
        buttonGroup.addComponent(userButton);
82
        addComponent(buttonGroup);
83
        setComponentAlignment(buttonGroup, Alignment.MIDDLE_RIGHT);
84
        updateAuthenticationButtons();
85
    }
86

    
87

    
88
    @EventListener
89
    public void onAuthenticationSuccessEvent(AuthenticationSuccessEvent event){
90
        boolean isInitialized = userButton != null;
91
        // The RegistrationToolbar is being initialize even if not needed only because it is a EventListener
92
        // which causes Spring to initialize it.
93
        // TODO After switching to an other event bus this check can be removed
94
        if(isInitialized){
95
            updateAuthenticationButtons();
96
        }
97
    }
98

    
99
    /**
100
     * @param event
101
     */
102
    protected void updateAuthenticationButtons() {
103

    
104
        if(userHelper.userIsAutheticated() && !userHelper.userIsAnnonymous()){
105
            userButton.setCaption(userHelper.userName());
106
            userButton.setVisible(true);
107
            messageButton.setVisible(true);
108
            logoutButton.setVisible(true);
109
            loginButton.setVisible(false);
110

    
111
        } else {
112
            userButton.setCaption(null);
113
            userButton.setVisible(false);
114
            messageButton.setVisible(false);
115
            logoutButton.setVisible(false);
116
            loginButton.setVisible(true);
117
        }
118
    }
119

    
120
    /**
121
     * @return
122
     */
123
    private void performLogin() {
124
        eventBus.publishEvent(new NavigationEvent("login", navigationManager.getCurrentViewName()));
125
    }
126

    
127

    
128
    private void performLogut() {
129

    
130
        userHelper.logout();
131
        updateAuthenticationButtons();
132
        navigationManager.reloadCurrentView();
133
    }
134

    
135
}
(1-1/2)