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