1 package org
.argeo
.cms
.text
;
4 import javax
.jcr
.RepositoryException
;
6 import org
.argeo
.cms
.CmsException
;
7 import org
.argeo
.cms
.CmsImageManager
;
8 import org
.argeo
.cms
.CmsSession
;
9 import org
.argeo
.cms
.internal
.JcrFileUploadReceiver
;
10 import org
.argeo
.cms
.util
.CmsUtils
;
11 import org
.argeo
.cms
.viewers
.NodePart
;
12 import org
.argeo
.cms
.viewers
.Section
;
13 import org
.argeo
.cms
.viewers
.SectionPart
;
14 import org
.argeo
.cms
.widgets
.EditableImage
;
15 import org
.eclipse
.rap
.addons
.fileupload
.FileUploadHandler
;
16 import org
.eclipse
.rap
.addons
.fileupload
.FileUploadListener
;
17 import org
.eclipse
.rap
.addons
.fileupload
.FileUploadReceiver
;
18 import org
.eclipse
.rap
.rwt
.service
.ServerPushSession
;
19 import org
.eclipse
.rap
.rwt
.widgets
.FileUpload
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.SelectionAdapter
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.graphics
.Point
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
27 /** An image within the Argeo Text framework */
28 public class Img
extends EditableImage
implements SectionPart
, NodePart
{
29 private static final long serialVersionUID
= 6233572783968188476L;
31 private final Section section
;
33 private final CmsImageManager imageManager
;
34 private FileUploadHandler currentUploadHandler
= null;
35 private FileUploadListener fileUploadListener
;
37 public Img(Composite parent
, int swtStyle
, Node imgNode
,
38 Point preferredImageSize
) throws RepositoryException
{
39 this(Section
.findSection(parent
), parent
, swtStyle
, imgNode
,
41 setStyle(TextStyles
.TEXT_IMAGE
);
44 public Img(Composite parent
, int swtStyle
, Node imgNode
)
45 throws RepositoryException
{
46 this(Section
.findSection(parent
), parent
, swtStyle
, imgNode
, null);
47 setStyle(TextStyles
.TEXT_IMAGE
);
50 Img(Section section
, Composite parent
, int swtStyle
, Node imgNode
,
51 Point preferredImageSize
) throws RepositoryException
{
52 super(parent
, swtStyle
, imgNode
, false, preferredImageSize
);
53 this.section
= section
;
54 imageManager
= CmsSession
.current
.get().getImageManager();
55 CmsUtils
.style(this, TextStyles
.TEXT_IMG
);
59 protected Control
createControl(Composite box
, String style
) {
62 return createImageChooser(box
, style
);
63 } catch (RepositoryException e
) {
64 throw new CmsException("Cannot create image chooser", e
);
67 return createLabel(box
, style
);
72 public synchronized void stopEditing() {
74 fileUploadListener
= null;
78 protected synchronized Boolean
load(Control lbl
) {
80 Node imgNode
= getNode();
81 boolean loaded
= imageManager
.load(imgNode
, lbl
,
82 getPreferredImageSize());
83 // getParent().layout();
85 } catch (RepositoryException e
) {
86 throw new CmsException("Cannot load " + getNodeId()
87 + " from image manager", e
);
91 protected Control
createImageChooser(Composite box
, String style
)
92 throws RepositoryException
{
93 // FileDialog fileDialog = new FileDialog(getShell());
95 // String fileName = fileDialog.getFileName();
96 CmsImageManager imageManager
= CmsSession
.current
.get()
98 Node node
= getNode();
99 JcrFileUploadReceiver receiver
= new JcrFileUploadReceiver(
100 node
.getParent(), node
.getName() + '[' + node
.getIndex() + ']',
102 if (currentUploadHandler
!= null)
103 currentUploadHandler
.dispose();
104 currentUploadHandler
= prepareUpload(receiver
);
105 final ServerPushSession pushSession
= new ServerPushSession();
106 final FileUpload fileUpload
= new FileUpload(box
, SWT
.NONE
);
107 CmsUtils
.style(fileUpload
, style
);
108 fileUpload
.addSelectionListener(new SelectionAdapter() {
109 private static final long serialVersionUID
= -9158471843941668562L;
112 public void widgetSelected(SelectionEvent e
) {
114 fileUpload
.submit(currentUploadHandler
.getUploadUrl());
120 protected FileUploadHandler
prepareUpload(FileUploadReceiver receiver
) {
121 final FileUploadHandler uploadHandler
= new FileUploadHandler(receiver
);
122 if (fileUploadListener
!= null)
123 uploadHandler
.addUploadListener(fileUploadListener
);
124 return uploadHandler
;
128 public Section
getSection() {
132 public void setFileUploadListener(FileUploadListener fileUploadListener
) {
133 this.fileUploadListener
= fileUploadListener
;
134 if (currentUploadHandler
!= null)
135 currentUploadHandler
.addUploadListener(fileUploadListener
);
139 public Node
getItem() throws RepositoryException
{
144 public String
getPartId() {
149 public String
toString() {
150 return "Img #" + getPartId();