1 package org.argeo.suite.ui;
3 import java.io.IOException;
4 import java.io.InputStream;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
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;
35 /** UI utilities related to the APAF project. */
36 public class SuiteUiUtils {
39 private SuiteUiUtils() {
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);
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);
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");
62 public static Label addFormLabel(Composite parent, String label) {
63 Label lbl = new Label(parent, SWT.WRAP);
65 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
66 CmsUiUtils.style(lbl, SuiteStyle.simpleLabel);
70 public static Text addFormTextField(Composite parent, String text, String message) {
71 return addFormTextField(parent, text, message, SWT.NONE);
74 public static Text addFormTextField(Composite parent, String text, String message, int style) {
75 Text txt = new Text(parent, style);
79 txt.setMessage(message);
80 txt.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
81 CmsUiUtils.style(txt, SuiteStyle.simpleText);
85 public static Text addFormInputField(Composite parent, String placeholder) {
86 Text txt = new Text(parent, SWT.BORDER);
88 GridData gridData = CmsUiUtils.fillWidth();
89 txt.setLayoutData(gridData);
91 if (placeholder != null)
92 txt.setText(placeholder);
94 CmsUiUtils.style(txt, SuiteStyle.simpleInput);
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());
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());
127 txt.setEditable(false);
129 txt.setLayoutData(CmsUiUtils.fillWidth());
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());
145 * creates a single horizontal-block composite for key:value display, with
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());
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());
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());
190 txt.setEditable(false);
192 txt.setLayoutData(CmsUiUtils.fillWidth());
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));
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);
211 return addPicture(lineComposite, fileNode);
214 public static Label addPicture(Composite parent, Node fileNode) throws RepositoryException {
215 return addPicture(parent, fileNode, null);
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);
238 if (content.isNodeType(EntityType.box.get())) {
239 width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
240 height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
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);
251 if (maxWidth != null && width > maxWidth) {
252 Double ratio = maxWidth.doubleValue() / width.doubleValue();
253 width = maxWidth.longValue();
254 height = Math.round(ratio * height);
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);
265 img.addMouseListener(new MouseListener() {
266 private static final long serialVersionUID = -1362242049325206168L;
269 public void mouseUp(MouseEvent e) {
273 public void mouseDown(MouseEvent e) {
277 public void mouseDoubleClick(MouseEvent e) {
278 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
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);
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));
303 protected Point getInitialSize() {
304 Point shellSize = img.getShell().getSize();
305 return new Point(shellSize.x - 100, shellSize.y - 100);
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);
320 button.setImage(icon.getBigIcon(theme));
321 button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
322 // button.setToolTipText(msg.lead());
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));
329 CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
333 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
334 // String... additionnalProps) {
336 // Session tmpSession = null;
337 // Session mainSession = null;
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]);
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();
359 // } catch (RepositoryException e1) {
360 // throw new JcrException(
361 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
363 // JcrUtils.logoutQuietly(tmpSession);
364 // JcrUtils.logoutQuietly(mainSession);