Statistics
| Revision:

root / trunk / libraries / libIverUtiles / src / com / iver / utiles / listManager / ListManager.java @ 19351

History | View | Annotate | Download (8.03 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. 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
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.utiles.listManager;
42

    
43
import java.util.ResourceBundle;
44

    
45
import javax.swing.JButton;
46
import javax.swing.JList;
47
import javax.swing.JPanel;
48
import javax.swing.JScrollPane;
49

    
50

    
51
/**
52
 * Control de gesti?n de una lista
53
 *
54
 * @author Fernando Gonz?lez Cort?s
55
 */
56
public class ListManager {
57
    private JScrollPane jScrollPane = null;
58
    private JList list = null;
59
    private JButton btnAdd = null;
60
    private JButton btnDel = null;
61
    private JPanel jPanel = null;
62
    private JButton btnProperties = null;
63
    private JButton btnUp = null;
64
    private JButton btnDown = null;
65
    private ListModel listModel;// = new DefaultListModel();
66
    private ListManagerListener listener;
67
    private ResourceBundle bundle;
68

    
69
    /**
70
     * This is the default constructor
71
     * down==true if list is in order.
72
     */
73
    public ListManager(boolean down) {
74
        super();
75
        listModel = new DefaultListModel(down);
76
    }
77

    
78
    /**
79
     * This method initializes this
80
     */
81
    public void initialize() {
82
        actualizar();
83
    }
84

    
85
    /**
86
     * Obtiene la traducci?n de una palabra
87
     *
88
     * @param key Clave en el resource bundle
89
     *
90
     * @return valor de la clave
91
     */
92
    private String getText(String key) {
93
        if (bundle == null) {
94
            return key;
95
        }
96

    
97
        String ret = bundle.getString(key);
98

    
99
        if (ret == null) {
100
            ret = key;
101
        }
102

    
103
        return ret;
104
    }
105

    
106
    /**
107
     * Habilita/Deshabilita los botones
108
     */
109
    private void actualizar() {
110
        int index = list.getSelectedIndex();
111

    
112
        if (index != -1) {
113
            if (btnUp != null) {
114
                btnUp.setEnabled(index > 0);
115
            }
116

    
117
            if (btnDown != null) {
118
                btnDown.setEnabled(index < (listModel.getSize() - 1));
119
            }
120

    
121
            if (btnDel != null) {
122
                btnDel.setEnabled(true);
123
            }
124

    
125
            if (btnProperties != null) {
126
                btnProperties.setEnabled(listModel.getElementAt(index) instanceof Propertiable);
127
            }
128
        } else {
129
            if (btnUp != null) {
130
                btnUp.setEnabled(false);
131
            }
132

    
133
            if (btnDown != null) {
134
                btnDown.setEnabled(false);
135
            }
136

    
137
            if (btnDel != null) {
138
                btnDel.setEnabled(false);
139
            }
140

    
141
            if (btnProperties != null) {
142
                btnProperties.setEnabled(false);
143
            }
144
        }
145
    }
146

    
147
    /**
148
     * This method initializes list
149
     *
150
     * @param list DOCUMENT ME!
151
     */
152
    public void setList(JList list) {
153
        this.list = list;
154
        list.setModel(listModel);
155
        list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
156
                public void valueChanged(javax.swing.event.ListSelectionEvent e) {
157
                    actualizar();
158
                }
159
            });
160
    }
161

    
162
    /**
163
     * This method initializes btnAdd
164
     *
165
     * @param btn DOCUMENT ME!
166
     */
167
    public void setBtnAdd(JButton btn) {
168
        btnAdd = btn;
169
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
170
                public void actionPerformed(java.awt.event.ActionEvent e) {
171
                    Object[] objs = listener.addObjects();
172

    
173
                    for (int i = 0; i < objs.length; i++) {
174
                        listModel.add(objs[i]);
175
                    }
176
                    actualizar();
177
                }
178
            });
179
    }
180

    
181
    /**
182
     * This method initializes btnDel
183
     *
184
     * @param btn DOCUMENT ME!
185
     */
186
    public void setBtnDel(JButton btn) {
187
        btnDel = btn;
188
        btnDel.addActionListener(new java.awt.event.ActionListener() {
189
                public void actionPerformed(java.awt.event.ActionEvent e) {
190
                    int i = list.getSelectedIndex();
191
                    listModel.remove(i);
192

    
193
                    if (i < listModel.getSize()) {
194
                        list.setSelectedIndex(i);
195
                    }
196

    
197
                    actualizar();
198
                }
199
            });
200
    }
201

    
202
    /**
203
     * This method initializes btnProperties
204
     *
205
     * @param btn DOCUMENT ME!
206
     */
207
    public void setBtnProperties(JButton btn) {
208
        btnProperties = btn;
209
        btnProperties.addActionListener(new java.awt.event.ActionListener() {
210
                public void actionPerformed(java.awt.event.ActionEvent e) {
211
                    int index = list.getSelectedIndex();
212
                    Object prop = listener.getProperties(listModel.getElementAt(
213
                                index));
214
                    ((Propertiable) listModel.getElementAt(index)).setProperties(prop);
215
                    list.repaint();
216
                }
217
            });
218
    }
219

    
220
    /**
221
     * This method initializes btnUp
222
     *
223
     * @param btn DOCUMENT ME!
224
     */
225
    public void setBtnUp(JButton btn) {
226
        btnUp = btn;
227
        btnUp.addActionListener(new java.awt.event.ActionListener() {
228
                public void actionPerformed(java.awt.event.ActionEvent e) {
229
                    int index = list.getSelectedIndex();
230
                    Object o1=listModel.getElementAt(index-1);
231
                    Object o2=listModel.getElementAt(index);
232
                    listModel.remove(index-1);
233
                    listModel.insertAt(index-1,o2);
234
                    listModel.remove(index);
235
                    listModel.insertAt(index,o1);
236
                    list.setSelectedIndex(index - 1);
237
                }
238
            });
239
    }
240

    
241
    /**
242
     * This method initializes btnDown
243
     *
244
     * @param btn DOCUMENT ME!
245
     */
246
    public void setBtnDown(JButton btn) {
247
        btnDown = btn;
248
        btnDown.addActionListener(new java.awt.event.ActionListener() {
249
                public void actionPerformed(java.awt.event.ActionEvent e) {
250
                    int index = list.getSelectedIndex();
251
                    Object o = listModel.remove(index);
252
                    listModel.insertAt(index + 1, o);
253
                    list.setSelectedIndex(index + 1);
254
                }
255
            });
256
    }
257

    
258
    /**
259
     * Obtiene el listModel
260
     *
261
     * @return Returns the listModel.
262
     */
263
    public ListModel getListModel() {
264
        return listModel;
265
    }
266

    
267
    /**
268
     * Establece el listModel
269
     *
270
     * @param listModel The listModel to set.
271
     */
272
    public void setListModel(ListModel listModel) {
273
        this.listModel = listModel;
274
    }
275

    
276
    /**
277
     * Obtiene el listener de eventos del control
278
     *
279
     * @return Returns the listener.
280
     */
281
    public ListManagerListener getListener() {
282
        return listener;
283
    }
284

    
285
    /**
286
     * Establece el listener de eventos del control
287
     *
288
     * @param listener The listener to set.
289
     */
290
    public void setListener(ListManagerListener listener) {
291
        this.listener = listener;
292
    }
293

    
294
    /**
295
     * DOCUMENT ME!
296
     *
297
     * @param bundle The bundle to set.
298
     */
299
    public void setBundle(ResourceBundle bundle) {
300
        this.bundle = bundle;
301
    }
302
}