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