KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
18 import java.util.Locale JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.apache.tapestry.BaseComponent;
23 import org.apache.tapestry.IActionListener;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.annotations.Parameter;
26 import org.apache.tapestry.contrib.table.model.IPrimaryKeyConvertor;
27
28 /**
29  * @author mindbridge
30  */

31 public abstract class LocaleList extends BaseComponent
32 {
33
34     @Parameter(required = true)
35     public abstract ILocaleSelectionListener getLocaleSelectionListener();
36
37     // immutable values
38
private IPrimaryKeyConvertor m_objLocaleConvertor;
39
40     public LocaleList()
41     {
42         // define an IPrimaryKeyConvertor that represents
43
// a Locale object as a String and converts it back
44
m_objLocaleConvertor = new IPrimaryKeyConvertor()
45         {
46             public Object JavaDoc getPrimaryKey(Object JavaDoc objValue)
47             {
48                 Locale JavaDoc objLocale = (Locale JavaDoc) objValue;
49                 return objLocale.toString();
50             }
51
52             public Object JavaDoc getValue(Object JavaDoc objPrimaryKey)
53             {
54                 StringTokenizer JavaDoc objTokenizer = new StringTokenizer JavaDoc((String JavaDoc) objPrimaryKey, "_");
55
56                 String JavaDoc strLanguage = "";
57                 if (objTokenizer.hasMoreTokens())
58                     strLanguage = objTokenizer.nextToken();
59
60                 String JavaDoc strCountry = "";
61                 if (objTokenizer.hasMoreTokens())
62                     strCountry = objTokenizer.nextToken();
63
64                 String JavaDoc strVariant = "";
65                 if (objTokenizer.hasMoreTokens())
66                     strVariant = objTokenizer.nextToken();
67
68                 return new Locale JavaDoc(strLanguage, strCountry, strVariant);
69             }
70         };
71     }
72
73     public IPrimaryKeyConvertor getLocaleConvertor()
74     {
75         return m_objLocaleConvertor;
76     }
77
78     public boolean getCheckboxSelected()
79     {
80         return getSelectedLocales().contains(getCurrentLocale());
81     }
82
83     public void setCheckboxSelected(boolean bSelected)
84     {
85         Locale JavaDoc objLocale = getCurrentLocale();
86         Set JavaDoc setSelectedLocales = getSelectedLocales();
87
88         if (bSelected)
89             setSelectedLocales.add(objLocale);
90         else
91             setSelectedLocales.remove(objLocale);
92
93         // persist value
94
setSelectedLocales(setSelectedLocales);
95     }
96
97     public void selectLocales(IRequestCycle objCycle)
98     {
99         Set JavaDoc setSelectedLocales = getSelectedLocales();
100         Locale JavaDoc[] arrLocales = new Locale JavaDoc[setSelectedLocales.size()];
101         setSelectedLocales.toArray(arrLocales);
102
103         ILocaleSelectionListener objListener = getLocaleSelectionListener();
104
105         objListener.localesSelected(arrLocales);
106
107         // clear selection
108
setSelectedLocales(new HashSet JavaDoc());
109     }
110
111     public abstract Locale JavaDoc getCurrentLocale();
112
113     public abstract Set JavaDoc getSelectedLocales();
114
115     public abstract void setSelectedLocales(Set JavaDoc set);
116 }
Popular Tags