1 package org
.argeo
.app
.ui
;
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
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
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
;
47 /** UI utilities related to the APAF project. */
48 public class SuiteUiUtils
{
51 private SuiteUiUtils() {
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
);
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
);
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");
74 public static Label
addFormLabel(Composite parent
, Localized msg
) {
75 return addFormLabel(parent
, msg
.lead());
78 public static Label
addFormLabel(Composite parent
, String label
) {
79 Label lbl
= new Label(parent
, SWT
.WRAP
);
81 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
82 CmsSwtUtils
.style(lbl
, SuiteStyle
.simpleLabel
);
86 public static Text
addFormTextField(Composite parent
, String text
, String message
) {
87 return addFormTextField(parent
, text
, message
, SWT
.NONE
);
90 public static Text
addFormTextField(Composite parent
, String text
, String message
, int style
) {
91 Text txt
= new Text(parent
, style
);
95 txt
.setMessage(message
);
96 txt
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, true));
97 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleText
);
101 public static Text
addFormInputField(Composite parent
, String placeholder
) {
102 Text txt
= new Text(parent
, SWT
.BORDER
);
104 GridData gridData
= CmsSwtUtils
.fillWidth();
105 txt
.setLayoutData(gridData
);
107 if (placeholder
!= null)
108 txt
.setText(placeholder
);
110 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleInput
);
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());
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());
143 txt
.setEditable(false);
145 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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());
161 * creates a single horizontal-block composite for key:value display, with
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());
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());
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());
206 txt
.setEditable(false);
208 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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));
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
);
227 return addPicture(lineComposite
, fileNode
);
230 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
231 return addPicture(parent
, fileNode
, null);
234 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
235 return addPicture(parent
, fileNode
, maxWidth
, null);
238 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
, Node link
)
239 throws RepositoryException
{
240 Node content
= fileNode
.getNode(Node
.JCR_CONTENT
);
242 boolean test
= false;
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
);
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);
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
);
279 if (content
.isNodeType(EntityType
.box
.get())) {
280 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
281 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
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
);
292 if (maxWidth
!= null && width
> maxWidth
) {
293 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
294 width
= maxWidth
.longValue();
295 height
= Math
.round(ratio
* height
);
297 Label img
= new Label(parent
, SWT
.NONE
);
298 CmsSwtUtils
.markup(img
);
299 StringBuffer txt
= new StringBuffer();
300 String target
= toLink(link
);
302 txt
.append("<a href='").append(target
).append("'>");
303 txt
.append(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
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
);
315 img
.addMouseListener(new MouseListener() {
316 private static final long serialVersionUID
= -1362242049325206168L;
319 public void mouseUp(MouseEvent e
) {
323 public void mouseDown(MouseEvent e
) {
327 public void mouseDoubleClick(MouseEvent e
) {
328 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
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);
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));
353 protected Point
getInitialSize() {
354 Point shellSize
= img
.getShell().getSize();
355 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
365 public static String
toLink(Content node
) {
366 return node
!= null ?
"#" + CmsSwtUtils
.cleanPathForUrl(SuiteApp
.nodeToState(node
)) : null;
369 public static String
toLink(Node node
) {
370 return node
!= null ?
"#" + CmsSwtUtils
.cleanPathForUrl(SuiteApp
.nodeToState(JcrContent
.nodeToContent(node
)))
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
);
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();
387 if (plainCssAnchorClass
!= null)
388 txt
.append(" class='" + plainCssAnchorClass
+ "'");
389 txt
.append(" href='").append(url
).append("'");
391 txt
.append(" target='blank_'");
396 lbl
.setText(txt
.toString());
400 // public static boolean isCoworker(CmsView cmsView) {
401 // boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
405 public static boolean isTopic(String topic
, CmsEvent cmsEvent
) {
406 Objects
.requireNonNull(topic
);
407 return topic
.equals(cmsEvent
.topic());
410 public static Button
createLayerButton(Composite parent
, String layer
, Localized msg
, CmsIcon icon
,
411 ClassLoader l10nClassLoader
) {
412 CmsSwtTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
413 Button button
= new Button(parent
, SWT
.PUSH
);
414 CmsSwtUtils
.style(button
, SuiteStyle
.leadPane
);
416 button
.setImage(theme
.getBigIcon(icon
));
417 button
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.BOTTOM
, true, false));
418 // button.setToolTipText(msg.lead());
420 Label lbl
= new Label(parent
, SWT
.CENTER
);
421 CmsSwtUtils
.style(lbl
, SuiteStyle
.leadPane
);
422 String txt
= LocaleUtils
.lead(msg
, l10nClassLoader
);
423 // String txt = msg.lead();
425 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true, false));
427 CmsSwtUtils
.sendEventOnSelect(button
, SuiteUxEvent
.switchLayer
.topic(), SuiteUxEvent
.LAYER
, layer
);
431 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
432 // String... additionnalProps) {
434 // Session tmpSession = null;
435 // Session mainSession = null;
437 // // FIXME would not work if home is another physical workspace
438 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
439 // Node draftNode = null;
440 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
441 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
443 // Wizard wizard = null;
444 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
445 // // WizardDialog dialog = new WizardDialog(shell, wizard);
446 // if (dialog.open() == Window.OK) {
447 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
448 // // FIXME it should be possible to specify the workspace
449 // mainSession = referenceSession.getRepository().login();
450 // Node parent = mainSession.getNode(parentPath);
451 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
452 //// task = appService.saveEntity(task, false);
453 // referenceSession.refresh(true);
454 // return task.getPath();
457 // } catch (RepositoryException e1) {
458 // throw new JcrException(
459 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
461 // JcrUtils.logoutQuietly(tmpSession);
462 // JcrUtils.logoutQuietly(mainSession);