Statistics
| Revision:

root / branches / v10 / libraries / libJCRS / src / org / gvsig / crs / gui / panels / TreePanel.java @ 9061

History | View | Annotate | Download (6.16 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional 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
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs.gui.panels;
42

    
43
import java.awt.FlowLayout;
44
import java.awt.GridBagConstraints;
45
import java.awt.GridBagLayout;
46
import java.util.Hashtable;
47

    
48
import javax.swing.JPanel;
49
import javax.swing.JScrollPane;
50
import javax.swing.JTextArea;
51
import javax.swing.JTree;
52
import javax.swing.tree.DefaultMutableTreeNode;
53
import javax.swing.tree.DefaultTreeModel;
54

    
55
/**
56
 * 
57
 * @author Diego Guerrero (diego.guerrero@uclm.es)
58
 *
59
 */
60
public class TreePanel extends JPanel {
61
        
62
        private int                 wComp = 190;
63
        private int                 hComp = 360;
64
        private int                 hTree = (int)Math.floor(hComp * 0.68);
65
        private int                 hList = hComp - hTree;
66
        private JScrollPane pTree = null;
67
        private JScrollPane pList = null;
68
        private JTree tree = null;
69
        //private JList list = null;
70
        private JTextArea list = null;
71
        private DefaultMutableTreeNode raiz = null;
72
        private Hashtable        map;
73
        String rootName = "";
74

    
75
        public TreePanel(String rootName) {
76
                super();
77
                this.rootName = rootName;
78
                initialize();
79
        }
80
        
81
        private void initialize() {
82
                map = new Hashtable();
83
                raiz =  new DefaultMutableTreeNode(rootName);
84
        FlowLayout flowLayout = new FlowLayout();
85
        flowLayout.setHgap(0);
86
        flowLayout.setVgap(0);
87
        this.setLayout(flowLayout);
88
                
89
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
90
                gridBagConstraints1.gridx = 0;
91
                gridBagConstraints1.gridy = 1;
92
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
93
                gridBagConstraints.gridx = 0;
94
                gridBagConstraints.gridy = 0;
95
                setLayout(new GridBagLayout());
96
                setPreferredSize(new java.awt.Dimension(wComp,hComp));
97
                add(getPTree(), gridBagConstraints);
98
                add(getPList(), gridBagConstraints1);
99

    
100
        }
101
        
102
        /**
103
         * This method initializes jPanel        
104
         *         
105
         * @return javax.swing.JPanel        
106
         */
107
        private JScrollPane getPTree() {
108
                if (pTree == null) {
109
                        pTree = new JScrollPane();
110
                        pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree));
111
                        pTree.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
112
                        pTree.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
113
                        pTree.setViewportView(getTree());
114
                }
115
                return pTree;
116
        }
117

    
118
        /**
119
         * This method initializes jPanel1        
120
         *         
121
         * @return javax.swing.JPanel        
122
         */
123
        private JScrollPane getPList() {
124
                if (pList == null) {
125
                        pList = new JScrollPane();
126
                        pList.setPreferredSize(new java.awt.Dimension(wComp, hList));
127
                        pList.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
128
                        pList.setBackground(java.awt.Color.white);
129
                        pList.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
130
                        pList.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
131
                        pList.setViewportView(getList());
132
                }
133
                return pList;
134
        }
135

    
136
        /**
137
         * This method initializes jTree        
138
         *         
139
         * @return javax.swing.JTree        
140
         */
141
        public JTree getTree() {
142
                if (tree == null) {
143
                        tree = new JTree(raiz);
144
                }
145
                return tree;
146
        }
147

    
148
        /**
149
         * This method initializes jList        
150
         *         
151
         * @return javax.swing.JList        
152
         */
153
        public JTextArea getList() {
154
                if (list == null) {
155
                        list = new JTextArea();
156
                        list.setLineWrap(true);
157
                        list.setWrapStyleWord(true);
158
                        list.setEditable(false);
159
                }
160
                return list;
161
        }
162
        
163
        public void setPanelSize(int w, int h){
164
                wComp = w;
165
                hComp = h;
166
                hTree = (int)Math.floor(hComp * 0.68);
167
                hList = hComp - hTree;
168
        this.setPreferredSize(new java.awt.Dimension(wComp, hComp));
169
                setPreferredSize(new java.awt.Dimension(wComp, hComp));
170
                pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree));
171
                pList.setPreferredSize(new java.awt.Dimension(wComp, hList));
172
        }
173
        
174
        /**
175
         * A?ade una nueva categoria al arbol
176
         * @param name        Etiqueta que aparece en el arbol. 
177
         * @param pos        Posici?n en el arbol de la nueva categoria
178
         */
179
        public void addClass(String name, int pos){
180
                DefaultTreeModel model =(DefaultTreeModel)tree.getModel();
181
                DefaultMutableTreeNode r = new DefaultMutableTreeNode( name );
182
                model.insertNodeInto(r, raiz, pos);
183
        }
184
        
185
        /**
186
         * A?ade una entrada a una categoria
187
         * @param name Nombre de la entrada a a?adir
188
         * @param parentName Categoria a la que a?adimos
189
         * @param value Valor asociado a la entrada
190
         */
191
        public void addEntry(String name, String parentName, String value){
192
                DefaultTreeModel model =(DefaultTreeModel)tree.getModel();
193
                for(int i = 0; i < model.getChildCount(raiz); i++){
194
                        if(model.getChild(raiz, i).toString().equals(parentName)){
195
                                DefaultMutableTreeNode node = (DefaultMutableTreeNode)model.getChild(raiz, i);
196
                                node.add(new DefaultMutableTreeNode(name));
197
                                if (value!=null)
198
                                        map.put(name,value);
199
                        }
200
                }
201
        }
202
        
203
        public void setRoot(String name){
204
                DefaultTreeModel model =(DefaultTreeModel)tree.getModel();
205
                //for (int index = 0; index<model.getChildCount(model.getRoot());index++)
206
                //        model.removeNodeFromParent((DefaultMutableTreeNode)model.getChild(raiz, index));
207
                rootName=name;
208
                raiz =  new DefaultMutableTreeNode(rootName);
209
                model.setRoot(raiz);
210
                map.clear();
211
                
212
        }
213
}