]>
git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteUiUtils.java
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
.ui
.CmsEditable
;
12 import org
.argeo
.cms
.ui
.dialogs
.LightweightDialog
;
13 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
14 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
15 import org
.argeo
.entity
.EntityNames
;
16 import org
.argeo
.entity
.EntityType
;
17 import org
.argeo
.jcr
.Jcr
;
18 import org
.argeo
.jcr
.JcrUtils
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
21 import org
.eclipse
.swt
.events
.MouseEvent
;
22 import org
.eclipse
.swt
.events
.MouseListener
;
23 import org
.eclipse
.swt
.graphics
.ImageData
;
24 import org
.eclipse
.swt
.graphics
.Point
;
25 import org
.eclipse
.swt
.layout
.GridData
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.widgets
.Button
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Label
;
31 import org
.eclipse
.swt
.widgets
.Text
;
33 /** UI utilities related to the APAF project. */
34 public class SuiteUiUtils
{
37 private SuiteUiUtils() {
40 /** creates a title bar composite with label and optional button */
41 public static void addTitleBar(Composite parent
, String title
, Boolean isEditable
) {
42 Composite titleBar
= new Composite(parent
, SWT
.NONE
);
43 titleBar
.setLayoutData(CmsUiUtils
.fillWidth());
44 CmsUiUtils
.style(titleBar
, SuiteStyle
.titleContainer
);
46 titleBar
.setLayout(CmsUiUtils
.noSpaceGridLayout(new GridLayout(2, false)));
47 Label titleLbl
= new Label(titleBar
, SWT
.NONE
);
48 titleLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, true));
49 CmsUiUtils
.style(titleLbl
, SuiteStyle
.titleLabel
);
50 titleLbl
.setText(title
);
53 Button editBtn
= new Button(titleBar
, SWT
.PUSH
);
54 editBtn
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false));
55 CmsUiUtils
.style(editBtn
, SuiteStyle
.inlineButton
);
56 editBtn
.setText("Edit");
60 public static Label
addFormLabel(Composite parent
, String label
) {
61 Label lbl
= new Label(parent
, SWT
.WRAP
);
63 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
64 CmsUiUtils
.style(lbl
, SuiteStyle
.simpleLabel
);
68 public static Text
addFormTextField(Composite parent
, String text
, String message
) {
69 return addFormTextField(parent
, text
, message
, SWT
.NONE
);
72 public static Text
addFormTextField(Composite parent
, String text
, String message
, int style
) {
73 Text txt
= new Text(parent
, style
);
77 txt
.setMessage(message
);
78 txt
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, true));
79 CmsUiUtils
.style(txt
, SuiteStyle
.simpleText
);
83 public static Text
addFormInputField(Composite parent
, String placeholder
) {
84 Text txt
= new Text(parent
, SWT
.BORDER
);
86 GridData gridData
= CmsUiUtils
.fillWidth();
87 txt
.setLayoutData(gridData
);
89 if (placeholder
!= null)
90 txt
.setText(placeholder
);
92 CmsUiUtils
.style(txt
, SuiteStyle
.simpleInput
);
96 /** creates a single horizontal-block composite for key:value display */
97 public static Text
addFormLine(Composite parent
, String label
, String text
) {
98 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
99 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
100 lineComposite
.setLayout(new GridLayout(2, false));
101 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
102 addFormLabel(lineComposite
, label
);
103 Text txt
= addFormTextField(lineComposite
, text
, null);
104 txt
.setEditable(false);
105 txt
.setLayoutData(CmsUiUtils
.fillWidth());
109 public static Text
addFormLine(Composite parent
, String label
, Node node
, String property
,
110 CmsEditable cmsEditable
) {
111 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
112 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
113 lineComposite
.setLayout(new GridLayout(2, false));
114 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
115 addFormLabel(lineComposite
, label
);
116 String text
= Jcr
.get(node
, property
);
117 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
118 Text txt
= addFormTextField(lineComposite
, text
, null, SWT
.WRAP
);
119 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
120 txt
.addModifyListener((e
) -> {
121 Jcr
.set(node
, property
, txt
.getText());
125 txt
.setEditable(false);
127 txt
.setLayoutData(CmsUiUtils
.fillWidth());
131 public static Text
addFormInput(Composite parent
, String label
, String placeholder
) {
132 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
133 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
134 lineComposite
.setLayout(new GridLayout(2, false));
135 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
136 addFormLabel(lineComposite
, label
);
137 Text txt
= addFormInputField(lineComposite
, placeholder
);
138 txt
.setLayoutData(CmsUiUtils
.fillWidth());
143 * creates a single horizontal-block composite for key:value display, with
146 public static Text
addFormLine(Composite parent
, String label
, String text
, Integer offset
) {
147 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
148 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
149 lineComposite
.setLayout(new GridLayout(3, false));
150 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
151 Label offsetLbl
= new Label(lineComposite
, SWT
.NONE
);
152 GridData gridData
= new GridData();
153 gridData
.widthHint
= offset
;
154 offsetLbl
.setLayoutData(gridData
);
155 addFormLabel(lineComposite
, label
);
156 Text txt
= addFormTextField(lineComposite
, text
, null);
157 txt
.setLayoutData(CmsUiUtils
.fillWidth());
161 /** creates a single vertical-block composite for key:value display */
162 public static Text
addFormColumn(Composite parent
, String label
, String text
) {
163 // Composite columnComposite = new Composite(parent, SWT.NONE);
164 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
165 // columnComposite.setLayout(new GridLayout(1, false));
166 addFormLabel(parent
, label
);
167 Text txt
= addFormTextField(parent
, text
, null);
168 txt
.setEditable(false);
169 txt
.setLayoutData(CmsUiUtils
.fillWidth());
173 public static Text
addFormColumn(Composite parent
, String label
, Node node
, String property
,
174 CmsEditable cmsEditable
) {
175 // Composite columnComposite = new Composite(parent, SWT.NONE);
176 // columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
177 // columnComposite.setLayout(new GridLayout(1, false));
178 addFormLabel(parent
, label
);
179 String text
= Jcr
.get(node
, property
);
180 // int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
181 Text txt
= addFormTextField(parent
, text
, null, SWT
.WRAP
);
182 if (cmsEditable
!= null && cmsEditable
.isEditing()) {
183 txt
.addModifyListener((e
) -> {
184 Jcr
.set(node
, property
, txt
.getText());
188 txt
.setEditable(false);
190 txt
.setLayoutData(CmsUiUtils
.fillWidth());
194 public static Label
createBoldLabel(Composite parent
, Localized localized
) {
195 Label label
= new Label(parent
, SWT
.LEAD
);
196 label
.setText(localized
.lead());
197 label
.setFont(EclipseUiUtils
.getBoldFont(parent
));
198 label
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false));
202 public static Label
addFormPicture(Composite parent
, String label
, Node fileNode
) throws RepositoryException
{
203 Composite lineComposite
= new Composite(parent
, SWT
.NONE
);
204 lineComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
205 lineComposite
.setLayout(new GridLayout(2, true));
206 CmsUiUtils
.style(lineComposite
, SuiteStyle
.formLine
);
207 addFormLabel(lineComposite
, label
);
209 return addPicture(lineComposite
, fileNode
);
212 public static Label
addPicture(Composite parent
, Node fileNode
) throws RepositoryException
{
213 return addPicture(parent
, fileNode
, null);
216 public static Label
addPicture(Composite parent
, Node fileNode
, Integer maxWidth
) throws RepositoryException
{
217 Node content
= fileNode
.getNode(Node
.JCR_CONTENT
);
218 // TODO move it deeper in the middleware.
219 if (!content
.isNodeType(EntityType
.box
.get())) {
220 if (content
.getSession().hasPermission(content
.getPath(), Session
.ACTION_SET_PROPERTY
)) {
221 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
222 ImageData imageData
= new ImageData(in
);
223 content
.addMixin(EntityType
.box
.get());
224 content
.setProperty(EntityNames
.SVG_WIDTH
, imageData
.width
);
225 content
.setProperty(EntityNames
.SVG_HEIGHT
, imageData
.height
);
226 content
.getSession().save();
227 } catch (IOException e
) {
228 throw new RuntimeException(e
);
236 if (content
.isNodeType(EntityType
.box
.get())) {
237 width
= content
.getProperty(EntityNames
.SVG_WIDTH
).getLong();
238 height
= content
.getProperty(EntityNames
.SVG_HEIGHT
).getLong();
240 try (InputStream in
= JcrUtils
.getFileAsStream(fileNode
)) {
241 ImageData imageData
= new ImageData(in
);
242 width
= Long
.valueOf(imageData
.width
);
243 height
= Long
.valueOf(imageData
.height
);
244 } catch (IOException e
) {
245 throw new RuntimeException(e
);
249 if (maxWidth
!= null && width
> maxWidth
) {
250 Double ratio
= maxWidth
.doubleValue() / width
.doubleValue();
251 width
= maxWidth
.longValue();
252 height
= Math
.round(ratio
* height
);
254 Label img
= new Label(parent
, SWT
.NONE
);
255 CmsUiUtils
.markup(img
);
256 img
.setText(CmsUiUtils
.img(fileNode
, width
.toString(), height
.toString()));
257 if (parent
.getLayout() instanceof GridLayout
) {
258 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
259 gd
.widthHint
= width
.intValue();
260 gd
.heightHint
= height
.intValue();
261 img
.setLayoutData(gd
);
263 img
.addMouseListener(new MouseListener() {
264 private static final long serialVersionUID
= -1362242049325206168L;
267 public void mouseUp(MouseEvent e
) {
271 public void mouseDown(MouseEvent e
) {
275 public void mouseDoubleClick(MouseEvent e
) {
276 LightweightDialog dialog
= new LightweightDialog(img
.getShell()) {
279 protected Control
createDialogArea(Composite parent
) {
280 parent
.setLayout(new GridLayout());
281 ScrolledComposite scroll
= new ScrolledComposite(parent
, SWT
.H_SCROLL
| SWT
.V_SCROLL
);
282 scroll
.setLayoutData(CmsUiUtils
.fillAll());
283 scroll
.setLayout(CmsUiUtils
.noSpaceGridLayout());
284 scroll
.setExpandHorizontal(true);
285 scroll
.setExpandVertical(true);
286 // scroll.setAlwaysShowScrollBars(true);
288 Composite c
= new Composite(scroll
, SWT
.NONE
);
289 scroll
.setContent(c
);
290 c
.setLayout(new GridLayout());
291 c
.setLayoutData(CmsUiUtils
.fillAll());
292 Label bigImg
= new Label(c
, SWT
.NONE
);
293 CmsUiUtils
.markup(bigImg
);
294 bigImg
.setText(CmsUiUtils
.img(fileNode
, Jcr
.get(content
, EntityNames
.SVG_WIDTH
),
295 Jcr
.get(content
, EntityNames
.SVG_HEIGHT
)));
296 bigImg
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, true));
301 protected Point
getInitialSize() {
302 Point shellSize
= img
.getShell().getSize();
303 return new Point(shellSize
.x
- 100, shellSize
.y
- 100);
313 // public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
314 // String... additionnalProps) {
316 // Session tmpSession = null;
317 // Session mainSession = null;
319 // // FIXME would not work if home is another physical workspace
320 // tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
321 // Node draftNode = null;
322 // for (int i = 0; i < additionnalProps.length - 1; i += 2) {
323 // draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
325 // Wizard wizard = null;
326 // CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
327 // // WizardDialog dialog = new WizardDialog(shell, wizard);
328 // if (dialog.open() == Window.OK) {
329 // String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
330 // // FIXME it should be possible to specify the workspace
331 // mainSession = referenceSession.getRepository().login();
332 // Node parent = mainSession.getNode(parentPath);
333 // Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
334 //// task = appService.saveEntity(task, false);
335 // referenceSession.refresh(true);
336 // return task.getPath();
339 // } catch (RepositoryException e1) {
340 // throw new JcrException(
341 // "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
343 // JcrUtils.logoutQuietly(tmpSession);
344 // JcrUtils.logoutQuietly(mainSession);