Introduce footer.
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / DefaultHeader.java
1 package org.argeo.suite.ui;
2
3 import java.util.Map;
4
5 import javax.jcr.Node;
6 import javax.jcr.RepositoryException;
7
8 import org.argeo.cms.Localized;
9 import org.argeo.cms.auth.CurrentUser;
10 import org.argeo.cms.ui.CmsTheme;
11 import org.argeo.cms.ui.CmsUiProvider;
12 import org.argeo.cms.ui.CmsView;
13 import org.argeo.cms.ui.util.CmsUiUtils;
14 import org.eclipse.swt.SWT;
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 createUi(Composite parent, Node context) throws RepositoryException {
33                 CmsView cmsView = CmsView.getCmsView(parent);
34                 CmsTheme theme = CmsTheme.getCmsTheme(parent);
35
36                 parent.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(3, true)));
37
38                 // TODO right to left
39                 Composite lead = new Composite(parent, SWT.NONE);
40                 CmsUiUtils.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                 CmsUiUtils.style(lbl, SuiteStyle.headerTitle);
50                 lbl.setLayoutData(CmsUiUtils.fillWidth());
51
52                 Composite middle = new Composite(parent, SWT.NONE);
53                 CmsUiUtils.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                 CmsUiUtils.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                         CmsUiUtils.style(userL, SuiteStyle.header);
65                         userL.setText(CurrentUser.getDisplayName());
66                         Button logoutB = new Button(end, SWT.FLAT);
67 //                      CmsUiUtils.style(logoutB, SuiteStyle.header);
68                         logoutB.setImage(SuiteIcon.logout.getSmallIcon(theme));
69                         logoutB.addSelectionListener(new SelectionAdapter() {
70                                 private static final long serialVersionUID = 7116760083964201233L;
71
72                                 @Override
73                                 public void widgetSelected(SelectionEvent e) {
74                                         cmsView.logout();
75                                 }
76
77                         });
78                 } else {
79                         end.setLayout(new GridLayout(1, false));
80                         // required in order to avoid wrong height after logout
81                         new Label(end, SWT.NONE).setText("");
82
83                 }
84                 return lbl;
85         }
86
87         public void init(BundleContext bundleContext, Map<String, String> properties) {
88                 String titleStr = (String) properties.get(TITLE_PROPERTY);
89                 if (titleStr != null) {
90                         if (titleStr.startsWith("%")) {
91                                 title = new Localized() {
92
93                                         @Override
94                                         public String name() {
95                                                 return titleStr;
96                                         }
97
98                                         @Override
99                                         public ClassLoader getL10nClassLoader() {
100                                                 return bundleContext != null
101                                                                 ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
102                                                                 : getClass().getClassLoader();
103                                         }
104                                 };
105                         } else {
106                                 title = new Localized.Untranslated(titleStr);
107                         }
108                 }
109         }
110
111         public void destroy(BundleContext bundleContext, Map<String, String> properties) {
112
113         }
114
115         public Localized getTitle() {
116                 return title;
117         }
118
119 }