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