1 package org.argeo.suite.ui;
3 import java.util.ArrayList;
8 import javax.jcr.RepositoryException;
10 import org.argeo.cms.Localized;
11 import org.argeo.cms.ui.CmsTheme;
12 import org.argeo.cms.ui.CmsUiProvider;
13 import org.argeo.cms.ui.util.CmsUiUtils;
14 import org.argeo.jcr.JcrException;
15 import org.argeo.suite.ui.widgets.TabbedArea;
16 import org.argeo.util.LangUtils;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.SashForm;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.wiring.BundleWiring;
25 /** An app layer based on an entry area and an editor area. */
26 public class DefaultEditionLayer implements SuiteLayer {
27 private CmsUiProvider entryArea;
28 private CmsUiProvider defaultView;
29 private CmsUiProvider workArea;
30 private List<String> weights = new ArrayList<>();
31 private boolean startMaximized = false;
32 private boolean fixedEntryArea = false;
33 private boolean singleTab = false;
34 private Localized title = null;
37 public Control createUi(Composite parent, Node context) throws RepositoryException {
38 // TODO Factorize more, or split into more specialised classes?
39 if (entryArea != null) {
41 FixedEditionArea editionArea = new FixedEditionArea(parent, parent.getStyle());
42 entryArea.createUi(editionArea.getEntryArea(), context);
43 if (this.defaultView != null) {
44 editionArea.getTabbedArea().view(defaultView, context);
48 SashFormEditionArea editionArea = new SashFormEditionArea(parent, parent.getStyle());
49 entryArea.createUi(editionArea.getEntryArea(), context);
50 if (this.defaultView != null) {
51 editionArea.getTabbedArea().view(defaultView, context);
56 if (this.workArea != null) {
57 Composite area = new Composite(parent, SWT.NONE);
58 this.workArea.createUi(area, context);
61 CmsTheme theme = CmsTheme.getCmsTheme(parent);
62 TabbedArea tabbedArea = createTabbedArea(parent, theme);
68 public void view(CmsUiProvider uiProvider, Composite workAreaC, Node context) {
69 if (workArea != null) {
71 CmsUiUtils.clear(workAreaC);
72 workArea.createUi(workAreaC, context);
73 workAreaC.requestLayout();
75 } catch (RepositoryException e) {
76 throw new JcrException("Cannot rebuild work area", e);
81 TabbedArea tabbedArea = findTabbedArea(workAreaC);
82 if (tabbedArea == null)
83 throw new IllegalArgumentException("Unsupported work area " + workAreaC.getClass().getName());
84 if (uiProvider == null) {
86 tabbedArea.closeAllTabs();
87 if (this.defaultView != null) {
88 tabbedArea.view(defaultView, context);
91 tabbedArea.view(uiProvider, context);
96 public Node getCurrentContext(Composite workArea) {
97 TabbedArea tabbedArea = findTabbedArea(workArea);
98 if (tabbedArea == null)
100 return tabbedArea.getCurrentContext();
103 private TabbedArea findTabbedArea(Composite workArea) {
104 TabbedArea tabbedArea = null;
105 if (workArea instanceof SashFormEditionArea) {
106 tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
107 } else if (workArea instanceof FixedEditionArea) {
108 tabbedArea = ((FixedEditionArea) workArea).getTabbedArea();
109 } else if (workArea instanceof TabbedArea) {
110 tabbedArea = (TabbedArea) workArea;
116 public void open(CmsUiProvider uiProvider, Composite workArea, Node context) {
117 TabbedArea tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
118 tabbedArea.open(uiProvider, context);
122 public Localized getTitle() {
126 public void init(BundleContext bundleContext, Map<String, Object> properties) {
127 weights = LangUtils.toStringList(properties.get(Property.weights.name()));
128 startMaximized = properties.containsKey(Property.startMaximized.name())
129 && "true".equals(properties.get(Property.startMaximized.name()));
130 fixedEntryArea = properties.containsKey(Property.fixedEntryArea.name())
131 && "true".equals(properties.get(Property.fixedEntryArea.name()));
132 if (fixedEntryArea && weights.size() != 0) {
133 throw new IllegalArgumentException("Property " + Property.weights.name() + " should not be set if property "
134 + Property.fixedEntryArea.name() + " is set.");
136 singleTab = properties.containsKey(Property.singleTab.name())
137 && "true".equals(properties.get(Property.singleTab.name()));
139 String titleStr = (String) properties.get(SuiteLayer.Property.title.name());
140 if (titleStr != null) {
141 if (titleStr.startsWith("%")) {
142 title = new Localized() {
145 public String name() {
150 public ClassLoader getL10nClassLoader() {
151 return bundleContext != null
152 ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
153 : getClass().getClassLoader();
157 title = new Localized.Untranslated(titleStr);
162 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
166 public void setEntryArea(CmsUiProvider entryArea) {
167 this.entryArea = entryArea;
170 public void setWorkArea(CmsUiProvider workArea) {
171 this.workArea = workArea;
174 public void setDefaultView(CmsUiProvider defaultView) {
175 this.defaultView = defaultView;
178 TabbedArea createTabbedArea(Composite parent, CmsTheme theme) {
179 TabbedArea tabbedArea = new TabbedArea(parent, SWT.NONE);
180 tabbedArea.setSingleTab(singleTab);
181 tabbedArea.setBodyStyle(SuiteStyle.mainTabBody.style());
182 tabbedArea.setTabStyle(SuiteStyle.mainTab.style());
183 tabbedArea.setTabSelectedStyle(SuiteStyle.mainTabSelected.style());
184 tabbedArea.setCloseIcon(SuiteIcon.close.getSmallIcon(theme));
185 tabbedArea.setLayoutData(CmsUiUtils.fillAll());
189 // /** A work area based on an entry area and and a tabbed area. */
190 class SashFormEditionArea extends SashForm {
191 private static final long serialVersionUID = 2219125778722702618L;
192 private TabbedArea tabbedArea;
193 private Composite entryC;
195 SashFormEditionArea(Composite parent, int style) {
196 super(parent, SWT.HORIZONTAL);
197 CmsTheme theme = CmsTheme.getCmsTheme(parent);
200 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
201 editorC = new Composite(this, SWT.BORDER);
202 entryC = new Composite(this, SWT.BORDER);
204 entryC = new Composite(this, SWT.NONE);
205 editorC = new Composite(this, SWT.NONE);
208 // sash form specific
209 if (weights.size() != 0) {
210 int[] actualWeight = new int[weights.size()];
211 for (int i = 0; i < weights.size(); i++) {
212 actualWeight[i] = Integer.parseInt(weights.get(i));
214 setWeights(actualWeight);
216 int[] actualWeights = new int[] { 3000, 7000 };
217 setWeights(actualWeights);
220 setMaximizedControl(editorC);
222 GridLayout editorAreaLayout = CmsUiUtils.noSpaceGridLayout();
223 // editorAreaLayout.verticalSpacing = 0;
224 // editorAreaLayout.marginBottom = 0;
225 // editorAreaLayout.marginHeight = 0;
226 // editorAreaLayout.marginLeft = 0;
227 // editorAreaLayout.marginRight = 0;
228 editorC.setLayout(editorAreaLayout);
230 tabbedArea = createTabbedArea(editorC, theme);
233 TabbedArea getTabbedArea() {
237 Composite getEntryArea() {
243 class FixedEditionArea extends Composite {
244 private static final long serialVersionUID = -5525672639277322465L;
245 private TabbedArea tabbedArea;
246 private Composite entryC;
248 public FixedEditionArea(Composite parent, int style) {
249 super(parent, style);
250 CmsTheme theme = CmsTheme.getCmsTheme(parent);
252 setLayout(CmsUiUtils.noSpaceGridLayout(2));
255 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
256 editorC = new Composite(this, SWT.NONE);
257 entryC = new Composite(this, SWT.NONE);
259 entryC = new Composite(this, SWT.NONE);
260 editorC = new Composite(this, SWT.NONE);
262 entryC.setLayoutData(CmsUiUtils.fillHeight());
264 GridLayout editorAreaLayout = CmsUiUtils.noSpaceGridLayout();
265 // editorAreaLayout.verticalSpacing = 0;
266 // editorAreaLayout.marginBottom = 0;
267 // editorAreaLayout.marginHeight = 0;
268 // editorAreaLayout.marginLeft = 0;
269 // editorAreaLayout.marginRight = 0;
270 editorC.setLayout(editorAreaLayout);
271 editorC.setLayoutData(CmsUiUtils.fillAll());
273 tabbedArea = createTabbedArea(editorC, theme);
276 TabbedArea getTabbedArea() {
280 Composite getEntryArea() {