Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / listManager / ListManager.java @ 40561

History | View | Annotate | Download (7.78 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.listManager;
25

    
26
import java.util.ResourceBundle;
27

    
28
import javax.swing.JButton;
29
import javax.swing.JList;
30
import javax.swing.JPanel;
31
import javax.swing.JScrollPane;
32

    
33

    
34
/**
35
 * Control de gesti?n de una lista
36
 *
37
 * @author Fernando Gonz?lez Cort?s
38
 */
39
public class ListManager {
40
    private JScrollPane jScrollPane = null;
41
    private JList list = null;
42
    private JButton btnAdd = null;
43
    private JButton btnDel = null;
44
    private JPanel jPanel = null;
45
    private JButton btnProperties = null;
46
    private JButton btnUp = null;
47
    private JButton btnDown = null;
48
    private ListModel listModel;// = new DefaultListModel();
49
    private ListManagerListener listener;
50
    private ResourceBundle bundle;
51

    
52
    /**
53
     * This is the default constructor
54
     * down==true if list is in order.
55
     */
56
    public ListManager(boolean down) {
57
        super();
58
        listModel = new DefaultListModel(down);
59
    }
60

    
61
    /**
62
     * This method initializes this
63
     */
64
    public void initialize() {
65
        actualizar();
66
    }
67

    
68
    /**
69
     * Obtiene la traducci?n de una palabra
70
     *
71
     * @param key Clave en el resource bundle
72
     *
73
     * @return valor de la clave
74
     */
75
    private String getText(String key) {
76
        if (bundle == null) {
77
            return key;
78
        }
79

    
80
        String ret = bundle.getString(key);
81

    
82
        if (ret == null) {
83
            ret = key;
84
        }
85

    
86
        return ret;
87
    }
88

    
89
    /**
90
     * Habilita/Deshabilita los botones
91
     */
92
    private void actualizar() {
93
        int index = list.getSelectedIndex();
94

    
95
        if (index != -1) {
96
            if (btnUp != null) {
97
                btnUp.setEnabled(index > 0);
98
            }
99

    
100
            if (btnDown != null) {
101
                btnDown.setEnabled(index < (listModel.getSize() - 1));
102
            }
103

    
104
            if (btnDel != null) {
105
                btnDel.setEnabled(true);
106
            }
107

    
108
            if (btnProperties != null) {
109
                btnProperties.setEnabled(listModel.getElementAt(index) instanceof Propertiable);
110
            }
111
        } else {
112
            if (btnUp != null) {
113
                btnUp.setEnabled(false);
114
            }
115

    
116
            if (btnDown != null) {
117
                btnDown.setEnabled(false);
118
            }
119

    
120
            if (btnDel != null) {
121
                btnDel.setEnabled(false);
122
            }
123

    
124
            if (btnProperties != null) {
125
                btnProperties.setEnabled(false);
126
            }
127
        }
128
    }
129

    
130
    /**
131
     * This method initializes list
132
     *
133
     * @param list DOCUMENT ME!
134
     */
135
    public void setList(JList list) {
136
        this.list = list;
137
        list.setModel(listModel);
138
        list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
139
                public void valueChanged(javax.swing.event.ListSelectionEvent e) {
140
                    actualizar();
141
                }
142
            });
143
    }
144

    
145
    /**
146
     * This method initializes btnAdd
147
     *
148
     * @param btn DOCUMENT ME!
149
     */
150
    public void setBtnAdd(JButton btn) {
151
        btnAdd = btn;
152
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
153
                public void actionPerformed(java.awt.event.ActionEvent e) {
154
                    Object[] objs = listener.addObjects();
155

    
156
                    for (int i = 0; i < objs.length; i++) {
157
                        listModel.add(objs[i]);
158
                    }
159
                    actualizar();
160
                }
161
            });
162
    }
163

    
164
    /**
165
     * This method initializes btnDel
166
     *
167
     * @param btn DOCUMENT ME!
168
     */
169
    public void setBtnDel(JButton btn) {
170
        btnDel = btn;
171
        btnDel.addActionListener(new java.awt.event.ActionListener() {
172
                public void actionPerformed(java.awt.event.ActionEvent e) {
173
                    int i = list.getSelectedIndex();
174
                    listModel.remove(i);
175

    
176
                    if (i < listModel.getSize()) {
177
                        list.setSelectedIndex(i);
178
                    }
179

    
180
                    actualizar();
181
                }
182
            });
183
    }
184

    
185
    /**
186
     * This method initializes btnProperties
187
     *
188
     * @param btn DOCUMENT ME!
189
     */
190
    public void setBtnProperties(JButton btn) {
191
        btnProperties = btn;
192
        btnProperties.addActionListener(new java.awt.event.ActionListener() {
193
                public void actionPerformed(java.awt.event.ActionEvent e) {
194
                    int index = list.getSelectedIndex();
195
                    Object prop = listener.getProperties(listModel.getElementAt(
196
                                index));
197
                    ((Propertiable) listModel.getElementAt(index)).setProperties(prop);
198
                    list.repaint();
199
                }
200
            });
201
    }
202

    
203
    /**
204
     * This method initializes btnUp
205
     *
206
     * @param btn DOCUMENT ME!
207
     */
208
    public void setBtnUp(JButton btn) {
209
        btnUp = btn;
210
        btnUp.addActionListener(new java.awt.event.ActionListener() {
211
                public void actionPerformed(java.awt.event.ActionEvent e) {
212
                    int index = list.getSelectedIndex();
213
                    Object o1=listModel.getElementAt(index-1);
214
                    Object o2=listModel.getElementAt(index);
215
                    listModel.remove(index-1);
216
                    listModel.insertAt(index-1,o2);
217
                    listModel.remove(index);
218
                    listModel.insertAt(index,o1);
219
                    list.setSelectedIndex(index - 1);
220
                }
221
            });
222
    }
223

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

    
241
    /**
242
     * Obtiene el listModel
243
     *
244
     * @return Returns the listModel.
245
     */
246
    public ListModel getListModel() {
247
        return listModel;
248
    }
249

    
250
    /**
251
     * Establece el listModel
252
     *
253
     * @param listModel The listModel to set.
254
     */
255
    public void setListModel(ListModel listModel) {
256
        this.listModel = listModel;
257
    }
258

    
259
    /**
260
     * Obtiene el listener de eventos del control
261
     *
262
     * @return Returns the listener.
263
     */
264
    public ListManagerListener getListener() {
265
        return listener;
266
    }
267

    
268
    /**
269
     * Establece el listener de eventos del control
270
     *
271
     * @param listener The listener to set.
272
     */
273
    public void setListener(ListManagerListener listener) {
274
        this.listener = listener;
275
    }
276

    
277
    /**
278
     * DOCUMENT ME!
279
     *
280
     * @param bundle The bundle to set.
281
     */
282
    public void setBundle(ResourceBundle bundle) {
283
        this.bundle = bundle;
284
    }
285
}