01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
05: * (C) 2003, Refractions Reserach Inc.
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation;
10: * version 2.1 of the License.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: */
17: package org.geotools.graph.util;
18:
19: import java.awt.event.MouseAdapter;
20: import java.awt.event.MouseEvent;
21: import java.util.ArrayList;
22: import java.util.Collection;
23: import java.util.List;
24:
25: import javax.swing.AbstractListModel;
26: import javax.swing.JList;
27: import javax.swing.ListModel;
28: import javax.swing.event.ListSelectionEvent;
29: import javax.swing.event.ListSelectionListener;
30:
31: public class SwingUtil {
32:
33: public static ListModel toListModel(final List elements) {
34: return (new AbstractListModel() {
35: public int getSize() {
36: return (elements.size());
37: }
38:
39: public Object getElementAt(int index) {
40: return (elements.get(index));
41: }
42: });
43: }
44:
45: public static ListModel toListModel(Collection elements) {
46: return (toListModel(new ArrayList(elements)));
47: }
48:
49: public static List toList(ListModel model) {
50: ArrayList list = new ArrayList(model.getSize());
51: for (int i = 0; i < model.getSize(); i++) {
52: list.add(model.getElementAt(i));
53: }
54:
55: return (list);
56: }
57:
58: public static void setSelection(JList list, Object element) {
59: for (int i = 0; i < list.getModel().getSize(); i++) {
60: Object value = (Object) list.getModel().getElementAt(i);
61: if (value == element) {
62: list.setSelectedIndex(i);
63: list.scrollRectToVisible(list.getCellBounds(i, i));
64: return;
65: }
66: }
67: }
68:
69: public static void addDoubleClickEvent(JList list) {
70: list.addMouseListener(new MouseAdapter() {
71: public void mouseClicked(MouseEvent e) {
72: JList source = (JList) e.getSource();
73: if (e.getClickCount() == 2) {
74: ListSelectionListener[] listeners = source
75: .getListSelectionListeners();
76: for (int i = 0; i < listeners.length; i++) {
77: listeners[i]
78: .valueChanged(new ListSelectionEvent(
79: source, source
80: .getSelectedIndex(),
81: source.getSelectedIndex(),
82: false));
83: }
84: }
85: }
86: });
87: }
88: }
|