Improve lead pane localization.
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / SuiteUiUtils.java
1 package org.argeo.suite.ui;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
10 import org.argeo.cms.Localized;
11 import org.argeo.cms.auth.CurrentUser;
12 import org.argeo.cms.ui.CmsEditable;
13 import org.argeo.cms.ui.CmsView;
14 import org.argeo.cms.ui.dialogs.LightweightDialog;
15 import org.argeo.cms.ui.util.CmsUiUtils;
16 import org.argeo.eclipse.ui.EclipseUiUtils;
17 import org.argeo.entity.EntityNames;
18 import org.argeo.entity.EntityType;
19 import org.argeo.jcr.Jcr;
20 import org.argeo.jcr.JcrUtils;
21 import org.argeo.suite.SuiteRole;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.ScrolledComposite;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.events.MouseListener;
26 import org.eclipse.swt.graphics.ImageData;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Text;
35
36 /** UI utilities related to the APAF project. */
37 public class SuiteUiUtils {
38
39         /** Singleton. */
40         private SuiteUiUtils() {
41         }
42
43         /** creates a title bar composite with label and optional button */
44         public static void addTitleBar(Composite parent, String title, Boolean isEditable) {
45                 Composite titleBar = new Composite(parent, SWT.NONE);
46                 titleBar.setLayoutData(CmsUiUtils.fillWidth());
47                 CmsUiUtils.style(titleBar, SuiteStyle.titleContainer);
48
49                 titleBar.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(2, false)));
50                 Label titleLbl = new Label(titleBar, SWT.NONE);
51                 titleLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
52                 CmsUiUtils.style(titleLbl, SuiteStyle.titleLabel);
53                 titleLbl.setText(title);
54
55                 if (isEditable) {
56                         Button editBtn = new Button(titleBar, SWT.PUSH);
57                         editBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
58                         CmsUiUtils.style(editBtn, SuiteStyle.inlineButton);
59                         editBtn.setText("Edit");
60                 }
61         }
62
63         public static Label addFormLabel(Composite parent, String label) {
64                 Label lbl = new Label(parent, SWT.WRAP);
65                 lbl.setText(label);
66                 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
67                 CmsUiUtils.style(lbl, SuiteStyle.simpleLabel);
68                 return lbl;
69         }
70
71         public static Text addFormTextField(Composite parent, String text, String message) {
72                 return addFormTextField(parent, text, message, SWT.NONE);
73         }
74
75         public static Text addFormTextField(Composite parent, String text, String message, int style) {
76                 Text txt = new Text(parent, style);
77                 if (text != null)
78                         txt.setText(text);
79                 if (message != null)
80                         txt.setMessage(message);
81                 txt.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
82                 CmsUiUtils.style(txt, SuiteStyle.simpleText);
83                 return txt;
84         }
85
86         public static Text addFormInputField(Composite parent, String placeholder) {
87                 Text txt = new Text(parent, SWT.BORDER);
88
89                 GridData gridData = CmsUiUtils.fillWidth();
90                 txt.setLayoutData(gridData);
91
92                 if (placeholder != null)
93                         txt.setText(placeholder);
94
95                 CmsUiUtils.style(txt, SuiteStyle.simpleInput);
96                 return txt;
97         }
98
99         /** creates a single horizontal-block composite for key:value display */
100         public static Text addFormLine(Composite parent, String label, String text) {
101                 Composite lineComposite = new Composite(parent, SWT.NONE);
102                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
103                 lineComposite.setLayout(new GridLayout(2, false));
104                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
105                 addFormLabel(lineComposite, label);
106                 Text txt = addFormTextField(lineComposite, text, null);
107                 txt.setEditable(false);
108                 txt.setLayoutData(CmsUiUtils.fillWidth());
109                 return txt;
110         }
111
112         public static Text addFormLine(Composite parent, String label, Node node, String property,
113                         CmsEditable cmsEditable) {
114                 Composite lineComposite = new Composite(parent, SWT.NONE);
115                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
116                 lineComposite.setLayout(new GridLayout(2, false));
117                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
118                 addFormLabel(lineComposite, label);
119                 String text = Jcr.get(node, property);
120 //              int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
121                 Text txt = addFormTextField(lineComposite, text, null, SWT.WRAP);
122                 if (cmsEditable != null && cmsEditable.isEditing()) {
123                         txt.addModifyListener((e) -> {
124                                 Jcr.set(node, property, txt.getText());
125                                 Jcr.save(node);
126                         });
127                 } else {
128                         txt.setEditable(false);
129                 }
130                 txt.setLayoutData(CmsUiUtils.fillWidth());
131                 return txt;
132         }
133
134         public static Text addFormInput(Composite parent, String label, String placeholder) {
135                 Composite lineComposite = new Composite(parent, SWT.NONE);
136                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
137                 lineComposite.setLayout(new GridLayout(2, false));
138                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
139                 addFormLabel(lineComposite, label);
140                 Text txt = addFormInputField(lineComposite, placeholder);
141                 txt.setLayoutData(CmsUiUtils.fillWidth());
142                 return txt;
143         }
144
145         /**
146          * creates a single horizontal-block composite for key:value display, with
147          * offset value
148          */
149         public static Text addFormLine(Composite parent, String label, String text, Integer offset) {
150                 Composite lineComposite = new Composite(parent, SWT.NONE);
151                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
152                 lineComposite.setLayout(new GridLayout(3, false));
153                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
154                 Label offsetLbl = new Label(lineComposite, SWT.NONE);
155                 GridData gridData = new GridData();
156                 gridData.widthHint = offset;
157                 offsetLbl.setLayoutData(gridData);
158                 addFormLabel(lineComposite, label);
159                 Text txt = addFormTextField(lineComposite, text, null);
160                 txt.setLayoutData(CmsUiUtils.fillWidth());
161                 return txt;
162         }
163
164         /** creates a single vertical-block composite for key:value display */
165         public static Text addFormColumn(Composite parent, String label, String text) {
166 //              Composite columnComposite = new Composite(parent, SWT.NONE);
167 //              columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
168 //              columnComposite.setLayout(new GridLayout(1, false));
169                 addFormLabel(parent, label);
170                 Text txt = addFormTextField(parent, text, null);
171                 txt.setEditable(false);
172                 txt.setLayoutData(CmsUiUtils.fillWidth());
173                 return txt;
174         }
175
176         public static Text addFormColumn(Composite parent, String label, Node node, String property,
177                         CmsEditable cmsEditable) {
178 //              Composite columnComposite = new Composite(parent, SWT.NONE);
179 //              columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
180 //              columnComposite.setLayout(new GridLayout(1, false));
181                 addFormLabel(parent, label);
182                 String text = Jcr.get(node, property);
183 //              int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
184                 Text txt = addFormTextField(parent, text, null, SWT.WRAP);
185                 if (cmsEditable != null && cmsEditable.isEditing()) {
186                         txt.addModifyListener((e) -> {
187                                 Jcr.set(node, property, txt.getText());
188                                 Jcr.save(node);
189                         });
190                 } else {
191                         txt.setEditable(false);
192                 }
193                 txt.setLayoutData(CmsUiUtils.fillWidth());
194                 return txt;
195         }
196
197         public static Label createBoldLabel(Composite parent, Localized localized) {
198                 Label label = new Label(parent, SWT.LEAD);
199                 label.setText(localized.lead());
200                 label.setFont(EclipseUiUtils.getBoldFont(parent));
201                 label.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
202                 return label;
203         }
204
205         public static Label addFormPicture(Composite parent, String label, Node fileNode) throws RepositoryException {
206                 Composite lineComposite = new Composite(parent, SWT.NONE);
207                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
208                 lineComposite.setLayout(new GridLayout(2, true));
209                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
210                 addFormLabel(lineComposite, label);
211
212                 return addPicture(lineComposite, fileNode);
213         }
214
215         public static Label addPicture(Composite parent, Node fileNode) throws RepositoryException {
216                 return addPicture(parent, fileNode, null);
217         }
218
219         public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth) throws RepositoryException {
220                 Node content = fileNode.getNode(Node.JCR_CONTENT);
221                 // TODO move it deeper in the middleware.
222                 if (!content.isNodeType(EntityType.box.get())) {
223                         if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
224                                 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
225                                         ImageData imageData = new ImageData(in);
226                                         content.addMixin(EntityType.box.get());
227                                         content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
228                                         content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
229                                         content.getSession().save();
230                                 } catch (IOException e) {
231                                         throw new RuntimeException(e);
232                                 }
233                         }
234                 }
235
236                 // TODO optimise
237                 Long width;
238                 Long height;
239                 if (content.isNodeType(EntityType.box.get())) {
240                         width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
241                         height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
242                 } else {
243                         try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
244                                 ImageData imageData = new ImageData(in);
245                                 width = Long.valueOf(imageData.width);
246                                 height = Long.valueOf(imageData.height);
247                         } catch (IOException e) {
248                                 throw new RuntimeException(e);
249                         }
250                 }
251
252                 if (maxWidth != null && width > maxWidth) {
253                         Double ratio = maxWidth.doubleValue() / width.doubleValue();
254                         width = maxWidth.longValue();
255                         height = Math.round(ratio * height);
256                 }
257                 Label img = new Label(parent, SWT.NONE);
258                 CmsUiUtils.markup(img);
259                 img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
260                 if (parent.getLayout() instanceof GridLayout) {
261                         GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
262                         gd.widthHint = width.intValue();
263                         gd.heightHint = height.intValue();
264                         img.setLayoutData(gd);
265                 }
266                 img.addMouseListener(new MouseListener() {
267                         private static final long serialVersionUID = -1362242049325206168L;
268
269                         @Override
270                         public void mouseUp(MouseEvent e) {
271                         }
272
273                         @Override
274                         public void mouseDown(MouseEvent e) {
275                         }
276
277                         @Override
278                         public void mouseDoubleClick(MouseEvent e) {
279                                 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
280
281                                         @Override
282                                         protected Control createDialogArea(Composite parent) {
283                                                 parent.setLayout(new GridLayout());
284                                                 ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
285                                                 scroll.setLayoutData(CmsUiUtils.fillAll());
286                                                 scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
287                                                 scroll.setExpandHorizontal(true);
288                                                 scroll.setExpandVertical(true);
289                                                 // scroll.setAlwaysShowScrollBars(true);
290
291                                                 Composite c = new Composite(scroll, SWT.NONE);
292                                                 scroll.setContent(c);
293                                                 c.setLayout(new GridLayout());
294                                                 c.setLayoutData(CmsUiUtils.fillAll());
295                                                 Label bigImg = new Label(c, SWT.NONE);
296                                                 CmsUiUtils.markup(bigImg);
297                                                 bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
298                                                                 Jcr.get(content, EntityNames.SVG_HEIGHT)));
299                                                 bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
300                                                 return bigImg;
301                                         }
302
303                                         @Override
304                                         protected Point getInitialSize() {
305                                                 Point shellSize = img.getShell().getSize();
306                                                 return new Point(shellSize.x - 100, shellSize.y - 100);
307                                         }
308
309                                 };
310                                 dialog.open();
311                         }
312                 });
313                 return img;
314         }
315
316         public static boolean isCoworker(CmsView cmsView) {
317                 boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
318                 return coworker;
319         }
320
321 //      public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
322 //                      String... additionnalProps) {
323 //
324 //              Session tmpSession = null;
325 //              Session mainSession = null;
326 //              try {
327 //                      // FIXME would not work if home is another physical workspace
328 //                      tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
329 //                      Node draftNode = null;
330 //                      for (int i = 0; i < additionnalProps.length - 1; i += 2) {
331 //                              draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
332 //                      }
333 //                      Wizard wizard = null;
334 //                      CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
335 //                      // WizardDialog dialog = new WizardDialog(shell, wizard);
336 //                      if (dialog.open() == Window.OK) {
337 //                              String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
338 //                              // FIXME it should be possible to specify the workspace
339 //                              mainSession = referenceSession.getRepository().login();
340 //                              Node parent = mainSession.getNode(parentPath);
341 //                              Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
342 ////                            task = appService.saveEntity(task, false);
343 //                              referenceSession.refresh(true);
344 //                              return task.getPath();
345 //                      }
346 //                      return null;
347 //              } catch (RepositoryException e1) {
348 //                      throw new JcrException(
349 //                                      "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
350 //              } finally {
351 //                      JcrUtils.logoutQuietly(tmpSession);
352 //                      JcrUtils.logoutQuietly(mainSession);
353 //              }
354 //      }
355
356 }