Project

General

Profile

Download (7.31 KB) Statistics
| Branch: | Tag: | Revision:
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.VaadinCdmUserHelper;
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
/**
28
 * A toolbar for the distribution app. Displayed at the top of the screen.
29
 */
30
@SpringComponent("distributionToolbar")
31
@ViewScope
32
public class DistributionToolbar extends HorizontalLayout implements Serializable{
33

    
34
	/**
35
	 * automatically generated ID
36
	 */
37
	private static final long serialVersionUID = 5344340511582993289L;
38

    
39
    private EventBus.UIEventBus uiEventBus;
40

    
41
    @Autowired
42
    private final void setViewEventBus(EventBus.UIEventBus viewEventBus){
43
        this.uiEventBus = viewEventBus;
44
        viewEventBus.subscribe(AuthenticationSuccessEvent.class);
45
    }
46

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

    
51
    @Autowired
52
    private NavigationManager navigationManager;
53

    
54
    @Autowired
55
    private VaadinCdmUserHelper userHelper;
56

    
57
    private final Button loginButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_LOGIN));
58

    
59
    private final Button logoutButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_LOGOUT));
60

    
61
    private final Button userButton = new Button(FontAwesome.USER);
62

    
63
	private final Button editButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_EDIT));
64

    
65
	private final Button saveButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_SAVE));
66

    
67
	private final Button detailButton = new Button(Messages.getLocalizedString(Messages.DistributionToolbar_DETAIL));
68

    
69
	private final Button distributionSettingsButton =  new Button(Messages.getLocalizedString(Messages.DistributionToolbar_AREAS_AND_TAXA));
70

    
71
	private final Button settingsButton =  new Button(Messages.getLocalizedString(Messages.DistributionToolbar_STATUS));
72

    
73
	private final Button helpButton =  new Button(Messages.getLocalizedString(Messages.DistributionToolbar_HELP));
74

    
75
//	private final Authentication authentication;
76
//	private ExcelExporter exporter = new ExcelExporter();
77

    
78
	/**
79
	 * Constructs the toolbar.
80
	 */
81
	@PostConstruct
82
    public void init() {
83
		setMargin(true);
84
		setSpacing(true);
85
		setStyleName("toolbar"); //$NON-NLS-1$
86
		setWidth("100%"); //$NON-NLS-1$
87
		setHeight("75px"); //$NON-NLS-1$
88

    
89
//		exporter.setCaption("Export");
90
//		exporter.setIcon(new ThemeResource("icons/32/document-xsl.png"));
91
		loginButton.addClickListener(e -> performLogin());
92
		logoutButton.addClickListener(e -> performLogout());
93
		saveButton.setIcon(new ThemeResource("icons/32/document-save.png")); //$NON-NLS-1$
94
		editButton.setIcon(new ThemeResource("icons/32/document-edit.png")); //$NON-NLS-1$
95
		detailButton.setIcon(new ThemeResource("icons/32/document-txt.png")); //$NON-NLS-1$
96
		settingsButton.setIcon(new ThemeResource("icons/32/settings_1.png")); //$NON-NLS-1$
97
		distributionSettingsButton.setIcon(new ThemeResource("icons/32/settings_1.png")); //$NON-NLS-1$
98

    
99
        HorizontalLayout leftLayout = new HorizontalLayout();
100
        leftLayout.addComponent(distributionSettingsButton);
101
        leftLayout.addComponent(settingsButton);
102
        leftLayout.addComponent(detailButton);
103

    
104
		HorizontalLayout rightLayout = new HorizontalLayout();
105
		rightLayout.addComponent(helpButton);
106
		rightLayout.addComponent(loginButton);
107
		rightLayout.addComponent(logoutButton);
108
        rightLayout.addComponent(userButton);
109

    
110
        addComponent(leftLayout);
111
        setComponentAlignment(leftLayout, Alignment.MIDDLE_LEFT);
112
		addComponent(rightLayout);
113
		setComponentAlignment(rightLayout, Alignment.MIDDLE_RIGHT);
114
		setExpandRatio(rightLayout, 1);
115
		updateAuthenticationButtons();
116
    }
117

    
118
	/**
119
	 * Called when authentication was successful. Updates the user menu buttons.
120
	 * @param event
121
	 */
122
	@EventBusListenerMethod
123
    public void onAuthenticationSuccessEvent(org.vaadin.spring.events.Event<AuthenticationSuccessEvent> event){
124
        boolean isInitialized = userButton != null;
125
        // The RegistrationToolbar is being initialize even if not needed only because it is a EventListener
126
        // which causes Spring to initialize it.
127
        // TODO After switching to an other event bus this check can be removed
128
        if(isInitialized){
129
            updateAuthenticationButtons();
130
        }
131
    }
132

    
133
	/**
134
	 * Updates the user menu buttons on login and logout.
135
	 */
136
    protected void updateAuthenticationButtons() {
137
        if(userHelper.userIsAutheticated() && !userHelper.userIsAnnonymous()){
138
            userButton.setCaption(userHelper.userName());
139
            userButton.setVisible(true);
140
            logoutButton.setVisible(true);
141
            loginButton.setVisible(false);
142
            saveButton.setVisible(true);
143
            editButton.setVisible(true);
144
            detailButton.setVisible(true);
145
            settingsButton.setVisible(true);
146
            distributionSettingsButton.setVisible(true);
147
        } else {
148
            userButton.setCaption(null);
149
            userButton.setVisible(false);
150
            logoutButton.setVisible(false);
151
            loginButton.setVisible(true);
152
            saveButton.setVisible(false);
153
            editButton.setVisible(false);
154
            detailButton.setVisible(false);
155
            settingsButton.setVisible(false);
156
            distributionSettingsButton.setVisible(false);
157
        }
158
    }
159

    
160
    /**
161
     * @return
162
     */
163
    private void performLogin() {
164
        uiEventBus.publish(this, new NavigationEvent("login", navigationManager.getCurrentViewName())); //$NON-NLS-1$
165
    }
166

    
167
    /**
168
     * Logs the user out and closes the session.
169
     */
170
    private void performLogout() {
171
        userHelper.logout();
172
        updateAuthenticationButtons();
173
        navigationManager.reloadCurrentView();
174
    }
175

    
176
    /**
177
     * The settings button.
178
     * @return
179
     */
180
    public Button getSettingsButton(){
181
        return settingsButton;
182
    }
183

    
184
    /**
185
     * Button to open the {@link DistributionStatusSettingsConfigWindow}.
186
     * @return Button to display the {@link DistributionStatusSettingsConfigWindow}.
187
     */
188
    public Button getDistributionSettingsButton() {
189
		return distributionSettingsButton;
190
	}
191

    
192
    /**
193
     * Unused.
194
     * @return
195
     */
196
	public Button getEditButton() {
197
		return editButton;
198
	}
199

    
200
	/**
201
	 * Unused.
202
	 * @return
203
	 */
204
	public Button getSaveButton() {
205
		return saveButton;
206
	}
207

    
208
	/**
209
	 * Button to open the {@link DetailWindow}.
210
	 * @return  Button to open the {@link DetailWindow}.
211
	 */
212
	public Button getDetailButton() {
213
		return detailButton;
214
	}
215

    
216
	/**
217
	 * Button to open the {@link HelpWindow}.
218
	 * @return Button to open the {@link HelpWindow}.
219
	 */
220
	public Button getHelpButton() {
221
		return helpButton;
222
	}
223
}
(4-4/6)