View Javadoc
1   package org.argeo.cms.ui.forms;
2   
3   import javax.jcr.Node;
4   import javax.jcr.RepositoryException;
5   
6   import org.argeo.cms.ui.viewers.EditablePart;
7   import org.argeo.eclipse.ui.EclipseUiUtils;
8   import org.eclipse.swt.SWT;
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 String that displays a browsable link when read-only */
15  public class EditableLink extends EditablePropertyString implements
16  		EditablePart {
17  	private static final long serialVersionUID = 5055000749992803591L;
18  
19  	private String type;
20  	private String message;
21  	private boolean readOnly;
22  
23  	public EditableLink(Composite parent, int style, Node node,
24  			String propertyName, String type, String message)
25  			throws RepositoryException {
26  		super(parent, style, node, propertyName, message);
27  		this.message = message;
28  		this.type = type;
29  
30  		readOnly = SWT.READ_ONLY == (style & SWT.READ_ONLY);
31  		if (node.hasProperty(propertyName)) {
32  			this.setStyle(FormStyle.propertyText.style());
33  			this.setText(node.getProperty(propertyName).getString());
34  		} else {
35  			this.setStyle(FormStyle.propertyMessage.style());
36  			this.setText("");
37  		}
38  	}
39  
40  	public void setText(String text) {
41  		Control child = getControl();
42  		if (child instanceof Label) {
43  			Label lbl = (Label) child;
44  			if (EclipseUiUtils.isEmpty(text))
45  				lbl.setText(message);
46  			else if (readOnly)
47  				setLinkValue(lbl, text);
48  			else
49  				// if canEdit() we put only the value with no link
50  				// to avoid glitches of the edition life cycle
51  				lbl.setText(text);
52  		} else if (child instanceof Text) {
53  			Text txt = (Text) child;
54  			if (EclipseUiUtils.isEmpty(text)) {
55  				txt.setText("");
56  				txt.setMessage(message);
57  			} else
58  				txt.setText(text);
59  		}
60  	}
61  
62  	private void setLinkValue(Label lbl, String text) {
63  		if (FormStyle.email.style().equals(type))
64  			lbl.setText(FormUtils.getMailLink(text));
65  		else if (FormStyle.phone.style().equals(type))
66  			lbl.setText(FormUtils.getPhoneLink(text));
67  		else if (FormStyle.website.style().equals(type))
68  			lbl.setText(FormUtils.getUrlLink(text));
69  		else if (FormStyle.facebook.style().equals(type)
70  				|| FormStyle.instagram.style().equals(type)
71  				|| FormStyle.linkedIn.style().equals(type)
72  				|| FormStyle.twitter.style().equals(type))
73  			lbl.setText(FormUtils.getUrlLink(text));
74  	}
75  }