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
.cms
.LocaleUtils
;
11 import org
.argeo
.cms
.Localized
;
12 import org
.argeo
.cms
.auth
.CurrentUser
;
13 import org
.argeo
.cms
.ui
.CmsEditable
;
14 import org
.argeo
.cms
.ui
.CmsTheme
;
15 import org
.argeo
.cms
.ui
.CmsView
;
16 import org
.argeo
.cms
.ui
.dialogs
.LightweightDialog
;
17 import org
.argeo
.cms
.ui
.util
.CmsEvent
;
18 import org
.argeo
.cms
.ui
.util
.CmsIcon
;
19 import org
.argeo
.cms
.ui
.util
.CmsLink
;
20 import org
.argeo
.cms
.ui
.util
.CmsStyle
;
21 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
22 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
23 import org
.argeo
.entity
.EntityNames
;
24 import org
.argeo
.entity
.EntityType
;
25 import org
.argeo
.jcr
.Jcr
;
26 import org
.argeo
.jcr
.JcrException
;
27 import org
.argeo
.jcr
.JcrUtils
;
28 import org
.argeo
.suite
.SuiteRole
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
31 import org
.eclipse
.swt
.events
.MouseEvent
;
32 import org
.eclipse
.swt
.events
.MouseListener
;
33 import org
.eclipse
.swt
.graphics
.ImageData
;
34 import org
.eclipse
.swt
.graphics
.Point
;
35 import org
.eclipse
.swt
.layout
.GridData
;
36 import org
.eclipse
.swt
.layout
.GridLayout
;
37 import org
.eclipse
.swt
.widgets
.Button
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Control
;
40 import org
.eclipse
.swt
.widgets
.Label
;
41 import org
.eclipse
.swt
.widgets
.Text
;
42 import org
.osgi
.service
.event
.Event
;
44 /** UI utilities related to the APAF project. */
45 public class SuiteUiUtils
{
48 private SuiteUiUtils() {
51 /** creates a title bar composite with label and optional button */
52 public static void addTitleBar(Composite parent
, String title
, Boolean isEditable
) {
53 Composite titleBar
= new Composite(parent
, SWT
.NONE
);
54 titleBar
.setLayoutData(CmsUiUtils
.fillWidth());
55 CmsUiUtils
.style(titleBar
, SuiteStyle
.titleContainer
);
57 titleBar
.setLayout(CmsUiUtils
.noSpaceGridLayout(new GridLayout(2, false)));
58 Label titleLbl
= new Label(titleBar
, SWT
.NONE
);
59 titleLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, true));
60 CmsUiUtils
.style(titleLbl
, SuiteStyle
.titleLabel
);
61 titleLbl
.setText(title
);
64 Button editBtn
= new Button(titleBar
, SWT
.PUSH
);
65 editBtn
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false));
66 CmsUiUtils
.style(editBtn
, SuiteStyle
.inlineButton
);
67 editBtn
.setText("Edit");
71 public static Label
addFormLabel(Composite parent
, String label
) {
72 Label lbl
= new Label(parent
, SWT
.WRAP
);
74 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
75 CmsUiUtils
.style(lbl
, SuiteStyle
.simpleLabel
);
79 public static Text
addFormTextField(Composite parent
, String text
, String message
) {
80 return addFormTextField(parent
, text
, message
, SWT
.NONE
);
83 public static Text
addFormTextField(Composite parent
, String text
, String message
, int style
) {
84 Text txt
= new Text(parent
, style
);
88 txt
.setMessage(message
);
89 txt
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, true));
90 CmsUiUtils
.style(txt
, SuiteStyle
.simpleText
);
94 public static Text
addFormInputField(Composite parent
, String placeholder
) {
95 Text txt
= new Text(parent
, SWT
.BORDER
);
97 GridData gridData
= CmsUiUtils
.fillWidth();
98 txt
.setLayoutData(gridData
);
100 if (placeholder
!= null)
101 txt
.setText(placeholder
);
103 CmsUiUtils
.style(txt
, SuiteStyle
.simpleInput
);
107 /** creates a single horizontal-block composite for key:value display */
108 public static Text
addFormLine(Composite parent
, String label
, String text
) {
109 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
110 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
111 lineComposite
.setLayout(new GridLayout(2, false));
112 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
113 addFormLabel(lineComposite
, label
);
114 Text txt
= addFormTextField(lineComposite
, text
, null);
115 txt
.setEditable(false);
116 txt
.setLayoutData(CmsUiUtils
.fillWidth());
120 public static Text
addFormLine(Composite parent
, String label
, Node node
, String property
,
121 CmsEditable cmsEditable
) {
122 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
123 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
124 lineComposite
.setLayout(new GridLayout(2, false));
125 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
126 addFormLabel(lineComposite
, label
);
127 String text
= Jcr
.get(node
, property
);
128 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
129 Text txt
= addFormTextField(lineComposite
, text
, null, SWT
.WRAP
);
130 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
131 txt
.addModifyListener((e
) -> {
132 Jcr
.set(node
, property
, txt
.getText());
136 txt
.setEditable(false);
138 txt
.setLayoutData(CmsUiUtils
.fillWidth());
142 public static Text
addFormInput(Composite parent
, String label
, String placeholder
) {
143 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
144 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
145 lineComposite
.setLayout(new GridLayout(2, false));
146 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
147 addFormLabel(lineComposite
, label
);
148 Text txt
= addFormInputField(lineComposite
, placeholder
);
149 txt
.setLayoutData(CmsUiUtils
.fillWidth());
154 * creates a single horizontal-block composite for key:value display, with
157 public static Text
addFormLine(Composite parent
, String label
, String text
, Integer offset
) {
158 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
159 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
160 lineComposite
.setLayout(new GridLayout(3, false));
161 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
162 Label offsetLbl
= new Label(lineComposite
, SWT
.NONE
);
163 GridData gridData
= new GridData();
164 gridData
.widthHint
= offset
;
165 offsetLbl
.setLayoutData(gridData
);
166 addFormLabel(lineComposite
, label
);
167 Text txt
= addFormTextField(lineComposite
, text
, null);
168 txt
.setLayoutData(CmsUiUtils
.fillWidth());
172 /** creates a single vertical-block composite for key:value display */
173 public static Text
addFormColumn(Composite parent
, String label
, String text
) {
174 // Composite columnComposite = new Composite(parent, SWT.NONE);
175 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
176 // columnComposite.setLayout(new GridLayout(1, false));
177 addFormLabel(parent
, label
);
178 Text txt
= addFormTextField(parent
, text
, null);
179 txt
.setEditable(false);
180 txt
.setLayoutData(CmsUiUtils
.fillWidth());
184 public static Text
addFormColumn(Composite parent
, String label
, Node node
, String property
,
185 CmsEditable cmsEditable
) {
186 // Composite columnComposite = new Composite(parent, SWT.NONE);
187 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
188 // columnComposite.setLayout(new GridLayout(1, false));
189 addFormLabel(parent
, label
);
190 String text
= Jcr
.get(node
, property
);
191 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
192 Text txt
= addFormTextField(parent
, text
, null, SWT
.WRAP
);
193 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
194 txt
.addModifyListener((e
) -> {
195 Jcr
.set(node
, property
, txt
.getText());
199 txt
.setEditable(false);
201 txt
.setLayoutData(CmsUiUtils
.fillWidth());
205 public static Label
createBoldLabel(Composite parent
, Localized localized
) {
206 Label label
= new Label(parent
, SWT
.LEAD
);
207 label
.setText(localized
.lead());
208 label
.setFont(EclipseUiUtils
.getBoldFont(parent
));
209 label
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false));
213 public static Label
addFormPicture(Composite parent
, String label
, Node fileNode
) throws RepositoryException
{
214 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
215 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
216 lineComposite
.setLayout(new GridLayout(2, true));
217 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
218 addFormLabel(lineComposite
, label
);
220 return addPicture(lineComposite
, fileNode
);
223 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
224 return addPicture(parent
, fileNode
, null);
227 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
228 return addPicture(parent
, fileNode
, maxWidth
, null);
231 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
, Node link
)
232 throws RepositoryException
{
233 Node content
= fileNode
.getNode(Node
.JCR_CONTENT
);
234 // TODO move it deeper in the middleware.
235 if (!content
.isNodeType(EntityType
.box
.get())) {
236 if (content
.getSession().hasPermission(content
.getPath(), Session
.ACTION_SET_PROPERTY
)) {
237 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
238 ImageData imageData
= new ImageData(in
);
239 content
.addMixin(EntityType
.box
.get());
240 content
.setProperty(EntityNames
.SVG_WIDTH
, imageData
.width
);
241 content
.setProperty(EntityNames
.SVG_HEIGHT
, imageData
.height
);
242 content
.getSession().save();
243 } catch (IOException e
) {
244 throw new RuntimeException(e
);
252 if (content
.isNodeType(EntityType
.box
.get())) {
253 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
254 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
256 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
257 ImageData imageData
= new ImageData(in
);
258 width
= Long
.valueOf(imageData
.width
);
259 height
= Long
.valueOf(imageData
.height
);
260 } catch (IOException e
) {
261 throw new RuntimeException(e
);
265 if (maxWidth
!= null && width
> maxWidth
) {
266 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
267 width
= maxWidth
.longValue();
268 height
= Math
.round(ratio
* height
);
270 Label img
= new Label(parent
, SWT
.NONE
);
271 CmsUiUtils
.markup(img
);
272 StringBuffer txt
= new StringBuffer();
273 String target
= toLink(link
);
275 txt
.append("<a href='").append(target
).append("'>");
276 txt
.append(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
279 img
.setText(txt
.toString());
280 if (parent
.getLayout() instanceof GridLayout
) {
281 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
282 gd
.widthHint
= width
.intValue();
283 gd
.heightHint
= height
.intValue();
284 img
.setLayoutData(gd
);
288 img
.addMouseListener(new MouseListener() {
289 private static final long serialVersionUID
= -1362242049325206168L;
292 public void mouseUp(MouseEvent e
) {
296 public void mouseDown(MouseEvent e
) {
300 public void mouseDoubleClick(MouseEvent e
) {
301 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
304 protected Control
createDialogArea(Composite parent
) {
305 parent
.setLayout(new GridLayout());
306 ScrolledComposite scroll
= new ScrolledComposite(parent
, SWT
.H_SCROLL
| SWT
.V_SCROLL
);
307 scroll
.setLayoutData(CmsUiUtils
.fillAll());
308 scroll
.setLayout(CmsUiUtils
.noSpaceGridLayout());
309 scroll
.setExpandHorizontal(true);
310 scroll
.setExpandVertical(true);
311 // scroll.setAlwaysShowScrollBars(true);
313 Composite c
= new Composite(scroll
, SWT
.NONE
);
314 scroll
.setContent(c
);
315 c
.setLayout(new GridLayout());
316 c
.setLayoutData(CmsUiUtils
.fillAll());
317 Label bigImg
= new Label(c
, SWT
.NONE
);
318 CmsUiUtils
.markup(bigImg
);
319 bigImg
.setText(CmsUiUtils
.img(fileNode
, Jcr
.get(content
, EntityNames
.SVG_WIDTH
),
320 Jcr
.get(content
, EntityNames
.SVG_HEIGHT
)));
321 bigImg
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, true));
326 protected Point
getInitialSize() {
327 Point shellSize
= img
.getShell().getSize();
328 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
338 public static String
toLink(Node node
) {
340 return node
!= null ?
"#" + CmsUiUtils
.cleanPathForUrl(SuiteApp
.nodeToState(node
)) : null;
341 } catch (RepositoryException e
) {
342 throw new JcrException("Cannot get link from " + node
, e
);
346 public static Control
addLink(Composite parent
, String label
, Node node
, CmsStyle style
)
347 throws RepositoryException
{
348 String target
= toLink(node
);
349 CmsLink link
= new CmsLink(label
, target
, style
);
350 return link
.createUi(parent
, node
);
353 public static boolean isCoworker(CmsView cmsView
) {
354 boolean coworker
= cmsView
.doAs(() -> CurrentUser
.isInRole(SuiteRole
.coworker
.dn()));
358 public static boolean isTopic(Event event
, CmsEvent cmsEvent
) {
359 return event
.getTopic().equals(cmsEvent
.topic());
362 public static Button
createLayerButton(Composite parent
, String layer
, Localized msg
, CmsIcon icon
,
363 ClassLoader l10nClassLoader
) {
364 CmsTheme theme
= CmsTheme
.getCmsTheme(parent
);
365 Button button
= new Button(parent
, SWT
.PUSH
);
366 CmsUiUtils
.style(button
, SuiteStyle
.leadPane
);
368 button
.setImage(icon
.getBigIcon(theme
));
369 button
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.BOTTOM
, true, false));
370 // button.setToolTipText(msg.lead());
372 Label lbl
= new Label(parent
, SWT
.CENTER
);
373 CmsUiUtils
.style(lbl
, SuiteStyle
.leadPane
);
374 String txt
= LocaleUtils
.lead(msg
, l10nClassLoader
);
375 // String txt = msg.lead();
377 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true, false));
379 CmsUiUtils
.sendEventOnSelect(button
, SuiteEvent
.switchLayer
.topic(), SuiteEvent
.LAYER
, layer
);
383 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
384 // String... additionnalProps) {
386 // Session tmpSession = null;
387 // Session mainSession = null;
389 // // FIXME would not work if home is another physical workspace
390 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
391 // Node draftNode = null;
392 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
393 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
395 // Wizard wizard = null;
396 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
397 // // WizardDialog dialog = new WizardDialog(shell, wizard);
398 // if (dialog.open() == Window.OK) {
399 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
400 // // FIXME it should be possible to specify the workspace
401 // mainSession = referenceSession.getRepository().login();
402 // Node parent = mainSession.getNode(parentPath);
403 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
404 //// task = appService.saveEntity(task, false);
405 // referenceSession.refresh(true);
406 // return task.getPath();
409 // } catch (RepositoryException e1) {
410 // throw new JcrException(
411 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
413 // JcrUtils.logoutQuietly(tmpSession);
414 // JcrUtils.logoutQuietly(mainSession);