]>
git.argeo.org Git - gpl/argeo-suite.git/blob - swt/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java
1 package org
.argeo
.app
.ui
;
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
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.jcr
.Session
;
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
;
42 /** UI utilities around SWT and JCR. */
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());
57 txt
.setEditable(false);
59 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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());
74 txt
.setEditable(false);
76 txt
.setLayoutData(CmsSwtUtils
.fillWidth());
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);
84 // return addPicture(lineComposite, fileNode);
87 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
88 return addPicture(parent
, fileNode
, null);
91 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
92 return addPicture(parent
, fileNode
, maxWidth
, null);
95 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
, Node link
)
96 throws RepositoryException
{
97 Node content
= fileNode
.getNode(Node
.JCR_CONTENT
);
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
);
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);
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
);
136 if (content
.isNodeType(EntityType
.box
.get())) {
137 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
138 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
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
);
149 if (maxWidth
!= null && width
> maxWidth
) {
150 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
151 width
= maxWidth
.longValue();
152 height
= Math
.round(ratio
* height
);
154 Label img
= new Label(parent
, SWT
.NONE
);
155 CmsSwtUtils
.markup(img
);
156 StringBuffer txt
= new StringBuffer();
157 String target
= toLink(link
);
159 txt
.append("<a href='").append(target
).append("'>");
160 txt
.append(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
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
);
172 img
.addMouseListener(new MouseListener() {
173 private static final long serialVersionUID
= -1362242049325206168L;
176 public void mouseUp(MouseEvent e
) {
180 public void mouseDown(MouseEvent e
) {
184 public void mouseDoubleClick(MouseEvent e
) {
185 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
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);
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));
210 protected Point
getInitialSize() {
211 Point shellSize
= img
.getShell().getSize();
212 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
222 public static String
toLink(Node node
) {
223 return node
!= null ?
"#" + CmsSwtUtils
.cleanPathForUrl(JcrContent
.nodeToContent(node
).getPath()) : null;
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
);
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
);
242 private SuiteUiUtils() {