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