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