Project

General

Profile

Download (4.14 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.DisposableBean;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Qualifier;
14
import org.vaadin.spring.events.EventBus;
15
import org.vaadin.spring.events.EventBus.UIEventBus;
16
import org.vaadin.spring.events.EventBusListener;
17

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

    
27
import eu.etaxonomy.cdm.api.application.CdmRepository;
28
import eu.etaxonomy.cdm.api.utility.UserHelper;
29
import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
30
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
31
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
32

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

    
43
    private static final long serialVersionUID = 2594781255088231474L;
44

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

    
49
    private UIEventBus uiEventBus;
50

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

    
57
    @Autowired
58
    protected NavigationManager navigationManager;
59

    
60
    @Autowired
61
    UserHelper userHelper;
62

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

    
69
    public RegistrationToolbar() {
70
    }
71

    
72
    @Override
73
    public void initialize(){
74

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

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

    
94

    
95
    @Override
96
    public void onEvent(org.vaadin.spring.events.Event<AuthenticationSuccessEvent> event) {
97
        updateAuthenticationButtons();
98
    }
99

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

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

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

    
121
    /**
122
     * @return
123
     */
124
    private void performLogin() {
125
        uiEventBus.publish(this, new NavigationEvent("login", navigationManager.getCurrentViewName()));
126
    }
127

    
128

    
129
    private void performLogut() {
130

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

    
136
    /**
137
     * {@inheritDoc}
138
     */
139
    @Override
140
    public void destroy() throws Exception {
141
        uiEventBus.unsubscribe(this);
142
    }
143

    
144
}
(1-1/2)