01: /*
02: * Created on 12/10/2004
03: *
04: * ============================================================================
05: * GNU Lesser General Public License
06: * ============================================================================
07: *
08: * Swing Components - visit http://sf.net/projects/gfd
09: *
10: * Copyright (C) 2004 Igor Regis da Silva Simões
11: *
12: * This library is free software; you can redistribute it and/or
13: * modify it under the terms of the GNU Lesser General Public
14: * License as published by the Free Software Foundation; either
15: * version 2.1 of the License, or (at your option) any later version.
16: *
17: * This library is distributed in the hope that it will be useful,
18: * but WITHOUT ANY WARRANTY; without even the implied warranty of
19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20: * Lesser General Public License for more details.
21: *
22: * You should have received a copy of the GNU Lesser General Public
23: * License along with this library; if not, write to the Free Software
24: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
25: */
26:
27: package br.com.igor.beans;
28:
29: import java.awt.Component;
30: import java.util.HashMap;
31: import java.util.Locale;
32: import java.util.Map;
33:
34: import javax.swing.JList;
35: import javax.swing.plaf.basic.BasicComboBoxRenderer;
36:
37: /**
38: * Este é um componente derivado de BasicComboBoxRenderer que possui e capacidade de
39: * renderizar dados que estiverem contidos em um Map. Ao instanciá-lo deve-se passar a
40: * String representando o chave (key) da Map que contem o valor a ser exibido.
41: *
42: * @author Igor Regis da Silva Simões
43: */
44: public class LocaleComboBoxRenderer extends BasicComboBoxRenderer {
45:
46: private Map idiomas = null;
47:
48: private Map paises = null;
49:
50: /**
51: * Cria uma nova instância de ComboBoxHashtableRenderer
52: * @param keyToShow Chave que contem a referencia para o valor a ser exibido
53: */
54: public LocaleComboBoxRenderer() {
55:
56: }
57:
58: /**
59: * @see javax.swing.plaf.basic.BasicComboBoxRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
60: */
61: @Override
62: public Component getListCellRendererComponent(JList list,
63: Object value, int index, boolean isSelected,
64: boolean cellHasFocus) {
65: super .getListCellRendererComponent(list, value, index,
66: isSelected, cellHasFocus);
67:
68: Object texto = null;
69:
70: if (idiomas == null) {
71: Locale[] locais = Locale.getAvailableLocales();
72: idiomas = new HashMap(30);
73: paises = new HashMap(30);
74:
75: for (int i = 0; i < locais.length; i++) {
76: idiomas.put(locais[i].getLanguage(), locais[i]
77: .getDisplayLanguage());
78: paises.put(locais[i].getCountry(), locais[i]
79: .getDisplayCountry());
80: }
81: }
82:
83: if ((texto = idiomas.get(value)) == null)
84: texto = paises.get(value);
85:
86: setText(texto == null ? value.toString() : texto.toString());
87:
88: return this;
89: }
90: }
|