Project

General

Profile

Download (7.55 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.DisposableBean;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.beans.factory.annotation.Qualifier;
10
import org.vaadin.spring.events.EventBus;
11
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
12

    
13
import com.vaadin.server.FontAwesome;
14
import com.vaadin.server.ThemeResource;
15
import com.vaadin.spring.annotation.SpringComponent;
16
import com.vaadin.spring.annotation.ViewScope;
17
import com.vaadin.ui.Alignment;
18
import com.vaadin.ui.Button;
19
import com.vaadin.ui.HorizontalLayout;
20

    
21
import eu.etaxonomy.cdm.api.application.CdmRepository;
22
import eu.etaxonomy.cdm.api.util.UserHelper;
23
import eu.etaxonomy.cdm.i18n.Messages;
24
import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
25
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
26
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
27

    
28
/**
29
 * A toolbar for the distribution app. Displayed at the top of the screen.
30
 */
31
@SpringComponent("distributionToolbar")
32
@ViewScope
33
public class DistributionToolbar extends HorizontalLayout implements Serializable, DisposableBean{
34

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

    
40
    private EventBus.UIEventBus uiEventBus;
41

    
42
    @Autowired
43
    private final void setViewEventBus(EventBus.UIEventBus viewEventBus){
44
        this.uiEventBus = viewEventBus;
45
        viewEventBus.subscribe(AuthenticationSuccessEvent.class); // FIXME shouldn't this be .subscribe(this) ??
46
    }
47

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

    
52
    @Autowired
53
    private NavigationManager navigationManager;
54

    
55
    @Autowired
56
    private UserHelper userHelper;
57

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
225
    /**
226
     * {@inheritDoc}
227
     */
228
    @Override
229
    public void destroy() throws Exception {
230
        uiEventBus.unsubscribe(this);
231
    }
232
}
(4-4/6)