1 package org
.argeo
.cms
.swt
.widgets
;
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
;
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
;
18 private Composite container
;
19 private Composite box
;
21 protected MouseListener mouseListener
;
22 protected FocusListener focusListener
;
24 private Boolean editing
= Boolean
.FALSE
;
26 private Composite ancestorToLayout
;
28 public StyledControl(Composite parent
, int swtStyle
) {
29 super(parent
, swtStyle
);
30 setLayout(CmsSwtUtils
.noSpaceGridLayout());
33 protected abstract Control
createControl(Composite box
, String style
);
35 protected Composite
createBox() {
36 Composite box
= new Composite(container
, SWT
.INHERIT_DEFAULT
);
37 setContainerLayoutData(box
);
38 box
.setLayout(CmsSwtUtils
.noSpaceGridLayout(3));
42 protected Composite
createContainer() {
43 Composite container
= new Composite(this, SWT
.INHERIT_DEFAULT
);
44 setContainerLayoutData(container
);
45 container
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
50 public Control
getControl() {
54 protected synchronized Boolean
isEditing() {
59 public synchronized void startEditing() {
62 // int height = control.getSize().y;
63 String style
= (String
) EclipseUiSpecificUtils
.getStyleData(control
);
65 refreshControl(style
);
67 // add the focus listener to the newly created edition control
68 if (focusListener
!= null)
69 control
.addFocusListener(focusListener
);
73 public synchronized void stopEditing() {
76 String style
= (String
) EclipseUiSpecificUtils
.getStyleData(control
);
78 refreshControl(style
);
81 protected void refreshControl(String style
) {
82 control
= createControl(box
, style
);
83 setControlLayoutData(control
);
84 if (ancestorToLayout
!= null)
85 ancestorToLayout
.layout(true, true);
87 getParent().layout(true, true);
90 public void setStyle(CmsStyle style
) {
91 setStyle(style
.style());
95 * Set the style, creating all related controls and composites. It should be
96 * called <b>after</b> all properties have been set.
98 public void setStyle(String style
) {
99 Object currentStyle
= null;
101 currentStyle
= EclipseUiSpecificUtils
.getStyleData(control
);
102 if (currentStyle
!= null && currentStyle
.equals(style
))
106 refreshControl(style
);
109 CmsSwtUtils
.style(box
, style
+ "_box");
110 CmsSwtUtils
.style(container
, style
+ "_container");
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>
120 public void initControl() {
123 setStyle((String
) null);
126 /** To be overridden */
127 protected void setControlLayoutData(Control control
) {
128 control
.setLayoutData(CmsSwtUtils
.fillWidth());
131 /** To be overridden */
132 protected void setContainerLayoutData(Composite composite
) {
133 composite
.setLayoutData(CmsSwtUtils
.fillWidth());
136 protected void clear(boolean deep
) {
138 for (Control control
: getChildren())
140 container
= createContainer();
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
);
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
);
163 public void setAncestorToLayout(Composite ancestorToLayout
) {
164 this.ancestorToLayout
= ancestorToLayout
;