Project

General

Profile

Download (3.95 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.vaadin.spring.events.EventBus;
14
import org.vaadin.spring.events.EventBus.UIEventBus;
15
import org.vaadin.spring.events.EventBusListener;
16

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

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

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

    
42
    private static final long serialVersionUID = 2594781255088231474L;
43

    
44
    @Autowired
45
    @Qualifier("cdmRepository")
46
    private CdmRepository repo;
47

    
48
    private UIEventBus uiEventBus;
49

    
50
    @Autowired
51
    protected final void setUIEventBus(EventBus.UIEventBus uiEventBus){
52
        this.uiEventBus = uiEventBus;
53
        uiEventBus.subscribe(this);
54
    }
55

    
56
    @Autowired
57
    protected NavigationManager navigationManager;
58

    
59
    @Autowired
60
    VaadinCdmUserHelper userHelper;
61

    
62
    CssLayout buttonGroup = new CssLayout();
63
    Button messageButton;
64
    Button loginButton;
65
    Button logoutButton;
66
    Button userButton;
67

    
68
    public RegistrationToolbar() {
69
    }
70

    
71
    @Override
72
    public void initialize(){
73

    
74
        setWidth("100%");
75
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
76
        messageButton = new Button(FontAwesome.COMMENT);
77
        loginButton = new Button("login");
78
        userButton = new Button(FontAwesome.USER);
79
        logoutButton = new Button("logout");
80

    
81
        messageButton.setEnabled(false);
82
        logoutButton.addClickListener(e -> performLogut());
83
        loginButton.addClickListener(e -> performLogin());
84
        buttonGroup.addComponent(messageButton);
85
        buttonGroup.addComponent(loginButton);
86
        buttonGroup.addComponent(logoutButton);
87
        buttonGroup.addComponent(userButton);
88
        addComponent(buttonGroup);
89
        setComponentAlignment(buttonGroup, Alignment.MIDDLE_RIGHT);
90
        updateAuthenticationButtons();
91
    }
92

    
93

    
94
    @Override
95
    public void onEvent(org.vaadin.spring.events.Event<AuthenticationSuccessEvent> event) {
96
        updateAuthenticationButtons();
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
        uiEventBus.publish(this, 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)