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