]> git.argeo.org Git - gpl/argeo-suite.git/blob - ui/SuiteUiUtils.java
Prepare next development cycle
[gpl/argeo-suite.git] / 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.HashMap;
9 import java.util.Map;
10
11 import javax.jcr.Node;
12 import javax.jcr.RepositoryException;
13 import javax.jcr.Session;
14
15 import org.apache.commons.io.IOUtils;
16 import org.argeo.api.acr.Content;
17 import org.argeo.api.cms.ux.CmsEditable;
18 import org.argeo.api.cms.ux.CmsStyle;
19 import org.argeo.app.api.EntityNames;
20 import org.argeo.app.api.EntityType;
21 import org.argeo.app.swt.ux.SwtArgeoApp;
22 import org.argeo.app.ux.SuiteStyle;
23 import org.argeo.app.ux.SuiteUxEvent;
24 import org.argeo.cms.Localized;
25 import org.argeo.cms.jcr.acr.JcrContent;
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(
246 Paths.get(System.getProperty("user.home") + "/tmp/" + fileNode.getName()));) {
247 // BufferedImage img = ImageIO.read(in);
248 // System.out.println(fileNode.getName() + ": width=" + img.getWidth() + ", height=" + img.getHeight());
249 IOUtils.copy(in, out);
250 } catch (IOException e) {
251 throw new RuntimeException(e);
252 }
253
254 // try (InputStream in = JcrUtils.getFileAsStream(fileNode);) {
255 // ImageData imageData = new ImageData(in);
256 // System.out.println(fileNode.getName() + ": width=" + imageData.width + ", height=" + imageData.height);
257 // } catch (IOException e) {
258 // throw new RuntimeException(e);
259 // }
260 }
261 // TODO move it deeper in the middleware.
262 if (!content.isNodeType(EntityType.box.get())) {
263 if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
264 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
265 ImageData imageData = new ImageData(in);
266 content.addMixin(EntityType.box.get());
267 content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
268 content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
269 content.getSession().save();
270 } catch (IOException e) {
271 throw new RuntimeException(e);
272 }
273 }
274 }
275
276 // TODO optimise
277 Long width;
278 Long height;
279 if (content.isNodeType(EntityType.box.get())) {
280 width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
281 height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
282 } else {
283 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
284 ImageData imageData = new ImageData(in);
285 width = Long.valueOf(imageData.width);
286 height = Long.valueOf(imageData.height);
287 } catch (IOException e) {
288 throw new RuntimeException(e);
289 }
290 }
291
292 if (maxWidth != null && width > maxWidth) {
293 Double ratio = maxWidth.doubleValue() / width.doubleValue();
294 width = maxWidth.longValue();
295 height = Math.round(ratio * height);
296 }
297 Label img = new Label(parent, SWT.NONE);
298 CmsSwtUtils.markup(img);
299 StringBuffer txt = new StringBuffer();
300 String target = toLink(link);
301 if (target != null)
302 txt.append("<a href='").append(target).append("'>");
303 txt.append(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
304 if (target != null)
305 txt.append("</a>");
306 img.setText(txt.toString());
307 if (parent.getLayout() instanceof GridLayout) {
308 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
309 gd.widthHint = width.intValue();
310 gd.heightHint = height.intValue();
311 img.setLayoutData(gd);
312 }
313
314 if (target == null)
315 img.addMouseListener(new MouseListener() {
316 private static final long serialVersionUID = -1362242049325206168L;
317
318 @Override
319 public void mouseUp(MouseEvent e) {
320 }
321
322 @Override
323 public void mouseDown(MouseEvent e) {
324 }
325
326 @Override
327 public void mouseDoubleClick(MouseEvent e) {
328 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
329
330 @Override
331 protected Control createDialogArea(Composite parent) {
332 parent.setLayout(new GridLayout());
333 ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
334 scroll.setLayoutData(CmsSwtUtils.fillAll());
335 scroll.setLayout(CmsSwtUtils.noSpaceGridLayout());
336 scroll.setExpandHorizontal(true);
337 scroll.setExpandVertical(true);
338 // scroll.setAlwaysShowScrollBars(true);
339
340 Composite c = new Composite(scroll, SWT.NONE);
341 scroll.setContent(c);
342 c.setLayout(new GridLayout());
343 c.setLayoutData(CmsSwtUtils.fillAll());
344 Label bigImg = new Label(c, SWT.NONE);
345 CmsSwtUtils.markup(bigImg);
346 bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
347 Jcr.get(content, EntityNames.SVG_HEIGHT)));
348 bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
349 return bigImg;
350 }
351
352 @Override
353 protected Point getInitialSize() {
354 Point shellSize = img.getShell().getSize();
355 return new Point(shellSize.x - 100, shellSize.y - 100);
356 }
357
358 };
359 dialog.open();
360 }
361 });
362 return img;
363 }
364
365 public static String toLink(Content node) {
366 return node != null ? "#" + CmsSwtUtils.cleanPathForUrl(SwtArgeoApp.nodeToState(node)) : null;
367 }
368
369 public static String toLink(Node node) {
370 return node != null ? "#" + CmsSwtUtils.cleanPathForUrl(SwtArgeoApp.nodeToState(JcrContent.nodeToContent(node)))
371 : null;
372 }
373
374 public static Control addLink(Composite parent, String label, Node node, CmsStyle style)
375 throws RepositoryException {
376 String target = toLink(node);
377 CmsLink link = new CmsLink(label, target, style);
378 return link.createUi(parent, node);
379 }
380
381 public static Control addExternalLink(Composite parent, String label, String url, String plainCssAnchorClass,
382 boolean newWindow) throws RepositoryException {
383 Label lbl = new Label(parent, SWT.NONE);
384 CmsSwtUtils.markup(lbl);
385 StringBuilder txt = new StringBuilder();
386 txt.append("<a");
387 if (plainCssAnchorClass != null)
388 txt.append(" class='" + plainCssAnchorClass + "'");
389 txt.append(" href='").append(url).append("'");
390 if (newWindow) {
391 txt.append(" target='blank_'");
392 }
393 txt.append(">");
394 txt.append(label);
395 txt.append("</a>");
396 lbl.setText(txt.toString());
397 return lbl;
398 }
399
400 @Deprecated
401 public static Map<String, Object> eventProperties(Node node) {
402 Map<String, Object> properties = new HashMap<>();
403 String contentPath = '/' + Jcr.getWorkspaceName(node) + Jcr.getPath(node);
404 properties.put(SuiteUxEvent.CONTENT_PATH, contentPath);
405 return properties;
406 }
407
408 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
409 // String... additionnalProps) {
410 //
411 // Session tmpSession = null;
412 // Session mainSession = null;
413 // try {
414 // // FIXME would not work if home is another physical workspace
415 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
416 // Node draftNode = null;
417 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
418 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
419 // }
420 // Wizard wizard = null;
421 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
422 // // WizardDialog dialog = new WizardDialog(shell, wizard);
423 // if (dialog.open() == Window.OK) {
424 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
425 // // FIXME it should be possible to specify the workspace
426 // mainSession = referenceSession.getRepository().login();
427 // Node parent = mainSession.getNode(parentPath);
428 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
429 //// task = appService.saveEntity(task, false);
430 // referenceSession.refresh(true);
431 // return task.getPath();
432 // }
433 // return null;
434 // } catch (RepositoryException e1) {
435 // throw new JcrException(
436 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
437 // } finally {
438 // JcrUtils.logoutQuietly(tmpSession);
439 // JcrUtils.logoutQuietly(mainSession);
440 // }
441 // }
442
443 }