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.auth.CurrentUser;
12 import org.argeo.cms.ui.CmsEditable;
13 import org.argeo.cms.ui.CmsView;
14 import org.argeo.cms.ui.dialogs.LightweightDialog;
15 import org.argeo.cms.ui.util.CmsEvent;
16 import org.argeo.cms.ui.util.CmsUiUtils;
17 import org.argeo.eclipse.ui.EclipseUiUtils;
18 import org.argeo.entity.EntityNames;
19 import org.argeo.entity.EntityType;
20 import org.argeo.jcr.Jcr;
21 import org.argeo.jcr.JcrUtils;
22 import org.argeo.suite.SuiteRole;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.ScrolledComposite;
25 import org.eclipse.swt.events.MouseEvent;
26 import org.eclipse.swt.events.MouseListener;
27 import org.eclipse.swt.graphics.ImageData;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Text;
36 import org.osgi.service.event.Event;
38 /** UI utilities related to the APAF project. */
39 public class SuiteUiUtils {
42 private SuiteUiUtils() {
45 /** creates a title bar composite with label and optional button */
46 public static void addTitleBar(Composite parent, String title, Boolean isEditable) {
47 Composite titleBar = new Composite(parent, SWT.NONE);
48 titleBar.setLayoutData(CmsUiUtils.fillWidth());
49 CmsUiUtils.style(titleBar, SuiteStyle.titleContainer);
51 titleBar.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(2, false)));
52 Label titleLbl = new Label(titleBar, SWT.NONE);
53 titleLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
54 CmsUiUtils.style(titleLbl, SuiteStyle.titleLabel);
55 titleLbl.setText(title);
58 Button editBtn = new Button(titleBar, SWT.PUSH);
59 editBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
60 CmsUiUtils.style(editBtn, SuiteStyle.inlineButton);
61 editBtn.setText("Edit");
65 public static Label addFormLabel(Composite parent, String label) {
66 Label lbl = new Label(parent, SWT.WRAP);
68 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
69 CmsUiUtils.style(lbl, SuiteStyle.simpleLabel);
73 public static Text addFormTextField(Composite parent, String text, String message) {
74 return addFormTextField(parent, text, message, SWT.NONE);
77 public static Text addFormTextField(Composite parent, String text, String message, int style) {
78 Text txt = new Text(parent, style);
82 txt.setMessage(message);
83 txt.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
84 CmsUiUtils.style(txt, SuiteStyle.simpleText);
88 public static Text addFormInputField(Composite parent, String placeholder) {
89 Text txt = new Text(parent, SWT.BORDER);
91 GridData gridData = CmsUiUtils.fillWidth();
92 txt.setLayoutData(gridData);
94 if (placeholder != null)
95 txt.setText(placeholder);
97 CmsUiUtils.style(txt, SuiteStyle.simpleInput);
101 /** creates a single horizontal-block composite for key:value display */
102 public static Text addFormLine(Composite parent, String label, String text) {
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 = addFormTextField(lineComposite, text, null);
109 txt.setEditable(false);
110 txt.setLayoutData(CmsUiUtils.fillWidth());
114 public static Text addFormLine(Composite parent, String label, Node node, String property,
115 CmsEditable cmsEditable) {
116 Composite lineComposite = new Composite(parent, SWT.NONE);
117 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
118 lineComposite.setLayout(new GridLayout(2, false));
119 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
120 addFormLabel(lineComposite, label);
121 String text = Jcr.get(node, property);
122 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
123 Text txt = addFormTextField(lineComposite, text, null, SWT.WRAP);
124 if (cmsEditable != null && cmsEditable.isEditing()) {
125 txt.addModifyListener((e) -> {
126 Jcr.set(node, property, txt.getText());
130 txt.setEditable(false);
132 txt.setLayoutData(CmsUiUtils.fillWidth());
136 public static Text addFormInput(Composite parent, String label, String placeholder) {
137 Composite lineComposite = new Composite(parent, SWT.NONE);
138 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
139 lineComposite.setLayout(new GridLayout(2, false));
140 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
141 addFormLabel(lineComposite, label);
142 Text txt = addFormInputField(lineComposite, placeholder);
143 txt.setLayoutData(CmsUiUtils.fillWidth());
148 * creates a single horizontal-block composite for key:value display, with
151 public static Text addFormLine(Composite parent, String label, String text, Integer offset) {
152 Composite lineComposite = new Composite(parent, SWT.NONE);
153 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
154 lineComposite.setLayout(new GridLayout(3, false));
155 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
156 Label offsetLbl = new Label(lineComposite, SWT.NONE);
157 GridData gridData = new GridData();
158 gridData.widthHint = offset;
159 offsetLbl.setLayoutData(gridData);
160 addFormLabel(lineComposite, label);
161 Text txt = addFormTextField(lineComposite, text, null);
162 txt.setLayoutData(CmsUiUtils.fillWidth());
166 /** creates a single vertical-block composite for key:value display */
167 public static Text addFormColumn(Composite parent, String label, String text) {
168 // Composite columnComposite = new Composite(parent, SWT.NONE);
169 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
170 // columnComposite.setLayout(new GridLayout(1, false));
171 addFormLabel(parent, label);
172 Text txt = addFormTextField(parent, text, null);
173 txt.setEditable(false);
174 txt.setLayoutData(CmsUiUtils.fillWidth());
178 public static Text addFormColumn(Composite parent, String label, Node node, String property,
179 CmsEditable cmsEditable) {
180 // Composite columnComposite = new Composite(parent, SWT.NONE);
181 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
182 // columnComposite.setLayout(new GridLayout(1, false));
183 addFormLabel(parent, label);
184 String text = Jcr.get(node, property);
185 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
186 Text txt = addFormTextField(parent, text, null, SWT.WRAP);
187 if (cmsEditable != null && cmsEditable.isEditing()) {
188 txt.addModifyListener((e) -> {
189 Jcr.set(node, property, txt.getText());
193 txt.setEditable(false);
195 txt.setLayoutData(CmsUiUtils.fillWidth());
199 public static Label createBoldLabel(Composite parent, Localized localized) {
200 Label label = new Label(parent, SWT.LEAD);
201 label.setText(localized.lead());
202 label.setFont(EclipseUiUtils.getBoldFont(parent));
203 label.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
207 public static Label addFormPicture(Composite parent, String label, Node fileNode) throws RepositoryException {
208 Composite lineComposite = new Composite(parent, SWT.NONE);
209 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
210 lineComposite.setLayout(new GridLayout(2, true));
211 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
212 addFormLabel(lineComposite, label);
214 return addPicture(lineComposite, fileNode);
217 public static Label addPicture(Composite parent, Node fileNode) throws RepositoryException {
218 return addPicture(parent, fileNode, null);
221 public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth) throws RepositoryException {
222 Node content = fileNode.getNode(Node.JCR_CONTENT);
223 // TODO move it deeper in the middleware.
224 if (!content.isNodeType(EntityType.box.get())) {
225 if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
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 RuntimeException(e);
241 if (content.isNodeType(EntityType.box.get())) {
242 width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
243 height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
245 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
246 ImageData imageData = new ImageData(in);
247 width = Long.valueOf(imageData.width);
248 height = Long.valueOf(imageData.height);
249 } catch (IOException e) {
250 throw new RuntimeException(e);
254 if (maxWidth != null && width > maxWidth) {
255 Double ratio = maxWidth.doubleValue() / width.doubleValue();
256 width = maxWidth.longValue();
257 height = Math.round(ratio * height);
259 Label img = new Label(parent, SWT.NONE);
260 CmsUiUtils.markup(img);
261 img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
262 if (parent.getLayout() instanceof GridLayout) {
263 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
264 gd.widthHint = width.intValue();
265 gd.heightHint = height.intValue();
266 img.setLayoutData(gd);
268 img.addMouseListener(new MouseListener() {
269 private static final long serialVersionUID = -1362242049325206168L;
272 public void mouseUp(MouseEvent e) {
276 public void mouseDown(MouseEvent e) {
280 public void mouseDoubleClick(MouseEvent e) {
281 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
284 protected Control createDialogArea(Composite parent) {
285 parent.setLayout(new GridLayout());
286 ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
287 scroll.setLayoutData(CmsUiUtils.fillAll());
288 scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
289 scroll.setExpandHorizontal(true);
290 scroll.setExpandVertical(true);
291 // scroll.setAlwaysShowScrollBars(true);
293 Composite c = new Composite(scroll, SWT.NONE);
294 scroll.setContent(c);
295 c.setLayout(new GridLayout());
296 c.setLayoutData(CmsUiUtils.fillAll());
297 Label bigImg = new Label(c, SWT.NONE);
298 CmsUiUtils.markup(bigImg);
299 bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
300 Jcr.get(content, EntityNames.SVG_HEIGHT)));
301 bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
306 protected Point getInitialSize() {
307 Point shellSize = img.getShell().getSize();
308 return new Point(shellSize.x - 100, shellSize.y - 100);
318 public static boolean isCoworker(CmsView cmsView) {
319 boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
323 public static boolean isTopic(Event event, CmsEvent cmsEvent) {
324 return event.getTopic().equals(cmsEvent.topic());
327 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
328 // String... additionnalProps) {
330 // Session tmpSession = null;
331 // Session mainSession = null;
333 // // FIXME would not work if home is another physical workspace
334 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
335 // Node draftNode = null;
336 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
337 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
339 // Wizard wizard = null;
340 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
341 // // WizardDialog dialog = new WizardDialog(shell, wizard);
342 // if (dialog.open() == Window.OK) {
343 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
344 // // FIXME it should be possible to specify the workspace
345 // mainSession = referenceSession.getRepository().login();
346 // Node parent = mainSession.getNode(parentPath);
347 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
348 //// task = appService.saveEntity(task, false);
349 // referenceSession.refresh(true);
350 // return task.getPath();
353 // } catch (RepositoryException e1) {
354 // throw new JcrException(
355 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
357 // JcrUtils.logoutQuietly(tmpSession);
358 // JcrUtils.logoutQuietly(mainSession);