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