Extend SUIte UI utils.
[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.entity.EntityNames;
17 import org.argeo.entity.EntityType;
18 import org.argeo.jcr.Jcr;
19 import org.argeo.jcr.JcrException;
20 import org.argeo.jcr.JcrUtils;
21 import org.eclipse.jface.window.Window;
22 import org.eclipse.jface.wizard.Wizard;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.ImageData;
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.Label;
30 import org.eclipse.swt.widgets.Shell;
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                         try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
221                                 ImageData imageData = new ImageData(in);
222                                 content.addMixin(EntityType.box.get());
223                                 content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
224                                 content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
225                                 content.getSession().save();
226                         } catch (IOException e) {
227                                 throw new IllegalStateException(e);
228                         }
229                 }
230
231                 Long width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
232                 Long height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
233                 if (maxWidth != null && width > maxWidth) {
234                         Double ratio = maxWidth.doubleValue() / width.doubleValue();
235                         width = maxWidth.longValue();
236                         height = Math.round(ratio * height);
237                 }
238                 Label img = new Label(parent, SWT.NONE);
239                 CmsUiUtils.markup(img);
240                 img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
241                 if (parent.getLayout() instanceof GridLayout)
242                         img.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
243                 return img;
244         }
245
246         public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
247                         String... additionnalProps) {
248
249                 Session tmpSession = null;
250                 Session mainSession = null;
251                 try {
252                         // FIXME would not work if home is another physical workspace
253                         tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
254                         Node draftNode = null;
255                         for (int i = 0; i < additionnalProps.length - 1; i += 2) {
256                                 draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
257                         }
258                         Wizard wizard = null;
259                         CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
260                         // WizardDialog dialog = new WizardDialog(shell, wizard);
261                         if (dialog.open() == Window.OK) {
262                                 String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
263                                 // FIXME it should be possible to specify the workspace
264                                 mainSession = referenceSession.getRepository().login();
265                                 Node parent = mainSession.getNode(parentPath);
266                                 Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
267 //                              task = appService.saveEntity(task, false);
268                                 referenceSession.refresh(true);
269                                 return task.getPath();
270                         }
271                         return null;
272                 } catch (RepositoryException e1) {
273                         throw new JcrException(
274                                         "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
275                 } finally {
276                         JcrUtils.logoutQuietly(tmpSession);
277                         JcrUtils.logoutQuietly(mainSession);
278                 }
279         }
280
281 }