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.util.CmsUiUtils;
7   import org.eclipse.swt.SWT;
8   import org.eclipse.swt.layout.GridData;
9   import org.eclipse.swt.widgets.Composite;
10  import org.eclipse.swt.widgets.Control;
11  import org.eclipse.swt.widgets.Label;
12  import org.eclipse.swt.widgets.Text;
13  
14  /** Editable text part displaying styled text. */
15  public class EditableText extends StyledControl {
16  	private static final long serialVersionUID = -6372283442330912755L;
17  
18  	public EditableText(Composite parent, int swtStyle) {
19  		super(parent, swtStyle);
20  	}
21  
22  	public EditableText(Composite parent, int style, Item item)
23  			throws RepositoryException {
24  		this(parent, style, item, false);
25  	}
26  
27  	public EditableText(Composite parent, int style, Item item,
28  			boolean cacheImmediately) throws RepositoryException {
29  		super(parent, style, item, cacheImmediately);
30  	}
31  
32  	@Override
33  	protected Control createControl(Composite box, String style) {
34  		if (isEditing())
35  			return createText(box, style);
36  		else
37  			return createLabel(box, style);
38  	}
39  
40  	protected Label createLabel(Composite box, String style) {
41  		Label lbl = new Label(box, getStyle() | SWT.WRAP);
42  		lbl.setLayoutData(CmsUiUtils.fillWidth());
43  		CmsUiUtils.style(lbl, style);
44  		CmsUiUtils.markup(lbl);
45  		if (mouseListener != null)
46  			lbl.addMouseListener(mouseListener);
47  		return lbl;
48  	}
49  
50  	protected Text createText(Composite box, String style) {
51  		final Text text = new Text(box, getStyle() | SWT.MULTI | SWT.WRAP);
52  		GridData textLayoutData = CmsUiUtils.fillWidth();
53  		// textLayoutData.heightHint = preferredHeight;
54  		text.setLayoutData(textLayoutData);
55  		CmsUiUtils.style(text, style);
56  		text.setFocus();
57  		return text;
58  	}
59  
60  	public void setText(String text) {
61  		Control child = getControl();
62  		if (child instanceof Label)
63  			((Label) child).setText(text);
64  		else if (child instanceof Text)
65  			((Text) child).setText(text);
66  	}
67  
68  	public Text getAsText() {
69  		return (Text) getControl();
70  	}
71  
72  	public Label getAsLabel() {
73  		return (Label) getControl();
74  	}
75  
76  }