Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / treelist / TreeListContainer.java @ 11440

History | View | Annotate | Download (7.91 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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
package org.gvsig.gui.beans.treelist;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Dimension;
23
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionListener;
25
import java.util.ArrayList;
26
import java.util.Hashtable;
27

    
28
import javax.swing.DefaultListModel;
29
import javax.swing.JButton;
30
import javax.swing.JList;
31
import javax.swing.JPanel;
32
import javax.swing.JScrollPane;
33
import javax.swing.JSplitPane;
34
import javax.swing.JTree;
35
import javax.swing.ListSelectionModel;
36
import javax.swing.tree.DefaultMutableTreeNode;
37
import javax.swing.tree.DefaultTreeModel;
38

    
39
import org.gvsig.gui.beans.treelist.listeners.TreeListComponentListener;
40
import org.gvsig.gui.beans.treelist.listeners.TreeListListener;
41
import org.gvsig.i18n.Messages;
42

    
43
/**
44
 * Componente consistente en un men? de arbol al que se le pueden a?adir entradas
45
 * y una lista de elementos debajo de este. 
46
 * Haciendo doble click o arrastrando un elemento del men? a la lista este queda a?adido
47
 * en esta. Haciendo doble click en un elemento de la lista se elimina de esta y arrastrando
48
 * elementos dentro de la lista se varia su posici?n en ella.
49
 * Nacho Brodin (brodin_ign@gva.es)
50
 */
51

    
52
public class TreeListContainer extends JPanel implements ActionListener {
53
        private static final long serialVersionUID = 6665259638830401366L;
54
        private Hashtable        map;
55
        
56
        private JScrollPane pTree = null;
57
        private JScrollPane pList = null;
58
        private JTree tree = null;
59
        private JList list = null;
60
        private JButton bAdd = null;
61
        private JButton bDel = null;
62
        private DefaultMutableTreeNode raiz = null;
63
        private TreeListListener listener = null;
64
        private ArrayList                listListeners = new ArrayList();
65
        private JSplitPane jSplitPane1 = null;
66
        private JPanel jPanelButtons = null;
67

    
68
        /**
69
         * This method initializes 
70
         */
71
        public TreeListContainer() {
72
                super();
73
                listener = new TreeListListener();
74
                initialize();
75
                listener.setList(getList());
76
                listener.setTree(getTree());
77
                this.setPreferredSize(new Dimension(160, 0));
78
        }
79

    
80
        /**
81
         * This method initializes this
82
         */
83
        private void initialize() {
84
                map = new Hashtable();
85
                raiz =  new DefaultMutableTreeNode(Messages.getText("filtros"));
86
                setLayout(new BorderLayout());
87
                
88
                getPList().setMinimumSize(new Dimension(0, 60));
89
                jSplitPane1 = new JSplitPane();
90
                jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
91
                jSplitPane1.setTopComponent(getPTree());
92
                jSplitPane1.setRightComponent(getPList());
93
                jSplitPane1.setResizeWeight(1.0);
94
                jSplitPane1.setContinuousLayout(true);
95
//                jSplitPane1.setOneTouchExpandable(true);
96
                this.add(jSplitPane1, BorderLayout.CENTER);
97
                this.add(getJPanelButtons(), BorderLayout.SOUTH);
98
        }
99

    
100
        private JPanel getJPanelButtons() {
101
                if (jPanelButtons == null) {
102
                        jPanelButtons = new JPanel();
103
                        jPanelButtons.setPreferredSize(new Dimension(0, 30));
104
                        bAdd = new JButton("+");
105
                        bDel = new JButton("-");
106
                        jPanelButtons.add(bAdd);
107
                        jPanelButtons.add(bDel);
108
                        bAdd.addActionListener(this);
109
                        bDel.addActionListener(this);
110
                }
111
                return jPanelButtons;
112
        }
113
        
114
        /**
115
         * This method initializes jPanel        
116
         *         
117
         * @return javax.swing.JPanel        
118
         */
119
        private JScrollPane getPTree() {
120
                if (pTree == null) {
121
                        pTree = new JScrollPane();
122
                        pTree.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
123
                        pTree.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
124
                        pTree.setViewportView(getTree());
125
                }
126
                return pTree;
127
        }
128

    
129
        /**
130
         * This method initializes jPanel1        
131
         *         
132
         * @return javax.swing.JPanel        
133
         */
134
        private JScrollPane getPList() {
135
                if (pList == null) {
136
                        pList = new JScrollPane();
137
                        pList.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
138
                        pList.setBackground(java.awt.Color.white);
139
                        pList.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
140
                        pList.setViewportView(getList());
141
                }
142
                return pList;
143
        }
144

    
145
        /**
146
         * This method initializes jTree        
147
         *         
148
         * @return javax.swing.JTree        
149
         */
150
        public JTree getTree() {
151
                if (tree == null) {
152
                        tree = new JTree(raiz);
153
                        tree.addMouseListener(listener);
154
                        tree.addMouseMotionListener(listener);
155
                }
156
                return tree;
157
        }
158

    
159
        /**
160
         * This method initializes jList        
161
         *         
162
         * @return javax.swing.JList        
163
         */
164
        public JList getList() {
165
                if (list == null) {
166
                        DefaultListModel m = new DefaultListModel(); 
167
                        list = new JList(m);
168
                        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
169
                        list.addMouseListener(listener);
170
                        list.addMouseMotionListener(listener);
171
                }
172
                return list;
173
        }
174
        
175
        //-------------------------------------------
176
        //M?TODOS DE ARBOL
177
        
178
        /**
179
         * A?ade una nueva categoria al arbol
180
         * @param name        Etiqueta que aparece en el arbol. 
181
         * @param pos        Posici?n en el arbol de la nueva categoria
182
         */
183
        public void addClass(String name, int pos){
184
                DefaultTreeModel model =(DefaultTreeModel)tree.getModel();
185
                DefaultMutableTreeNode r = new DefaultMutableTreeNode( name );
186
                model.insertNodeInto(r, raiz, pos);
187
        }
188
        
189
        /**
190
         * A?ade una entrada a una categoria
191
         * @param name Nombre de la entrada a a?adir
192
         * @param parentName Categoria a la que a?adimos
193
         * @param value Valor asociado a la entrada
194
         */
195
        public void addEntry(String name, String parentName, String value){
196
                DefaultTreeModel model =(DefaultTreeModel)tree.getModel();
197
                for(int i = 0; i < model.getChildCount(raiz); i++){
198
                        if(model.getChild(raiz, i).toString().equals(parentName)){
199
                                DefaultMutableTreeNode node = (DefaultMutableTreeNode)model.getChild(raiz, i);
200
                                node.add(new DefaultMutableTreeNode(name));
201
                                if (value!=null)
202
                                        map.put(name,value);
203
                        }
204
                }
205
        }
206

    
207
        /**
208
         * M?todo que comprueba si una entrada existe en la lista.
209
         * @param value Valor que se quiere comprobar si est? en la lista
210
         * @return true si el valor est? en la lista y false si no lo est?
211
         */
212
        public boolean isInList(String value){
213
                DefaultListModel model = (DefaultListModel)getList().getModel();
214
                for(int i = 0; i < model.getSize(); i++)
215
                        if(((String)model.get(i)).equals(value))
216
                                return true;
217
                return false;
218
        }
219
        
220
        /**
221
         * A?ade un listener TreeListComponent
222
         * @param e
223
         */
224
        public void addTreeListListener(TreeListComponentListener e){
225
                listListeners.add(e);
226
                listener.setListeners(listListeners);
227
        }
228
        
229
        /**
230
         * A?ade un elemento a la lista 
231
         * @param element Elemento a a?adir
232
         */
233
        public void addElementInList(String element){
234
                DefaultListModel model = (DefaultListModel)getList().getModel();        
235
                model.addElement(element);
236
        }
237
        
238
        /**
239
         * Elimina un elemento a la lista 
240
         * @param element Elemento a eliminar
241
         */
242
        public void removeElementInList(String element){
243
                DefaultListModel model = (DefaultListModel)getList().getModel();        
244
                model.removeElement(element);
245
        }
246
        
247
        /**
248
         * Elimina un elemento a la lista por indice 
249
         * @param element Indice del elemento a eliminar
250
         */
251
        public void removeElementInList(int element){
252
                DefaultListModel model = (DefaultListModel)getList().getModel();        
253
                model.remove(element);
254
        }
255

    
256
        public Hashtable getMap() {
257
                return map;
258
        }
259

    
260
        public void setMap(Hashtable map) {
261
                this.map = map;
262
        }
263

    
264
        public void actionPerformed(ActionEvent e) {
265
                if (e.getSource() == bAdd) {
266
                        listener.insertElement();
267
                }
268
                if (e.getSource() == bDel) {
269
                        listener.deleteElement();
270
                }
271
        }
272
}