]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/widgets/StyledControl.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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 public StyledControl(Composite parent, int swtStyle) {
28 super(parent, swtStyle);
29 setLayout(CmsUiUtils.noSpaceGridLayout());
30 }
31
32 public StyledControl(Composite parent, int style, Item item) {
33 super(parent, style, item);
34 }
35
36 public StyledControl(Composite parent, int style, Item item, boolean cacheImmediately) {
37 super(parent, style, item, cacheImmediately);
38 }
39
40 protected abstract Control createControl(Composite box, String style);
41
42 protected Composite createBox(Composite parent) {
43 Composite box = new Composite(parent, SWT.INHERIT_DEFAULT);
44 setContainerLayoutData(box);
45 box.setLayout(CmsUiUtils.noSpaceGridLayout());
46 // new Label(box, SWT.NONE).setText("BOX");
47 return box;
48 }
49
50 public Control getControl() {
51 return control;
52 }
53
54 protected synchronized Boolean isEditing() {
55 return editing;
56 }
57
58 public synchronized void startEditing() {
59 assert !isEditing();
60 editing = true;
61 // int height = control.getSize().y;
62 String style = (String) EclipseUiSpecificUtils.getStyleData(control);
63 clear(false);
64 control = createControl(box, style);
65 setControlLayoutData(control);
66
67 // add the focus listener to the newly created edition control
68 if (focusListener != null)
69 control.addFocusListener(focusListener);
70 }
71
72 public synchronized void stopEditing() {
73 assert isEditing();
74 editing = false;
75 String style = (String) EclipseUiSpecificUtils.getStyleData(control);
76 clear(false);
77 control = createControl(box, style);
78 setControlLayoutData(control);
79 }
80
81 public void setStyle(String style) {
82 Object currentStyle = null;
83 if (control != null)
84 currentStyle = EclipseUiSpecificUtils.getStyleData(control);
85 if (currentStyle != null && currentStyle.equals(style))
86 return;
87
88 // Integer preferredHeight = control != null ? control.getSize().y :
89 // null;
90 clear(true);
91 control = createControl(box, style);
92 setControlLayoutData(control);
93
94 // control.getParent().setData(STYLE, style + "_box");
95 EclipseUiSpecificUtils.setStyleData(control.getParent(), style + "_box");
96 // control.getParent().getParent().setData(STYLE, style + "_container");
97 EclipseUiSpecificUtils.setStyleData(control.getParent().getParent(), style + "_container");
98 }
99
100 /** To be overridden */
101 protected void setControlLayoutData(Control control) {
102 control.setLayoutData(CmsUiUtils.fillWidth());
103 }
104
105 /** To be overridden */
106 protected void setContainerLayoutData(Composite composite) {
107 composite.setLayoutData(CmsUiUtils.fillWidth());
108 }
109
110 protected void clear(boolean deep) {
111 if (deep) {
112 for (Control control : getChildren())
113 control.dispose();
114 container = createBox(this);
115 box = createBox(container);
116 } else {
117 control.dispose();
118 }
119 }
120
121 public void setMouseListener(MouseListener mouseListener) {
122 if (this.mouseListener != null && control != null)
123 control.removeMouseListener(this.mouseListener);
124 this.mouseListener = mouseListener;
125 if (control != null && this.mouseListener != null)
126 control.addMouseListener(mouseListener);
127 }
128
129 public void setFocusListener(FocusListener focusListener) {
130 if (this.focusListener != null && control != null)
131 control.removeFocusListener(this.focusListener);
132 this.focusListener = focusListener;
133 if (control != null && this.focusListener != null)
134 control.addFocusListener(focusListener);
135 }
136 }