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.gfpshare.beans;
28:
29: import java.awt.Component;
30: import java.util.Map;
31:
32: import javax.swing.JList;
33: import javax.swing.plaf.basic.BasicComboBoxRenderer;
34:
35: /**
36: * Este é um componente derivado de BasicComboBoxRenderer que possui e capacidade de
37: * renderizar dados que estiverem contidos em um Map. Ao instanciá-lo deve-se passar a
38: * String representando o chave (key) da Map que contem o valor a ser exibido.
39: *
40: * @author Igor Regis da Silva Simões
41: */
42: public class ComboBoxMapRenderer extends BasicComboBoxRenderer {
43: private String[] keyToShow = null;
44:
45: /**
46: * Cria uma nova instância de ComboBoxHashtableRenderer
47: * @param keyToShow Chave que contem a referencia para o valor a ser exibido
48: */
49: public ComboBoxMapRenderer(String keyToShow) {
50: this .keyToShow = new String[] { keyToShow };
51: }
52:
53: /**
54: * Cria uma nova instância de ComboBoxHashtableRenderer
55: * @param keyToShow Array de chaves que contem a referencia para os valores que serão exibidos
56: */
57: public ComboBoxMapRenderer(String[] keyToShow) {
58: this .keyToShow = keyToShow;
59: }
60:
61: /**
62: * @see javax.swing.plaf.basic.BasicComboBoxRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
63: */
64: @Override
65: public Component getListCellRendererComponent(JList list,
66: Object value, int index, boolean isSelected,
67: boolean cellHasFocus) {
68: super .getListCellRendererComponent(list, value, index,
69: isSelected, cellHasFocus);
70:
71: if (value != null && value instanceof Map
72: && ((Map) value).size() != 0) {
73: String texto = "";
74: for (int i = 0; i < keyToShow.length; i++) {
75: if (((Map) value).get(keyToShow[i]) != null)
76: texto += (i >= 1 ? " - " : "")
77: + ((Map) value).get(keyToShow[i])
78: .toString();
79:
80: }
81:
82: setText(texto);
83: } else {
84: setText("");
85: }
86: return this;
87: }
88: }
|