01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.uilib.list.util;
16:
17: import java.text.MessageFormat;
18: import java.util.MissingResourceException;
19: import org.araneaframework.framework.LocalizationContext;
20: import org.araneaframework.uilib.form.Control;
21: import org.araneaframework.uilib.form.Data;
22: import org.araneaframework.uilib.form.FormElement;
23: import org.araneaframework.uilib.list.structure.filter.FilterContext;
24: import org.araneaframework.uilib.support.UiLibMessages;
25: import org.araneaframework.uilib.util.Event;
26:
27: /**
28: * List filters form utils.
29: *
30: * @author <a href="mailto:rein@araneaframework.org">Rein Raudjärv</a>
31: */
32: public class FilterFormUtil {
33:
34: public static final String TEMPORARY_LABEL = "";
35: public static final String DO_NOT_LOCALIZE_PREFIX = "#";
36:
37: public static FormElement createElement(FilterContext ctx, String id) {
38: Class type = ctx.getFieldType(id);
39: FormElement result = FormUtil.createElement(TEMPORARY_LABEL,
40: FormUtil.createControl(type),
41: FormUtil.createData(type), false);
42: setLabel(ctx, result, id);
43: return result;
44: }
45:
46: public static FormElement createElement(FilterContext ctx,
47: String id, Control control) {
48: FormElement result = FormUtil.createElement(TEMPORARY_LABEL,
49: control, FormUtil.createData(ctx.getFieldType(id)),
50: false);
51: setLabel(ctx, result, id);
52: return result;
53: }
54:
55: public static FormElement createElement(FilterContext ctx,
56: String id, Control control, Data data) {
57: FormElement result = FormUtil.createElement(TEMPORARY_LABEL,
58: control, data, false);
59: setLabel(ctx, result, id);
60: return result;
61: }
62:
63: public static String getLabelForLowField(LocalizationContext loc,
64: String fieldLabelId) {
65: return DO_NOT_LOCALIZE_PREFIX
66: + format(localize(loc, UiLibMessages.LOW_OF), localize(
67: loc, fieldLabelId));
68: }
69:
70: public static String getLabelForHighField(LocalizationContext loc,
71: String fieldLabelId) {
72: return DO_NOT_LOCALIZE_PREFIX
73: + format(localize(loc, UiLibMessages.HIGH_OF),
74: localize(loc, fieldLabelId));
75: }
76:
77: private static String format(String pattern, String arg) {
78: return MessageFormat.format(pattern, new Object[] { arg });
79: }
80:
81: private static String localize(LocalizationContext loc, String key) {
82: try {
83: return loc.localize(key);
84: } catch (MissingResourceException e) {
85: return key;
86: }
87: }
88:
89: public static void setLabel(final FilterContext ctx,
90: final FormElement formElement, final String fieldId) {
91: ctx.addInitEvent(new Event() {
92: public void run() {
93: formElement.setLabel(ctx.getFieldLabel(fieldId));
94: }
95: });
96: }
97: }
|