]> git.argeo.org Git - gpl/argeo-jcr.git/blob - swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/ui/widgets/StyledControl.java
Adapt to changes in Argeo CMS
[gpl/argeo-jcr.git] / swt / org.argeo.cms.jcr.ui / src / org / argeo / cms / ui / widgets / StyledControl.java
1 package org.argeo.cms.ui.widgets;
2
3 import javax.jcr.Item;
4
5 import org.argeo.cms.swt.CmsSwtUtils;
6 import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.FocusListener;
9 import org.eclipse.swt.events.MouseListener;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12
13 /** Editable text part displaying styled text. */
14 public abstract class StyledControl extends JcrComposite {
15 private static final long serialVersionUID = -6372283442330912755L;
16 private Control control;
17
18 private Composite container;
19 private Composite box;
20
21 protected MouseListener mouseListener;
22 protected FocusListener focusListener;
23
24 private Boolean editing = Boolean.FALSE;
25
26 private Composite ancestorToLayout;
27
28 public StyledControl(Composite parent, int swtStyle) {
29 super(parent, swtStyle);
30 setLayout(CmsSwtUtils.noSpaceGridLayout());
31 }
32
33 public StyledControl(Composite parent, int style, Item item) {
34 super(parent, style, item);
35 }
36
37 public StyledControl(Composite parent, int style, Item item, boolean cacheImmediately) {
38 super(parent, style, item, cacheImmediately);
39 }
40
41 protected abstract Control createControl(Composite box, String style);
42
43 protected Composite createBox() {
44 Composite box = new Composite(container, SWT.INHERIT_DEFAULT);
45 setContainerLayoutData(box);
46 box.setLayout(CmsSwtUtils.noSpaceGridLayout(3));
47 return box;
48 }
49
50 protected Composite createContainer() {
51 Composite container = new Composite(this, SWT.INHERIT_DEFAULT);
52 setContainerLayoutData(container);
53 container.setLayout(CmsSwtUtils.noSpaceGridLayout());
54 return container;
55 }
56
57 public Control getControl() {
58 return control;
59 }
60
61 protected synchronized Boolean isEditing() {
62 return editing;
63 }
64
65 public synchronized void startEditing() {
66 assert !isEditing();
67 editing = true;
68 // int height = control.getSize().y;
69 String style = (String) EclipseUiSpecificUtils.getStyleData(control);
70 clear(false);
71 refreshControl(style);
72
73 // add the focus listener to the newly created edition control
74 if (focusListener != null)
75 control.addFocusListener(focusListener);
76 }
77
78 public synchronized void stopEditing() {
79 assert isEditing();
80 editing = false;
81 String style = (String) EclipseUiSpecificUtils.getStyleData(control);
82 clear(false);
83 refreshControl(style);
84 }
85
86 protected void refreshControl(String style) {
87 control = createControl(box, style);
88 setControlLayoutData(control);
89 if (ancestorToLayout != null)
90 ancestorToLayout.layout(true, true);
91 else
92 getParent().layout(true, true);
93 }
94
95 public void setStyle(String style) {
96 Object currentStyle = null;
97 if (control != null)
98 currentStyle = EclipseUiSpecificUtils.getStyleData(control);
99 if (currentStyle != null && currentStyle.equals(style))
100 return;
101
102 clear(true);
103 refreshControl(style);
104
105 if (style != null) {
106 CmsSwtUtils.style(box, style + "_box");
107 CmsSwtUtils.style(container, style + "_container");
108 }
109 }
110
111 /** To be overridden */
112 protected void setControlLayoutData(Control control) {
113 control.setLayoutData(CmsSwtUtils.fillWidth());
114 }
115
116 /** To be overridden */
117 protected void setContainerLayoutData(Composite composite) {
118 composite.setLayoutData(CmsSwtUtils.fillWidth());
119 }
120
121 protected void clear(boolean deep) {
122 if (deep) {
123 for (Control control : getChildren())
124 control.dispose();
125 container = createContainer();
126 box = createBox();
127 } else {
128 control.dispose();
129 }
130 }
131
132 public void setMouseListener(MouseListener mouseListener) {
133 if (this.mouseListener != null && control != null)
134 control.removeMouseListener(this.mouseListener);
135 this.mouseListener = mouseListener;
136 if (control != null && this.mouseListener != null)
137 control.addMouseListener(mouseListener);
138 }
139
140 public void setFocusListener(FocusListener focusListener) {
141 if (this.focusListener != null && control != null)
142 control.removeFocusListener(this.focusListener);
143 this.focusListener = focusListener;
144 if (control != null && this.focusListener != null)
145 control.addFocusListener(focusListener);
146 }
147
148 public void setAncestorToLayout(Composite ancestorToLayout) {
149 this.ancestorToLayout = ancestorToLayout;
150 }
151
152 }