]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/StyledControl.java
Add account-related RFC 2307bis LDAP objects and attributes
[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 public void setStyle(String style) {
95 Object currentStyle = null;
96 if (control != null)
97 currentStyle = EclipseUiSpecificUtils.getStyleData(control);
98 if (currentStyle != null && currentStyle.equals(style))
99 return;
100
101 clear(true);
102 refreshControl(style);
103
104 if (style != null) {
105 CmsSwtUtils.style(box, style + "_box");
106 CmsSwtUtils.style(container, style + "_container");
107 }
108 }
109
110 /** To be overridden */
111 protected void setControlLayoutData(Control control) {
112 control.setLayoutData(CmsSwtUtils.fillWidth());
113 }
114
115 /** To be overridden */
116 protected void setContainerLayoutData(Composite composite) {
117 composite.setLayoutData(CmsSwtUtils.fillWidth());
118 }
119
120 protected void clear(boolean deep) {
121 if (deep) {
122 for (Control control : getChildren())
123 control.dispose();
124 container = createContainer();
125 box = createBox();
126 } else {
127 control.dispose();
128 }
129 }
130
131 public void setMouseListener(MouseListener mouseListener) {
132 if (this.mouseListener != null && control != null)
133 control.removeMouseListener(this.mouseListener);
134 this.mouseListener = mouseListener;
135 if (control != null && this.mouseListener != null)
136 control.addMouseListener(mouseListener);
137 }
138
139 public void setFocusListener(FocusListener focusListener) {
140 if (this.focusListener != null && control != null)
141 control.removeFocusListener(this.focusListener);
142 this.focusListener = focusListener;
143 if (control != null && this.focusListener != null)
144 control.addFocusListener(focusListener);
145 }
146
147 public void setAncestorToLayout(Composite ancestorToLayout) {
148 this.ancestorToLayout = ancestorToLayout;
149 }
150
151 }