]> git.argeo.org Git - gpl/argeo-suite.git/blob - DefaultHeader.java
ede28d124db81ed999f91e963375fbe0ad446be5
[gpl/argeo-suite.git] / DefaultHeader.java
1 package org.argeo.app.ui;
2
3 import java.util.Map;
4
5 import org.argeo.api.acr.Content;
6 import org.argeo.api.cms.ux.CmsView;
7 import org.argeo.app.ux.SuiteIcon;
8 import org.argeo.app.ux.SuiteStyle;
9 import org.argeo.cms.CurrentUser;
10 import org.argeo.cms.Localized;
11 import org.argeo.cms.swt.CmsSwtTheme;
12 import org.argeo.cms.swt.CmsSwtUtils;
13 import org.argeo.cms.ui.CmsUiProvider;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.MouseAdapter;
16 import org.eclipse.swt.events.MouseEvent;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.wiring.BundleWiring;
24
25 /** Header of a standard Argeo Suite application. */
26 public class DefaultHeader implements CmsUiProvider {
27 public final static String TITLE_PROPERTY = "argeo.suite.ui.header.title";
28 private Localized title = null;
29
30 @Override
31 public Control createUiPart(Composite parent, Content context) {
32 CmsView cmsView = CmsSwtUtils.getCmsView(parent);
33 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
34
35 parent.setLayout(CmsSwtUtils.noSpaceGridLayout(new GridLayout(3, true)));
36
37 // TODO right to left
38 Composite lead = new Composite(parent, SWT.NONE);
39 CmsSwtUtils.style(lead, SuiteStyle.header);
40 lead.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, false));
41 lead.setLayout(new GridLayout());
42 Label lbl = new Label(lead, SWT.NONE);
43 // String title = properties.get(TITLE_PROPERTY);
44 // // TODO expose the localized
45 // lbl.setText(LocaleUtils.isLocaleKey(title) ? LocaleUtils.local(title, getClass().getClassLoader()).toString()
46 // : title);
47 lbl.setText(title.lead());
48 CmsSwtUtils.style(lbl, SuiteStyle.headerTitle);
49 lbl.setLayoutData(CmsSwtUtils.fillWidth());
50
51 Composite middle = new Composite(parent, SWT.NONE);
52 CmsSwtUtils.style(middle, SuiteStyle.header);
53 middle.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
54 middle.setLayout(new GridLayout());
55
56 Composite end = new Composite(parent, SWT.NONE);
57 CmsSwtUtils.style(end, SuiteStyle.header);
58 end.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
59
60 if (!cmsView.isAnonymous()) {
61 end.setLayout(new GridLayout(2, false));
62 Label userL = new Label(end, SWT.NONE);
63 CmsSwtUtils.style(userL, SuiteStyle.header);
64 userL.setText(CurrentUser.getDisplayName());
65 // Button logoutB = new Button(end, SWT.FLAT);
66 // logoutB.setImage(theme.getSmallIcon(SuiteIcon.logout));
67 // logoutB.addSelectionListener(new SelectionAdapter() {
68 // private static final long serialVersionUID = 7116760083964201233L;
69 //
70 // @Override
71 // public void widgetSelected(SelectionEvent e) {
72 // cmsView.logout();
73 // }
74 //
75 // });
76 Label logOutL = new Label(end, 0);
77 logOutL.setImage(theme.getSmallIcon(SuiteIcon.openUserMenu));
78 logOutL.addMouseListener(new MouseAdapter() {
79 private static final long serialVersionUID = 6908266850511460799L;
80
81 @Override
82 public void mouseDown(MouseEvent e) {
83 cmsView.logout();
84 }
85
86 });
87 } else {
88 end.setLayout(new GridLayout(1, false));
89 // required in order to avoid wrong height after logout
90 new Label(end, SWT.NONE).setText("");
91
92 }
93 return lbl;
94 }
95
96 public void init(BundleContext bundleContext, Map<String, String> properties) {
97 String titleStr = (String) properties.get(TITLE_PROPERTY);
98 if (titleStr != null) {
99 if (titleStr.startsWith("%")) {
100 title = new Localized() {
101
102 @Override
103 public String name() {
104 return titleStr;
105 }
106
107 @Override
108 public ClassLoader getL10nClassLoader() {
109 return bundleContext != null
110 ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
111 : getClass().getClassLoader();
112 }
113 };
114 } else {
115 title = new Localized.Untranslated(titleStr);
116 }
117 }
118 }
119
120 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
121
122 }
123
124 public Localized getTitle() {
125 return title;
126 }
127
128 }