Statistics
| Revision:

root / branches / v10 / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / AvailableFieldsCheckBoxList.java @ 11935

History | View | Annotate | Download (4.17 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package com.prodevelop.cit.gvsig.vectorialdb.wizard;
44

    
45
import java.awt.Component;
46
import java.awt.event.MouseAdapter;
47
import java.awt.event.MouseEvent;
48

    
49
import java.util.ArrayList;
50

    
51
import javax.swing.JList;
52
import javax.swing.ListCellRenderer;
53
import javax.swing.ListSelectionModel;
54
import javax.swing.UIManager;
55
import javax.swing.border.Border;
56
import javax.swing.border.EmptyBorder;
57

    
58

    
59
/**
60
 * Utility class to keep the list of available fields.
61
 *
62
 * @author jldominguez
63
 *
64
 */
65
public class AvailableFieldsCheckBoxList extends JList {
66
    protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
67

    
68
    public AvailableFieldsCheckBoxList() {
69
        setCellRenderer(new CellRenderer());
70

    
71
        addMouseListener(new MouseAdapter() {
72
                public void mousePressed(MouseEvent e) {
73
                    int index = locationToIndex(e.getPoint());
74

    
75
                    if (index == -1) {
76
                        return;
77
                    }
78

    
79
                    FieldsListItem sel = (FieldsListItem) getModel()
80
                                                              .getElementAt(index);
81

    
82
                    if ((e.getClickCount() == 2) || (e.getX() < 15)) {
83
                        sel.setSelected(!sel.isSelected());
84
                    }
85
                }
86
            });
87

    
88
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
89
    }
90

    
91
    public Object[] getCheckedItems() {
92
        int size = getModel().getSize();
93
        ArrayList resp = new ArrayList();
94

    
95
        for (int i = 0; i < size; i++) {
96
            FieldsListItem item = (FieldsListItem) getModel().getElementAt(i);
97

    
98
            if (item.isSelected()) {
99
                resp.add(item);
100
            }
101
        }
102

    
103
        return resp.toArray();
104
    }
105

    
106
    public void checkAll(boolean b) {
107
        int size = getModel().getSize();
108

    
109
        for (int i = 0; i < size; i++) {
110
            FieldsListItem item = (FieldsListItem) getModel().getElementAt(i);
111
            item.setSelected(b);
112
        }
113

    
114
        updateUI();
115
    }
116

    
117
    protected class CellRenderer implements ListCellRenderer {
118
        public Component getListCellRendererComponent(JList list, Object value,
119
            int index, boolean isSelected, boolean cellHasFocus) {
120
            FieldsListItem checkbox = (FieldsListItem) value;
121
            checkbox.setBackground(isSelected ? getSelectionBackground()
122
                                              : getBackground());
123
            checkbox.setForeground(isSelected ? getSelectionForeground()
124
                                              : getForeground());
125
            checkbox.setEnabled(isEnabled());
126
            checkbox.setFont(getFont());
127
            checkbox.setFocusPainted(false);
128
            checkbox.setBorderPainted(true);
129
            checkbox.setBorder(isSelected
130
                ? UIManager.getBorder("List.focusCellHighlightBorder")
131
                : noFocusBorder);
132

    
133
            return checkbox;
134
        }
135
    }
136
}