0cfee182596230c9875f0d87b84a49dc0ea966d5
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / SuiteUiUtils.java
1 package org.argeo.suite.ui;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
10 import org.argeo.cms.LocaleUtils;
11 import org.argeo.cms.Localized;
12 import org.argeo.cms.auth.CurrentUser;
13 import org.argeo.cms.ui.CmsEditable;
14 import org.argeo.cms.ui.CmsTheme;
15 import org.argeo.cms.ui.CmsView;
16 import org.argeo.cms.ui.dialogs.LightweightDialog;
17 import org.argeo.cms.ui.util.CmsEvent;
18 import org.argeo.cms.ui.util.CmsIcon;
19 import org.argeo.cms.ui.util.CmsUiUtils;
20 import org.argeo.eclipse.ui.EclipseUiUtils;
21 import org.argeo.entity.EntityNames;
22 import org.argeo.entity.EntityType;
23 import org.argeo.jcr.Jcr;
24 import org.argeo.jcr.JcrUtils;
25 import org.argeo.suite.SuiteRole;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.custom.ScrolledComposite;
28 import org.eclipse.swt.events.MouseEvent;
29 import org.eclipse.swt.events.MouseListener;
30 import org.eclipse.swt.graphics.ImageData;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Text;
39 import org.osgi.service.event.Event;
40
41 /** UI utilities related to the APAF project. */
42 public class SuiteUiUtils {
43
44         /** Singleton. */
45         private SuiteUiUtils() {
46         }
47
48         /** creates a title bar composite with label and optional button */
49         public static void addTitleBar(Composite parent, String title, Boolean isEditable) {
50                 Composite titleBar = new Composite(parent, SWT.NONE);
51                 titleBar.setLayoutData(CmsUiUtils.fillWidth());
52                 CmsUiUtils.style(titleBar, SuiteStyle.titleContainer);
53
54                 titleBar.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(2, false)));
55                 Label titleLbl = new Label(titleBar, SWT.NONE);
56                 titleLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
57                 CmsUiUtils.style(titleLbl, SuiteStyle.titleLabel);
58                 titleLbl.setText(title);
59
60                 if (isEditable) {
61                         Button editBtn = new Button(titleBar, SWT.PUSH);
62                         editBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
63                         CmsUiUtils.style(editBtn, SuiteStyle.inlineButton);
64                         editBtn.setText("Edit");
65                 }
66         }
67
68         public static Label addFormLabel(Composite parent, String label) {
69                 Label lbl = new Label(parent, SWT.WRAP);
70                 lbl.setText(label);
71                 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
72                 CmsUiUtils.style(lbl, SuiteStyle.simpleLabel);
73                 return lbl;
74         }
75
76         public static Text addFormTextField(Composite parent, String text, String message) {
77                 return addFormTextField(parent, text, message, SWT.NONE);
78         }
79
80         public static Text addFormTextField(Composite parent, String text, String message, int style) {
81                 Text txt = new Text(parent, style);
82                 if (text != null)
83                         txt.setText(text);
84                 if (message != null)
85                         txt.setMessage(message);
86                 txt.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
87                 CmsUiUtils.style(txt, SuiteStyle.simpleText);
88                 return txt;
89         }
90
91         public static Text addFormInputField(Composite parent, String placeholder) {
92                 Text txt = new Text(parent, SWT.BORDER);
93
94                 GridData gridData = CmsUiUtils.fillWidth();
95                 txt.setLayoutData(gridData);
96
97                 if (placeholder != null)
98                         txt.setText(placeholder);
99
100                 CmsUiUtils.style(txt, SuiteStyle.simpleInput);
101                 return txt;
102         }
103
104         /** creates a single horizontal-block composite for key:value display */
105         public static Text addFormLine(Composite parent, String label, String text) {
106                 Composite lineComposite = new Composite(parent, SWT.NONE);
107                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
108                 lineComposite.setLayout(new GridLayout(2, false));
109                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
110                 addFormLabel(lineComposite, label);
111                 Text txt = addFormTextField(lineComposite, text, null);
112                 txt.setEditable(false);
113                 txt.setLayoutData(CmsUiUtils.fillWidth());
114                 return txt;
115         }
116
117         public static Text addFormLine(Composite parent, String label, Node node, String property,
118                         CmsEditable cmsEditable) {
119                 Composite lineComposite = new Composite(parent, SWT.NONE);
120                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
121                 lineComposite.setLayout(new GridLayout(2, false));
122                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
123                 addFormLabel(lineComposite, label);
124                 String text = Jcr.get(node, property);
125 //              int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
126                 Text txt = addFormTextField(lineComposite, text, null, SWT.WRAP);
127                 if (cmsEditable != null && cmsEditable.isEditing()) {
128                         txt.addModifyListener((e) -> {
129                                 Jcr.set(node, property, txt.getText());
130                                 Jcr.save(node);
131                         });
132                 } else {
133                         txt.setEditable(false);
134                 }
135                 txt.setLayoutData(CmsUiUtils.fillWidth());
136                 return txt;
137         }
138
139         public static Text addFormInput(Composite parent, String label, String placeholder) {
140                 Composite lineComposite = new Composite(parent, SWT.NONE);
141                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
142                 lineComposite.setLayout(new GridLayout(2, false));
143                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
144                 addFormLabel(lineComposite, label);
145                 Text txt = addFormInputField(lineComposite, placeholder);
146                 txt.setLayoutData(CmsUiUtils.fillWidth());
147                 return txt;
148         }
149
150         /**
151          * creates a single horizontal-block composite for key:value display, with
152          * offset value
153          */
154         public static Text addFormLine(Composite parent, String label, String text, Integer offset) {
155                 Composite lineComposite = new Composite(parent, SWT.NONE);
156                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
157                 lineComposite.setLayout(new GridLayout(3, false));
158                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
159                 Label offsetLbl = new Label(lineComposite, SWT.NONE);
160                 GridData gridData = new GridData();
161                 gridData.widthHint = offset;
162                 offsetLbl.setLayoutData(gridData);
163                 addFormLabel(lineComposite, label);
164                 Text txt = addFormTextField(lineComposite, text, null);
165                 txt.setLayoutData(CmsUiUtils.fillWidth());
166                 return txt;
167         }
168
169         /** creates a single vertical-block composite for key:value display */
170         public static Text addFormColumn(Composite parent, String label, String text) {
171 //              Composite columnComposite = new Composite(parent, SWT.NONE);
172 //              columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
173 //              columnComposite.setLayout(new GridLayout(1, false));
174                 addFormLabel(parent, label);
175                 Text txt = addFormTextField(parent, text, null);
176                 txt.setEditable(false);
177                 txt.setLayoutData(CmsUiUtils.fillWidth());
178                 return txt;
179         }
180
181         public static Text addFormColumn(Composite parent, String label, Node node, String property,
182                         CmsEditable cmsEditable) {
183 //              Composite columnComposite = new Composite(parent, SWT.NONE);
184 //              columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
185 //              columnComposite.setLayout(new GridLayout(1, false));
186                 addFormLabel(parent, label);
187                 String text = Jcr.get(node, property);
188 //              int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
189                 Text txt = addFormTextField(parent, text, null, SWT.WRAP);
190                 if (cmsEditable != null && cmsEditable.isEditing()) {
191                         txt.addModifyListener((e) -> {
192                                 Jcr.set(node, property, txt.getText());
193                                 Jcr.save(node);
194                         });
195                 } else {
196                         txt.setEditable(false);
197                 }
198                 txt.setLayoutData(CmsUiUtils.fillWidth());
199                 return txt;
200         }
201
202         public static Label createBoldLabel(Composite parent, Localized localized) {
203                 Label label = new Label(parent, SWT.LEAD);
204                 label.setText(localized.lead());
205                 label.setFont(EclipseUiUtils.getBoldFont(parent));
206                 label.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
207                 return label;
208         }
209
210         public static Label addFormPicture(Composite parent, String label, Node fileNode) throws RepositoryException {
211                 Composite lineComposite = new Composite(parent, SWT.NONE);
212                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
213                 lineComposite.setLayout(new GridLayout(2, true));
214                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
215                 addFormLabel(lineComposite, label);
216
217                 return addPicture(lineComposite, fileNode);
218         }
219
220         public static Label addPicture(Composite parent, Node fileNode) throws RepositoryException {
221                 return addPicture(parent, fileNode, null);
222         }
223
224         public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth) throws RepositoryException {
225                 Node content = fileNode.getNode(Node.JCR_CONTENT);
226                 // TODO move it deeper in the middleware.
227                 if (!content.isNodeType(EntityType.box.get())) {
228                         if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
229                                 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
230                                         ImageData imageData = new ImageData(in);
231                                         content.addMixin(EntityType.box.get());
232                                         content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
233                                         content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
234                                         content.getSession().save();
235                                 } catch (IOException e) {
236                                         throw new RuntimeException(e);
237                                 }
238                         }
239                 }
240
241                 // TODO optimise
242                 Long width;
243                 Long height;
244                 if (content.isNodeType(EntityType.box.get())) {
245                         width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
246                         height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
247                 } else {
248                         try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
249                                 ImageData imageData = new ImageData(in);
250                                 width = Long.valueOf(imageData.width);
251                                 height = Long.valueOf(imageData.height);
252                         } catch (IOException e) {
253                                 throw new RuntimeException(e);
254                         }
255                 }
256
257                 if (maxWidth != null && width > maxWidth) {
258                         Double ratio = maxWidth.doubleValue() / width.doubleValue();
259                         width = maxWidth.longValue();
260                         height = Math.round(ratio * height);
261                 }
262                 Label img = new Label(parent, SWT.NONE);
263                 CmsUiUtils.markup(img);
264                 img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
265                 if (parent.getLayout() instanceof GridLayout) {
266                         GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
267                         gd.widthHint = width.intValue();
268                         gd.heightHint = height.intValue();
269                         img.setLayoutData(gd);
270                 }
271                 img.addMouseListener(new MouseListener() {
272                         private static final long serialVersionUID = -1362242049325206168L;
273
274                         @Override
275                         public void mouseUp(MouseEvent e) {
276                         }
277
278                         @Override
279                         public void mouseDown(MouseEvent e) {
280                         }
281
282                         @Override
283                         public void mouseDoubleClick(MouseEvent e) {
284                                 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
285
286                                         @Override
287                                         protected Control createDialogArea(Composite parent) {
288                                                 parent.setLayout(new GridLayout());
289                                                 ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
290                                                 scroll.setLayoutData(CmsUiUtils.fillAll());
291                                                 scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
292                                                 scroll.setExpandHorizontal(true);
293                                                 scroll.setExpandVertical(true);
294                                                 // scroll.setAlwaysShowScrollBars(true);
295
296                                                 Composite c = new Composite(scroll, SWT.NONE);
297                                                 scroll.setContent(c);
298                                                 c.setLayout(new GridLayout());
299                                                 c.setLayoutData(CmsUiUtils.fillAll());
300                                                 Label bigImg = new Label(c, SWT.NONE);
301                                                 CmsUiUtils.markup(bigImg);
302                                                 bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
303                                                                 Jcr.get(content, EntityNames.SVG_HEIGHT)));
304                                                 bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
305                                                 return bigImg;
306                                         }
307
308                                         @Override
309                                         protected Point getInitialSize() {
310                                                 Point shellSize = img.getShell().getSize();
311                                                 return new Point(shellSize.x - 100, shellSize.y - 100);
312                                         }
313
314                                 };
315                                 dialog.open();
316                         }
317                 });
318                 return img;
319         }
320
321         public static boolean isCoworker(CmsView cmsView) {
322                 boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
323                 return coworker;
324         }
325
326         public static boolean isTopic(Event event, CmsEvent cmsEvent) {
327                 return event.getTopic().equals(cmsEvent.topic());
328         }
329
330         public static Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon,
331                         ClassLoader l10nClassLoader) {
332                 CmsTheme theme = CmsTheme.getCmsTheme(parent);
333                 Button button = new Button(parent, SWT.PUSH);
334                 CmsUiUtils.style(button, SuiteStyle.leadPane);
335                 if (icon != null)
336                         button.setImage(icon.getBigIcon(theme));
337                 button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
338                 // button.setToolTipText(msg.lead());
339                 if (msg != null) {
340                         Label lbl = new Label(parent, SWT.CENTER);
341                         CmsUiUtils.style(lbl, SuiteStyle.leadPane);
342                         String txt = LocaleUtils.lead(msg, l10nClassLoader);
343 //                      String txt = msg.lead();
344                         lbl.setText(txt);
345                         lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
346                 }
347                 CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
348                 return button;
349         }
350
351 //      public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
352 //                      String... additionnalProps) {
353 //
354 //              Session tmpSession = null;
355 //              Session mainSession = null;
356 //              try {
357 //                      // FIXME would not work if home is another physical workspace
358 //                      tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
359 //                      Node draftNode = null;
360 //                      for (int i = 0; i < additionnalProps.length - 1; i += 2) {
361 //                              draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
362 //                      }
363 //                      Wizard wizard = null;
364 //                      CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
365 //                      // WizardDialog dialog = new WizardDialog(shell, wizard);
366 //                      if (dialog.open() == Window.OK) {
367 //                              String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
368 //                              // FIXME it should be possible to specify the workspace
369 //                              mainSession = referenceSession.getRepository().login();
370 //                              Node parent = mainSession.getNode(parentPath);
371 //                              Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
372 ////                            task = appService.saveEntity(task, false);
373 //                              referenceSession.refresh(true);
374 //                              return task.getPath();
375 //                      }
376 //                      return null;
377 //              } catch (RepositoryException e1) {
378 //                      throw new JcrException(
379 //                                      "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
380 //              } finally {
381 //                      JcrUtils.logoutQuietly(tmpSession);
382 //                      JcrUtils.logoutQuietly(mainSession);
383 //              }
384 //      }
385
386 }