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