1 package org
.argeo
.cms
.forms
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.text
.DateFormat
;
6 import java
.text
.SimpleDateFormat
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Calendar
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.jcr
.Session
;
14 import javax
.jcr
.Value
;
15 import javax
.jcr
.ValueFormatException
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.ArgeoException
;
20 import org
.argeo
.cms
.CmsEditable
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.cms
.CmsImageManager
;
23 import org
.argeo
.cms
.CmsNames
;
24 import org
.argeo
.cms
.internal
.text
.MarkupValidatorCopy
;
25 import org
.argeo
.cms
.text
.Img
;
26 import org
.argeo
.cms
.util
.CmsUtils
;
27 import org
.argeo
.cms
.viewers
.AbstractPageViewer
;
28 import org
.argeo
.cms
.viewers
.EditablePart
;
29 import org
.argeo
.cms
.viewers
.Section
;
30 import org
.argeo
.cms
.viewers
.SectionPart
;
31 import org
.argeo
.cms
.widgets
.EditableImage
;
32 import org
.argeo
.cms
.widgets
.StyledControl
;
33 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
36 import org
.eclipse
.rap
.addons
.fileupload
.FileDetails
;
37 import org
.eclipse
.rap
.addons
.fileupload
.FileUploadEvent
;
38 import org
.eclipse
.rap
.addons
.fileupload
.FileUploadHandler
;
39 import org
.eclipse
.rap
.addons
.fileupload
.FileUploadListener
;
40 import org
.eclipse
.rap
.addons
.fileupload
.FileUploadReceiver
;
41 import org
.eclipse
.rap
.rwt
.service
.ServerPushSession
;
42 import org
.eclipse
.rap
.rwt
.widgets
.FileUpload
;
43 import org
.eclipse
.swt
.SWT
;
44 import org
.eclipse
.swt
.events
.FocusEvent
;
45 import org
.eclipse
.swt
.events
.FocusListener
;
46 import org
.eclipse
.swt
.events
.MouseAdapter
;
47 import org
.eclipse
.swt
.events
.MouseEvent
;
48 import org
.eclipse
.swt
.events
.MouseListener
;
49 import org
.eclipse
.swt
.events
.SelectionAdapter
;
50 import org
.eclipse
.swt
.events
.SelectionEvent
;
51 import org
.eclipse
.swt
.events
.SelectionListener
;
52 import org
.eclipse
.swt
.graphics
.Point
;
53 import org
.eclipse
.swt
.layout
.FormAttachment
;
54 import org
.eclipse
.swt
.layout
.FormData
;
55 import org
.eclipse
.swt
.layout
.FormLayout
;
56 import org
.eclipse
.swt
.layout
.GridData
;
57 import org
.eclipse
.swt
.layout
.GridLayout
;
58 import org
.eclipse
.swt
.layout
.RowLayout
;
59 import org
.eclipse
.swt
.widgets
.Button
;
60 import org
.eclipse
.swt
.widgets
.Composite
;
61 import org
.eclipse
.swt
.widgets
.Control
;
62 import org
.eclipse
.swt
.widgets
.Label
;
63 import org
.eclipse
.swt
.widgets
.Text
;
65 /** Manage life cycle of a form page that is linked to a given node */
66 public class FormPageViewer
extends AbstractPageViewer
{
67 private final static Log log
= LogFactory
.getLog(FormPageViewer
.class);
68 private static final long serialVersionUID
= 5277789504209413500L;
70 private final Section mainSection
;
72 // TODO manage within the CSS
73 private int labelColWidth
= 150;
74 private int rowLayoutHSpacing
= 8;
76 // Context cached in the viewer
77 // The reference to translate from text to calendar and reverse
78 private DateFormat dateFormat
= new SimpleDateFormat(
79 FormUtils
.DEFAULT_SHORT_DATE_FORMAT
);
80 private CmsImageManager imageManager
;
81 private FileUploadListener fileUploadListener
;
83 public FormPageViewer(Section mainSection
, int style
,
84 CmsEditable cmsEditable
) throws RepositoryException
{
85 super(mainSection
, style
, cmsEditable
);
86 this.mainSection
= mainSection
;
88 if (getCmsEditable().canEdit()) {
89 fileUploadListener
= new FUL();
94 protected void prepare(EditablePart part
, Object caretPosition
) {
95 if (part
instanceof Img
) {
96 ((Img
) part
).setFileUploadListener(fileUploadListener
);
100 /** To be overridden.Save the edited part. */
101 protected void save(EditablePart part
) throws RepositoryException
{
103 if (part
instanceof EditableMultiStringProperty
) {
104 EditableMultiStringProperty ept
= (EditableMultiStringProperty
) part
;
106 List
<String
> values
= ept
.getValues();
108 node
= ept
.getNode();
109 String propName
= ept
.getPropertyName();
110 if (values
.isEmpty()) {
111 if (node
.hasProperty(propName
))
112 node
.getProperty(propName
).remove();
114 node
.setProperty(propName
, values
.toArray(new String
[0]));
116 // => Viewer : Controller
117 } else if (part
instanceof EditablePropertyString
) {
118 EditablePropertyString ept
= (EditablePropertyString
) part
;
120 String txt
= ((Text
) ept
.getControl()).getText();
122 node
= ept
.getNode();
123 String propName
= ept
.getPropertyName();
124 if (EclipseUiUtils
.isEmpty(txt
)) {
125 if (node
.hasProperty(propName
))
126 node
.getProperty(propName
).remove();
128 setPropertySilently(node
, propName
, txt
);
129 // node.setProperty(propName, txt);
131 // node.getSession().save();
132 // => Viewer : Controller
133 } else if (part
instanceof EditablePropertyDate
) {
134 EditablePropertyDate ept
= (EditablePropertyDate
) part
;
135 Calendar cal
= FormUtils
.parseDate(dateFormat
,
136 ((Text
) ept
.getControl()).getText());
137 node
= ept
.getNode();
138 String propName
= ept
.getPropertyName();
140 if (node
.hasProperty(propName
))
141 node
.getProperty(propName
).remove();
143 node
.setProperty(propName
, cal
);
145 // node.getSession().save();
146 // => Viewer : Controller
148 // TODO: make this configurable, sometimes we do not want to save the
149 // current session at this stage
150 if (node
!= null && node
.getSession().hasPendingChanges()) {
151 JcrUtils
.updateLastModified(node
);
152 node
.getSession().save();
157 protected void updateContent(EditablePart part
) throws RepositoryException
{
158 if (part
instanceof EditableMultiStringProperty
) {
159 EditableMultiStringProperty ept
= (EditableMultiStringProperty
) part
;
161 Node node
= ept
.getNode();
162 String propName
= ept
.getPropertyName();
163 List
<String
> valStrings
= new ArrayList
<String
>();
164 if (node
.hasProperty(propName
)) {
165 Value
[] values
= node
.getProperty(propName
).getValues();
166 for (Value val
: values
)
167 valStrings
.add(val
.getString());
169 ept
.setValues(valStrings
);
170 } else if (part
instanceof EditablePropertyString
) {
171 // || part instanceof EditableLink
172 EditablePropertyString ept
= (EditablePropertyString
) part
;
174 Node node
= ept
.getNode();
175 String propName
= ept
.getPropertyName();
176 if (node
.hasProperty(propName
)) {
177 String value
= node
.getProperty(propName
).getString();
181 // => Viewer : Controller
182 } else if (part
instanceof EditablePropertyDate
) {
183 EditablePropertyDate ept
= (EditablePropertyDate
) part
;
185 Node node
= ept
.getNode();
186 String propName
= ept
.getPropertyName();
187 if (node
.hasProperty(propName
))
188 ept
.setText(dateFormat
.format(node
.getProperty(propName
)
189 .getDate().getTime()));
192 } else if (part
instanceof SectionPart
) {
193 SectionPart sectionPart
= (SectionPart
) part
;
194 Node partNode
= sectionPart
.getNode();
195 // use control AFTER setting style, since it may have been reset
196 if (part
instanceof EditableImage
) {
197 EditableImage editableImage
= (EditableImage
) part
;
198 imageManager().load(partNode
, part
.getControl(),
199 editableImage
.getPreferredImageSize());
204 // FILE UPLOAD LISTENER
205 protected class FUL
implements FileUploadListener
{
210 public void uploadProgress(FileUploadEvent event
) {
211 // TODO Monitor upload progress
214 public void uploadFailed(FileUploadEvent event
) {
215 throw new CmsException("Upload failed " + event
,
216 event
.getException());
219 public void uploadFinished(FileUploadEvent event
) {
220 for (FileDetails file
: event
.getFileDetails()) {
221 if (log
.isDebugEnabled())
222 log
.debug("Received: " + file
.getFileName());
224 mainSection
.getDisplay().syncExec(new Runnable() {
230 FileUploadHandler uploadHandler
= (FileUploadHandler
) event
232 uploadHandler
.dispose();
236 // FOCUS OUT LISTENER
237 protected FocusListener
createFocusListener() {
238 return new FocusOutListener();
241 private class FocusOutListener
implements FocusListener
{
242 private static final long serialVersionUID
= -6069205786732354186L;
245 public void focusLost(FocusEvent event
) {
250 public void focusGained(FocusEvent event
) {
257 protected MouseListener
createMouseListener() {
261 private class ML
extends MouseAdapter
{
262 private static final long serialVersionUID
= 8526890859876770905L;
265 public void mouseDoubleClick(MouseEvent e
) {
267 Control source
= (Control
) e
.getSource();
268 if (getCmsEditable().canEdit()) {
269 if (getCmsEditable().isEditing()
270 && !(getEdited() instanceof Img
)) {
271 if (source
== mainSection
)
273 EditablePart part
= findDataParent(source
);
276 getCmsEditable().startEditing();
283 public void mouseDown(MouseEvent e
) {
284 if (getCmsEditable().isEditing()) {
286 Control source
= (Control
) e
.getSource();
287 EditablePart composite
= findDataParent(source
);
288 Point point
= new Point(e
.x
, e
.y
);
289 if (!(composite
instanceof Img
))
290 edit(composite
, source
.toDisplay(point
));
291 } else if (e
.button
== 3) {
292 // EditablePart composite = findDataParent((Control) e
294 // if (styledTools != null)
295 // styledTools.show(composite, new Point(e.x, e.y));
300 protected synchronized void upload(EditablePart part
) {
301 if (part
instanceof SectionPart
) {
302 if (part
instanceof Img
) {
303 if (getEdited() == part
)
306 layout(part
.getControl());
313 public Control
getControl() {
317 protected CmsImageManager
imageManager() {
318 if (imageManager
== null)
319 imageManager
= CmsUtils
.getCmsView().getImageManager();
324 protected Section
createSectionIfNeeded(Composite body
, Node node
)
325 throws RepositoryException
{
326 Section section
= null;
328 section
= new Section(body
, SWT
.NO_FOCUS
, node
);
329 section
.setLayoutData(CmsUtils
.fillWidth());
330 section
.setLayout(CmsUtils
.noSpaceGridLayout());
335 protected void createSimpleLT(Composite bodyRow
, Node node
,
336 String propName
, String label
, String msg
)
337 throws RepositoryException
{
338 if (getCmsEditable().canEdit() || node
.hasProperty(propName
)) {
339 createPropertyLbl(bodyRow
, label
);
340 EditablePropertyString eps
= new EditablePropertyString(bodyRow
,
341 SWT
.WRAP
| SWT
.LEFT
, node
, propName
, msg
);
342 eps
.setMouseListener(getMouseListener());
343 eps
.setFocusListener(getFocusListener());
344 eps
.setLayoutData(CmsUtils
.fillWidth());
348 protected void createMultiStringLT(Composite bodyRow
, Node node
,
349 String propName
, String label
, String msg
)
350 throws RepositoryException
{
351 boolean canEdit
= getCmsEditable().canEdit();
352 if (canEdit
|| node
.hasProperty(propName
)) {
353 createPropertyLbl(bodyRow
, label
);
355 List
<String
> valueStrings
= new ArrayList
<String
>();
357 if (node
.hasProperty(propName
)) {
358 Value
[] values
= node
.getProperty(propName
).getValues();
359 for (Value value
: values
)
360 valueStrings
.add(value
.getString());
363 // TODO use a drop down to display possible values to the end user
364 EditableMultiStringProperty emsp
= new EditableMultiStringProperty(
365 bodyRow
, SWT
.SINGLE
| SWT
.LEAD
, node
, propName
,
366 valueStrings
, new String
[] { "Implement this" }, msg
,
367 canEdit ?
getRemoveValueSelListener() : null);
369 // emsp.setMouseListener(getMouseListener());
370 emsp
.setStyle(FormStyle
.propertyMessage
.style());
371 emsp
.setLayoutData(CmsUtils
.fillWidth());
375 protected Label
createPropertyLbl(Composite parent
, String value
) {
376 return createPropertyLbl(parent
, value
, SWT
.TOP
);
379 protected Label
createPropertyLbl(Composite parent
, String value
, int vAlign
) {
380 boolean isSmall
= CmsUtils
.getCmsView().getUxContext().isSmall();
381 Label label
= new Label(parent
, isSmall ? SWT
.LEFT
: SWT
.RIGHT
383 label
.setText(value
+ " ");
384 CmsUtils
.style(label
, FormStyle
.propertyLabel
.style());
385 GridData gd
= new GridData(isSmall ? SWT
.LEFT
: SWT
.RIGHT
, vAlign
,
387 gd
.widthHint
= labelColWidth
;
388 label
.setLayoutData(gd
);
392 protected Label
newStyledLabel(Composite parent
, String style
, String value
) {
393 Label label
= new Label(parent
, SWT
.NONE
);
394 label
.setText(value
);
395 CmsUtils
.style(label
, style
);
399 protected Composite
createRowLayoutComposite(Composite parent
)
400 throws RepositoryException
{
401 Composite bodyRow
= new Composite(parent
, SWT
.NO_FOCUS
);
402 bodyRow
.setLayoutData(CmsUtils
.fillWidth());
403 RowLayout rl
= new RowLayout(SWT
.WRAP
);
404 rl
.type
= SWT
.HORIZONTAL
;
405 rl
.spacing
= rowLayoutHSpacing
;
406 rl
.marginHeight
= rl
.marginWidth
= 0;
407 rl
.marginTop
= rl
.marginBottom
= rl
.marginLeft
= rl
.marginRight
= 0;
408 bodyRow
.setLayout(rl
);
412 protected Composite
createAddImgComposite(final Section section
,
413 Composite parent
, final Node parentNode
) throws RepositoryException
{
415 Composite body
= new Composite(parent
, SWT
.NO_FOCUS
);
416 body
.setLayout(new GridLayout());
418 FormFileUploadReceiver receiver
= new FormFileUploadReceiver(section
,
420 final FileUploadHandler currentUploadHandler
= new FileUploadHandler(
422 if (fileUploadListener
!= null)
423 currentUploadHandler
.addUploadListener(fileUploadListener
);
426 final FileUpload fileUpload
= new FileUpload(body
, SWT
.BORDER
);
427 fileUpload
.setText("Import an image");
428 fileUpload
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true,
430 fileUpload
.addSelectionListener(new SelectionAdapter() {
431 private static final long serialVersionUID
= 4869523412991968759L;
434 public void widgetSelected(SelectionEvent e
) {
435 ServerPushSession pushSession
= new ServerPushSession();
437 String uploadURL
= currentUploadHandler
.getUploadUrl();
438 fileUpload
.submit(uploadURL
);
445 protected class FormFileUploadReceiver
extends FileUploadReceiver
implements
448 private Node context
;
449 private Section section
;
452 public FormFileUploadReceiver(Section section
, Node context
, String name
) {
453 this.context
= context
;
454 this.section
= section
;
459 public void receive(InputStream stream
, FileDetails details
)
463 name
= details
.getFileName();
465 imageManager().uploadImage(context
, name
, stream
);
466 // TODO clean refresh strategy
467 section
.getDisplay().asyncExec(new Runnable() {
471 FormPageViewer
.this.refresh(section
);
473 section
.getParent().layout();
474 } catch (RepositoryException re
) {
475 throw new ArgeoException("unable to refresh "
476 + "image section for " + context
);
480 } catch (RepositoryException re
) {
481 throw new ArgeoException("unable to upload image " + name
487 protected void addListeners(StyledControl control
) {
488 control
.setMouseListener(getMouseListener());
489 control
.setFocusListener(getFocusListener());
492 protected Img
createImgComposite(Composite parent
, Node node
,
493 Point preferredSize
) throws RepositoryException
{
494 Img img
= new Img(parent
, SWT
.NONE
, node
, preferredSize
) {
495 private static final long serialVersionUID
= 1297900641952417540L;
498 protected void setContainerLayoutData(Composite composite
) {
499 composite
.setLayoutData(CmsUtils
.grabWidth(SWT
.CENTER
,
504 protected void setControlLayoutData(Control control
) {
505 control
.setLayoutData(CmsUtils
.grabWidth(SWT
.CENTER
,
509 img
.setLayoutData(CmsUtils
.grabWidth(SWT
.CENTER
, SWT
.DEFAULT
));
515 protected Composite
addDeleteAbility(final Section section
,
516 final Node sessionNode
, int topWeight
, int rightWeight
) {
517 Composite comp
= new Composite(section
, SWT
.NONE
);
518 comp
.setLayoutData(CmsUtils
.fillAll());
519 comp
.setLayout(new FormLayout());
521 // The body to be populated
522 Composite body
= new Composite(comp
, SWT
.NO_FOCUS
);
523 body
.setLayoutData(EclipseUiUtils
.fillFormData());
525 if (getCmsEditable().canEdit()) {
527 Button deleteBtn
= new Button(comp
, SWT
.FLAT
);
528 CmsUtils
.style(deleteBtn
, FormStyle
.deleteOverlay
.style());
529 FormData formData
= new FormData();
530 formData
.right
= new FormAttachment(rightWeight
, 0);
531 formData
.top
= new FormAttachment(topWeight
, 0);
532 deleteBtn
.setLayoutData(formData
);
533 deleteBtn
.moveAbove(body
);
535 deleteBtn
.addSelectionListener(new SelectionAdapter() {
536 private static final long serialVersionUID
= 4304223543657238462L;
539 public void widgetSelected(SelectionEvent e
) {
540 super.widgetSelected(e
);
541 if (MessageDialog
.openConfirm(section
.getShell(),
543 "Are you really you want to remove this?")) {
546 session
= sessionNode
.getSession();
547 Section parSection
= section
.getParentSection();
548 sessionNode
.remove();
552 } catch (RepositoryException re
) {
553 throw new ArgeoException("Unable to delete "
565 // LOCAL HELPERS FOR NODE MANAGEMENT
566 protected Node
getOrCreateNode(Node parent
, String nodeType
, String nodeName
)
567 throws RepositoryException
{
569 if (getCmsEditable().canEdit() && !parent
.hasNode(nodeName
)) {
570 node
= JcrUtils
.mkdirs(parent
, nodeName
, nodeType
);
571 parent
.getSession().save();
574 if (getCmsEditable().canEdit() || parent
.hasNode(nodeName
))
575 node
= parent
.getNode(nodeName
);
580 private SelectionListener
getRemoveValueSelListener() {
581 return new SelectionAdapter() {
582 private static final long serialVersionUID
= 9022259089907445195L;
585 public void widgetSelected(SelectionEvent e
) {
586 Object source
= e
.getSource();
587 if (source
instanceof Button
) {
588 Button btn
= (Button
) source
;
589 Object obj
= btn
.getData(FormConstants
.LINKED_VALUE
);
590 EditablePart ep
= findDataParent(btn
);
591 if (ep
!= null && ep
instanceof EditableMultiStringProperty
) {
592 EditableMultiStringProperty emsp
= (EditableMultiStringProperty
) ep
;
593 List
<String
> values
= emsp
.getValues();
594 if (values
.contains(obj
)) {
595 values
.remove(values
.indexOf(obj
));
596 emsp
.setValues(values
);
599 // TODO workaround to force refresh
602 } catch (RepositoryException e1
) {
603 throw new ArgeoException(
604 "Unable to remove value " + obj
, e1
);
614 protected void setPropertySilently(Node node
, String propName
, String value
)
615 throws RepositoryException
{
618 // Format strings to replace \n
619 value
= value
.replaceAll("\n", "<br/>");
620 // Do not make the update if validation fails
622 MarkupValidatorCopy
.getInstance().validate(value
);
623 } catch (Exception e
) {
624 log
.warn("Cannot set [" + value
+ "] on prop " + propName
625 + "of " + node
+ ", String cannot be validated - "
629 // TODO check if the newly created property is of the correct type,
630 // otherwise the property will be silently created with a STRING
632 node
.setProperty(propName
, value
);
633 } catch (ValueFormatException vfe
) {
634 log
.warn("Cannot set [" + value
+ "] on prop " + propName
+ "of "
635 + node
+ " - " + vfe
.getMessage());