]> git.argeo.org Git - gpl/argeo-suite.git/blob - swt/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java
Clean up and document JavaScript related code.
[gpl/argeo-suite.git] / swt / org.argeo.app.ui / src / org / argeo / app / ui / SuiteUiUtils.java
1 package org.argeo.app.ui;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.nio.file.Files;
7 import java.nio.file.Paths;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import javax.jcr.Node;
12 import javax.jcr.RepositoryException;
13 import javax.jcr.Session;
14
15 import org.apache.commons.io.IOUtils;
16 import org.argeo.api.cms.ux.CmsEditable;
17 import org.argeo.api.cms.ux.CmsStyle;
18 import org.argeo.app.api.EntityNames;
19 import org.argeo.app.api.EntityType;
20 import org.argeo.app.swt.ux.SuiteSwtUtils;
21 import org.argeo.app.ux.SuiteUxEvent;
22 import org.argeo.cms.jcr.acr.JcrContent;
23 import org.argeo.cms.swt.CmsSwtUtils;
24 import org.argeo.cms.swt.dialogs.LightweightDialog;
25 import org.argeo.cms.ui.util.CmsLink;
26 import org.argeo.cms.ui.util.CmsUiUtils;
27 import org.argeo.jcr.Jcr;
28 import org.argeo.jcr.JcrUtils;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.ScrolledComposite;
31 import org.eclipse.swt.events.MouseEvent;
32 import org.eclipse.swt.events.MouseListener;
33 import org.eclipse.swt.graphics.ImageData;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Text;
41
42 /** UI utilities around SWT and JCR. */
43 @Deprecated
44 public class SuiteUiUtils {
45 public static Text addFormLine(Composite parent, String label, Node node, String property,
46 CmsEditable cmsEditable) {
47 Composite lineComposite = SuiteSwtUtils.addLineComposite(parent, 2);
48 SuiteSwtUtils.addFormLabel(lineComposite, label);
49 String text = Jcr.get(node, property);
50 Text txt = SuiteSwtUtils.addFormTextField(lineComposite, text, null, SWT.WRAP);
51 if (cmsEditable != null && cmsEditable.isEditing()) {
52 txt.addModifyListener((e) -> {
53 Jcr.set(node, property, txt.getText());
54 Jcr.save(node);
55 });
56 } else {
57 txt.setEditable(false);
58 }
59 txt.setLayoutData(CmsSwtUtils.fillWidth());
60 return txt;
61 }
62
63 public static Text addFormColumn(Composite parent, String label, Node node, String property,
64 CmsEditable cmsEditable) {
65 SuiteSwtUtils.addFormLabel(parent, label);
66 String text = Jcr.get(node, property);
67 Text txt = SuiteSwtUtils.addFormTextField(parent, text, null, SWT.WRAP);
68 if (cmsEditable != null && cmsEditable.isEditing()) {
69 txt.addModifyListener((e) -> {
70 Jcr.set(node, property, txt.getText());
71 Jcr.save(node);
72 });
73 } else {
74 txt.setEditable(false);
75 }
76 txt.setLayoutData(CmsSwtUtils.fillWidth());
77 return txt;
78 }
79
80 // public static Label addFormPicture(Composite parent, String label, Node fileNode) throws RepositoryException {
81 // Composite lineComposite = SuiteSwtUtils.addLineComposite(parent, 2);
82 // SuiteSwtUtils.addFormLabel(lineComposite, label);
83 //
84 // return addPicture(lineComposite, fileNode);
85 // }
86
87 public static Label addPicture(Composite parent, Node fileNode) throws RepositoryException {
88 return addPicture(parent, fileNode, null);
89 }
90
91 public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth) throws RepositoryException {
92 return addPicture(parent, fileNode, maxWidth, null);
93 }
94
95 public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth, Node link)
96 throws RepositoryException {
97 Node content = fileNode.getNode(Node.JCR_CONTENT);
98
99 boolean test = false;
100 if (test) {
101 try (InputStream in = JcrUtils.getFileAsStream(fileNode);
102 OutputStream out = Files.newOutputStream(
103 Paths.get(System.getProperty("user.home") + "/tmp/" + fileNode.getName()));) {
104 // BufferedImage img = ImageIO.read(in);
105 // System.out.println(fileNode.getName() + ": width=" + img.getWidth() + ", height=" + img.getHeight());
106 IOUtils.copy(in, out);
107 } catch (IOException e) {
108 throw new RuntimeException(e);
109 }
110
111 // try (InputStream in = JcrUtils.getFileAsStream(fileNode);) {
112 // ImageData imageData = new ImageData(in);
113 // System.out.println(fileNode.getName() + ": width=" + imageData.width + ", height=" + imageData.height);
114 // } catch (IOException e) {
115 // throw new RuntimeException(e);
116 // }
117 }
118 // TODO move it deeper in the middleware.
119 if (!content.isNodeType(EntityType.box.get())) {
120 if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
121 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
122 ImageData imageData = new ImageData(in);
123 content.addMixin(EntityType.box.get());
124 content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
125 content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
126 content.getSession().save();
127 } catch (IOException e) {
128 throw new RuntimeException(e);
129 }
130 }
131 }
132
133 // TODO optimise
134 Long width;
135 Long height;
136 if (content.isNodeType(EntityType.box.get())) {
137 width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
138 height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
139 } else {
140 try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
141 ImageData imageData = new ImageData(in);
142 width = Long.valueOf(imageData.width);
143 height = Long.valueOf(imageData.height);
144 } catch (IOException e) {
145 throw new RuntimeException(e);
146 }
147 }
148
149 if (maxWidth != null && width > maxWidth) {
150 Double ratio = maxWidth.doubleValue() / width.doubleValue();
151 width = maxWidth.longValue();
152 height = Math.round(ratio * height);
153 }
154 Label img = new Label(parent, SWT.NONE);
155 CmsSwtUtils.markup(img);
156 StringBuffer txt = new StringBuffer();
157 String target = toLink(link);
158 if (target != null)
159 txt.append("<a href='").append(target).append("'>");
160 txt.append(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
161 if (target != null)
162 txt.append("</a>");
163 img.setText(txt.toString());
164 if (parent.getLayout() instanceof GridLayout) {
165 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
166 gd.widthHint = width.intValue();
167 gd.heightHint = height.intValue();
168 img.setLayoutData(gd);
169 }
170
171 if (target == null)
172 img.addMouseListener(new MouseListener() {
173 private static final long serialVersionUID = -1362242049325206168L;
174
175 @Override
176 public void mouseUp(MouseEvent e) {
177 }
178
179 @Override
180 public void mouseDown(MouseEvent e) {
181 }
182
183 @Override
184 public void mouseDoubleClick(MouseEvent e) {
185 LightweightDialog dialog = new LightweightDialog(img.getShell()) {
186
187 @Override
188 protected Control createDialogArea(Composite parent) {
189 parent.setLayout(new GridLayout());
190 ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
191 scroll.setLayoutData(CmsSwtUtils.fillAll());
192 scroll.setLayout(CmsSwtUtils.noSpaceGridLayout());
193 scroll.setExpandHorizontal(true);
194 scroll.setExpandVertical(true);
195 // scroll.setAlwaysShowScrollBars(true);
196
197 Composite c = new Composite(scroll, SWT.NONE);
198 scroll.setContent(c);
199 c.setLayout(new GridLayout());
200 c.setLayoutData(CmsSwtUtils.fillAll());
201 Label bigImg = new Label(c, SWT.NONE);
202 CmsSwtUtils.markup(bigImg);
203 bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
204 Jcr.get(content, EntityNames.SVG_HEIGHT)));
205 bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
206 return bigImg;
207 }
208
209 @Override
210 protected Point getInitialSize() {
211 Point shellSize = img.getShell().getSize();
212 return new Point(shellSize.x - 100, shellSize.y - 100);
213 }
214
215 };
216 dialog.open();
217 }
218 });
219 return img;
220 }
221
222 public static String toLink(Node node) {
223 return node != null ? "#" + CmsSwtUtils.cleanPathForUrl(JcrContent.nodeToContent(node).getPath()) : null;
224 }
225
226 public static Control addLink(Composite parent, String label, Node node, CmsStyle style)
227 throws RepositoryException {
228 String target = toLink(node);
229 CmsLink link = new CmsLink(label, target, style);
230 return link.createUi(parent, node);
231 }
232
233 @Deprecated
234 public static Map<String, Object> eventProperties(Node node) {
235 Map<String, Object> properties = new HashMap<>();
236 String contentPath = '/' + Jcr.getWorkspaceName(node) + Jcr.getPath(node);
237 properties.put(SuiteUxEvent.CONTENT_PATH, contentPath);
238 return properties;
239 }
240
241 /** Singleton. */
242 private SuiteUiUtils() {
243 }
244
245 }