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