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