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