001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.addremove;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Color;
037: import java.awt.event.MouseListener;
038: import java.util.ArrayList;
039: import java.util.Arrays;
040: import java.util.Collection;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import javax.swing.DefaultListModel;
045: import javax.swing.JList;
046: import javax.swing.JPanel;
047: import javax.swing.border.Border;
048: import javax.swing.border.EtchedBorder;
049: import javax.swing.event.ListSelectionEvent;
050: import javax.swing.event.ListSelectionListener;
051:
052: import com.vividsolutions.jump.workbench.ui.InputChangedFirer;
053: import com.vividsolutions.jump.workbench.ui.InputChangedListener;
054: import com.vividsolutions.jump.workbench.ui.JListTypeAheadKeyListener;
055:
056: public class DefaultAddRemoveList extends JPanel implements
057: AddRemoveList {
058: private BorderLayout borderLayout1 = new BorderLayout();
059: private JList list = new JList();
060: private DefaultAddRemoveListModel model;
061: private InputChangedFirer inputChangedFirer = new InputChangedFirer();
062: private Border border1;
063:
064: public DefaultAddRemoveList() {
065: this (new DefaultListModel());
066: }
067:
068: public void add(MouseListener listener) {
069: list.addMouseListener(listener);
070: }
071:
072: public DefaultAddRemoveList(DefaultListModel listModel) {
073: model = new DefaultAddRemoveListModel(listModel);
074: list.setModel(listModel);
075: list.addKeyListener(new JListTypeAheadKeyListener(list));
076: list.getSelectionModel().addListSelectionListener(
077: new ListSelectionListener() {
078: public void valueChanged(ListSelectionEvent e) {
079: inputChangedFirer.fire();
080: }
081: });
082:
083: try {
084: jbInit();
085: } catch (Exception ex) {
086: ex.printStackTrace();
087: }
088: }
089:
090: public void setSelectedItems(Collection items) {
091: ArrayList indicesToSelect = new ArrayList();
092:
093: for (Iterator i = items.iterator(); i.hasNext();) {
094: Object item = (Object) i.next();
095: int index = getModel().getItems().indexOf(item);
096:
097: if (index == -1) {
098: continue;
099: }
100:
101: indicesToSelect.add(new Integer(index));
102: }
103:
104: int[] indexArray = new int[indicesToSelect.size()];
105:
106: for (int i = 0; i < indicesToSelect.size(); i++) {
107: Integer index = (Integer) indicesToSelect.get(i);
108: indexArray[i] = index.intValue();
109: }
110:
111: list.setSelectedIndices(indexArray);
112: }
113:
114: public AddRemoveListModel getModel() {
115: return model;
116: }
117:
118: public void add(InputChangedListener listener) {
119: inputChangedFirer.add(listener);
120: }
121:
122: public JList getList() {
123: return list;
124: }
125:
126: public List getSelectedItems() {
127: return Arrays.asList(list.getSelectedValues());
128: }
129:
130: void jbInit() throws Exception {
131: border1 = new EtchedBorder(EtchedBorder.RAISED, new Color(0, 0,
132: 51), new Color(0, 0, 25));
133: this.setLayout(borderLayout1);
134: this.add(list, BorderLayout.CENTER);
135: }
136: }
|