]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/StyledControl.java
Extract log method from asynchronous publisher
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / widgets / StyledControl.java
1 package org.argeo.cms.swt.widgets;
2
3 import org.argeo.api.cms.ux.CmsStyle;
4 import org.argeo.cms.swt.CmsSwtUtils;
5 import org.argeo.cms.swt.SwtEditablePart;
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 Composite implements SwtEditablePart {
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 protected abstract Control createControl(Composite box, String style);
34
35 protected Composite createBox() {
36 Composite box = new Composite(container, SWT.INHERIT_DEFAULT);
37 setContainerLayoutData(box);
38 box.setLayout(CmsSwtUtils.noSpaceGridLayout(3));
39 return box;
40 }
41
42 protected Composite createContainer() {
43 Composite container = new Composite(this, SWT.INHERIT_DEFAULT);
44 setContainerLayoutData(container);
45 container.setLayout(CmsSwtUtils.noSpaceGridLayout());
46 return container;
47 }
48
49 @Override
50 public Control getControl() {
51 return control;
52 }
53
54 protected synchronized Boolean isEditing() {
55 return editing;
56 }
57
58 @Override
59 public synchronized void startEditing() {
60 assert !isEditing();
61 editing = true;
62 // int height = control.getSize().y;
63 String style = (String) EclipseUiSpecificUtils.getStyleData(control);
64 clear(false);
65 refreshControl(style);
66
67 // add the focus listener to the newly created edition control
68 if (focusListener != null)
69 control.addFocusListener(focusListener);
70 }
71
72 @Override
73 public synchronized void stopEditing() {
74 assert isEditing();
75 editing = false;
76 String style = (String) EclipseUiSpecificUtils.getStyleData(control);
77 clear(false);
78 refreshControl(style);
79 }
80
81 protected void refreshControl(String style) {
82 control = createControl(box, style);
83 setControlLayoutData(control);
84 if (ancestorToLayout != null)
85 ancestorToLayout.layout(true, true);
86 else
87 getParent().layout(true, true);
88 }
89
90 public void setStyle(CmsStyle style) {
91 setStyle(style.style());
92 }
93
94 /**
95 * Set the style, creating all related controls and composites. It should be
96 * called <b>after</b> all properties have been set.
97 */
98 public void setStyle(String style) {
99 Object currentStyle = null;
100 if (control != null)
101 currentStyle = EclipseUiSpecificUtils.getStyleData(control);
102 if (currentStyle != null && currentStyle.equals(style))
103 return;
104
105 clear(true);
106 refreshControl(style);
107
108 if (style != null) {
109 CmsSwtUtils.style(box, style + "_box");
110 CmsSwtUtils.style(container, style + "_container");
111 }
112 }
113
114 /**
115 * Convenience method when no style is explicitly set, so that the control can
116 * effectively be created. Does nothing if a control already exists, otherwise
117 * it is equivalent to {@link #setStyle(String)} with a <code>null<code>
118 * argument.
119 */
120 public void initControl() {
121 if (control != null)
122 return;
123 setStyle((String) null);
124 }
125
126 /** To be overridden */
127 protected void setControlLayoutData(Control control) {
128 control.setLayoutData(CmsSwtUtils.fillWidth());
129 }
130
131 /** To be overridden */
132 protected void setContainerLayoutData(Composite composite) {
133 composite.setLayoutData(CmsSwtUtils.fillWidth());
134 }
135
136 protected void clear(boolean deep) {
137 if (deep) {
138 for (Control control : getChildren())
139 control.dispose();
140 container = createContainer();
141 box = createBox();
142 } else {
143 control.dispose();
144 }
145 }
146
147 public void setMouseListener(MouseListener mouseListener) {
148 if (this.mouseListener != null && control != null)
149 control.removeMouseListener(this.mouseListener);
150 this.mouseListener = mouseListener;
151 if (control != null && this.mouseListener != null)
152 control.addMouseListener(mouseListener);
153 }
154
155 public void setFocusListener(FocusListener focusListener) {
156 if (this.focusListener != null && control != null)
157 control.removeFocusListener(this.focusListener);
158 this.focusListener = focusListener;
159 if (control != null && this.focusListener != null)
160 control.addFocusListener(focusListener);
161 }
162
163 public void setAncestorToLayout(Composite ancestorToLayout) {
164 this.ancestorToLayout = ancestorToLayout;
165 }
166
167 }