root / trunk / libraries / libUIComponent_praster / src / org / gvsig / gui / beans / treelist / TreeListContainer.java @ 8026
History | View | Annotate | Download (8.17 KB)
1 | 8026 | nacho | /* 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.FlowLayout; |
||
22 | import java.awt.GridBagConstraints; |
||
23 | import java.awt.GridBagLayout; |
||
24 | import java.util.ArrayList; |
||
25 | import java.util.Hashtable; |
||
26 | |||
27 | import javax.swing.DefaultListModel; |
||
28 | import javax.swing.JList; |
||
29 | import javax.swing.JPanel; |
||
30 | import javax.swing.JScrollPane; |
||
31 | import javax.swing.JTree; |
||
32 | import javax.swing.ListSelectionModel; |
||
33 | import javax.swing.tree.DefaultMutableTreeNode; |
||
34 | import javax.swing.tree.DefaultTreeModel; |
||
35 | |||
36 | import org.gvsig.gui.beans.BaseComponent; |
||
37 | import org.gvsig.gui.beans.treelist.listeners.TreeListComponentListener; |
||
38 | import org.gvsig.gui.beans.treelist.listeners.TreeListListener; |
||
39 | import org.gvsig.i18n.Messages; |
||
40 | |||
41 | /**
|
||
42 | * Componente consistente en un men? de arbol al que se le pueden a?adir entradas
|
||
43 | * y una lista de elementos debajo de este.
|
||
44 | * Haciendo doble click o arrastrando un elemento del men? a la lista este queda a?adido
|
||
45 | * en esta. Haciendo doble click en un elemento de la lista se elimina de esta y arrastrando
|
||
46 | * elementos dentro de la lista se varia su posici?n en ella.
|
||
47 | * Nacho Brodin (brodin_ign@gva.es)
|
||
48 | */
|
||
49 | |||
50 | public class TreeListContainer extends BaseComponent { |
||
51 | |||
52 | private int wComp = 190; |
||
53 | private int hComp = 360; |
||
54 | private int hTree = (int)Math.floor(hComp * 0.68); |
||
55 | private int hList = hComp - hTree; |
||
56 | private Hashtable map; |
||
57 | |||
58 | private JPanel pMain = null; |
||
59 | private JScrollPane pTree = null; |
||
60 | private JScrollPane pList = null; |
||
61 | private JTree tree = null; |
||
62 | private JList list = null; |
||
63 | private DefaultMutableTreeNode raiz = null; |
||
64 | private TreeListListener listener = null; |
||
65 | private ArrayList listListeners = new ArrayList(); |
||
66 | |||
67 | /**
|
||
68 | * This method initializes
|
||
69 | *
|
||
70 | */
|
||
71 | public TreeListContainer() {
|
||
72 | super();
|
||
73 | listener = new TreeListListener();
|
||
74 | initialize(); |
||
75 | listener.setList(getList()); |
||
76 | listener.setTree(getTree()); |
||
77 | } |
||
78 | |||
79 | /**
|
||
80 | * This method initializes this
|
||
81 | *
|
||
82 | */
|
||
83 | private void initialize() { |
||
84 | map = new Hashtable(); |
||
85 | raiz = new DefaultMutableTreeNode(Messages.getText("filtros")); |
||
86 | this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
||
87 | FlowLayout flowLayout = new FlowLayout(); |
||
88 | flowLayout.setHgap(0);
|
||
89 | flowLayout.setVgap(0);
|
||
90 | this.setLayout(flowLayout);
|
||
91 | this.add(getPMain(), null); |
||
92 | |||
93 | } |
||
94 | |||
95 | /**
|
||
96 | * This method initializes jPanel
|
||
97 | *
|
||
98 | * @return javax.swing.JPanel
|
||
99 | */
|
||
100 | private JPanel getPMain() { |
||
101 | if (pMain == null) { |
||
102 | GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
||
103 | gridBagConstraints1.gridx = 0;
|
||
104 | gridBagConstraints1.gridy = 1;
|
||
105 | GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
||
106 | gridBagConstraints.gridx = 0;
|
||
107 | gridBagConstraints.gridy = 0;
|
||
108 | pMain = new JPanel(); |
||
109 | pMain.setLayout(new GridBagLayout()); |
||
110 | pMain.setPreferredSize(new java.awt.Dimension(wComp,hComp));
|
||
111 | pMain.add(getPTree(), gridBagConstraints); |
||
112 | pMain.add(getPList(), gridBagConstraints1); |
||
113 | } |
||
114 | return pMain;
|
||
115 | } |
||
116 | |||
117 | /**
|
||
118 | * This method initializes jPanel
|
||
119 | *
|
||
120 | * @return javax.swing.JPanel
|
||
121 | */
|
||
122 | private JScrollPane getPTree() { |
||
123 | if (pTree == null) { |
||
124 | pTree = new JScrollPane(); |
||
125 | pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree));
|
||
126 | pTree.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
||
127 | pTree.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
||
128 | pTree.setViewportView(getTree()); |
||
129 | } |
||
130 | return pTree;
|
||
131 | } |
||
132 | |||
133 | /**
|
||
134 | * This method initializes jPanel1
|
||
135 | *
|
||
136 | * @return javax.swing.JPanel
|
||
137 | */
|
||
138 | private JScrollPane getPList() { |
||
139 | if (pList == null) { |
||
140 | pList = new JScrollPane(); |
||
141 | pList.setPreferredSize(new java.awt.Dimension(wComp, hList));
|
||
142 | pList.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED)); |
||
143 | pList.setBackground(java.awt.Color.white); |
||
144 | pList.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
||
145 | pList.setViewportView(getList()); |
||
146 | } |
||
147 | return pList;
|
||
148 | } |
||
149 | |||
150 | /**
|
||
151 | * This method initializes jTree
|
||
152 | *
|
||
153 | * @return javax.swing.JTree
|
||
154 | */
|
||
155 | public JTree getTree() { |
||
156 | if (tree == null) { |
||
157 | tree = new JTree(raiz); |
||
158 | tree.addMouseListener(listener); |
||
159 | tree.addMouseMotionListener(listener); |
||
160 | } |
||
161 | return tree;
|
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | * This method initializes jList
|
||
166 | *
|
||
167 | * @return javax.swing.JList
|
||
168 | */
|
||
169 | public JList getList() { |
||
170 | if (list == null) { |
||
171 | DefaultListModel m = new DefaultListModel(); |
||
172 | list = new JList(m); |
||
173 | list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||
174 | list.addMouseListener(listener); |
||
175 | list.addMouseMotionListener(listener); |
||
176 | } |
||
177 | return list;
|
||
178 | } |
||
179 | |||
180 | |||
181 | public void setComponentSize(int w, int h){ |
||
182 | wComp = w; |
||
183 | hComp = h; |
||
184 | hTree = (int)Math.floor(hComp * 0.68); |
||
185 | hList = hComp - hTree; |
||
186 | this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
||
187 | pMain.setPreferredSize(new java.awt.Dimension(wComp, hComp));
|
||
188 | pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree));
|
||
189 | pList.setPreferredSize(new java.awt.Dimension(wComp, hList));
|
||
190 | } |
||
191 | |||
192 | //-------------------------------------------
|
||
193 | //M?TODOS DE ARBOL
|
||
194 | |||
195 | /**
|
||
196 | * A?ade una nueva categoria al arbol
|
||
197 | * @param name Etiqueta que aparece en el arbol.
|
||
198 | * @param pos Posici?n en el arbol de la nueva categoria
|
||
199 | */
|
||
200 | public void addClass(String name, int pos){ |
||
201 | DefaultTreeModel model =(DefaultTreeModel)tree.getModel(); |
||
202 | DefaultMutableTreeNode r = new DefaultMutableTreeNode( name ); |
||
203 | model.insertNodeInto(r, raiz, pos); |
||
204 | } |
||
205 | |||
206 | /**
|
||
207 | * A?ade una entrada a una categoria
|
||
208 | * @param name Nombre de la entrada a a?adir
|
||
209 | * @param parentName Categoria a la que a?adimos
|
||
210 | * @param value Valor asociado a la entrada
|
||
211 | */
|
||
212 | public void addEntry(String name, String parentName, String value){ |
||
213 | DefaultTreeModel model =(DefaultTreeModel)tree.getModel(); |
||
214 | for(int i = 0; i < model.getChildCount(raiz); i++){ |
||
215 | if(model.getChild(raiz, i).toString().equals(parentName)){
|
||
216 | DefaultMutableTreeNode node = (DefaultMutableTreeNode)model.getChild(raiz, i); |
||
217 | node.add(new DefaultMutableTreeNode(name)); |
||
218 | if (value!=null) |
||
219 | map.put(name,value); |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /**
|
||
225 | * M?todo que comprueba si una entrada existe en la lista.
|
||
226 | * @param value Valor que se quiere comprobar si est? en la lista
|
||
227 | * @return true si el valor est? en la lista y false si no lo est?
|
||
228 | */
|
||
229 | public boolean isInList(String value){ |
||
230 | DefaultListModel model = (DefaultListModel)getList().getModel(); |
||
231 | for(int i = 0; i < model.getSize(); i++) |
||
232 | if(((String)model.get(i)).equals(value)) |
||
233 | return true; |
||
234 | return false; |
||
235 | } |
||
236 | |||
237 | /**
|
||
238 | * A?ade un listener TreeListComponent
|
||
239 | * @param e
|
||
240 | */
|
||
241 | public void addTreeListListener(TreeListComponentListener e){ |
||
242 | listListeners.add(e); |
||
243 | listener.setListeners(listListeners); |
||
244 | } |
||
245 | |||
246 | /**
|
||
247 | * A?ade un elemento a la lista
|
||
248 | * @param element Elemento a a?adir
|
||
249 | */
|
||
250 | public void addElementInList(String element){ |
||
251 | DefaultListModel model = (DefaultListModel)getList().getModel(); |
||
252 | model.addElement(element); |
||
253 | } |
||
254 | |||
255 | /**
|
||
256 | * Elimina un elemento a la lista
|
||
257 | * @param element Elemento a eliminar
|
||
258 | */
|
||
259 | public void removeElementInList(String element){ |
||
260 | DefaultListModel model = (DefaultListModel)getList().getModel(); |
||
261 | model.removeElement(element); |
||
262 | } |
||
263 | |||
264 | /**
|
||
265 | * Elimina un elemento a la lista por indice
|
||
266 | * @param element Indice del elemento a eliminar
|
||
267 | */
|
||
268 | public void removeElementInList(int element){ |
||
269 | DefaultListModel model = (DefaultListModel)getList().getModel(); |
||
270 | model.remove(element); |
||
271 | } |
||
272 | |||
273 | public Hashtable getMap() { |
||
274 | return map;
|
||
275 | } |
||
276 | |||
277 | public void setMap(Hashtable map) { |
||
278 | this.map = map;
|
||
279 | } |
||
280 | } |