]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/DistUiHelpers.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / legacy / org.argeo.slc.client.ui.dist / src / org / argeo / slc / client / ui / dist / utils / DistUiHelpers.java
1 package org.argeo.slc.client.ui.dist.utils;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6
7 import javax.jcr.PropertyType;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Value;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.SlcNames;
15 import org.argeo.slc.SlcTypes;
16 import org.argeo.slc.client.ui.dist.DistConstants;
17
18 public class DistUiHelpers implements DistConstants, SlcTypes, SlcNames {
19 private final static Log log = LogFactory.getLog(DistUiHelpers.class);
20 private final static DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT);
21
22 /**
23 * Returns a user-friendly label for a given jcr property name. If the
24 * corresponding mapping is not found, the input String is returned. If
25 * input String is null "(No name)" is returned
26 */
27 public static String getLabelJcrName(String jcrName) {
28 return (String) getLabelAndDefaultValueWidth(jcrName)[0];
29 }
30
31 /**
32 * Returns a label ( (String) object[0] )and default value width ( (int)
33 * object[1] ) for a given property name
34 */
35 public static Object[] getLabelAndDefaultValueWidth(String propertyName) {
36 // to avoid npe :
37 if (propertyName == null)
38 return new Object[] { "(No name)", 60 };
39
40 // ArtifactId
41 if (propertyName.equals(SLC_ARTIFACT + "." + SLC_ARTIFACT_ID)
42 || propertyName.equals(SLC_ARTIFACT_BASE + "."
43 + SLC_ARTIFACT_ID)
44 || propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
45 + SLC_ARTIFACT_ID)
46 || propertyName.equals(SLC_ARTIFACT_ID)) {
47 return new Object[] { "Artifact ID", 200 };
48 } // GroupId
49 else if (propertyName.equals(SLC_ARTIFACT + "." + SLC_GROUP_ID)
50 || propertyName.equals(SLC_ARTIFACT_BASE + "." + SLC_GROUP_ID)
51 || propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
52 + SLC_GROUP_ID) || propertyName.equals(SLC_GROUP_ID)) {
53 return new Object[] { "Group ID", 120 };
54 } // Version
55 else if (propertyName.equals(SLC_ARTIFACT + "." + SLC_ARTIFACT_VERSION)
56 || propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
57 + SLC_ARTIFACT_VERSION)
58 || propertyName.equals(SLC_ARTIFACT_VERSION)) {
59 return new Object[] { "Version", 60 };
60 } else if (propertyName.equals(SLC_ARTIFACT + "."
61 + SLC_ARTIFACT_CLASSIFIER)
62 || propertyName.equals(SLC_ARTIFACT_CLASSIFIER)) {
63 return new Object[] { "Classifier", 60 };
64 } else if (propertyName.equals(SLC_ARTIFACT + "."
65 + SLC_ARTIFACT_EXTENSION)
66 || propertyName.equals(SLC_ARTIFACT_EXTENSION)) {
67 return new Object[] { "Type", 40 };
68 } else if (propertyName.equals(SLC_BUNDLE_ARTIFACT + "."
69 + SLC_SYMBOLIC_NAME)
70 || propertyName.equals(SLC_SYMBOLIC_NAME)) {
71 return new Object[] { "Symbolic name", 180 };
72 } else if (propertyName.equals(SLC_BUNDLE_ARTIFACT + "."
73 + SLC_BUNDLE_VERSION)
74 || propertyName.equals(SLC_BUNDLE_VERSION)) {
75 return new Object[] { "Bundle version", 120 };
76 } else if (propertyName
77 .equals(SLC_BUNDLE_ARTIFACT + "." + SLC_MANIFEST)
78 || propertyName.equals(SLC_MANIFEST)) {
79 return new Object[] { "Manifest", 60 };
80 } // TODO remove hard coded strings
81 else if (propertyName.equals("slc:Bundle-ManifestVersion")) {
82 return new Object[] { "Bundle Manifest Version", 60 };
83 } else if (propertyName.equals("slc:Manifest-Version")) {
84 return new Object[] { "Manifest Version", 60 };
85 } else if (propertyName.equals("slc:Bundle-Vendor")) {
86 return new Object[] { "Bundle Vendor", 60 };
87 } else if (propertyName.equals("slc:Bundle-SymbolicName")) {
88 return new Object[] { "Bundle symbolic name", 60 };
89 } else if (propertyName.equals("slc:Bundle-Name")) {
90 return new Object[] { "Bundle name", 60 };
91 } else if (propertyName.equals("slc:Bundle-DocURL")) {
92 return new Object[] { "Doc URL", 120 };
93 } else if (propertyName.equals("slc:Bundle-Licence")) {
94 return new Object[] { "Bundle licence", 120 };
95 } else if (propertyName.equals(SLC_ARTIFACT_VERSION_BASE + "."
96 + JCR_IDENTIFIER)) {
97 return new Object[] { "UUID", 0 };
98 } else {
99 if (log.isTraceEnabled())
100 log.trace("No Column label provider defined for property: ["
101 + propertyName + "]");
102 return new Object[] { propertyName, 60 };
103 }
104 }
105
106 public static String formatValueAsString(Value value) {
107 try {
108 String strValue;
109
110 if (value.getType() == PropertyType.BINARY)
111 strValue = "<binary>";
112 else if (value.getType() == PropertyType.DATE)
113 strValue = df.format(value.getDate().getTime());
114 else
115 strValue = value.getString();
116 return strValue;
117 } catch (RepositoryException e) {
118 throw new SlcException("unexpected error while formatting value",
119 e);
120 }
121 }
122
123 public static String formatAsString(Object value) {
124 String strValue;
125 if (value instanceof Calendar)
126 strValue = df.format(((Calendar) value).getTime());
127 else
128 strValue = value.toString();
129 return strValue;
130 }
131 }