Improve documents management.
[gpl/argeo-suite.git] / org.argeo.suite.ui / src / org / argeo / suite / ui / DefaultLeadPane.java
1 package org.argeo.suite.ui;
2
3 import java.util.Collections;
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.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.cms.Localized;
13 import org.argeo.cms.ui.CmsTheme;
14 import org.argeo.cms.ui.CmsUiProvider;
15 import org.argeo.cms.ui.CmsView;
16 import org.argeo.cms.ui.util.CmsIcon;
17 import org.argeo.cms.ui.util.CmsUiUtils;
18 import org.argeo.suite.RankedObject;
19 import org.eclipse.swt.SWT;
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.Constants;
27
28 /** Side pane listing various perspectives. */
29 public class DefaultLeadPane implements CmsUiProvider {
30         private final static Log log = LogFactory.getLog(DefaultLeadPane.class);
31
32         public static enum Property {
33                 defaultLayers;
34         }
35
36         private Map<String, RankedObject<SuiteLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
37         private String[] defaultLayers;
38
39         @Override
40         public Control createUi(Composite parent, Node node) throws RepositoryException {
41                 CmsView cmsView = CmsView.getCmsView(parent);
42                 GridLayout layout = new GridLayout();
43                 layout.verticalSpacing = 10;
44                 layout.marginTop = 10;
45                 layout.marginLeft = 10;
46                 layout.marginRight = 10;
47                 parent.setLayout(layout);
48
49                 Button first = null;
50                 for (String layerId : defaultLayers) {
51                         if (layers.containsKey(layerId)) {
52                                 RankedObject<SuiteLayer> layerObj = layers.get(layerId);
53
54                                 // TODO deal with i10n
55                                 String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
56                                 Localized title = null;
57                                 if (titleStr != null)
58                                         title = new Localized.Untranslated(titleStr);
59
60                                 String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
61                                 SuiteIcon icon = null;
62                                 if (iconName != null)
63                                         icon = SuiteIcon.valueOf(iconName);
64
65                                 Button b = createButton(parent, layerId, title, icon);
66                                 if (first == null)
67                                         first = b;
68                         }
69                 }
70
71 //              Button dashboardB = createButton(parent, SuiteMsg.dashboard.name(), SuiteMsg.dashboard, SuiteIcon.dashboard);
72                 if (!cmsView.isAnonymous()) {
73 //                      createButton(parent, SuiteMsg.documents.name(), SuiteMsg.documents, SuiteIcon.documents);
74 //                      createButton(parent, SuiteMsg.people.name(), SuiteMsg.people, SuiteIcon.people);
75 //                      createButton(parent, SuiteMsg.locations.name(), SuiteMsg.locations, SuiteIcon.location);
76                 }
77                 return first;
78         }
79
80         protected Button createButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
81                 CmsTheme theme = CmsTheme.getCmsTheme(parent);
82                 Button button = new Button(parent, SWT.PUSH);
83                 CmsUiUtils.style(button, SuiteStyle.leadPane);
84                 if (icon != null)
85                         button.setImage(icon.getBigIcon(theme));
86                 button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
87                 // button.setToolTipText(msg.lead());
88                 if (msg != null) {
89                         Label lbl = new Label(parent, SWT.NONE);
90                         CmsUiUtils.style(lbl, SuiteStyle.leadPane);
91                         lbl.setText(msg.lead());
92                         lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
93                 }
94                 CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
95                 return button;
96         }
97
98         public void init(Map<String, Object> properties) {
99                 defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
100                 if (defaultLayers == null)
101                         throw new IllegalArgumentException("Default layers must be set.");
102                 if (log.isDebugEnabled())
103                         log.debug("Default layers: " + defaultLayers);
104         }
105
106         public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
107                 if (properties.containsKey(Constants.SERVICE_PID)) {
108                         String pid = (String) properties.get(Constants.SERVICE_PID);
109                         RankedObject.putIfHigherRank(layers, pid, layer, properties);
110                 }
111         }
112
113 }