View Javadoc
1   package org.argeo.connect.ui;
2   
3   import java.text.DateFormat;
4   import java.text.SimpleDateFormat;
5   
6   import javax.jcr.Node;
7   import javax.jcr.Property;
8   import javax.jcr.RepositoryException;
9   import javax.jcr.nodetype.NodeType;
10  
11  import org.argeo.connect.ConnectConstants;
12  import org.argeo.connect.util.ConnectUtils;
13  
14  /** Helper methods to generate HTML snippets */
15  public class ConnectUiSnippets {
16  
17  	/**
18  	 * Simply formats a couple href / label to display a link in a markup
19  	 * enabled tree / table / label that will trigger a corresponding RWT
20  	 * specific listener. Such a listener must be able to understand the
21  	 * specific format of the value of this href attribute
22  	 */
23  	public static String getRWTLink(String href, String value) {
24  		// We formerly used to add " style='color:#383838; text-decoration:none;' "
25  		// Check if still necessary
26  		return "<a href=\"" + href + "\" target=\"_rwt\">" + value + "</a>";
27  	}
28  
29  	/**
30  	 * Creates the read-only HTML snippet to display in a label with styling
31  	 * enabled in order to provide a click-able phone number
32  	 */
33  	public static String getPhoneLink(String value) {
34  		return getPhoneLink(value, value);
35  	}
36  
37  	/**
38  	 * Creates the read-only HTML snippet to display in a label with styling
39  	 * enabled in order to provide a click-able phone number
40  	 * 
41  	 * @param value
42  	 * @param label
43  	 *            a potentially distinct label
44  	 * @return
45  	 */
46  	public static String getPhoneLink(String value, String label) {
47  		StringBuilder builder = new StringBuilder();
48  		builder.append("<a href=\"tel:");
49  		builder.append(value).append("\" target=\"_blank\" >").append(label).append("</a>");
50  		return builder.toString();
51  	}
52  
53  	/**
54  	 * Creates the read-only HTML snippet to display in a label with styling
55  	 * enabled in order to provide a click-able mail
56  	 */
57  	public static String getMailLink(String value) {
58  		return getMailLink(value, value);
59  	}
60  
61  	/**
62  	 * Creates the read-only HTML snippet to display in a label with styling
63  	 * enabled in order to provide a click-able mail
64  	 * 
65  	 * @param value
66  	 * @param label
67  	 *            a potentially distinct label
68  	 * @return
69  	 */
70  	public static String getMailLink(String value, String label) {
71  		StringBuilder builder = new StringBuilder();
72  		value = ConnectUtils.replaceAmpersand(value);
73  		builder.append("<a href=\"mailto:");
74  		builder.append(value).append("\" >").append(label).append("</a>");
75  		return builder.toString();
76  	}
77  
78  	/**
79  	 * Creates the read-only HTML snippet to display in a label with styling
80  	 * enabled in order to provide a click-able link
81  	 */
82  	public static String getUrlLink(String value) {
83  		return getUrlLink(value, value);
84  	}
85  
86  	/**
87  	 * Creates the read-only HTML snippet to display in a label with styling
88  	 * enabled in order to provide a click-able link
89  	 */
90  	public static String getUrlLink(String value, String label) {
91  		StringBuilder builder = new StringBuilder();
92  		value = ConnectUtils.replaceAmpersand(value);
93  		label = ConnectUtils.replaceAmpersand(label);
94  		if (!(value.startsWith("http://") || value.startsWith("https://")))
95  			value = "http://" + value;
96  		builder.append("<a href=\"");
97  		builder.append(value + "\" target=\"_blank\" >" + label + "</a>");
98  		return builder.toString();
99  	}
100 
101 	public static String getLastUpdateSnippet(Node entity) {
102 		StringBuilder builder = new StringBuilder();
103 
104 		/** shortcut to set form data while dealing with switching panel */
105 		// private final static
106 		DateFormat df = new SimpleDateFormat(ConnectConstants.DEFAULT_DATE_TIME_FORMAT);
107 
108 		try {
109 			if (entity.isNodeType(NodeType.MIX_LAST_MODIFIED)) {
110 				builder.append("<i>").append("Last updated on ");
111 				builder.append(df.format(entity.getProperty(Property.JCR_LAST_MODIFIED).getDate().getTime()));
112 				builder.append(", by ");
113 				builder.append(entity.getProperty(Property.JCR_LAST_MODIFIED_BY).getString());
114 				builder.append(". </i>");
115 			}
116 			return builder.toString();
117 		} catch (RepositoryException re) {
118 			throw new RuntimeException("Cannot create organizations content", re);
119 		}
120 	}
121 }