3 import static org
.argeo
.cms
.internal
.kernel
.KernelConstants
.SPRING_SECURITY_CONTEXT_KEY
;
5 import javax
.servlet
.http
.HttpSession
;
7 import org
.eclipse
.rap
.rwt
.RWT
;
8 import org
.eclipse
.swt
.SWT
;
9 import org
.eclipse
.swt
.events
.MouseAdapter
;
10 import org
.eclipse
.swt
.events
.MouseEvent
;
11 import org
.eclipse
.swt
.events
.ShellAdapter
;
12 import org
.eclipse
.swt
.events
.ShellEvent
;
13 import org
.eclipse
.swt
.events
.TraverseEvent
;
14 import org
.eclipse
.swt
.events
.TraverseListener
;
15 import org
.eclipse
.swt
.layout
.GridData
;
16 import org
.eclipse
.swt
.layout
.GridLayout
;
17 import org
.eclipse
.swt
.widgets
.Control
;
18 import org
.eclipse
.swt
.widgets
.Label
;
19 import org
.eclipse
.swt
.widgets
.Shell
;
20 import org
.eclipse
.swt
.widgets
.Text
;
21 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
23 /** The site-related user menu */
24 public class UserMenu
extends Shell
implements CmsStyles
{
25 private static final long serialVersionUID
= -5788157651532106301L;
27 private CmsLogin cmsLogin
;
28 private String username
= null;
30 public UserMenu(CmsLogin cmsLogin
, Control source
) {
31 super(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
32 this.cmsLogin
= cmsLogin
;
34 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
36 username
= SecurityContextHolder
.getContext().getAuthentication()
38 if (username
.equals("anonymous")) {
47 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
50 addShellListener(new ShellAdapter() {
51 private static final long serialVersionUID
= 5178980294808435833L;
54 public void shellDeactivated(ShellEvent e
) {
65 protected void userUi() {
66 setLayout(new GridLayout());
68 Label l
= new Label(this, SWT
.NONE
);
69 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
70 l
.setData(RWT
.MARKUP_ENABLED
, true);
71 l
.setLayoutData(CmsUtils
.fillWidth());
72 l
.setText("<b>" + username
+ "</b>");
74 final CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
76 l
= new Label(this, SWT
.NONE
);
77 l
.setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU_ITEM
);
78 l
.setText(CmsMsg
.logout
.lead());
79 GridData lData
= CmsUtils
.fillWidth();
80 lData
.widthHint
= 120;
81 l
.setLayoutData(lData
);
83 l
.addMouseListener(new MouseAdapter() {
84 private static final long serialVersionUID
= 6444395812777413116L;
86 public void mouseDown(MouseEvent e
) {
87 SecurityContextHolder
.getContext().setAuthentication(null);
88 HttpSession httpSession
= RWT
.getRequest().getSession();
89 httpSession
.removeAttribute(SPRING_SECURITY_CONTEXT_KEY
);
92 cmsSession
.authChange();
97 protected void anonymousUi() {
98 Integer textWidth
= 150;
99 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
100 setLayout(new GridLayout(2, false));
102 new Label(this, SWT
.NONE
).setText(CmsMsg
.username
.lead());
103 final Text username
= new Text(this, SWT
.BORDER
);
104 username
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_USERNAME
);
105 GridData gd
= CmsUtils
.fillWidth();
106 gd
.widthHint
= textWidth
;
107 username
.setLayoutData(gd
);
109 new Label(this, SWT
.NONE
).setText(CmsMsg
.password
.lead());
110 final Text password
= new Text(this, SWT
.BORDER
| SWT
.PASSWORD
);
111 password
.setData(RWT
.CUSTOM_VARIANT
, CMS_LOGIN_DIALOG_PASSWORD
);
112 gd
= CmsUtils
.fillWidth();
113 gd
.widthHint
= textWidth
;
114 password
.setLayoutData(gd
);
117 TraverseListener tl
= new TraverseListener() {
118 private static final long serialVersionUID
= -1158892811534971856L;
120 public void keyTraversed(TraverseEvent e
) {
121 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
122 login(username
.getText(), password
.getTextChars());
125 username
.addTraverseListener(tl
);
126 password
.addTraverseListener(tl
);
129 protected void login(String username
, char[] password
) {
130 CmsSession cmsSession
= (CmsSession
) getDisplay().getData(
132 cmsLogin
.logInWithPassword(username
, password
);
135 // refreshUi(source.getParent());
136 cmsSession
.authChange();