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