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.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.argeo.suite.SuiteRole;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.ScrolledComposite;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.events.MouseListener;
26 import org.eclipse.swt.graphics.ImageData;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Text;
36 /** UI utilities related to the APAF project. */
37 public class SuiteUiUtils {
40 private SuiteUiUtils() {
43 /** creates a title bar composite with label and optional button */
44 public static void addTitleBar(Composite parent, String title, Boolean isEditable) {
45 Composite titleBar = new Composite(parent, SWT.NONE);
46 titleBar.setLayoutData(CmsUiUtils.fillWidth());
47 CmsUiUtils.style(titleBar, SuiteStyle.titleContainer);
49 titleBar.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(2, false)));
50 Label titleLbl = new Label(titleBar, SWT.NONE);
51 titleLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
52 CmsUiUtils.style(titleLbl, SuiteStyle.titleLabel);
53 titleLbl.setText(title);
56 Button editBtn = new Button(titleBar, SWT.PUSH);
57 editBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
58 CmsUiUtils.style(editBtn, SuiteStyle.inlineButton);
59 editBtn.setText("Edit");
63 public static Label addFormLabel(Composite parent, String label) {
64 Label lbl = new Label(parent, SWT.WRAP);
66 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
67 CmsUiUtils.style(lbl, SuiteStyle.simpleLabel);
71 public static Text addFormTextField(Composite parent, String text, String message) {
72 return addFormTextField(parent, text, message, SWT.NONE);
75 public static Text addFormTextField(Composite parent, String text, String message, int style) {
76 Text txt = new Text(parent, style);
80 txt.setMessage(message);
81 txt.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
82 CmsUiUtils.style(txt, SuiteStyle.simpleText);
86 public static Text addFormInputField(Composite parent, String placeholder) {
87 Text txt = new Text(parent, SWT.BORDER);
89 GridData gridData = CmsUiUtils.fillWidth();
90 txt.setLayoutData(gridData);
92 if (placeholder != null)
93 txt.setText(placeholder);
95 CmsUiUtils.style(txt, SuiteStyle.simpleInput);
99 /** creates a single horizontal-block composite for key:value display */
100 public static Text addFormLine(Composite parent, String label, String text) {
101 Composite lineComposite = new Composite(parent, SWT.NONE);
102 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
103 lineComposite.setLayout(new GridLayout(2, false));
104 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
105 addFormLabel(lineComposite, label);
106 Text txt = addFormTextField(lineComposite, text, null);
107 txt.setEditable(false);
108 txt.setLayoutData(CmsUiUtils.fillWidth());
112 public static Text addFormLine(Composite parent, String label, Node node, String property,
113 CmsEditable cmsEditable) {
114 Composite lineComposite = new Composite(parent, SWT.NONE);
115 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
116 lineComposite.setLayout(new GridLayout(2, false));
117 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
118 addFormLabel(lineComposite, label);
119 String text = Jcr.get(node, property);
120 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
121 Text txt = addFormTextField(lineComposite, text, null, SWT.WRAP);
122 if (cmsEditable != null && cmsEditable.isEditing()) {
123 txt.addModifyListener((e) -> {
124 Jcr.set(node, property, txt.getText());
128 txt.setEditable(false);
130 txt.setLayoutData(CmsUiUtils.fillWidth());
134 public static Text addFormInput(Composite parent, String label, String placeholder) {
135 Composite lineComposite = new Composite(parent, SWT.NONE);
136 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
137 lineComposite.setLayout(new GridLayout(2, false));
138 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
139 addFormLabel(lineComposite, label);
140 Text txt = addFormInputField(lineComposite, placeholder);
141 txt.setLayoutData(CmsUiUtils.fillWidth());
146 * creates a single horizontal-block composite for key:value display, with
149 public static Text addFormLine(Composite parent, String label, String text, Integer offset) {
150 Composite lineComposite = new Composite(parent, SWT.NONE);
151 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
152 lineComposite.setLayout(new GridLayout(3, false));
153 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
154 Label offsetLbl = new Label(lineComposite, SWT.NONE);
155 GridData gridData = new GridData();
156 gridData.widthHint = offset;
157 offsetLbl.setLayoutData(gridData);
158 addFormLabel(lineComposite, label);
159 Text txt = addFormTextField(lineComposite, text, null);
160 txt.setLayoutData(CmsUiUtils.fillWidth());
164 /** creates a single vertical-block composite for key:value display */
165 public static Text addFormColumn(Composite parent, String label, String text) {
166 // Composite columnComposite = new Composite(parent, SWT.NONE);
167 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
168 // columnComposite.setLayout(new GridLayout(1, false));
169 addFormLabel(parent, label);
170 Text txt = addFormTextField(parent, text, null);
171 txt.setEditable(false);
172 txt.setLayoutData(CmsUiUtils.fillWidth());
176 public static Text addFormColumn(Composite parent, String label, Node node, String property,
177 CmsEditable cmsEditable) {
178 // Composite columnComposite = new Composite(parent, SWT.NONE);
179 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
180 // columnComposite.setLayout(new GridLayout(1, false));
181 addFormLabel(parent, label);
182 String text = Jcr.get(node, property);
183 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
184 Text txt = addFormTextField(parent, text, null, SWT.WRAP);
185 if (cmsEditable != null && cmsEditable.isEditing()) {
186 txt.addModifyListener((e) -> {
187 Jcr.set(node, property, txt.getText());
191 txt.setEditable(false);
193 txt.setLayoutData(CmsUiUtils.fillWidth());
197 public static Label createBoldLabel(Composite parent, Localized localized) {
198 Label label = new Label(parent, SWT.LEAD);
199 label.setText(localized.lead());
200 label.setFont(EclipseUiUtils.getBoldFont(parent));
201 label.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
205 public static Label addFormPicture(Composite parent, String label, Node fileNode) throws RepositoryException {
206 Composite lineComposite = new Composite(parent, SWT.NONE);
207 lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
208 lineComposite.setLayout(new GridLayout(2, true));
209 CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
210 addFormLabel(lineComposite, label);
212 return addPicture(lineComposite, fileNode);
215 public static Label addPicture(Composite parent, Node fileNode) throws RepositoryException {
216 return addPicture(parent, fileNode, null);
219 public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth) throws RepositoryException {
220 Node content = fileNode.getNode(Node.JCR_CONTENT);
221 // TODO move it deeper in the middleware.
222 if (!content.isNodeType(EntityType.box.get())) {
223 if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
224 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
225 ImageData imageData = new ImageData(in);
226 content.addMixin(EntityType.box.get());
227 content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
228 content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
229 content.getSession().save();
230 } catch (IOException e) {
231 throw new RuntimeException(e);
239 if (content.isNodeType(EntityType.box.get())) {
240 width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
241 height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
243 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
244 ImageData imageData = new ImageData(in);
245 width = Long.valueOf(imageData.width);
246 height = Long.valueOf(imageData.height);
247 } catch (IOException e) {
248 throw new RuntimeException(e);
252 if (maxWidth != null && width > maxWidth) {
253 Double ratio = maxWidth.doubleValue() / width.doubleValue();
254 width = maxWidth.longValue();
255 height = Math.round(ratio * height);
257 Label img = new Label(parent, SWT.NONE);
258 CmsUiUtils.markup(img);
259 img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
260 if (parent.getLayout() instanceof GridLayout) {
261 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
262 gd.widthHint = width.intValue();
263 gd.heightHint = height.intValue();
264 img.setLayoutData(gd);
266 img.addMouseListener(new MouseListener() {
267 private static final long serialVersionUID = -1362242049325206168L;
270 public void mouseUp(MouseEvent e) {
274 public void mouseDown(MouseEvent e) {
278 public void mouseDoubleClick(MouseEvent e) {
279 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
282 protected Control createDialogArea(Composite parent) {
283 parent.setLayout(new GridLayout());
284 ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
285 scroll.setLayoutData(CmsUiUtils.fillAll());
286 scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
287 scroll.setExpandHorizontal(true);
288 scroll.setExpandVertical(true);
289 // scroll.setAlwaysShowScrollBars(true);
291 Composite c = new Composite(scroll, SWT.NONE);
292 scroll.setContent(c);
293 c.setLayout(new GridLayout());
294 c.setLayoutData(CmsUiUtils.fillAll());
295 Label bigImg = new Label(c, SWT.NONE);
296 CmsUiUtils.markup(bigImg);
297 bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
298 Jcr.get(content, EntityNames.SVG_HEIGHT)));
299 bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
304 protected Point getInitialSize() {
305 Point shellSize = img.getShell().getSize();
306 return new Point(shellSize.x - 100, shellSize.y - 100);
316 public static boolean isCoworker(CmsView cmsView) {
317 boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
321 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
322 // String... additionnalProps) {
324 // Session tmpSession = null;
325 // Session mainSession = null;
327 // // FIXME would not work if home is another physical workspace
328 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
329 // Node draftNode = null;
330 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
331 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
333 // Wizard wizard = null;
334 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
335 // // WizardDialog dialog = new WizardDialog(shell, wizard);
336 // if (dialog.open() == Window.OK) {
337 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
338 // // FIXME it should be possible to specify the workspace
339 // mainSession = referenceSession.getRepository().login();
340 // Node parent = mainSession.getNode(parentPath);
341 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
342 //// task = appService.saveEntity(task, false);
343 // referenceSession.refresh(true);
344 // return task.getPath();
347 // } catch (RepositoryException e1) {
348 // throw new JcrException(
349 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
351 // JcrUtils.logoutQuietly(tmpSession);
352 // JcrUtils.logoutQuietly(mainSession);