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
.HashMap
;
10 import java
.util
.Objects
;
12 import javax
.jcr
.Node
;
13 import javax
.jcr
.RepositoryException
;
14 import javax
.jcr
.Session
;
16 import org
.apache
.commons
.io
.IOUtils
;
17 import org
.argeo
.api
.acr
.Content
;
18 import org
.argeo
.api
.cms
.CmsEvent
;
19 import org
.argeo
.api
.cms
.ux
.CmsEditable
;
20 import org
.argeo
.api
.cms
.ux
.CmsIcon
;
21 import org
.argeo
.api
.cms
.ux
.CmsStyle
;
22 import org
.argeo
.app
.api
.EntityNames
;
23 import org
.argeo
.app
.api
.EntityType
;
24 import org
.argeo
.app
.ux
.SuiteStyle
;
25 import org
.argeo
.app
.ux
.SuiteUxEvent
;
26 import org
.argeo
.cms
.LocaleUtils
;
27 import org
.argeo
.cms
.Localized
;
28 import org
.argeo
.cms
.jcr
.acr
.JcrContent
;
29 import org
.argeo
.cms
.swt
.CmsSwtTheme
;
30 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
31 import org
.argeo
.cms
.swt
.dialogs
.LightweightDialog
;
32 import org
.argeo
.cms
.ui
.util
.CmsLink
;
33 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
34 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
35 import org
.argeo
.jcr
.Jcr
;
36 import org
.argeo
.jcr
.JcrUtils
;
37 import org
.eclipse
.swt
.SWT
;
38 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
39 import org
.eclipse
.swt
.events
.MouseEvent
;
40 import org
.eclipse
.swt
.events
.MouseListener
;
41 import org
.eclipse
.swt
.graphics
.ImageData
;
42 import org
.eclipse
.swt
.graphics
.Point
;
43 import org
.eclipse
.swt
.layout
.GridData
;
44 import org
.eclipse
.swt
.layout
.GridLayout
;
45 import org
.eclipse
.swt
.widgets
.Button
;
46 import org
.eclipse
.swt
.widgets
.Composite
;
47 import org
.eclipse
.swt
.widgets
.Control
;
48 import org
.eclipse
.swt
.widgets
.Label
;
49 import org
.eclipse
.swt
.widgets
.Text
;
51 /** UI utilities related to the APAF project. */
52 public class SuiteUiUtils
{
55 private SuiteUiUtils() {
58 /** creates a title bar composite with label and optional button */
59 public static void addTitleBar(Composite parent
, String title
, Boolean isEditable
) {
60 Composite titleBar
= new Composite(parent
, SWT
.NONE
);
61 titleBar
.setLayoutData(CmsSwtUtils
.fillWidth());
62 CmsSwtUtils
.style(titleBar
, SuiteStyle
.titleContainer
);
64 titleBar
.setLayout(CmsSwtUtils
.noSpaceGridLayout(new GridLayout(2, false)));
65 Label titleLbl
= new Label(titleBar
, SWT
.NONE
);
66 titleLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, true));
67 CmsSwtUtils
.style(titleLbl
, SuiteStyle
.titleLabel
);
68 titleLbl
.setText(title
);
71 Button editBtn
= new Button(titleBar
, SWT
.PUSH
);
72 editBtn
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false));
73 CmsSwtUtils
.style(editBtn
, SuiteStyle
.inlineButton
);
74 editBtn
.setText("Edit");
78 public static Label
addFormLabel(Composite parent
, Localized msg
) {
79 return addFormLabel(parent
, msg
.lead());
82 public static Label
addFormLabel(Composite parent
, String label
) {
83 Label lbl
= new Label(parent
, SWT
.WRAP
);
85 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
86 CmsSwtUtils
.style(lbl
, SuiteStyle
.simpleLabel
);
90 public static Text
addFormTextField(Composite parent
, String text
, String message
) {
91 return addFormTextField(parent
, text
, message
, SWT
.NONE
);
94 public static Text
addFormTextField(Composite parent
, String text
, String message
, int style
) {
95 Text txt
= new Text(parent
, style
);
99 txt
.setMessage(message
);
100 txt
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, true));
101 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleText
);
105 public static Text
addFormInputField(Composite parent
, String placeholder
) {
106 Text txt
= new Text(parent
, SWT
.BORDER
);
108 GridData gridData
= CmsSwtUtils
.fillWidth();
109 txt
.setLayoutData(gridData
);
111 if (placeholder
!= null)
112 txt
.setText(placeholder
);
114 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleInput
);
118 /** creates a single horizontal-block composite for key:value display */
119 public static Text
addFormLine(Composite parent
, String label
, String text
) {
120 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
121 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
122 lineComposite
.setLayout(new GridLayout(2, false));
123 CmsSwtUtils
.style(lineComposite
, SuiteStyle
.formLine
);
124 addFormLabel(lineComposite
, label
);
125 Text txt
= addFormTextField(lineComposite
, text
, null);
126 txt
.setEditable(false);
127 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
131 public static Text
addFormLine(Composite parent
, String label
, Node node
, String property
,
132 CmsEditable cmsEditable
) {
133 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
134 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
135 lineComposite
.setLayout(new GridLayout(2, false));
136 CmsSwtUtils
.style(lineComposite
, SuiteStyle
.formLine
);
137 addFormLabel(lineComposite
, label
);
138 String text
= Jcr
.get(node
, property
);
139 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
140 Text txt
= addFormTextField(lineComposite
, text
, null, SWT
.WRAP
);
141 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
142 txt
.addModifyListener((e
) -> {
143 Jcr
.set(node
, property
, txt
.getText());
147 txt
.setEditable(false);
149 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
153 public static Text
addFormInput(Composite parent
, String label
, String placeholder
) {
154 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
155 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
156 lineComposite
.setLayout(new GridLayout(2, false));
157 CmsSwtUtils
.style(lineComposite
, SuiteStyle
.formLine
);
158 addFormLabel(lineComposite
, label
);
159 Text txt
= addFormInputField(lineComposite
, placeholder
);
160 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
165 * creates a single horizontal-block composite for key:value display, with
168 public static Text
addFormLine(Composite parent
, String label
, String text
, Integer offset
) {
169 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
170 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
171 lineComposite
.setLayout(new GridLayout(3, false));
172 CmsSwtUtils
.style(lineComposite
, SuiteStyle
.formLine
);
173 Label offsetLbl
= new Label(lineComposite
, SWT
.NONE
);
174 GridData gridData
= new GridData();
175 gridData
.widthHint
= offset
;
176 offsetLbl
.setLayoutData(gridData
);
177 addFormLabel(lineComposite
, label
);
178 Text txt
= addFormTextField(lineComposite
, text
, null);
179 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
183 /** creates a single vertical-block composite for key:value display */
184 public static Text
addFormColumn(Composite parent
, String label
, String text
) {
185 // Composite columnComposite = new Composite(parent, SWT.NONE);
186 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
187 // columnComposite.setLayout(new GridLayout(1, false));
188 addFormLabel(parent
, label
);
189 Text txt
= addFormTextField(parent
, text
, null);
190 txt
.setEditable(false);
191 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
195 public static Text
addFormColumn(Composite parent
, String label
, Node node
, String property
,
196 CmsEditable cmsEditable
) {
197 // Composite columnComposite = new Composite(parent, SWT.NONE);
198 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
199 // columnComposite.setLayout(new GridLayout(1, false));
200 addFormLabel(parent
, label
);
201 String text
= Jcr
.get(node
, property
);
202 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
203 Text txt
= addFormTextField(parent
, text
, null, SWT
.WRAP
);
204 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
205 txt
.addModifyListener((e
) -> {
206 Jcr
.set(node
, property
, txt
.getText());
210 txt
.setEditable(false);
212 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
216 public static Label
createBoldLabel(Composite parent
, Localized localized
) {
217 Label label
= new Label(parent
, SWT
.LEAD
);
218 label
.setText(localized
.lead());
219 label
.setFont(EclipseUiUtils
.getBoldFont(parent
));
220 label
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false));
224 public static Label
addFormPicture(Composite parent
, String label
, Node fileNode
) throws RepositoryException
{
225 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
226 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
227 lineComposite
.setLayout(new GridLayout(2, true));
228 CmsSwtUtils
.style(lineComposite
, SuiteStyle
.formLine
);
229 addFormLabel(lineComposite
, label
);
231 return addPicture(lineComposite
, fileNode
);
234 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
235 return addPicture(parent
, fileNode
, null);
238 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
239 return addPicture(parent
, fileNode
, maxWidth
, null);
242 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
, Node link
)
243 throws RepositoryException
{
244 Node content
= fileNode
.getNode(Node
.JCR_CONTENT
);
246 boolean test
= false;
248 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
);
249 OutputStream out
= Files
.newOutputStream(
250 Paths
.get(System
.getProperty("user.home") + "/tmp/" + fileNode
.getName()));) {
251 // BufferedImage img = ImageIO.read(in);
252 // System.out.println(fileNode.getName() + ": width=" + img.getWidth() + ", height=" + img.getHeight());
253 IOUtils
.copy(in
, out
);
254 } catch (IOException e
) {
255 throw new RuntimeException(e
);
258 // try (InputStream in = JcrUtils.getFileAsStream(fileNode);) {
259 // ImageData imageData = new ImageData(in);
260 // System.out.println(fileNode.getName() + ": width=" + imageData.width + ", height=" + imageData.height);
261 // } catch (IOException e) {
262 // throw new RuntimeException(e);
265 // TODO move it deeper in the middleware.
266 if (!content
.isNodeType(EntityType
.box
.get())) {
267 if (content
.getSession().hasPermission(content
.getPath(), Session
.ACTION_SET_PROPERTY
)) {
268 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
269 ImageData imageData
= new ImageData(in
);
270 content
.addMixin(EntityType
.box
.get());
271 content
.setProperty(EntityNames
.SVG_WIDTH
, imageData
.width
);
272 content
.setProperty(EntityNames
.SVG_HEIGHT
, imageData
.height
);
273 content
.getSession().save();
274 } catch (IOException e
) {
275 throw new RuntimeException(e
);
283 if (content
.isNodeType(EntityType
.box
.get())) {
284 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
285 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
287 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
288 ImageData imageData
= new ImageData(in
);
289 width
= Long
.valueOf(imageData
.width
);
290 height
= Long
.valueOf(imageData
.height
);
291 } catch (IOException e
) {
292 throw new RuntimeException(e
);
296 if (maxWidth
!= null && width
> maxWidth
) {
297 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
298 width
= maxWidth
.longValue();
299 height
= Math
.round(ratio
* height
);
301 Label img
= new Label(parent
, SWT
.NONE
);
302 CmsSwtUtils
.markup(img
);
303 StringBuffer txt
= new StringBuffer();
304 String target
= toLink(link
);
306 txt
.append("<a href='").append(target
).append("'>");
307 txt
.append(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
310 img
.setText(txt
.toString());
311 if (parent
.getLayout() instanceof GridLayout
) {
312 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
313 gd
.widthHint
= width
.intValue();
314 gd
.heightHint
= height
.intValue();
315 img
.setLayoutData(gd
);
319 img
.addMouseListener(new MouseListener() {
320 private static final long serialVersionUID
= -1362242049325206168L;
323 public void mouseUp(MouseEvent e
) {
327 public void mouseDown(MouseEvent e
) {
331 public void mouseDoubleClick(MouseEvent e
) {
332 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
335 protected Control
createDialogArea(Composite parent
) {
336 parent
.setLayout(new GridLayout());
337 ScrolledComposite scroll
= new ScrolledComposite(parent
, SWT
.H_SCROLL
| SWT
.V_SCROLL
);
338 scroll
.setLayoutData(CmsSwtUtils
.fillAll());
339 scroll
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
340 scroll
.setExpandHorizontal(true);
341 scroll
.setExpandVertical(true);
342 // scroll.setAlwaysShowScrollBars(true);
344 Composite c
= new Composite(scroll
, SWT
.NONE
);
345 scroll
.setContent(c
);
346 c
.setLayout(new GridLayout());
347 c
.setLayoutData(CmsSwtUtils
.fillAll());
348 Label bigImg
= new Label(c
, SWT
.NONE
);
349 CmsSwtUtils
.markup(bigImg
);
350 bigImg
.setText(CmsUiUtils
.img(fileNode
, Jcr
.get(content
, EntityNames
.SVG_WIDTH
),
351 Jcr
.get(content
, EntityNames
.SVG_HEIGHT
)));
352 bigImg
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, true));
357 protected Point
getInitialSize() {
358 Point shellSize
= img
.getShell().getSize();
359 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
369 public static String
toLink(Content node
) {
370 return node
!= null ?
"#" + CmsSwtUtils
.cleanPathForUrl(SuiteApp
.nodeToState(node
)) : null;
373 public static String
toLink(Node node
) {
374 return node
!= null ?
"#" + CmsSwtUtils
.cleanPathForUrl(SuiteApp
.nodeToState(JcrContent
.nodeToContent(node
)))
378 public static Control
addLink(Composite parent
, String label
, Node node
, CmsStyle style
)
379 throws RepositoryException
{
380 String target
= toLink(node
);
381 CmsLink link
= new CmsLink(label
, target
, style
);
382 return link
.createUi(parent
, node
);
385 public static Control
addExternalLink(Composite parent
, String label
, String url
, String plainCssAnchorClass
,
386 boolean newWindow
) throws RepositoryException
{
387 Label lbl
= new Label(parent
, SWT
.NONE
);
388 CmsSwtUtils
.markup(lbl
);
389 StringBuilder txt
= new StringBuilder();
391 if (plainCssAnchorClass
!= null)
392 txt
.append(" class='" + plainCssAnchorClass
+ "'");
393 txt
.append(" href='").append(url
).append("'");
395 txt
.append(" target='blank_'");
400 lbl
.setText(txt
.toString());
404 // public static boolean isCoworker(CmsView cmsView) {
405 // boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
409 public static boolean isTopic(String topic
, CmsEvent cmsEvent
) {
410 Objects
.requireNonNull(topic
);
411 return topic
.equals(cmsEvent
.topic());
414 public static Button
createLayerButton(Composite parent
, String layer
, Localized msg
, CmsIcon icon
,
415 ClassLoader l10nClassLoader
) {
416 CmsSwtTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
417 Button button
= new Button(parent
, SWT
.PUSH
);
418 CmsSwtUtils
.style(button
, SuiteStyle
.leadPane
);
420 button
.setImage(theme
.getBigIcon(icon
));
421 button
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.BOTTOM
, true, false));
422 // button.setToolTipText(msg.lead());
424 Label lbl
= new Label(parent
, SWT
.CENTER
);
425 CmsSwtUtils
.style(lbl
, SuiteStyle
.leadPane
);
426 String txt
= LocaleUtils
.lead(msg
, l10nClassLoader
);
427 // String txt = msg.lead();
429 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true, false));
431 CmsSwtUtils
.sendEventOnSelect(button
, SuiteUxEvent
.switchLayer
.topic(), SuiteUxEvent
.LAYER
, layer
);
436 public static Map
<String
, Object
> eventProperties(Node node
) {
437 Map
<String
, Object
> properties
= new HashMap
<>();
438 String contentPath
= '/' + Jcr
.getWorkspaceName(node
) + Jcr
.getPath(node
);
439 properties
.put(SuiteUxEvent
.CONTENT_PATH
, contentPath
);
443 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
444 // String... additionnalProps) {
446 // Session tmpSession = null;
447 // Session mainSession = null;
449 // // FIXME would not work if home is another physical workspace
450 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
451 // Node draftNode = null;
452 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
453 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
455 // Wizard wizard = null;
456 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
457 // // WizardDialog dialog = new WizardDialog(shell, wizard);
458 // if (dialog.open() == Window.OK) {
459 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
460 // // FIXME it should be possible to specify the workspace
461 // mainSession = referenceSession.getRepository().login();
462 // Node parent = mainSession.getNode(parentPath);
463 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
464 //// task = appService.saveEntity(task, false);
465 // referenceSession.refresh(true);
466 // return task.getPath();
469 // } catch (RepositoryException e1) {
470 // throw new JcrException(
471 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
473 // JcrUtils.logoutQuietly(tmpSession);
474 // JcrUtils.logoutQuietly(mainSession);