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