Merge branch 'release/4.7.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / bar / AuthenticatedUserBar.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.ui.bar;
11
12 import java.util.Observable;
13 import java.util.Observer;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
21
22 import eu.etaxonomy.cdm.model.common.User;
23 import eu.etaxonomy.taxeditor.l10n.Messages;
24 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26 import eu.etaxonomy.taxeditor.util.ApplicationUtil;
27
28 /**
29 * Shows the currently logged in user in status bar
30 *
31 * @author n.hoffmann
32 * @created 01.07.2009
33 * @version 1.0
34 */
35 public class AuthenticatedUserBar extends WorkbenchWindowControlContribution implements Observer{
36
37 private Label label_authenticatedUser;
38
39 public AuthenticatedUserBar(){
40 CdmStore.getLoginManager().addObserver(this);
41 }
42
43
44
45 @Override
46 public boolean isDynamic() {
47 return true;
48 }
49
50 /** {@inheritDoc} */
51 @Override
52 protected Control createControl(Composite parent) {
53 parent.getParent().setRedraw(true);
54 label_authenticatedUser = new Label(parent, SWT.NULL);
55
56 update(null, null);
57
58 return label_authenticatedUser;
59 }
60
61 /** {@inheritDoc} */
62 @Override
63 public void update(Observable o, Object arg) {
64 User authenticatedUser = CdmStore.getLoginManager().getAuthenticatedUser();
65 // TODO find a method to recompute width for parental toolbar item
66 String text = ""; //$NON-NLS-1$
67 if(authenticatedUser == null) {
68 text = Messages.AuthenticatedUserBar_NOT_LOGGED_IN ;
69 } else {
70 CdmRemoteSource source = (CdmRemoteSource) CdmStore.getActiveCdmSource();
71 String loginInfo = String.format("%s@%s:%s",authenticatedUser.getUsername(), source.getName(), source.getContextPath()); //$NON-NLS-1$
72 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setText(ApplicationUtil.getTitle() + " " + loginInfo); //$NON-NLS-1$
73
74 text = String.format(Messages.AuthenticatedUserBar_LOGGED_IN_AS, authenticatedUser.getUsername());
75 }
76
77 label_authenticatedUser.setText(text);
78 label_authenticatedUser.setRedraw(true);
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public void dispose() {
84 super.dispose();
85 CdmStore.getLoginManager().deleteObserver(this);
86 }
87
88 }