]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/widgets/StyledControl.java
Merge application and entry point factory
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / widgets / StyledControl.java
1 package org.argeo.cms.widgets;
2
3 import javax.jcr.Item;
4 import javax.jcr.RepositoryException;
5
6 import org.argeo.cms.CmsConstants;
7 import org.argeo.cms.CmsNames;
8 import org.argeo.cms.CmsUtils;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.MouseListener;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13
14 /** Editable text part displaying styled text. */
15 public abstract class StyledControl extends JcrComposite implements
16 CmsConstants, CmsNames {
17 private static final long serialVersionUID = -6372283442330912755L;
18 private Control control;
19
20 private Composite container;
21 private Composite box;
22
23 protected MouseListener mouseListener;
24
25 private Boolean editing = Boolean.FALSE;
26
27 public StyledControl(Composite parent, int swtStyle) {
28 super(parent, swtStyle);
29 setLayout(CmsUtils.noSpaceGridLayout());
30 }
31
32 public StyledControl(Composite parent, int style, Item item)
33 throws RepositoryException {
34 super(parent, style, item);
35 }
36
37 public StyledControl(Composite parent, int style, Item item,
38 boolean cacheImmediately) throws RepositoryException {
39 super(parent, style, item, cacheImmediately);
40 }
41
42 protected abstract Control createControl(Composite box, String style);
43
44 protected Composite createBox(Composite parent) {
45 Composite box = new Composite(parent, SWT.INHERIT_DEFAULT);
46 setContainerLayoutData(box);
47 box.setLayout(CmsUtils.noSpaceGridLayout());
48 // new Label(box, SWT.NONE).setText("BOX");
49 return box;
50 }
51
52 public Control getControl() {
53 return control;
54 }
55
56 protected synchronized Boolean isEditing() {
57 return editing;
58 }
59
60 public synchronized void startEditing() {
61 assert !isEditing();
62 editing = true;
63 // int height = control.getSize().y;
64 String style = (String) control.getData(STYLE);
65 clear(false);
66 control = createControl(box, style);
67 setControlLayoutData(control);
68 }
69
70 public synchronized void stopEditing() {
71 assert isEditing();
72 editing = false;
73 String style = (String) control.getData(STYLE);
74 clear(false);
75 control = createControl(box, style);
76 setControlLayoutData(control);
77 }
78
79 public void setStyle(String style) {
80 Object currentStyle = null;
81 if (control != null)
82 currentStyle = control.getData(STYLE);
83 if (currentStyle != null && currentStyle.equals(style))
84 return;
85
86 // Integer preferredHeight = control != null ? control.getSize().y :
87 // null;
88 clear(true);
89 control = createControl(box, style);
90 setControlLayoutData(control);
91
92 control.getParent().setData(STYLE, style + "_box");
93 control.getParent().getParent().setData(STYLE, style + "_container");
94 }
95
96 /** To be overridden */
97 protected void setControlLayoutData(Control control) {
98 control.setLayoutData(CmsUtils.fillWidth());
99 }
100
101 /** To be overridden */
102 protected void setContainerLayoutData(Composite composite) {
103 composite.setLayoutData(CmsUtils.fillWidth());
104 }
105
106 protected void clear(boolean deep) {
107 if (deep) {
108 for (Control control : getChildren())
109 control.dispose();
110 container = createBox(this);
111 box = createBox(container);
112 } else {
113 control.dispose();
114 }
115 }
116
117 public void setMouseListener(MouseListener mouseListener) {
118 if (this.mouseListener != null && control != null)
119 control.removeMouseListener(this.mouseListener);
120 this.mouseListener = mouseListener;
121 if (control != null && this.mouseListener != null)
122 control.addMouseListener(mouseListener);
123 }
124 }