LayoutItemFIeld: getName(): Use the Field if it is set.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / shared / libglom / layout / LayoutItemField.java
1 package org.glom.web.shared.libglom.layout;
2
3 import org.glom.web.client.StringUtils;
4 import org.glom.web.shared.libglom.CustomTitle;
5 import org.glom.web.shared.libglom.Field;
6 import org.glom.web.shared.libglom.Field.GlomFieldType;
7 import org.glom.web.shared.libglom.Relationship;
8
9 public class LayoutItemField extends LayoutItemWithFormatting implements UsesRelationship {
10
11         private static final long serialVersionUID = -7101706636312130106L;
12         private Field field;
13
14         /*
15          * Don't make this final, because that breaks GWT compilation. See
16          * http://code.google.com/p/google-web-toolkit/issues/detail?id=1054
17          */
18         private/* final */UsesRelationship usesRel = new UsesRelationshipImpl();
19         private boolean useDefaultFormatting = true;
20
21         /*
22          * Don't make this final, because that breaks GWT serialization. See
23          * http://code.google.com/p/google-web-toolkit/issues/detail?id=1054
24          */
25         private/* final */CustomTitle customTitle = new CustomTitle();
26
27         // Extras:
28         private String navigationTableName = null; // If any.
29
30         @Override
31         public String getName() {
32                 if(field == null) {
33                         return super.getName();
34                 } else {
35                         return field.getName();
36                 }
37         }
38
39         /**
40          * @return the field
41          */
42         public Field getFullFieldDetails() {
43                 return field;
44         }
45
46         /**
47          * @param field
48          *            the field to set
49          */
50         public void setFullFieldDetails(final Field field) {
51                 this.field = field;
52         }
53
54         /**
55          * @return
56          */
57         public Formatting getFormattingUsed() {
58                 if (useDefaultFormatting && (field != null)) {
59                         return field.getFormatting();
60                 } else {
61                         return super.getFormatting();
62                 }
63         }
64
65         /**
66          * @return
67          */
68         public GlomFieldType getGlomType() {
69                 if (field != null) {
70                         return field.getGlomType();
71                 }
72
73                 return GlomFieldType.TYPE_INVALID;
74         }
75
76         /*
77          * (non-Javadoc)
78          * 
79          * @see org.glom.web.shared.libglom.layout.UsesRelationship#get_table_used(java.lang.String)
80          */
81         @Override
82         public String getTableUsed(final String tableName) {
83                 return usesRel.getTableUsed(tableName);
84         }
85
86         /**
87          * @param forDetailsView
88          * @return
89          */
90         public Formatting.HorizontalAlignment getFormattingUsedHorizontalAlignment(final boolean forDetailsView) {
91                 return null; // TODO
92         }
93
94         // TODO: This should actually be in LayoutItem, with an override here.
95         /**
96          * @return
97          */
98         public String getLayoutDisplayName() {
99                 String result = "";
100
101                 if (field != null) {
102                         result = field.getName();
103                 } else {
104                         result = getName();
105                 }
106
107                 // Indicate if it's a field in another table.
108                 if (getHasRelatedRelationshipName()) {
109                         final Relationship rel = getRelatedRelationship();
110                         if (rel != null) {
111                                 result = rel.getName() + "::" + result;
112                         }
113                 }
114
115                 if (getHasRelationshipName()) {
116                         final Relationship rel = getRelationship();
117                         if (rel != null) {
118                                 result = rel.getName() + "::" + result;
119                         }
120                 }
121
122                 return result;
123         }
124
125         /*
126          * (non-Javadoc)
127          * 
128          * @see
129          * org.glom.web.shared.libglom.layout.UsesRelationship#setRelationship(org.glom.web.shared.libglom.Relationship)
130          */
131         @Override
132         public void setRelationship(final Relationship relationship) {
133                 usesRel.setRelationship(relationship);
134         }
135
136         /*
137          * (non-Javadoc)
138          * 
139          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getRelationship()
140          */
141         @Override
142         public Relationship getRelationship() {
143                 return usesRel.getRelationship();
144         }
145
146         /*
147          * (non-Javadoc)
148          * 
149          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getHasRelationshipName()
150          */
151         @Override
152         public boolean getHasRelationshipName() {
153                 return usesRel.getHasRelationshipName();
154         }
155
156         /*
157          * (non-Javadoc)
158          * 
159          * @see
160          * org.glom.web.shared.libglom.layout.UsesRelationship#setRelatedRelationship(org.glom.web.shared.libglom.Relationship
161          * )
162          */
163         @Override
164         public void setRelatedRelationship(final Relationship relationship) {
165                 usesRel.setRelatedRelationship(relationship);
166         }
167
168         /*
169          * (non-Javadoc)
170          * 
171          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getRelatedRelationship()
172          */
173         @Override
174         public Relationship getRelatedRelationship() {
175                 return usesRel.getRelatedRelationship();
176         }
177
178         /*
179          * (non-Javadoc)
180          * 
181          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getHasRelatedRelationshipName()
182          */
183         @Override
184         public boolean getHasRelatedRelationshipName() {
185                 return usesRel.getHasRelatedRelationshipName();
186         }
187
188         /*
189          * (non-Javadoc)
190          * 
191          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getSqlJoinAliasName()
192          */
193         @Override
194         public String getSqlJoinAliasName() {
195                 return usesRel.getSqlJoinAliasName();
196         }
197
198         /**
199          * @param attribute
200          */
201         public void setUseDefaultFormatting(final boolean useDefaultFormatting) {
202                 this.useDefaultFormatting = useDefaultFormatting;
203         }
204
205         /**
206          * @return
207          */
208         public boolean getUseDefaultFormatting() {
209                 return useDefaultFormatting;
210         }
211
212         /*
213          * (non-Javadoc)
214          * 
215          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getSqlTableOrJoinAliasName(java.lang.String)
216          */
217         @Override
218         public String getSqlTableOrJoinAliasName(final String tableName) {
219                 return usesRel.getSqlTableOrJoinAliasName(tableName);
220         }
221
222         /**
223          * @return
224          */
225         public String getNavigationTableName() {
226                 return navigationTableName;
227         }
228
229         // TODO: Use this.
230         public void setNavigationTableName(final String navigationtableName) {
231                 this.navigationTableName = navigationtableName;
232         }
233
234         /*
235          * (non-Javadoc)
236          * 
237          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getRelationshipNameUsed()
238          */
239         @Override
240         public String getRelationshipNameUsed() {
241                 return usesRel.getRelationshipNameUsed();
242         }
243
244         @Override
245         public String getTitleOriginal() {
246                 if (customTitle.getUseCustomTitle()) {
247                         return customTitle.getTitleOriginal();
248                 }
249
250                 if (field != null) {
251                         return field.getTitleOriginal();
252                 }
253
254                 return "";
255         }
256
257         @Override
258         public String getTitle(final String locale) {
259                 if (customTitle.getUseCustomTitle()) {
260                         return customTitle.getTitle(locale);
261                 }
262
263                 // Fallback to the field's title:
264                 String title = "";
265                 if (field != null) {
266                         title = field.getTitle(locale);
267                 }
268
269                 // Fallback to the field's original title:
270                 if (StringUtils.isEmpty(title) && (field != null)) {
271                         title = field.getTitleOriginal();
272                 }
273
274                 return title;
275         }
276
277         @Override
278         public String getTitleOrName(final String locale) {
279                 if (customTitle.getUseCustomTitle()) {
280                         return customTitle.getTitle(locale);
281                         // TODO: Do not force the use of empty translations.
282                 }
283
284                 // Fallback to the field's original title:
285                 if (field != null) {
286                         return field.getTitleOrName(locale);
287                 }
288
289                 return getName();
290         }
291
292         public CustomTitle getCustomTitle() {
293                 return customTitle;
294         }
295
296         /*
297          * (non-Javadoc)
298          * 
299          * @see org.glom.web.shared.libglom.layout.UsesRelationship#getTitleUsed(java.lang.String, java.lang.String)
300          */
301         @Override
302         public String getTitleUsed(final String parentTableTitle, final String locale) {
303                 return usesRel.getTitleUsed(parentTableTitle, locale);
304         }
305
306 }