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
.cms
.CmsException
;
20 import org
.argeo
.cms
.CmsNames
;
21 import org
.argeo
.cms
.text
.Img
;
22 import org
.argeo
.cms
.ui
.CmsEditable
;
23 import org
.argeo
.cms
.ui
.CmsImageManager
;
24 import org
.argeo
.cms
.ui
.internal
.text
.MarkupValidatorCopy
;
25 import org
.argeo
.cms
.util
.CmsUtils
;
26 import org
.argeo
.cms
.viewers
.AbstractPageViewer
;
27 import org
.argeo
.cms
.viewers
.EditablePart
;
28 import org
.argeo
.cms
.viewers
.Section
;
29 import org
.argeo
.cms
.viewers
.SectionPart
;
30 import org
.argeo
.cms
.widgets
.EditableImage
;
31 import org
.argeo
.cms
.widgets
.StyledControl
;
32 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
33 import org
.argeo
.jcr
.JcrUtils
;
34 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
35 import org
.eclipse
.rap
.fileupload
.FileDetails
;
36 import org
.eclipse
.rap
.fileupload
.FileUploadEvent
;
37 import org
.eclipse
.rap
.fileupload
.FileUploadHandler
;
38 import org
.eclipse
.rap
.fileupload
.FileUploadListener
;
39 import org
.eclipse
.rap
.fileupload
.FileUploadReceiver
;
40 import org
.eclipse
.rap
.rwt
.service
.ServerPushSession
;
41 import org
.eclipse
.rap
.rwt
.widgets
.FileUpload
;
42 import org
.eclipse
.swt
.SWT
;
43 import org
.eclipse
.swt
.events
.FocusEvent
;
44 import org
.eclipse
.swt
.events
.FocusListener
;
45 import org
.eclipse
.swt
.events
.MouseAdapter
;
46 import org
.eclipse
.swt
.events
.MouseEvent
;
47 import org
.eclipse
.swt
.events
.MouseListener
;
48 import org
.eclipse
.swt
.events
.SelectionAdapter
;
49 import org
.eclipse
.swt
.events
.SelectionEvent
;
50 import org
.eclipse
.swt
.events
.SelectionListener
;
51 import org
.eclipse
.swt
.graphics
.Point
;
52 import org
.eclipse
.swt
.layout
.FormAttachment
;
53 import org
.eclipse
.swt
.layout
.FormData
;
54 import org
.eclipse
.swt
.layout
.FormLayout
;
55 import org
.eclipse
.swt
.layout
.GridData
;
56 import org
.eclipse
.swt
.layout
.GridLayout
;
57 import org
.eclipse
.swt
.layout
.RowLayout
;
58 import org
.eclipse
.swt
.widgets
.Button
;
59 import org
.eclipse
.swt
.widgets
.Composite
;
60 import org
.eclipse
.swt
.widgets
.Control
;
61 import org
.eclipse
.swt
.widgets
.Label
;
62 import org
.eclipse
.swt
.widgets
.Text
;
64 /** Manage life cycle of a form page that is linked to a given node */
65 public class FormPageViewer
extends AbstractPageViewer
{
66 private final static Log log
= LogFactory
.getLog(FormPageViewer
.class);
67 private static final long serialVersionUID
= 5277789504209413500L;
69 private final Section mainSection
;
71 // TODO manage within the CSS
72 private int labelColWidth
= 150;
73 private int rowLayoutHSpacing
= 8;
75 // Context cached in the viewer
76 // The reference to translate from text to calendar and reverse
77 private DateFormat dateFormat
= new SimpleDateFormat(FormUtils
.DEFAULT_SHORT_DATE_FORMAT
);
78 private CmsImageManager imageManager
;
79 private FileUploadListener fileUploadListener
;
81 public FormPageViewer(Section mainSection
, int style
, CmsEditable cmsEditable
) throws RepositoryException
{
82 super(mainSection
, style
, cmsEditable
);
83 this.mainSection
= mainSection
;
85 if (getCmsEditable().canEdit()) {
86 fileUploadListener
= new FUL();
91 protected void prepare(EditablePart part
, Object caretPosition
) {
92 if (part
instanceof Img
) {
93 ((Img
) part
).setFileUploadListener(fileUploadListener
);
97 /** To be overridden.Save the edited part. */
98 protected void save(EditablePart part
) throws RepositoryException
{
100 if (part
instanceof EditableMultiStringProperty
) {
101 EditableMultiStringProperty ept
= (EditableMultiStringProperty
) part
;
103 List
<String
> values
= ept
.getValues();
105 node
= ept
.getNode();
106 String propName
= ept
.getPropertyName();
107 if (values
.isEmpty()) {
108 if (node
.hasProperty(propName
))
109 node
.getProperty(propName
).remove();
111 node
.setProperty(propName
, values
.toArray(new String
[0]));
113 // => Viewer : Controller
114 } else if (part
instanceof EditablePropertyString
) {
115 EditablePropertyString ept
= (EditablePropertyString
) part
;
117 String txt
= ((Text
) ept
.getControl()).getText();
119 node
= ept
.getNode();
120 String propName
= ept
.getPropertyName();
121 if (EclipseUiUtils
.isEmpty(txt
)) {
122 if (node
.hasProperty(propName
))
123 node
.getProperty(propName
).remove();
125 setPropertySilently(node
, propName
, txt
);
126 // node.setProperty(propName, txt);
128 // node.getSession().save();
129 // => Viewer : Controller
130 } else if (part
instanceof EditablePropertyDate
) {
131 EditablePropertyDate ept
= (EditablePropertyDate
) part
;
132 Calendar cal
= FormUtils
.parseDate(dateFormat
, ((Text
) ept
.getControl()).getText());
133 node
= ept
.getNode();
134 String propName
= ept
.getPropertyName();
136 if (node
.hasProperty(propName
))
137 node
.getProperty(propName
).remove();
139 node
.setProperty(propName
, cal
);
141 // node.getSession().save();
142 // => Viewer : Controller
144 // TODO: make this configurable, sometimes we do not want to save the
145 // current session at this stage
146 if (node
!= null && node
.getSession().hasPendingChanges()) {
147 JcrUtils
.updateLastModified(node
);
148 node
.getSession().save();
153 protected void updateContent(EditablePart part
) throws RepositoryException
{
154 if (part
instanceof EditableMultiStringProperty
) {
155 EditableMultiStringProperty ept
= (EditableMultiStringProperty
) part
;
157 Node node
= ept
.getNode();
158 String propName
= ept
.getPropertyName();
159 List
<String
> valStrings
= new ArrayList
<String
>();
160 if (node
.hasProperty(propName
)) {
161 Value
[] values
= node
.getProperty(propName
).getValues();
162 for (Value val
: values
)
163 valStrings
.add(val
.getString());
165 ept
.setValues(valStrings
);
166 } else if (part
instanceof EditablePropertyString
) {
167 // || part instanceof EditableLink
168 EditablePropertyString ept
= (EditablePropertyString
) part
;
170 Node node
= ept
.getNode();
171 String propName
= ept
.getPropertyName();
172 if (node
.hasProperty(propName
)) {
173 String value
= node
.getProperty(propName
).getString();
177 // => Viewer : Controller
178 } else if (part
instanceof EditablePropertyDate
) {
179 EditablePropertyDate ept
= (EditablePropertyDate
) part
;
181 Node node
= ept
.getNode();
182 String propName
= ept
.getPropertyName();
183 if (node
.hasProperty(propName
))
184 ept
.setText(dateFormat
.format(node
.getProperty(propName
).getDate().getTime()));
187 } else if (part
instanceof SectionPart
) {
188 SectionPart sectionPart
= (SectionPart
) part
;
189 Node partNode
= sectionPart
.getNode();
190 // use control AFTER setting style, since it may have been reset
191 if (part
instanceof EditableImage
) {
192 EditableImage editableImage
= (EditableImage
) part
;
193 imageManager().load(partNode
, part
.getControl(), editableImage
.getPreferredImageSize());
198 // FILE UPLOAD LISTENER
199 protected class FUL
implements FileUploadListener
{
204 public void uploadProgress(FileUploadEvent event
) {
205 // TODO Monitor upload progress
208 public void uploadFailed(FileUploadEvent event
) {
209 throw new CmsException("Upload failed " + event
, event
.getException());
212 public void uploadFinished(FileUploadEvent event
) {
213 for (FileDetails file
: event
.getFileDetails()) {
214 if (log
.isDebugEnabled())
215 log
.debug("Received: " + file
.getFileName());
217 mainSection
.getDisplay().syncExec(new Runnable() {
223 FileUploadHandler uploadHandler
= (FileUploadHandler
) event
.getSource();
224 uploadHandler
.dispose();
228 // FOCUS OUT LISTENER
229 protected FocusListener
createFocusListener() {
230 return new FocusOutListener();
233 private class FocusOutListener
implements FocusListener
{
234 private static final long serialVersionUID
= -6069205786732354186L;
237 public void focusLost(FocusEvent event
) {
242 public void focusGained(FocusEvent event
) {
249 protected MouseListener
createMouseListener() {
253 private class ML
extends MouseAdapter
{
254 private static final long serialVersionUID
= 8526890859876770905L;
257 public void mouseDoubleClick(MouseEvent e
) {
259 Control source
= (Control
) e
.getSource();
260 if (getCmsEditable().canEdit()) {
261 if (getCmsEditable().isEditing() && !(getEdited() instanceof Img
)) {
262 if (source
== mainSection
)
264 EditablePart part
= findDataParent(source
);
267 getCmsEditable().startEditing();
274 public void mouseDown(MouseEvent e
) {
275 if (getCmsEditable().isEditing()) {
277 Control source
= (Control
) e
.getSource();
278 EditablePart composite
= findDataParent(source
);
279 Point point
= new Point(e
.x
, e
.y
);
280 if (!(composite
instanceof Img
))
281 edit(composite
, source
.toDisplay(point
));
282 } else if (e
.button
== 3) {
283 // EditablePart composite = findDataParent((Control) e
285 // if (styledTools != null)
286 // styledTools.show(composite, new Point(e.x, e.y));
291 protected synchronized void upload(EditablePart part
) {
292 if (part
instanceof SectionPart
) {
293 if (part
instanceof Img
) {
294 if (getEdited() == part
)
297 layout(part
.getControl());
304 public Control
getControl() {
308 protected CmsImageManager
imageManager() {
309 if (imageManager
== null)
310 imageManager
= CmsUtils
.getCmsView().getImageManager();
315 protected Section
createSectionIfNeeded(Composite body
, Node node
) throws RepositoryException
{
316 Section section
= null;
318 section
= new Section(body
, SWT
.NO_FOCUS
, node
);
319 section
.setLayoutData(CmsUtils
.fillWidth());
320 section
.setLayout(CmsUtils
.noSpaceGridLayout());
325 protected void createSimpleLT(Composite bodyRow
, Node node
, String propName
, String label
, String msg
)
326 throws RepositoryException
{
327 if (getCmsEditable().canEdit() || node
.hasProperty(propName
)) {
328 createPropertyLbl(bodyRow
, label
);
329 EditablePropertyString eps
= new EditablePropertyString(bodyRow
, SWT
.WRAP
| SWT
.LEFT
, node
, propName
, msg
);
330 eps
.setMouseListener(getMouseListener());
331 eps
.setFocusListener(getFocusListener());
332 eps
.setLayoutData(CmsUtils
.fillWidth());
336 protected void createMultiStringLT(Composite bodyRow
, Node node
, String propName
, String label
, String msg
)
337 throws RepositoryException
{
338 boolean canEdit
= getCmsEditable().canEdit();
339 if (canEdit
|| node
.hasProperty(propName
)) {
340 createPropertyLbl(bodyRow
, label
);
342 List
<String
> valueStrings
= new ArrayList
<String
>();
344 if (node
.hasProperty(propName
)) {
345 Value
[] values
= node
.getProperty(propName
).getValues();
346 for (Value value
: values
)
347 valueStrings
.add(value
.getString());
350 // TODO use a drop down to display possible values to the end user
351 EditableMultiStringProperty emsp
= new EditableMultiStringProperty(bodyRow
, SWT
.SINGLE
| SWT
.LEAD
, node
,
352 propName
, valueStrings
, new String
[] { "Implement this" }, msg
,
353 canEdit ?
getRemoveValueSelListener() : null);
355 // emsp.setMouseListener(getMouseListener());
356 emsp
.setStyle(FormStyle
.propertyMessage
.style());
357 emsp
.setLayoutData(CmsUtils
.fillWidth());
361 protected Label
createPropertyLbl(Composite parent
, String value
) {
362 return createPropertyLbl(parent
, value
, SWT
.TOP
);
365 protected Label
createPropertyLbl(Composite parent
, String value
, int vAlign
) {
366 boolean isSmall
= CmsUtils
.getCmsView().getUxContext().isSmall();
367 Label label
= new Label(parent
, isSmall ? SWT
.LEFT
: SWT
.RIGHT
| SWT
.WRAP
);
368 label
.setText(value
+ " ");
369 CmsUtils
.style(label
, FormStyle
.propertyLabel
.style());
370 GridData gd
= new GridData(isSmall ? SWT
.LEFT
: SWT
.RIGHT
, vAlign
, false, false);
371 gd
.widthHint
= labelColWidth
;
372 label
.setLayoutData(gd
);
376 protected Label
newStyledLabel(Composite parent
, String style
, String value
) {
377 Label label
= new Label(parent
, SWT
.NONE
);
378 label
.setText(value
);
379 CmsUtils
.style(label
, style
);
383 protected Composite
createRowLayoutComposite(Composite parent
) throws RepositoryException
{
384 Composite bodyRow
= new Composite(parent
, SWT
.NO_FOCUS
);
385 bodyRow
.setLayoutData(CmsUtils
.fillWidth());
386 RowLayout rl
= new RowLayout(SWT
.WRAP
);
387 rl
.type
= SWT
.HORIZONTAL
;
388 rl
.spacing
= rowLayoutHSpacing
;
389 rl
.marginHeight
= rl
.marginWidth
= 0;
390 rl
.marginTop
= rl
.marginBottom
= rl
.marginLeft
= rl
.marginRight
= 0;
391 bodyRow
.setLayout(rl
);
395 protected Composite
createAddImgComposite(final Section section
, Composite parent
, final Node parentNode
)
396 throws RepositoryException
{
398 Composite body
= new Composite(parent
, SWT
.NO_FOCUS
);
399 body
.setLayout(new GridLayout());
401 FormFileUploadReceiver receiver
= new FormFileUploadReceiver(section
, parentNode
, null);
402 final FileUploadHandler currentUploadHandler
= new FileUploadHandler(receiver
);
403 if (fileUploadListener
!= null)
404 currentUploadHandler
.addUploadListener(fileUploadListener
);
407 final FileUpload fileUpload
= new FileUpload(body
, SWT
.BORDER
);
408 fileUpload
.setText("Import an image");
409 fileUpload
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, true));
410 fileUpload
.addSelectionListener(new SelectionAdapter() {
411 private static final long serialVersionUID
= 4869523412991968759L;
414 public void widgetSelected(SelectionEvent e
) {
415 ServerPushSession pushSession
= new ServerPushSession();
417 String uploadURL
= currentUploadHandler
.getUploadUrl();
418 fileUpload
.submit(uploadURL
);
425 protected class FormFileUploadReceiver
extends FileUploadReceiver
implements CmsNames
{
427 private Node context
;
428 private Section section
;
431 public FormFileUploadReceiver(Section section
, Node context
, String name
) {
432 this.context
= context
;
433 this.section
= section
;
438 public void receive(InputStream stream
, FileDetails details
) throws IOException
{
441 name
= details
.getFileName();
443 // TODO clean image name more carefully
444 String cleanedName
= name
.replaceAll("[^a-zA-Z0-9-.]", "");
445 // We add a unique prefix to workaround the cache issue: when
446 // deleting and re-adding a new image with same name, the end user
447 // browser will use the cache and the image will remain unchanged
449 cleanedName
= System
.currentTimeMillis() % 100000 + "_" + cleanedName
;
452 imageManager().uploadImage(context
, cleanedName
, stream
);
453 // TODO clean refresh strategy
454 section
.getDisplay().asyncExec(new Runnable() {
458 FormPageViewer
.this.refresh(section
);
460 section
.getParent().layout();
461 } catch (RepositoryException re
) {
462 throw new CmsException("unable to refresh " + "image section for " + context
);
466 } catch (RepositoryException re
) {
467 throw new CmsException("unable to upload image " + name
+ " at " + context
);
472 protected void addListeners(StyledControl control
) {
473 control
.setMouseListener(getMouseListener());
474 control
.setFocusListener(getFocusListener());
477 protected Img
createImgComposite(Composite parent
, Node node
, Point preferredSize
) throws RepositoryException
{
478 Img img
= new Img(parent
, SWT
.NONE
, node
, preferredSize
) {
479 private static final long serialVersionUID
= 1297900641952417540L;
482 protected void setContainerLayoutData(Composite composite
) {
483 composite
.setLayoutData(CmsUtils
.grabWidth(SWT
.CENTER
, SWT
.DEFAULT
));
487 protected void setControlLayoutData(Control control
) {
488 control
.setLayoutData(CmsUtils
.grabWidth(SWT
.CENTER
, SWT
.DEFAULT
));
491 img
.setLayoutData(CmsUtils
.grabWidth(SWT
.CENTER
, SWT
.DEFAULT
));
497 protected Composite
addDeleteAbility(final Section section
, final Node sessionNode
, int topWeight
,
499 Composite comp
= new Composite(section
, SWT
.NONE
);
500 comp
.setLayoutData(CmsUtils
.fillAll());
501 comp
.setLayout(new FormLayout());
503 // The body to be populated
504 Composite body
= new Composite(comp
, SWT
.NO_FOCUS
);
505 body
.setLayoutData(EclipseUiUtils
.fillFormData());
507 if (getCmsEditable().canEdit()) {
509 Button deleteBtn
= new Button(comp
, SWT
.FLAT
);
510 CmsUtils
.style(deleteBtn
, FormStyle
.deleteOverlay
.style());
511 FormData formData
= new FormData();
512 formData
.right
= new FormAttachment(rightWeight
, 0);
513 formData
.top
= new FormAttachment(topWeight
, 0);
514 deleteBtn
.setLayoutData(formData
);
515 deleteBtn
.moveAbove(body
);
517 deleteBtn
.addSelectionListener(new SelectionAdapter() {
518 private static final long serialVersionUID
= 4304223543657238462L;
521 public void widgetSelected(SelectionEvent e
) {
522 super.widgetSelected(e
);
523 if (MessageDialog
.openConfirm(section
.getShell(), "Confirm deletion",
524 "Are you really you want to remove this?")) {
527 session
= sessionNode
.getSession();
528 Section parSection
= section
.getParentSection();
529 sessionNode
.remove();
533 } catch (RepositoryException re
) {
534 throw new CmsException("Unable to delete " + sessionNode
, re
);
545 // // LOCAL HELPERS FOR NODE MANAGEMENT
546 // private Node getOrCreateNode(Node parent, String nodeName, String nodeType) throws RepositoryException {
548 // if (getCmsEditable().canEdit() && !parent.hasNode(nodeName)) {
549 // node = JcrUtils.mkdirs(parent, nodeName, nodeType);
550 // parent.getSession().save();
553 // if (getCmsEditable().canEdit() || parent.hasNode(nodeName))
554 // node = parent.getNode(nodeName);
559 private SelectionListener
getRemoveValueSelListener() {
560 return new SelectionAdapter() {
561 private static final long serialVersionUID
= 9022259089907445195L;
564 public void widgetSelected(SelectionEvent e
) {
565 Object source
= e
.getSource();
566 if (source
instanceof Button
) {
567 Button btn
= (Button
) source
;
568 Object obj
= btn
.getData(FormConstants
.LINKED_VALUE
);
569 EditablePart ep
= findDataParent(btn
);
570 if (ep
!= null && ep
instanceof EditableMultiStringProperty
) {
571 EditableMultiStringProperty emsp
= (EditableMultiStringProperty
) ep
;
572 List
<String
> values
= emsp
.getValues();
573 if (values
.contains(obj
)) {
574 values
.remove(values
.indexOf(obj
));
575 emsp
.setValues(values
);
578 // TODO workaround to force refresh
581 } catch (RepositoryException e1
) {
582 throw new CmsException("Unable to remove value " + obj
, e1
);
592 protected void setPropertySilently(Node node
, String propName
, String value
) throws RepositoryException
{
595 // Format strings to replace \n
596 value
= value
.replaceAll("\n", "<br/>");
597 // Do not make the update if validation fails
599 MarkupValidatorCopy
.getInstance().validate(value
);
600 } catch (Exception e
) {
601 log
.warn("Cannot set [" + value
+ "] on prop " + propName
+ "of " + node
602 + ", String cannot be validated - " + e
.getMessage());
605 // TODO check if the newly created property is of the correct type,
606 // otherwise the property will be silently created with a STRING
608 node
.setProperty(propName
, value
);
609 } catch (ValueFormatException vfe
) {
610 log
.warn("Cannot set [" + value
+ "] on prop " + propName
+ "of " + node
+ " - " + vfe
.getMessage());