root / branches / F2 / extensions / extJCRS / src / org / gvsig / crs / gui / panels / TreePanel.java @ 10788
History | View | Annotate | Download (5.77 KB)
1 | 10301 | dguerrero | /* 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 | 10786 | lmfernandez | import java.awt.BorderLayout; |
44 | 10301 | dguerrero | import java.util.Hashtable; |
45 | |||
46 | import javax.swing.JPanel; |
||
47 | import javax.swing.JScrollPane; |
||
48 | import javax.swing.JTextArea; |
||
49 | import javax.swing.JTree; |
||
50 | 10786 | lmfernandez | import javax.swing.border.EmptyBorder; |
51 | 10301 | dguerrero | import javax.swing.tree.DefaultMutableTreeNode; |
52 | import javax.swing.tree.DefaultTreeModel; |
||
53 | |||
54 | /**
|
||
55 | *
|
||
56 | * @author Diego Guerrero (diego.guerrero@uclm.es)
|
||
57 | *
|
||
58 | */
|
||
59 | public class TreePanel extends JPanel { |
||
60 | 10786 | lmfernandez | |
61 | private static final long serialVersionUID = 1L; |
||
62 | 10301 | dguerrero | private int wComp = 190; |
63 | private int hComp = 360; |
||
64 | private int hTree = (int)Math.floor(hComp * 0.68); |
||
65 | 10786 | lmfernandez | private int hList = 50; |
66 | 10301 | dguerrero | 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 | 10786 | lmfernandez | this.setLayout(new BorderLayout()); |
85 | this.setBorder(new EmptyBorder(0,10,10,10)); |
||
86 | this.add(getPTree(),BorderLayout.CENTER); |
||
87 | this.add(getPList(),BorderLayout.SOUTH); |
||
88 | 10301 | dguerrero | } |
89 | |||
90 | /**
|
||
91 | * This method initializes jPanel
|
||
92 | *
|
||
93 | * @return javax.swing.JPanel
|
||
94 | */
|
||
95 | private JScrollPane getPTree() { |
||
96 | if (pTree == null) { |
||
97 | pTree = new JScrollPane(); |
||
98 | 10786 | lmfernandez | //pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree));
|
99 | 10301 | dguerrero | pTree.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
100 | pTree.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
||
101 | pTree.setViewportView(getTree()); |
||
102 | } |
||
103 | return pTree;
|
||
104 | } |
||
105 | |||
106 | /**
|
||
107 | * This method initializes jPanel1
|
||
108 | *
|
||
109 | * @return javax.swing.JPanel
|
||
110 | */
|
||
111 | private JScrollPane getPList() { |
||
112 | if (pList == null) { |
||
113 | pList = new JScrollPane(); |
||
114 | 10786 | lmfernandez | //pList.setPreferredSize(new java.awt.Dimension(wComp, hList));
|
115 | 10301 | dguerrero | pList.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED)); |
116 | pList.setBackground(java.awt.Color.white); |
||
117 | pList.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
||
118 | pList.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
||
119 | pList.setViewportView(getList()); |
||
120 | } |
||
121 | return pList;
|
||
122 | } |
||
123 | |||
124 | /**
|
||
125 | * This method initializes jTree
|
||
126 | *
|
||
127 | * @return javax.swing.JTree
|
||
128 | */
|
||
129 | public JTree getTree() { |
||
130 | if (tree == null) { |
||
131 | tree = new JTree(raiz); |
||
132 | } |
||
133 | return tree;
|
||
134 | } |
||
135 | |||
136 | /**
|
||
137 | * This method initializes jList
|
||
138 | *
|
||
139 | * @return javax.swing.JList
|
||
140 | */
|
||
141 | public JTextArea getList() { |
||
142 | if (list == null) { |
||
143 | list = new JTextArea(); |
||
144 | list.setLineWrap(true);
|
||
145 | list.setWrapStyleWord(true);
|
||
146 | list.setEditable(false);
|
||
147 | } |
||
148 | return list;
|
||
149 | } |
||
150 | |||
151 | public void setPanelSize(int w, int h){ |
||
152 | wComp = w; |
||
153 | hComp = h; |
||
154 | hTree = (int)Math.floor(hComp * 0.68); |
||
155 | hList = hComp - hTree; |
||
156 | this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
||
157 | setPreferredSize(new java.awt.Dimension(wComp, hComp));
|
||
158 | pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree));
|
||
159 | pList.setPreferredSize(new java.awt.Dimension(wComp, hList));
|
||
160 | } |
||
161 | |||
162 | /**
|
||
163 | * A?ade una nueva categoria al arbol
|
||
164 | * @param name Etiqueta que aparece en el arbol.
|
||
165 | * @param pos Posici?n en el arbol de la nueva categoria
|
||
166 | */
|
||
167 | public void addClass(String name, int pos){ |
||
168 | DefaultTreeModel model =(DefaultTreeModel)tree.getModel(); |
||
169 | DefaultMutableTreeNode r = new DefaultMutableTreeNode( name ); |
||
170 | model.insertNodeInto(r, raiz, pos); |
||
171 | } |
||
172 | |||
173 | /**
|
||
174 | * A?ade una entrada a una categoria
|
||
175 | * @param name Nombre de la entrada a a?adir
|
||
176 | * @param parentName Categoria a la que a?adimos
|
||
177 | * @param value Valor asociado a la entrada
|
||
178 | */
|
||
179 | public void addEntry(String name, String parentName, String value){ |
||
180 | DefaultTreeModel model =(DefaultTreeModel)tree.getModel(); |
||
181 | for(int i = 0; i < model.getChildCount(raiz); i++){ |
||
182 | if(model.getChild(raiz, i).toString().equals(parentName)){
|
||
183 | DefaultMutableTreeNode node = (DefaultMutableTreeNode)model.getChild(raiz, i); |
||
184 | node.add(new DefaultMutableTreeNode(name)); |
||
185 | if (value!=null) |
||
186 | map.put(name,value); |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | public void setRoot(String name){ |
||
192 | DefaultTreeModel model =(DefaultTreeModel)tree.getModel(); |
||
193 | //for (int index = 0; index<model.getChildCount(model.getRoot());index++)
|
||
194 | // model.removeNodeFromParent((DefaultMutableTreeNode)model.getChild(raiz, index));
|
||
195 | rootName=name; |
||
196 | raiz = new DefaultMutableTreeNode(rootName); |
||
197 | model.setRoot(raiz); |
||
198 | map.clear(); |
||
199 | |||
200 | } |
||
201 | } |