View Javadoc
1   package org.argeo.cms.ui.widgets;
2   
3   import javax.jcr.Item;
4   import javax.jcr.RepositoryException;
5   
6   import org.argeo.cms.ui.CmsConstants;
7   import org.argeo.cms.ui.util.CmsUiUtils;
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) throws RepositoryException {
33  		super(parent, style, item);
34  	}
35  
36  	public StyledControl(Composite parent, int style, Item item, boolean cacheImmediately) throws RepositoryException {
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) control.getData(STYLE);
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) control.getData(STYLE);
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 = control.getData(STYLE);
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  		control.getParent().getParent().setData(STYLE, style + "_container");
96  	}
97  
98  	/** To be overridden */
99  	protected void setControlLayoutData(Control control) {
100 		control.setLayoutData(CmsUiUtils.fillWidth());
101 	}
102 
103 	/** To be overridden */
104 	protected void setContainerLayoutData(Composite composite) {
105 		composite.setLayoutData(CmsUiUtils.fillWidth());
106 	}
107 
108 	protected void clear(boolean deep) {
109 		if (deep) {
110 			for (Control control : getChildren())
111 				control.dispose();
112 			container = createBox(this);
113 			box = createBox(container);
114 		} else {
115 			control.dispose();
116 		}
117 	}
118 
119 	public void setMouseListener(MouseListener mouseListener) {
120 		if (this.mouseListener != null && control != null)
121 			control.removeMouseListener(this.mouseListener);
122 		this.mouseListener = mouseListener;
123 		if (control != null && this.mouseListener != null)
124 			control.addMouseListener(mouseListener);
125 	}
126 
127 	public void setFocusListener(FocusListener focusListener) {
128 		if (this.focusListener != null && control != null)
129 			control.removeFocusListener(this.focusListener);
130 		this.focusListener = focusListener;
131 		if (control != null && this.focusListener != null)
132 			control.addFocusListener(focusListener);
133 	}
134 }