svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / treelist / TreeListContainer.java @ 13191
History | View | Annotate | Download (11 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.FlowLayout; |
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Hashtable; |
28 |
import java.util.Iterator; |
29 |
|
30 |
import javax.swing.DefaultListModel; |
31 |
import javax.swing.ImageIcon; |
32 |
import javax.swing.JButton; |
33 |
import javax.swing.JList; |
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.JScrollPane; |
36 |
import javax.swing.JSplitPane; |
37 |
import javax.swing.JTree; |
38 |
import javax.swing.ListModel; |
39 |
import javax.swing.ListSelectionModel; |
40 |
import javax.swing.event.ListSelectionEvent; |
41 |
import javax.swing.event.ListSelectionListener; |
42 |
import javax.swing.event.TreeSelectionEvent; |
43 |
import javax.swing.event.TreeSelectionListener; |
44 |
import javax.swing.tree.DefaultMutableTreeNode; |
45 |
import javax.swing.tree.DefaultTreeModel; |
46 |
import javax.swing.tree.TreePath; |
47 |
|
48 |
import org.gvsig.gui.beans.messages.Messages; |
49 |
import org.gvsig.gui.beans.treelist.event.TreeListChangeEvent; |
50 |
import org.gvsig.gui.beans.treelist.listeners.TreeListChangeListener; |
51 |
import org.gvsig.gui.beans.treelist.listeners.TreeListComponentListener; |
52 |
import org.gvsig.gui.beans.treelist.listeners.TreeListListener; |
53 |
/**
|
54 |
* Componente consistente en un men? de arbol al que se le pueden a?adir
|
55 |
* entradas y una lista de elementos debajo de este. Haciendo doble click o
|
56 |
* arrastrando un elemento del men? a la lista este queda a?adido en esta.
|
57 |
* Haciendo doble click en un elemento de la lista se elimina de esta y
|
58 |
* arrastrando elementos dentro de la lista se varia su posici?n en ella.
|
59 |
*
|
60 |
* @version 31/05/2007
|
61 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
62 |
*/
|
63 |
public class TreeListContainer extends JPanel implements ActionListener, TreeSelectionListener, ListSelectionListener { |
64 |
private static final long serialVersionUID = 6665259638830401366L; |
65 |
private ArrayList listListeners = new ArrayList(); |
66 |
|
67 |
private Hashtable map = null; |
68 |
|
69 |
// Componentes visuales
|
70 |
private JScrollPane pTree = null; |
71 |
private JScrollPane pList = null; |
72 |
private JTree tree = null; |
73 |
private JList list = null; |
74 |
private JButton bAdd = null; |
75 |
private JButton bDel = null; |
76 |
private JSplitPane jSplitPane1 = null; |
77 |
private JPanel jPanelButtons = null; |
78 |
|
79 |
private DefaultMutableTreeNode raiz = null; |
80 |
private TreeListListener listener = null; |
81 |
private String pathToImages = "images/"; |
82 |
|
83 |
/**
|
84 |
* This method initializes
|
85 |
*/
|
86 |
public TreeListContainer() {
|
87 |
super();
|
88 |
listener = new TreeListListener();
|
89 |
initialize(); |
90 |
listener.setList(getList()); |
91 |
listener.setTree(getTree()); |
92 |
this.setPreferredSize(new Dimension(160, 0)); |
93 |
getTree().addTreeSelectionListener(this);
|
94 |
getList().addListSelectionListener(this);
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* This method initializes this
|
99 |
*/
|
100 |
private void initialize() { |
101 |
map = new Hashtable(); |
102 |
raiz = new DefaultMutableTreeNode(Messages.getText("filtros")); |
103 |
setLayout(new BorderLayout()); |
104 |
|
105 |
getPList().setMinimumSize(new Dimension(0, 60)); |
106 |
jSplitPane1 = new JSplitPane(); |
107 |
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); |
108 |
jSplitPane1.setTopComponent(getPTree()); |
109 |
jSplitPane1.setBottomComponent(getPList()); |
110 |
jSplitPane1.setResizeWeight(1.0);
|
111 |
jSplitPane1.setContinuousLayout(true);
|
112 |
this.add(jSplitPane1, BorderLayout.CENTER); |
113 |
this.add(getJPanelButtons(), BorderLayout.SOUTH); |
114 |
} |
115 |
|
116 |
private JPanel getJPanelButtons() { |
117 |
if (jPanelButtons == null) { |
118 |
jPanelButtons = new JPanel(); |
119 |
jPanelButtons.setPreferredSize(new Dimension(0, 21)); |
120 |
bAdd = new JButton(); |
121 |
bAdd.setIcon(new ImageIcon(getClass().getResource(pathToImages + "addlayer.png"))); |
122 |
bAdd.setPreferredSize(new Dimension(22, 19)); |
123 |
|
124 |
bDel = new JButton(); |
125 |
bDel.setIcon(new ImageIcon(getClass().getResource(pathToImages + "delall.png"))); |
126 |
bDel.setPreferredSize(new Dimension(22, 19)); |
127 |
|
128 |
|
129 |
FlowLayout flowLayout5 = new FlowLayout(); |
130 |
flowLayout5.setHgap(5);
|
131 |
flowLayout5.setVgap(0);
|
132 |
flowLayout5.setAlignment(java.awt.FlowLayout.CENTER); |
133 |
jPanelButtons.setLayout(flowLayout5); |
134 |
|
135 |
jPanelButtons.add(bAdd, null);
|
136 |
jPanelButtons.add(bDel, null);
|
137 |
bAdd.addActionListener(this);
|
138 |
bDel.addActionListener(this);
|
139 |
} |
140 |
return jPanelButtons;
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* This method initializes jPanel
|
145 |
*
|
146 |
* @return javax.swing.JPanel
|
147 |
*/
|
148 |
private JScrollPane getPTree() { |
149 |
if (pTree == null) { |
150 |
pTree = new JScrollPane(); |
151 |
pTree.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
152 |
pTree.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
153 |
pTree.setViewportView(getTree()); |
154 |
} |
155 |
return pTree;
|
156 |
} |
157 |
|
158 |
/**
|
159 |
* This method initializes jPanel1
|
160 |
*
|
161 |
* @return javax.swing.JPanel
|
162 |
*/
|
163 |
private JScrollPane getPList() { |
164 |
if (pList == null) { |
165 |
pList = new JScrollPane(); |
166 |
pList.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED)); |
167 |
pList.setBackground(java.awt.Color.white); |
168 |
pList.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
169 |
pList.setViewportView(getList()); |
170 |
} |
171 |
return pList;
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* This method initializes jTree
|
176 |
*
|
177 |
* @return javax.swing.JTree
|
178 |
*/
|
179 |
public JTree getTree() { |
180 |
if (tree == null) { |
181 |
tree = new JTree(raiz); |
182 |
tree.addMouseListener(listener); |
183 |
tree.addMouseMotionListener(listener); |
184 |
} |
185 |
return tree;
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* This method initializes jList
|
190 |
*
|
191 |
* @return javax.swing.JList
|
192 |
*/
|
193 |
public JList getList() { |
194 |
if (list == null) { |
195 |
DefaultListModel m = new DefaultListModel(); |
196 |
list = new JList(m); |
197 |
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
198 |
list.addMouseListener(listener); |
199 |
list.addMouseMotionListener(listener); |
200 |
} |
201 |
return list;
|
202 |
} |
203 |
|
204 |
public ListModel getListModel() { |
205 |
return list.getModel();
|
206 |
} |
207 |
|
208 |
//-------------------------------------------
|
209 |
//M?TODOS DE ARBOL
|
210 |
|
211 |
/**
|
212 |
* A?ade una nueva categoria al arbol
|
213 |
* @param name Etiqueta que aparece en el arbol.
|
214 |
* @param pos Posici?n en el arbol de la nueva categoria
|
215 |
*/
|
216 |
public void addClass(String name, int pos) { |
217 |
DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); |
218 |
DefaultMutableTreeNode r = new DefaultMutableTreeNode(name); |
219 |
model.insertNodeInto(r, raiz, pos); |
220 |
} |
221 |
|
222 |
/**
|
223 |
* A?ade una entrada a una categoria
|
224 |
* @param name Nombre de la entrada a a?adir
|
225 |
* @param parentName Categoria a la que a?adimos
|
226 |
* @param value Valor asociado a la entrada
|
227 |
*/
|
228 |
public void addEntry(String name, String parentName, String value){ |
229 |
DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); |
230 |
for (int i = 0; i < model.getChildCount(raiz); i++) { |
231 |
if (model.getChild(raiz, i).toString().equals(parentName)) {
|
232 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getChild(raiz, i); |
233 |
node.add(new DefaultMutableTreeNode(name)); |
234 |
if (value != null) |
235 |
map.put(name, value); |
236 |
} |
237 |
} |
238 |
} |
239 |
|
240 |
/**
|
241 |
* M?todo que comprueba si una entrada existe en la lista de seleccion.
|
242 |
* @param value Valor que se quiere comprobar si est? en la lista
|
243 |
* @return true si el valor est? en la lista y false si no lo est?
|
244 |
*/
|
245 |
public boolean isInList(String value) { |
246 |
DefaultListModel model = (DefaultListModel) getList().getModel(); |
247 |
for (int i = 0; i < model.getSize(); i++) |
248 |
if (((String) model.get(i)).equals(value)) |
249 |
return true; |
250 |
return false; |
251 |
} |
252 |
|
253 |
/**
|
254 |
* A?ade un listener TreeListComponent
|
255 |
* @param e
|
256 |
*/
|
257 |
public void addTreeListListener(TreeListComponentListener e){ |
258 |
listListeners.add(e); |
259 |
listener.setListeners(listListeners); |
260 |
} |
261 |
|
262 |
/**
|
263 |
* A?ade un elemento a la lista
|
264 |
* @param element Elemento a a?adir
|
265 |
*/
|
266 |
public void addElementInList(String element){ |
267 |
DefaultListModel model = (DefaultListModel)getList().getModel(); |
268 |
model.addElement(element); |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Elimina un elemento a la lista
|
273 |
* @param element Elemento a eliminar
|
274 |
*/
|
275 |
public void removeElementInList(String element){ |
276 |
DefaultListModel model = (DefaultListModel)getList().getModel(); |
277 |
model.removeElement(element); |
278 |
} |
279 |
|
280 |
/**
|
281 |
* Elimina un elemento a la lista por indice
|
282 |
* @param element Indice del elemento a eliminar
|
283 |
*/
|
284 |
public void removeElementInList(int element){ |
285 |
DefaultListModel model = (DefaultListModel) getList().getModel(); |
286 |
model.remove(element); |
287 |
} |
288 |
|
289 |
public Hashtable getMap() { |
290 |
return map;
|
291 |
} |
292 |
|
293 |
public void setMap(Hashtable map) { |
294 |
this.map = map;
|
295 |
} |
296 |
|
297 |
public void actionPerformed(ActionEvent e) { |
298 |
if (e.getSource() == bAdd) {
|
299 |
listener.insertElement(); |
300 |
} |
301 |
if (e.getSource() == bDel) {
|
302 |
listener.deleteElement(); |
303 |
} |
304 |
} |
305 |
|
306 |
/**
|
307 |
* A?adir el disparador de cuando se pulsa un bot?n.
|
308 |
* @param listener
|
309 |
*/
|
310 |
private ArrayList actionChangeListeners = new ArrayList(); |
311 |
|
312 |
public void addChangeSelectionListener(TreeListChangeListener listener) { |
313 |
if (!actionChangeListeners.contains(listener))
|
314 |
actionChangeListeners.add(listener); |
315 |
} |
316 |
|
317 |
/**
|
318 |
* Borrar el disparador de eventos de los botones.
|
319 |
* @param listener
|
320 |
*/
|
321 |
public void removeChangeSelectionListener(TreeListChangeListener listener) { |
322 |
actionChangeListeners.remove(listener); |
323 |
} |
324 |
|
325 |
private void callActionChangeListeners(String item) { |
326 |
Iterator acIterator = actionChangeListeners.iterator();
|
327 |
while (acIterator.hasNext()) {
|
328 |
TreeListChangeListener listener = (TreeListChangeListener) acIterator.next(); |
329 |
String name = (String) map.get(item); |
330 |
if (name != null) |
331 |
listener.actionChangeSelection(new TreeListChangeEvent(this, name)); |
332 |
else
|
333 |
listener.actionChangeSelection(new TreeListChangeEvent(this, item)); |
334 |
} |
335 |
} |
336 |
|
337 |
public void valueChanged(TreeSelectionEvent e) { |
338 |
String draggLabel = null; |
339 |
if (e.getSource() == tree) {
|
340 |
TreePath[] tp = tree.getSelectionPaths(); |
341 |
if (tp != null) { |
342 |
String val = tp[0].toString(); |
343 |
// Comprobamos que se haya pinchado sobre una rama
|
344 |
String[] pathTree = val.split(", "); |
345 |
if (pathTree.length > 2) { |
346 |
draggLabel = val.substring(val.lastIndexOf(", ") + 2, val.lastIndexOf("]")); |
347 |
callActionChangeListeners(draggLabel); |
348 |
} |
349 |
} |
350 |
} |
351 |
} |
352 |
|
353 |
public void valueChanged(ListSelectionEvent e) { |
354 |
if (!e.getValueIsAdjusting()) {
|
355 |
if (getList().getSelectedValue() != null) |
356 |
callActionChangeListeners(getList().getSelectedValue().toString()); |
357 |
} |
358 |
} |
359 |
} |