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