View Javadoc
1   package org.argeo.cms.ui.forms;
2   
3   import static org.argeo.cms.ui.forms.FormStyle.propertyMessage;
4   import static org.argeo.cms.ui.forms.FormStyle.propertyText;
5   
6   import javax.jcr.Node;
7   import javax.jcr.RepositoryException;
8   
9   import org.argeo.cms.ui.viewers.EditablePart;
10  import org.argeo.cms.ui.widgets.EditableText;
11  import org.argeo.eclipse.ui.EclipseUiUtils;
12  import org.eclipse.swt.widgets.Composite;
13  import org.eclipse.swt.widgets.Control;
14  import org.eclipse.swt.widgets.Label;
15  import org.eclipse.swt.widgets.Text;
16  
17  /** Editable String in a CMS context */
18  public class EditablePropertyString extends EditableText implements
19  		EditablePart {
20  	private static final long serialVersionUID = 5055000749992803591L;
21  
22  	private String propertyName;
23  	private String message;
24  
25  	// encode the '&' character in rap
26  	private final static String AMPERSAND = "&";
27  	private final static String AMPERSAND_REGEX = "&(?![#a-zA-Z0-9]+;)";
28  
29  	public EditablePropertyString(Composite parent, int style, Node node,
30  			String propertyName, String message) throws RepositoryException {
31  		super(parent, style, node, true);
32  
33  		this.propertyName = propertyName;
34  		this.message = message;
35  
36  		if (node.hasProperty(propertyName)) {
37  			this.setStyle(propertyText.style());
38  			this.setText(node.getProperty(propertyName).getString());
39  		} else {
40  			this.setStyle(propertyMessage.style());
41  			this.setText(message + "  ");
42  		}
43  	}
44  
45  	public void setText(String text) {
46  		Control child = getControl();
47  		if (child instanceof Label) {
48  			Label lbl = (Label) child;
49  			if (EclipseUiUtils.isEmpty(text))
50  				lbl.setText(message + "  ");
51  			else
52  				// TODO enhance this
53  				lbl.setText(text.replaceAll(AMPERSAND_REGEX, AMPERSAND));
54  		} else if (child instanceof Text) {
55  			Text txt = (Text) child;
56  			if (EclipseUiUtils.isEmpty(text)) {
57  				txt.setText("");
58  				txt.setMessage(message + " ");
59  			} else
60  				txt.setText(text.replaceAll("<br/>", "\n"));
61  		}
62  	}
63  
64  	public synchronized void startEditing() {
65  		getControl().setData(STYLE, propertyText.style());
66  		super.startEditing();
67  	}
68  
69  	public synchronized void stopEditing() {
70  		if (EclipseUiUtils.isEmpty(((Text) getControl()).getText()))
71  			getControl().setData(STYLE, propertyMessage.style());
72  		else
73  			getControl().setData(STYLE, propertyText.style());
74  		super.stopEditing();
75  	}
76  
77  	public String getPropertyName() {
78  		return propertyName;
79  	}
80  }