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
.CmsUiUtils
;
20 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
21 import org
.argeo
.entity
.EntityNames
;
22 import org
.argeo
.entity
.EntityType
;
23 import org
.argeo
.jcr
.Jcr
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.argeo
.suite
.SuiteRole
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
28 import org
.eclipse
.swt
.events
.MouseEvent
;
29 import org
.eclipse
.swt
.events
.MouseListener
;
30 import org
.eclipse
.swt
.graphics
.ImageData
;
31 import org
.eclipse
.swt
.graphics
.Point
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.widgets
.Button
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Label
;
38 import org
.eclipse
.swt
.widgets
.Text
;
39 import org
.osgi
.service
.event
.Event
;
41 /** UI utilities related to the APAF project. */
42 public class SuiteUiUtils
{
45 private SuiteUiUtils() {
48 /** creates a title bar composite with label and optional button */
49 public static void addTitleBar(Composite parent
, String title
, Boolean isEditable
) {
50 Composite titleBar
= new Composite(parent
, SWT
.NONE
);
51 titleBar
.setLayoutData(CmsUiUtils
.fillWidth());
52 CmsUiUtils
.style(titleBar
, SuiteStyle
.titleContainer
);
54 titleBar
.setLayout(CmsUiUtils
.noSpaceGridLayout(new GridLayout(2, false)));
55 Label titleLbl
= new Label(titleBar
, SWT
.NONE
);
56 titleLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, true));
57 CmsUiUtils
.style(titleLbl
, SuiteStyle
.titleLabel
);
58 titleLbl
.setText(title
);
61 Button editBtn
= new Button(titleBar
, SWT
.PUSH
);
62 editBtn
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false));
63 CmsUiUtils
.style(editBtn
, SuiteStyle
.inlineButton
);
64 editBtn
.setText("Edit");
68 public static Label
addFormLabel(Composite parent
, String label
) {
69 Label lbl
= new Label(parent
, SWT
.WRAP
);
71 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
72 CmsUiUtils
.style(lbl
, SuiteStyle
.simpleLabel
);
76 public static Text
addFormTextField(Composite parent
, String text
, String message
) {
77 return addFormTextField(parent
, text
, message
, SWT
.NONE
);
80 public static Text
addFormTextField(Composite parent
, String text
, String message
, int style
) {
81 Text txt
= new Text(parent
, style
);
85 txt
.setMessage(message
);
86 txt
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, true));
87 CmsUiUtils
.style(txt
, SuiteStyle
.simpleText
);
91 public static Text
addFormInputField(Composite parent
, String placeholder
) {
92 Text txt
= new Text(parent
, SWT
.BORDER
);
94 GridData gridData
= CmsUiUtils
.fillWidth();
95 txt
.setLayoutData(gridData
);
97 if (placeholder
!= null)
98 txt
.setText(placeholder
);
100 CmsUiUtils
.style(txt
, SuiteStyle
.simpleInput
);
104 /** creates a single horizontal-block composite for key:value display */
105 public static Text
addFormLine(Composite parent
, String label
, String text
) {
106 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
107 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
108 lineComposite
.setLayout(new GridLayout(2, false));
109 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
110 addFormLabel(lineComposite
, label
);
111 Text txt
= addFormTextField(lineComposite
, text
, null);
112 txt
.setEditable(false);
113 txt
.setLayoutData(CmsUiUtils
.fillWidth());
117 public static Text
addFormLine(Composite parent
, String label
, Node node
, String property
,
118 CmsEditable cmsEditable
) {
119 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
120 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
121 lineComposite
.setLayout(new GridLayout(2, false));
122 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
123 addFormLabel(lineComposite
, label
);
124 String text
= Jcr
.get(node
, property
);
125 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
126 Text txt
= addFormTextField(lineComposite
, text
, null, SWT
.WRAP
);
127 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
128 txt
.addModifyListener((e
) -> {
129 Jcr
.set(node
, property
, txt
.getText());
133 txt
.setEditable(false);
135 txt
.setLayoutData(CmsUiUtils
.fillWidth());
139 public static Text
addFormInput(Composite parent
, String label
, String placeholder
) {
140 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
141 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
142 lineComposite
.setLayout(new GridLayout(2, false));
143 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
144 addFormLabel(lineComposite
, label
);
145 Text txt
= addFormInputField(lineComposite
, placeholder
);
146 txt
.setLayoutData(CmsUiUtils
.fillWidth());
151 * creates a single horizontal-block composite for key:value display, with
154 public static Text
addFormLine(Composite parent
, String label
, String text
, Integer offset
) {
155 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
156 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
157 lineComposite
.setLayout(new GridLayout(3, false));
158 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
159 Label offsetLbl
= new Label(lineComposite
, SWT
.NONE
);
160 GridData gridData
= new GridData();
161 gridData
.widthHint
= offset
;
162 offsetLbl
.setLayoutData(gridData
);
163 addFormLabel(lineComposite
, label
);
164 Text txt
= addFormTextField(lineComposite
, text
, null);
165 txt
.setLayoutData(CmsUiUtils
.fillWidth());
169 /** creates a single vertical-block composite for key:value display */
170 public static Text
addFormColumn(Composite parent
, String label
, String text
) {
171 // Composite columnComposite = new Composite(parent, SWT.NONE);
172 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
173 // columnComposite.setLayout(new GridLayout(1, false));
174 addFormLabel(parent
, label
);
175 Text txt
= addFormTextField(parent
, text
, null);
176 txt
.setEditable(false);
177 txt
.setLayoutData(CmsUiUtils
.fillWidth());
181 public static Text
addFormColumn(Composite parent
, String label
, Node node
, String property
,
182 CmsEditable cmsEditable
) {
183 // Composite columnComposite = new Composite(parent, SWT.NONE);
184 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
185 // columnComposite.setLayout(new GridLayout(1, false));
186 addFormLabel(parent
, label
);
187 String text
= Jcr
.get(node
, property
);
188 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
189 Text txt
= addFormTextField(parent
, text
, null, SWT
.WRAP
);
190 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
191 txt
.addModifyListener((e
) -> {
192 Jcr
.set(node
, property
, txt
.getText());
196 txt
.setEditable(false);
198 txt
.setLayoutData(CmsUiUtils
.fillWidth());
202 public static Label
createBoldLabel(Composite parent
, Localized localized
) {
203 Label label
= new Label(parent
, SWT
.LEAD
);
204 label
.setText(localized
.lead());
205 label
.setFont(EclipseUiUtils
.getBoldFont(parent
));
206 label
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false));
210 public static Label
addFormPicture(Composite parent
, String label
, Node fileNode
) throws RepositoryException
{
211 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
212 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
213 lineComposite
.setLayout(new GridLayout(2, true));
214 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
215 addFormLabel(lineComposite
, label
);
217 return addPicture(lineComposite
, fileNode
);
220 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
221 return addPicture(parent
, fileNode
, null);
224 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
225 Node content
= fileNode
.getNode(Node
.JCR_CONTENT
);
226 // TODO move it deeper in the middleware.
227 if (!content
.isNodeType(EntityType
.box
.get())) {
228 if (content
.getSession().hasPermission(content
.getPath(), Session
.ACTION_SET_PROPERTY
)) {
229 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
230 ImageData imageData
= new ImageData(in
);
231 content
.addMixin(EntityType
.box
.get());
232 content
.setProperty(EntityNames
.SVG_WIDTH
, imageData
.width
);
233 content
.setProperty(EntityNames
.SVG_HEIGHT
, imageData
.height
);
234 content
.getSession().save();
235 } catch (IOException e
) {
236 throw new RuntimeException(e
);
244 if (content
.isNodeType(EntityType
.box
.get())) {
245 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
246 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
248 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
249 ImageData imageData
= new ImageData(in
);
250 width
= Long
.valueOf(imageData
.width
);
251 height
= Long
.valueOf(imageData
.height
);
252 } catch (IOException e
) {
253 throw new RuntimeException(e
);
257 if (maxWidth
!= null && width
> maxWidth
) {
258 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
259 width
= maxWidth
.longValue();
260 height
= Math
.round(ratio
* height
);
262 Label img
= new Label(parent
, SWT
.NONE
);
263 CmsUiUtils
.markup(img
);
264 img
.setText(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
265 if (parent
.getLayout() instanceof GridLayout
) {
266 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
267 gd
.widthHint
= width
.intValue();
268 gd
.heightHint
= height
.intValue();
269 img
.setLayoutData(gd
);
271 img
.addMouseListener(new MouseListener() {
272 private static final long serialVersionUID
= -1362242049325206168L;
275 public void mouseUp(MouseEvent e
) {
279 public void mouseDown(MouseEvent e
) {
283 public void mouseDoubleClick(MouseEvent e
) {
284 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
287 protected Control
createDialogArea(Composite parent
) {
288 parent
.setLayout(new GridLayout());
289 ScrolledComposite scroll
= new ScrolledComposite(parent
, SWT
.H_SCROLL
| SWT
.V_SCROLL
);
290 scroll
.setLayoutData(CmsUiUtils
.fillAll());
291 scroll
.setLayout(CmsUiUtils
.noSpaceGridLayout());
292 scroll
.setExpandHorizontal(true);
293 scroll
.setExpandVertical(true);
294 // scroll.setAlwaysShowScrollBars(true);
296 Composite c
= new Composite(scroll
, SWT
.NONE
);
297 scroll
.setContent(c
);
298 c
.setLayout(new GridLayout());
299 c
.setLayoutData(CmsUiUtils
.fillAll());
300 Label bigImg
= new Label(c
, SWT
.NONE
);
301 CmsUiUtils
.markup(bigImg
);
302 bigImg
.setText(CmsUiUtils
.img(fileNode
, Jcr
.get(content
, EntityNames
.SVG_WIDTH
),
303 Jcr
.get(content
, EntityNames
.SVG_HEIGHT
)));
304 bigImg
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, true));
309 protected Point
getInitialSize() {
310 Point shellSize
= img
.getShell().getSize();
311 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
321 public static boolean isCoworker(CmsView cmsView
) {
322 boolean coworker
= cmsView
.doAs(() -> CurrentUser
.isInRole(SuiteRole
.coworker
.dn()));
326 public static boolean isTopic(Event event
, CmsEvent cmsEvent
) {
327 return event
.getTopic().equals(cmsEvent
.topic());
330 public static Button
createLayerButton(Composite parent
, String layer
, Localized msg
, CmsIcon icon
,
331 ClassLoader l10nClassLoader
) {
332 CmsTheme theme
= CmsTheme
.getCmsTheme(parent
);
333 Button button
= new Button(parent
, SWT
.PUSH
);
334 CmsUiUtils
.style(button
, SuiteStyle
.leadPane
);
336 button
.setImage(icon
.getBigIcon(theme
));
337 button
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.BOTTOM
, true, false));
338 // button.setToolTipText(msg.lead());
340 Label lbl
= new Label(parent
, SWT
.CENTER
);
341 CmsUiUtils
.style(lbl
, SuiteStyle
.leadPane
);
342 String txt
= LocaleUtils
.lead(msg
, l10nClassLoader
);
343 // String txt = msg.lead();
345 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true, false));
347 CmsUiUtils
.sendEventOnSelect(button
, SuiteEvent
.switchLayer
.topic(), SuiteEvent
.LAYER
, layer
);
351 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
352 // String... additionnalProps) {
354 // Session tmpSession = null;
355 // Session mainSession = null;
357 // // FIXME would not work if home is another physical workspace
358 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
359 // Node draftNode = null;
360 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
361 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
363 // Wizard wizard = null;
364 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
365 // // WizardDialog dialog = new WizardDialog(shell, wizard);
366 // if (dialog.open() == Window.OK) {
367 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
368 // // FIXME it should be possible to specify the workspace
369 // mainSession = referenceSession.getRepository().login();
370 // Node parent = mainSession.getNode(parentPath);
371 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
372 //// task = appService.saveEntity(task, false);
373 // referenceSession.refresh(true);
374 // return task.getPath();
377 // } catch (RepositoryException e1) {
378 // throw new JcrException(
379 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
381 // JcrUtils.logoutQuietly(tmpSession);
382 // JcrUtils.logoutQuietly(mainSession);