1 package org
.argeo
.cms
.ui
.widgets
;
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
;
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
;
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 public StyledControl(Composite parent
, int style
, Item item
) {
34 super(parent
, style
, item
);
37 public StyledControl(Composite parent
, int style
, Item item
, boolean cacheImmediately
) {
38 super(parent
, style
, item
, cacheImmediately
);
41 protected abstract Control
createControl(Composite box
, String style
);
43 protected Composite
createBox() {
44 Composite box
= new Composite(container
, SWT
.INHERIT_DEFAULT
);
45 setContainerLayoutData(box
);
46 box
.setLayout(CmsSwtUtils
.noSpaceGridLayout(3));
50 protected Composite
createContainer() {
51 Composite container
= new Composite(this, SWT
.INHERIT_DEFAULT
);
52 setContainerLayoutData(container
);
53 container
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
57 public Control
getControl() {
61 protected synchronized Boolean
isEditing() {
65 public synchronized void startEditing() {
68 // int height = control.getSize().y;
69 String style
= (String
) EclipseUiSpecificUtils
.getStyleData(control
);
71 refreshControl(style
);
73 // add the focus listener to the newly created edition control
74 if (focusListener
!= null)
75 control
.addFocusListener(focusListener
);
78 public synchronized void stopEditing() {
81 String style
= (String
) EclipseUiSpecificUtils
.getStyleData(control
);
83 refreshControl(style
);
86 protected void refreshControl(String style
) {
87 control
= createControl(box
, style
);
88 setControlLayoutData(control
);
89 if (ancestorToLayout
!= null)
90 ancestorToLayout
.layout(true, true);
92 getParent().layout(true, true);
95 public void setStyle(String style
) {
96 Object currentStyle
= null;
98 currentStyle
= EclipseUiSpecificUtils
.getStyleData(control
);
99 if (currentStyle
!= null && currentStyle
.equals(style
))
103 refreshControl(style
);
106 CmsSwtUtils
.style(box
, style
+ "_box");
107 CmsSwtUtils
.style(container
, style
+ "_container");
111 /** To be overridden */
112 protected void setControlLayoutData(Control control
) {
113 control
.setLayoutData(CmsSwtUtils
.fillWidth());
116 /** To be overridden */
117 protected void setContainerLayoutData(Composite composite
) {
118 composite
.setLayoutData(CmsSwtUtils
.fillWidth());
121 protected void clear(boolean deep
) {
123 for (Control control
: getChildren())
125 container
= createContainer();
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
);
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
);
148 public void setAncestorToLayout(Composite ancestorToLayout
) {
149 this.ancestorToLayout
= ancestorToLayout
;