KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > workbench > table > LocaleSelection


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.workbench.table;
16
17 import java.text.DateFormat JavaDoc;
18 import java.text.DecimalFormatSymbols JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.apache.tapestry.BaseComponent;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.contrib.table.model.ITableColumn;
26 import org.apache.tapestry.contrib.table.model.simple.ITableColumnEvaluator;
27 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn;
28
29 /**
30  * @author mindbridge
31  */

32 public abstract class LocaleSelection extends BaseComponent implements ILocaleSelectionListener
33 {
34     // temporary
35
private Locale JavaDoc m_objCurrentLocale;
36
37     // properties
38
public abstract Locale JavaDoc getCurrentLocale();
39
40     public abstract Set JavaDoc getLocaleSet();
41
42     public abstract void setLocaleSet(Set JavaDoc objLocaleSet);
43
44     /**
45      * @see org.apache.tapestry.workbench.table.ILocaleSelectionListener#localesSelected(Locale[])
46      */

47     public void localesSelected(Locale JavaDoc[] arrLocales)
48     {
49         Set JavaDoc objLocaleSet = getLocaleSet();
50         addAll(objLocaleSet, arrLocales);
51         // ensure that the framework knows about the change and the set is persisted
52
setLocaleSet(objLocaleSet);
53     }
54
55     private void addAll(Set JavaDoc objLocaleSet, Locale JavaDoc[] arrLocales)
56     {
57         for (int i = 0; i < arrLocales.length; i++)
58             objLocaleSet.add(arrLocales[i]);
59     }
60
61     public ITableColumn getCurrencyColumn()
62     {
63         // The column value is extracted in a custom evaluator class
64
return new SimpleTableColumn("Currency", new CurrencyEvaluator(), true);
65     }
66
67     public ITableColumn getDateFormatColumn()
68     {
69         // The entire column is defined using a custom column class
70
return new DateFormatColumn(new Date JavaDoc());
71     }
72
73     /**
74      * Returns the verbosity of the current locale. This is used by the Block rendering the
75      * 'Verbosity' column
76      *
77      * @return int the current locale verbosity
78      */

79     public int getCurrentLocaleVerbosity()
80     {
81         int nVerbosity = VerbosityRating.calculateVerbosity(getCurrentLocale());
82         return nVerbosity;
83     }
84
85     /**
86      * Generates the context that will be passed to the deleteLocale() listener if a "remove" link
87      * is selected.
88      * <p>
89      * This is used by the Block rendering the 'Remove' column.
90      *
91      * @return String[] the context for the deleteLocale() listener
92      */

93     public String JavaDoc[] getDeleteLocaleContext()
94     {
95         Locale JavaDoc objLocale = getCurrentLocale();
96         return new String JavaDoc[]
97         { objLocale.getLanguage(), objLocale.getCountry(), objLocale.getVariant() };
98     }
99
100     /**
101      * A listener invoked when a "remove" link is selected. It removes from the data model the
102      * locale corresponding to the link.
103      * <p>
104      *
105      * @param objCycle
106      * the request cycle
107      */

108     public void deleteLocale(IRequestCycle objCycle)
109     {
110         Object JavaDoc[] arrParams = objCycle.getListenerParameters();
111         Locale JavaDoc objLocale = new Locale JavaDoc(arrParams[0].toString(), arrParams[1].toString(),
112                 arrParams[2].toString());
113         getLocaleSet().remove(objLocale);
114     }
115
116     /**
117      * A class defining the logic for getting the currency symbol from a locale
118      */

119     private static class CurrencyEvaluator implements ITableColumnEvaluator
120     {
121         private static final long serialVersionUID = 1L;
122         
123         /**
124          * @see org.apache.tapestry.contrib.table.model.simple.ITableColumnEvaluator#getColumnValue(ITableColumn,
125          * Object)
126          */

127         public Object JavaDoc getColumnValue(ITableColumn objColumn, Object JavaDoc objRow)
128         {
129             Locale JavaDoc objLocale = (Locale JavaDoc) objRow;
130             String JavaDoc strCountry = objLocale.getCountry();
131             if (strCountry == null || strCountry.equals(""))
132                 return "";
133
134             DecimalFormatSymbols JavaDoc objSymbols = new DecimalFormatSymbols JavaDoc(objLocale);
135             return objSymbols.getCurrencySymbol();
136         }
137     }
138
139     /**
140      * A class defining a column for displaying the date format
141      */

142     private static class DateFormatColumn extends SimpleTableColumn
143     {
144         private static final long serialVersionUID = 1L;
145         private Date JavaDoc m_objDate;
146
147         public DateFormatColumn(Date JavaDoc objDate)
148         {
149             super("Date Format", true);
150             m_objDate = objDate;
151         }
152
153         public Object JavaDoc getColumnValue(Object JavaDoc objRow)
154         {
155             Locale JavaDoc objLocale = (Locale JavaDoc) objRow;
156             DateFormat JavaDoc objFormat = DateFormat.getDateTimeInstance(
157                     DateFormat.LONG,
158                     DateFormat.LONG,
159                     objLocale);
160             return objFormat.format(m_objDate);
161         }
162     }
163
164 }
Popular Tags