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
.api
.cms
.ux
.CmsView
;
21 import org
.argeo
.app
.api
.EntityNames
;
22 import org
.argeo
.app
.api
.EntityType
;
23 import org
.argeo
.app
.api
.SuiteRole
;
24 import org
.argeo
.cms
.LocaleUtils
;
25 import org
.argeo
.cms
.Localized
;
26 import org
.argeo
.cms
.auth
.CurrentUser
;
27 import org
.argeo
.cms
.jcr
.acr
.JcrContent
;
28 import org
.argeo
.cms
.swt
.CmsSwtTheme
;
29 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
30 import org
.argeo
.cms
.swt
.dialogs
.LightweightDialog
;
31 import org
.argeo
.cms
.ui
.util
.CmsLink
;
32 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
33 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
34 import org
.argeo
.jcr
.Jcr
;
35 import org
.argeo
.jcr
.JcrUtils
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
38 import org
.eclipse
.swt
.events
.MouseEvent
;
39 import org
.eclipse
.swt
.events
.MouseListener
;
40 import org
.eclipse
.swt
.graphics
.ImageData
;
41 import org
.eclipse
.swt
.graphics
.Point
;
42 import org
.eclipse
.swt
.layout
.GridData
;
43 import org
.eclipse
.swt
.layout
.GridLayout
;
44 import org
.eclipse
.swt
.widgets
.Button
;
45 import org
.eclipse
.swt
.widgets
.Composite
;
46 import org
.eclipse
.swt
.widgets
.Control
;
47 import org
.eclipse
.swt
.widgets
.Label
;
48 import org
.eclipse
.swt
.widgets
.Text
;
50 /** UI utilities related to the APAF project. */
51 public class SuiteUiUtils
{
54 private SuiteUiUtils() {
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
);
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
);
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");
77 public static Label
addFormLabel(Composite parent
, String label
) {
78 Label lbl
= new Label(parent
, SWT
.WRAP
);
80 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
81 CmsSwtUtils
.style(lbl
, SuiteStyle
.simpleLabel
);
85 public static Text
addFormTextField(Composite parent
, String text
, String message
) {
86 return addFormTextField(parent
, text
, message
, SWT
.NONE
);
89 public static Text
addFormTextField(Composite parent
, String text
, String message
, int style
) {
90 Text txt
= new Text(parent
, style
);
94 txt
.setMessage(message
);
95 txt
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, true));
96 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleText
);
100 public static Text
addFormInputField(Composite parent
, String placeholder
) {
101 Text txt
= new Text(parent
, SWT
.BORDER
);
103 GridData gridData
= CmsSwtUtils
.fillWidth();
104 txt
.setLayoutData(gridData
);
106 if (placeholder
!= null)
107 txt
.setText(placeholder
);
109 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleInput
);
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());
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());
142 txt
.setEditable(false);
144 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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());
160 * creates a single horizontal-block composite for key:value display, with
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());
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());
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());
205 txt
.setEditable(false);
207 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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));
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
);
226 return addPicture(lineComposite
, fileNode
);
229 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
230 return addPicture(parent
, fileNode
, null);
233 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
234 return addPicture(parent
, fileNode
, maxWidth
, null);
237 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
, Node link
)
238 throws RepositoryException
{
239 Node content
= fileNode
.getNode(Node
.JCR_CONTENT
);
241 boolean test
= false;
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
);
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);
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
);
277 if (content
.isNodeType(EntityType
.box
.get())) {
278 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
279 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
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
);
290 if (maxWidth
!= null && width
> maxWidth
) {
291 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
292 width
= maxWidth
.longValue();
293 height
= Math
.round(ratio
* height
);
295 Label img
= new Label(parent
, SWT
.NONE
);
296 CmsSwtUtils
.markup(img
);
297 StringBuffer txt
= new StringBuffer();
298 String target
= toLink(link
);
300 txt
.append("<a href='").append(target
).append("'>");
301 txt
.append(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
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
);
313 img
.addMouseListener(new MouseListener() {
314 private static final long serialVersionUID
= -1362242049325206168L;
317 public void mouseUp(MouseEvent e
) {
321 public void mouseDown(MouseEvent e
) {
325 public void mouseDoubleClick(MouseEvent e
) {
326 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
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);
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));
351 protected Point
getInitialSize() {
352 Point shellSize
= img
.getShell().getSize();
353 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
363 public static String
toLink(Content node
) {
364 return node
!= null ?
"#" + CmsSwtUtils
.cleanPathForUrl(SuiteApp
.nodeToState(node
)) : null;
367 public static String
toLink(Node node
) {
368 return node
!= null ?
"#" + CmsSwtUtils
.cleanPathForUrl(SuiteApp
.nodeToState(JcrContent
.nodeToContent(node
)))
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
);
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("'");
387 txt
.append(" target='blank_'");
392 lbl
.setText(txt
.toString());
396 public static boolean isCoworker(CmsView cmsView
) {
397 boolean coworker
= cmsView
.doAs(() -> CurrentUser
.isInRole(SuiteRole
.coworker
.dn()));
401 public static boolean isTopic(String topic
, CmsEvent cmsEvent
) {
402 Objects
.requireNonNull(topic
);
403 return topic
.equals(cmsEvent
.topic());
406 public static Button
createLayerButton(Composite parent
, String layer
, Localized msg
, CmsIcon icon
,
407 ClassLoader l10nClassLoader
) {
408 CmsSwtTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
409 Button button
= new Button(parent
, SWT
.PUSH
);
410 CmsSwtUtils
.style(button
, SuiteStyle
.leadPane
);
412 button
.setImage(theme
.getBigIcon(icon
));
413 button
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.BOTTOM
, true, false));
414 // button.setToolTipText(msg.lead());
416 Label lbl
= new Label(parent
, SWT
.CENTER
);
417 CmsSwtUtils
.style(lbl
, SuiteStyle
.leadPane
);
418 String txt
= LocaleUtils
.lead(msg
, l10nClassLoader
);
419 // String txt = msg.lead();
421 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true, false));
423 CmsSwtUtils
.sendEventOnSelect(button
, SuiteEvent
.switchLayer
.topic(), SuiteEvent
.LAYER
, layer
);
427 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
428 // String... additionnalProps) {
430 // Session tmpSession = null;
431 // Session mainSession = null;
433 // // FIXME would not work if home is another physical workspace
434 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
435 // Node draftNode = null;
436 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
437 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
439 // Wizard wizard = null;
440 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
441 // // WizardDialog dialog = new WizardDialog(shell, wizard);
442 // if (dialog.open() == Window.OK) {
443 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
444 // // FIXME it should be possible to specify the workspace
445 // mainSession = referenceSession.getRepository().login();
446 // Node parent = mainSession.getNode(parentPath);
447 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
448 //// task = appService.saveEntity(task, false);
449 // referenceSession.refresh(true);
450 // return task.getPath();
453 // } catch (RepositoryException e1) {
454 // throw new JcrException(
455 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
457 // JcrUtils.logoutQuietly(tmpSession);
458 // JcrUtils.logoutQuietly(mainSession);