28d775300f0b7c03a548e155453f8881d339ba41
[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.ui.dialogs.CmsWizardDialog;
12 import org.argeo.cms.ui.util.CmsUiUtils;
13 import org.argeo.entity.EntityNames;
14 import org.argeo.entity.EntityType;
15 import org.argeo.jcr.JcrException;
16 import org.argeo.jcr.JcrUtils;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.jface.wizard.Wizard;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.ImageData;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28
29 /** UI utilities related to the APAF project. */
30 public class SuiteUiUtils {
31
32         /** Singleton. */
33         private SuiteUiUtils() {
34         }
35
36         /** creates a title bar composite with label and optional button */
37         public static void addTitleBar(Composite parent, String title, Boolean isEditable) {
38                 Composite titleBar = new Composite(parent, SWT.NONE);
39                 titleBar.setLayoutData(CmsUiUtils.fillWidth());
40                 CmsUiUtils.style(titleBar, SuiteStyle.titleContainer);
41
42                 titleBar.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(2, false)));
43                 Label titleLbl = new Label(titleBar, SWT.NONE);
44                 titleLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
45                 CmsUiUtils.style(titleLbl, SuiteStyle.titleLabel);
46                 titleLbl.setText(title);
47
48                 if (isEditable) {
49                         Button editBtn = new Button(titleBar, SWT.PUSH);
50                         editBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
51                         CmsUiUtils.style(editBtn, SuiteStyle.inlineButton);
52                         editBtn.setText("Edit");
53                 }
54         }
55
56         public static Label addFormLabel(Composite parent, String label) {
57                 Label lbl = new Label(parent, SWT.WRAP);
58                 lbl.setText(label);
59                 lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
60                 CmsUiUtils.style(lbl, SuiteStyle.simpleLabel);
61                 return lbl;
62         }
63
64         public static Text addFormTextField(Composite parent, String text, String message) {
65                 Text txt = new Text(parent, SWT.WRAP);
66                 if (text != null)
67                         txt.setText(text);
68                 if (message != null)
69                         txt.setMessage(message);
70                 txt.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
71                 CmsUiUtils.style(txt, SuiteStyle.simpleText);
72                 return txt;
73         }
74
75         public static Text addFormInputField(Composite parent, String placeholder) {
76                 Text txt = new Text(parent, SWT.BORDER);
77
78                 GridData gridData = CmsUiUtils.fillWidth();
79                 txt.setLayoutData(gridData);
80
81                 if (placeholder != null)
82                         txt.setText(placeholder);
83
84                 CmsUiUtils.style(txt, SuiteStyle.simpleInput);
85                 return txt;
86         }
87
88         /** creates a single horizontal-block composite for key:value display */
89         public static Text addFormLine(Composite parent, String label, String text) {
90                 Composite lineComposite = new Composite(parent, SWT.NONE);
91                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
92                 lineComposite.setLayout(new GridLayout(2, true));
93                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
94                 addFormLabel(lineComposite, label);
95                 Text txt = addFormTextField(lineComposite, text, null);
96                 txt.setLayoutData(CmsUiUtils.fillWidth());
97                 return txt;
98         }
99
100         public static Text addFormInput(Composite parent, String label, String placeholder) {
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, true));
104                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
105                 addFormLabel(lineComposite, label);
106                 Text txt = addFormInputField(lineComposite, placeholder);
107                 txt.setLayoutData(CmsUiUtils.fillWidth());
108                 return txt;
109         }
110
111         /**
112          * creates a single horizontal-block composite for key:value display, with
113          * offset value
114          */
115         public static Text addFormLine(Composite parent, String label, String text, Integer offset) {
116                 Composite lineComposite = new Composite(parent, SWT.NONE);
117                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
118                 lineComposite.setLayout(new GridLayout(3, false));
119                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
120                 Label offsetLbl = new Label(lineComposite, SWT.NONE);
121                 GridData gridData = new GridData();
122                 gridData.widthHint = offset;
123                 offsetLbl.setLayoutData(gridData);
124                 addFormLabel(lineComposite, label);
125                 Text txt = addFormTextField(lineComposite, text, null);
126                 txt.setLayoutData(CmsUiUtils.fillWidth());
127                 return txt;
128         }
129
130         /** creates a single vertical-block composite for key:value display */
131         public static Text addFormColumn(Composite parent, String label, String text) {
132                 Composite columnComposite = new Composite(parent, SWT.NONE);
133                 columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
134                 columnComposite.setLayout(new GridLayout(1, false));
135                 addFormLabel(columnComposite, label);
136                 Text txt = addFormTextField(columnComposite, text, null);
137                 txt.setLayoutData(CmsUiUtils.fillWidth());
138                 return txt;
139         }
140
141         public static Label addFormPicture(Composite parent, String label, Node fileNode) throws RepositoryException {
142                 Composite lineComposite = new Composite(parent, SWT.NONE);
143                 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
144                 lineComposite.setLayout(new GridLayout(2, true));
145                 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
146                 addFormLabel(lineComposite, label);
147
148                 return addPicture(lineComposite, fileNode);
149         }
150
151         public static Label addPicture(Composite parent, Node fileNode) throws RepositoryException {
152                 return addPicture(parent, fileNode, null);
153         }
154
155         public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth) throws RepositoryException {
156                 Node content = fileNode.getNode(Node.JCR_CONTENT);
157                 // TODO move it deeper in the middleware.
158                 if (!content.isNodeType(EntityType.box.get())) {
159                         try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
160                                 ImageData imageData = new ImageData(in);
161                                 content.addMixin(EntityType.box.get());
162                                 content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
163                                 content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
164                                 content.getSession().save();
165                         } catch (IOException e) {
166                                 throw new IllegalStateException(e);
167                         }
168                 }
169
170                 Long width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
171                 Long height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
172                 if (maxWidth != null && width > maxWidth) {
173                         Double ratio = maxWidth.doubleValue() / width.doubleValue();
174                         width = maxWidth.longValue();
175                         height = Math.round(ratio * height);
176                 }
177                 Label img = new Label(parent, SWT.NONE);
178                 CmsUiUtils.markup(img);
179                 img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
180                 if (parent.getLayout() instanceof GridLayout)
181                         img.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
182                 return img;
183         }
184
185         public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
186                         String... additionnalProps) {
187
188                 Session tmpSession = null;
189                 Session mainSession = null;
190                 try {
191                         // FIXME would not work if home is another physical workspace
192                         tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
193                         Node draftNode = null;
194                         for (int i = 0; i < additionnalProps.length - 1; i += 2) {
195                                 draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
196                         }
197                         Wizard wizard = null;
198                         CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
199                         // WizardDialog dialog = new WizardDialog(shell, wizard);
200                         if (dialog.open() == Window.OK) {
201                                 String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
202                                 // FIXME it should be possible to specify the workspace
203                                 mainSession = referenceSession.getRepository().login();
204                                 Node parent = mainSession.getNode(parentPath);
205                                 Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
206 //                              task = appService.saveEntity(task, false);
207                                 referenceSession.refresh(true);
208                                 return task.getPath();
209                         }
210                         return null;
211                 } catch (RepositoryException e1) {
212                         throw new JcrException(
213                                         "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
214                 } finally {
215                         JcrUtils.logoutQuietly(tmpSession);
216                         JcrUtils.logoutQuietly(mainSession);
217                 }
218         }
219
220 }