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