View Javadoc
1   package org.argeo.cms.ui.widgets;
2   
3   import javax.jcr.Node;
4   import javax.jcr.RepositoryException;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.argeo.cms.ui.util.CmsUiUtils;
9   import org.eclipse.swt.graphics.Point;
10  import org.eclipse.swt.widgets.Composite;
11  import org.eclipse.swt.widgets.Control;
12  import org.eclipse.swt.widgets.Label;
13  import org.eclipse.swt.widgets.Text;
14  
15  /** A stylable and editable image. */
16  public abstract class EditableImage extends StyledControl {
17  	private static final long serialVersionUID = -5689145523114022890L;
18  	private final static Log log = LogFactory.getLog(EditableImage.class);
19  
20  	private Point preferredImageSize;
21  	private Boolean loaded = false;
22  
23  	public EditableImage(Composite parent, int swtStyle) {
24  		super(parent, swtStyle);
25  	}
26  
27  	public EditableImage(Composite parent, int swtStyle,
28  			Point preferredImageSize) {
29  		super(parent, swtStyle);
30  		this.preferredImageSize = preferredImageSize;
31  	}
32  
33  	public EditableImage(Composite parent, int style, Node node,
34  			boolean cacheImmediately, Point preferredImageSize)
35  			throws RepositoryException {
36  		super(parent, style, node, cacheImmediately);
37  		this.preferredImageSize = preferredImageSize;
38  	}
39  
40  	@Override
41  	protected void setContainerLayoutData(Composite composite) {
42  		// composite.setLayoutData(fillWidth());
43  	}
44  
45  	@Override
46  	protected void setControlLayoutData(Control control) {
47  		// control.setLayoutData(fillWidth());
48  	}
49  
50  	/** To be overriden. */
51  	protected String createImgTag() throws RepositoryException {
52  		return CmsUiUtils.noImg(preferredImageSize != null ? preferredImageSize
53  				: getSize());
54  	}
55  
56  	protected Label createLabel(Composite box, String style) {
57  		Label lbl = new Label(box, getStyle());
58  		// lbl.setLayoutData(CmsUiUtils.fillWidth());
59  		CmsUiUtils.markup(lbl);
60  		CmsUiUtils.style(lbl, style);
61  		if (mouseListener != null)
62  			lbl.addMouseListener(mouseListener);
63  		load(lbl);
64  		return lbl;
65  	}
66  
67  	/** To be overriden. */
68  	protected synchronized Boolean load(Control control) {
69  		String imgTag;
70  		try {
71  			imgTag = createImgTag();
72  		} catch (Exception e) {
73  			// throw new CmsException("Cannot retrieve image", e);
74  			log.error("Cannot retrieve image", e);
75  			imgTag = CmsUiUtils.noImg(preferredImageSize);
76  			loaded = false;
77  		}
78  
79  		if (imgTag == null) {
80  			loaded = false;
81  			imgTag = CmsUiUtils.noImg(preferredImageSize);
82  		} else
83  			loaded = true;
84  		if (control != null) {
85  			((Label) control).setText(imgTag);
86  			control.setSize(preferredImageSize != null ? preferredImageSize
87  					: getSize());
88  		} else {
89  			loaded = false;
90  		}
91  		getParent().layout();
92  		return loaded;
93  	}
94  
95  	public void setPreferredSize(Point size) {
96  		this.preferredImageSize = size;
97  		if (!loaded) {
98  			load((Label) getControl());
99  		}
100 	}
101 
102 	protected Text createText(Composite box, String style) {
103 		Text text = new Text(box, getStyle());
104 		CmsUiUtils.style(text, style);
105 		return text;
106 	}
107 
108 	public Point getPreferredImageSize() {
109 		return preferredImageSize;
110 	}
111 
112 }