Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / treelist / TreeListContainer.java @ 40561

History | View | Annotate | Download (11.8 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.gui.beans.treelist;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Dimension;
28
import java.awt.FlowLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.util.ArrayList;
32
import java.util.Hashtable;
33
import java.util.Iterator;
34

    
35
import javax.swing.DefaultListModel;
36
import javax.swing.ImageIcon;
37
import javax.swing.JButton;
38
import javax.swing.JList;
39
import javax.swing.JPanel;
40
import javax.swing.JScrollPane;
41
import javax.swing.JSplitPane;
42
import javax.swing.JTree;
43
import javax.swing.ListModel;
44
import javax.swing.ListSelectionModel;
45
import javax.swing.event.ListSelectionEvent;
46
import javax.swing.event.ListSelectionListener;
47
import javax.swing.event.TreeSelectionEvent;
48
import javax.swing.event.TreeSelectionListener;
49
import javax.swing.tree.DefaultMutableTreeNode;
50
import javax.swing.tree.DefaultTreeModel;
51
import javax.swing.tree.TreePath;
52

    
53
import org.gvsig.gui.beans.Messages;
54
import org.gvsig.gui.beans.treelist.event.TreeListChangeEvent;
55
import org.gvsig.gui.beans.treelist.listeners.TreeListChangeListener;
56
import org.gvsig.gui.beans.treelist.listeners.TreeListComponentListener;
57
import org.gvsig.gui.beans.treelist.listeners.TreeListListener;
58
/**
59
 * Componente consistente en un men? de arbol al que se le pueden a?adir
60
 * entradas y una lista de elementos debajo de este. Haciendo doble click o
61
 * arrastrando un elemento del men? a la lista este queda a?adido en esta.
62
 * Haciendo doble click en un elemento de la lista se elimina de esta y
63
 * arrastrando elementos dentro de la lista se varia su posici?n en ella.
64
 *
65
 * @version 31/05/2007
66
 * @author Nacho Brodin (brodin_ign@gva.es)
67
 */
68
public class TreeListContainer extends JPanel implements ActionListener, TreeSelectionListener, ListSelectionListener {
69
        private static final long serialVersionUID = 6665259638830401366L;
70
        private ArrayList<TreeListChangeListener> actionChangeListeners = new ArrayList<TreeListChangeListener>();
71
        private ArrayList<TreeListComponentListener> listListeners = new ArrayList<TreeListComponentListener>();
72
        private Hashtable<String, String> map = null;
73

    
74
        // Componentes visuales
75
        private JScrollPane            pTree         = null;
76
        private JScrollPane            pList         = null;
77
        private JTree                  tree          = null;
78
        private JList                  list          = null;
79
        private JButton                bAdd          = null;
80
        private JButton                bDel          = null;
81
        private JSplitPane             jSplitPane1   = null;
82
        private JPanel                 jPanelButtons = null;
83

    
84
        private DefaultMutableTreeNode raiz          = null;
85
        private TreeListListener       listener      = null;
86
        private String                 pathToImages  = "images/";
87

    
88
        /**
89
         * This method initializes
90
         */
91
        public TreeListContainer() {
92
                super();
93
                listener = new TreeListListener();
94
                initialize();
95
                listener.setList(getList());
96
                listener.setTree(getTree());
97
                this.setPreferredSize(new Dimension(160, 0));
98
                getTree().addTreeSelectionListener(this);
99
                getList().addListSelectionListener(this);
100
        }
101

    
102
        /**
103
         * This method initializes this
104
         */
105
        private void initialize() {
106
                map = new Hashtable<String, String>();
107
                raiz =  new DefaultMutableTreeNode(Messages.getText("filtros"));
108
                setLayout(new BorderLayout());
109

    
110
                getPList().setMinimumSize(new Dimension(0, 60));
111
                jSplitPane1 = new JSplitPane();
112
                jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
113
                jSplitPane1.setTopComponent(getPTree());
114
                jSplitPane1.setBottomComponent(getPList());
115
                jSplitPane1.setResizeWeight(1.0);
116
                jSplitPane1.setContinuousLayout(true);
117
                this.add(jSplitPane1, BorderLayout.CENTER);
118
                this.add(getJPanelButtons(), BorderLayout.SOUTH);
119
        }
120

    
121
        /**
122
         * Establece el ToolTip del boton de a?adir
123
         * @param text
124
         */
125
        public void setAddToolTipText(String text) {
126
                getAddButton().setToolTipText(text);
127
        }
128

    
129
        /**
130
         * Establece el ToolTip del boton de borrar
131
         * @param text
132
         */
133
        public void setDelToolTipText(String text) {
134
                getDelButton().setToolTipText(text);
135
        }
136

    
137
        /**
138
         * Devuelve el boton de A?adir
139
         * @return
140
         */
141
        private JButton getAddButton() {
142
                if (bAdd == null) {
143
                        bAdd = new JButton();
144
                        bAdd.setIcon(new ImageIcon(getClass().getResource(pathToImages + "addlayer.png")));
145
                        bAdd.setPreferredSize(new Dimension(22, 19));
146
                        bAdd.addActionListener(this);
147
                }
148
                return bAdd;
149
        }
150

    
151
        /**
152
         * Devuelve el boton de Borrar
153
         * @return
154
         */
155
        private JButton getDelButton() {
156
                if (bDel == null) {
157
                        bDel = new JButton();
158
                        bDel.setIcon(new ImageIcon(getClass().getResource(pathToImages + "delall.png")));
159
                        bDel.setPreferredSize(new Dimension(22, 19));
160
                        bDel.addActionListener(this);
161
                }
162
                return bDel;
163
        }
164
        
165
        private JPanel getJPanelButtons() {
166
                if (jPanelButtons == null) {
167
                        jPanelButtons = new JPanel();
168
                        jPanelButtons.setPreferredSize(new Dimension(0, 21));
169

    
170

    
171

    
172

    
173
                        FlowLayout flowLayout5 = new FlowLayout();
174
                        flowLayout5.setHgap(5);
175
                        flowLayout5.setVgap(0);
176
                        flowLayout5.setAlignment(java.awt.FlowLayout.CENTER);
177
                        jPanelButtons.setLayout(flowLayout5);
178

    
179
                        jPanelButtons.add(getAddButton(), null);
180
                        jPanelButtons.add(getDelButton(), null);
181
                }
182
                return jPanelButtons;
183
        }
184

    
185
        /**
186
         * This method initializes jPanel
187
         *
188
         * @return javax.swing.JPanel
189
         */
190
        private JScrollPane getPTree() {
191
                if (pTree == null) {
192
                        pTree = new JScrollPane();
193
                        pTree.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
194
                        pTree.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
195
                        pTree.setViewportView(getTree());
196
                }
197
                return pTree;
198
        }
199

    
200
        /**
201
         * This method initializes jPanel1
202
         *
203
         * @return javax.swing.JPanel
204
         */
205
        private JScrollPane getPList() {
206
                if (pList == null) {
207
                        pList = new JScrollPane();
208
                        pList.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
209
                        pList.setBackground(java.awt.Color.white);
210
                        pList.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
211
                        pList.setViewportView(getList());
212
                }
213
                return pList;
214
        }
215

    
216
        /**
217
         * This method initializes jTree
218
         *
219
         * @return javax.swing.JTree
220
         */
221
        public JTree getTree() {
222
                if (tree == null) {
223
                        tree = new JTree(raiz);
224
                        tree.addMouseListener(listener);
225
                        tree.addMouseMotionListener(listener);
226
                }
227
                return tree;
228
        }
229

    
230
        /**
231
         * This method initializes jList
232
         *
233
         * @return javax.swing.JList
234
         */
235
        public JList getList() {
236
                if (list == null) {
237
                        DefaultListModel m = new DefaultListModel();
238
                        list = new JList(m);
239
                        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
240
                        list.addMouseListener(listener);
241
                        list.addMouseMotionListener(listener);
242
                }
243
                return list;
244
        }
245

    
246
        public ListModel getListModel() {
247
                return list.getModel();
248
        }
249

    
250
        //-------------------------------------------
251
        //M?TODOS DE ARBOL
252

    
253
        /**
254
         * A?ade una nueva categoria al arbol
255
         * @param name        Etiqueta que aparece en el arbol.
256
         * @param pos        Posici?n en el arbol de la nueva categoria
257
         */
258
        public void addClass(String name, int pos) {
259
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
260
                DefaultMutableTreeNode r = new DefaultMutableTreeNode(name);
261
                model.insertNodeInto(r, raiz, pos);
262
        }
263

    
264
        /**
265
         * A?ade una entrada a una categoria
266
         * @param name Nombre de la entrada a a?adir
267
         * @param parentName Categoria a la que a?adimos
268
         * @param value Valor asociado a la entrada
269
         */
270
        public void addEntry(String name, String parentName, String value){
271
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
272
                for (int i = 0; i < model.getChildCount(raiz); i++) {
273
                        if (model.getChild(raiz, i).toString().equals(parentName)) {
274
                                DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getChild(raiz, i);
275
                                node.add(new DefaultMutableTreeNode(name));
276
                                if (value != null)
277
                                        map.put(name, value);
278
                        }
279
                }
280
        }
281

    
282
        /**
283
         * M?todo que comprueba si una entrada existe en la lista de seleccion.
284
         * @param value Valor que se quiere comprobar si est? en la lista
285
         * @return true si el valor est? en la lista y false si no lo est?
286
         */
287
        public boolean isInList(String value) {
288
                DefaultListModel model = (DefaultListModel) getList().getModel();
289
                for (int i = 0; i < model.getSize(); i++)
290
                        if (((String) model.get(i)).equals(value))
291
                                return true;
292
                return false;
293
        }
294

    
295
        /**
296
         * A?ade un listener TreeListComponent
297
         * @param e
298
         */
299
        public void addTreeListListener(TreeListComponentListener e){
300
                listListeners.add(e);
301
                listener.setListeners(listListeners);
302
        }
303

    
304
        /**
305
         * A?ade un elemento a la lista
306
         * @param element Elemento a a?adir
307
         */
308
        public void addElementInList(String element){
309
                DefaultListModel model = (DefaultListModel)getList().getModel();
310
                model.addElement(element);
311
        }
312

    
313
        /**
314
         * Elimina un elemento a la lista
315
         * @param element Elemento a eliminar
316
         */
317
        public void removeElementInList(String element){
318
                DefaultListModel model = (DefaultListModel)getList().getModel();
319
                model.removeElement(element);
320
        }
321

    
322
        /**
323
         * Elimina un elemento a la lista por indice
324
         * @param element Indice del elemento a eliminar
325
         */
326
        public void removeElementInList(int element){
327
                DefaultListModel model = (DefaultListModel) getList().getModel();
328
                model.remove(element);
329
        }
330

    
331
        @SuppressWarnings("unchecked")
332
        public Hashtable getMap() {
333
                return map;
334
        }
335

    
336
        @SuppressWarnings("unchecked")
337
        public void setMap(Hashtable map) {
338
                this.map = map;
339
        }
340

    
341
        public void actionPerformed(ActionEvent e) {
342
                if (e.getSource() == getAddButton()) {
343
                        listener.insertElement();
344
                }
345
                if (e.getSource() == getDelButton()) {
346
                        listener.deleteElement();
347
                }
348
        }
349

    
350
        /**
351
         * A?adir el disparador de cuando se pulsa un bot?n.
352
         * @param listener
353
         */
354
        public void addChangeSelectionListener(TreeListChangeListener listener) {
355
                if (!actionChangeListeners.contains(listener))
356
                        actionChangeListeners.add(listener);
357
        }
358

    
359
        /**
360
         * Borrar el disparador de eventos de los botones.
361
         * @param listener
362
         */
363
        public void removeChangeSelectionListener(TreeListChangeListener listener) {
364
                actionChangeListeners.remove(listener);
365
        }
366

    
367
        private void callActionChangeListeners(String item) {
368
                Iterator<TreeListChangeListener> acIterator = actionChangeListeners.iterator();
369
                while (acIterator.hasNext()) {
370
                        TreeListChangeListener listener = acIterator.next();
371
                        String name = map.get(item);
372
                        if (name != null)
373
                                listener.actionChangeSelection(new TreeListChangeEvent(this, name));
374
                        else
375
                                listener.actionChangeSelection(new TreeListChangeEvent(this, item));
376
                }
377
        }
378

    
379
        public void valueChanged(TreeSelectionEvent e) {
380
                String draggLabel = null;
381
                if (e.getSource() == tree) {
382
                        TreePath[] tp = tree.getSelectionPaths();
383
                        if (tp != null) {
384
                                String val = tp[0].toString();
385
                                // Comprobamos que se haya pinchado sobre una rama
386
                                String[] pathTree = val.split(", ");
387
                                if (pathTree.length > 2) {
388
                                        draggLabel = val.substring(val.lastIndexOf(", ") + 2, val.lastIndexOf("]"));
389
                                        callActionChangeListeners(draggLabel);
390
                                }
391
                        }
392
                }
393
        }
394

    
395
        public void valueChanged(ListSelectionEvent e) {
396
                if (!e.getValueIsAdjusting()) {
397
                        if (getList().getSelectedValue() != null)
398
                                callActionChangeListeners(getList().getSelectedValue().toString());
399
                }
400
        }
401
}