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