]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteUiUtils.java
Prepare lib pom for release.
[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 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
249 gd.widthHint = width.intValue();
250 gd.heightHint = height.intValue();
251 img.setLayoutData(gd);
252 }
253 img.addMouseListener(new MouseListener() {
254
255 @Override
256 public void mouseUp(MouseEvent e) {
257 }
258
259 @Override
260 public void mouseDown(MouseEvent e) {
261 }
262
263 @Override
264 public void mouseDoubleClick(MouseEvent e) {
265 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
266
267 @Override
268 protected Control createDialogArea(Composite parent) {
269 parent.setLayout(new GridLayout());
270 ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
271 scroll.setLayoutData(CmsUiUtils.fillAll());
272 scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
273 scroll.setExpandHorizontal(true);
274 scroll.setExpandVertical(true);
275 // scroll.setAlwaysShowScrollBars(true);
276
277 Composite c = new Composite(scroll, SWT.NONE);
278 scroll.setContent(c);
279 c.setLayout(new GridLayout());
280 c.setLayoutData(CmsUiUtils.fillAll());
281 Label bigImg = new Label(c, SWT.NONE);
282 CmsUiUtils.markup(bigImg);
283 bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
284 Jcr.get(content, EntityNames.SVG_HEIGHT)));
285 bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
286 return bigImg;
287 }
288
289 @Override
290 protected Point getInitialSize() {
291 Point shellSize = img.getShell().getSize();
292 return new Point(shellSize.x - 100, shellSize.y - 100);
293 }
294
295 };
296 dialog.open();
297 }
298 });
299 return img;
300 }
301
302 public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
303 String... additionnalProps) {
304
305 Session tmpSession = null;
306 Session mainSession = null;
307 try {
308 // FIXME would not work if home is another physical workspace
309 tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
310 Node draftNode = null;
311 for (int i = 0; i < additionnalProps.length - 1; i += 2) {
312 draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
313 }
314 Wizard wizard = null;
315 CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
316 // WizardDialog dialog = new WizardDialog(shell, wizard);
317 if (dialog.open() == Window.OK) {
318 String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
319 // FIXME it should be possible to specify the workspace
320 mainSession = referenceSession.getRepository().login();
321 Node parent = mainSession.getNode(parentPath);
322 Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
323 // task = appService.saveEntity(task, false);
324 referenceSession.refresh(true);
325 return task.getPath();
326 }
327 return null;
328 } catch (RepositoryException e1) {
329 throw new JcrException(
330 "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
331 } finally {
332 JcrUtils.logoutQuietly(tmpSession);
333 JcrUtils.logoutQuietly(mainSession);
334 }
335 }
336
337 }