1 package org
.argeo
.suite
.ui
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
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
.cms
.LocaleUtils
;
16 import org
.argeo
.cms
.Localized
;
17 import org
.argeo
.cms
.auth
.CurrentUser
;
18 import org
.argeo
.cms
.swt
.CmsIcon
;
19 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
20 import org
.argeo
.cms
.swt
.dialogs
.LightweightDialog
;
21 import org
.argeo
.cms
.ui
.util
.CmsLink
;
22 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
23 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
24 import org
.argeo
.entity
.EntityNames
;
25 import org
.argeo
.entity
.EntityType
;
26 import org
.argeo
.jcr
.Jcr
;
27 import org
.argeo
.jcr
.JcrException
;
28 import org
.argeo
.jcr
.JcrUtils
;
29 import org
.argeo
.suite
.SuiteRole
;
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
;
45 /** UI utilities related to the APAF project. */
46 public class SuiteUiUtils
{
49 private SuiteUiUtils() {
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
);
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
);
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");
72 public static Label
addFormLabel(Composite parent
, String label
) {
73 Label lbl
= new Label(parent
, SWT
.WRAP
);
75 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
76 CmsSwtUtils
.style(lbl
, SuiteStyle
.simpleLabel
);
80 public static Text
addFormTextField(Composite parent
, String text
, String message
) {
81 return addFormTextField(parent
, text
, message
, SWT
.NONE
);
84 public static Text
addFormTextField(Composite parent
, String text
, String message
, int style
) {
85 Text txt
= new Text(parent
, style
);
89 txt
.setMessage(message
);
90 txt
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, true));
91 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleText
);
95 public static Text
addFormInputField(Composite parent
, String placeholder
) {
96 Text txt
= new Text(parent
, SWT
.BORDER
);
98 GridData gridData
= CmsSwtUtils
.fillWidth();
99 txt
.setLayoutData(gridData
);
101 if (placeholder
!= null)
102 txt
.setText(placeholder
);
104 CmsSwtUtils
.style(txt
, SuiteStyle
.simpleInput
);
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());
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());
137 txt
.setEditable(false);
139 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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());
155 * creates a single horizontal-block composite for key:value display, with
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());
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());
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());
200 txt
.setEditable(false);
202 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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));
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
);
221 return addPicture(lineComposite
, fileNode
);
224 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
225 return addPicture(parent
, fileNode
, null);
228 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
229 return addPicture(parent
, fileNode
, maxWidth
, null);
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
);
253 if (content
.isNodeType(EntityType
.box
.get())) {
254 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
255 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
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
);
266 if (maxWidth
!= null && width
> maxWidth
) {
267 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
268 width
= maxWidth
.longValue();
269 height
= Math
.round(ratio
* height
);
271 Label img
= new Label(parent
, SWT
.NONE
);
272 CmsSwtUtils
.markup(img
);
273 StringBuffer txt
= new StringBuffer();
274 String target
= toLink(link
);
276 txt
.append("<a href='").append(target
).append("'>");
277 txt
.append(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
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
);
289 img
.addMouseListener(new MouseListener() {
290 private static final long serialVersionUID
= -1362242049325206168L;
293 public void mouseUp(MouseEvent e
) {
297 public void mouseDown(MouseEvent e
) {
301 public void mouseDoubleClick(MouseEvent e
) {
302 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
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);
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));
327 protected Point
getInitialSize() {
328 Point shellSize
= img
.getShell().getSize();
329 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
339 public static String
toLink(Node node
) {
340 return node
!= null ?
"#" + CmsUiUtils
.cleanPathForUrl(SuiteApp
.nodeToState(node
)) : null;
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
);
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("'");
358 txt
.append(" target='blank_'");
363 lbl
.setText(txt
.toString());
367 public static boolean isCoworker(CmsView cmsView
) {
368 boolean coworker
= cmsView
.doAs(() -> CurrentUser
.isInRole(SuiteRole
.coworker
.dn()));
372 public static boolean isTopic(Event event
, CmsEvent cmsEvent
) {
373 return event
.getTopic().equals(cmsEvent
.topic());
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
);
382 button
.setImage(icon
.getBigIcon(theme
));
383 button
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.BOTTOM
, true, false));
384 // button.setToolTipText(msg.lead());
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();
391 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true, false));
393 CmsSwtUtils
.sendEventOnSelect(button
, SuiteEvent
.switchLayer
.topic(), SuiteEvent
.LAYER
, layer
);
397 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
398 // String... additionnalProps) {
400 // Session tmpSession = null;
401 // Session mainSession = null;
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]);
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();
423 // } catch (RepositoryException e1) {
424 // throw new JcrException(
425 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
427 // JcrUtils.logoutQuietly(tmpSession);
428 // JcrUtils.logoutQuietly(mainSession);