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