001 /* $Id: ListPicker.java 12 2007-07-27 09:48:53Z ringler $
002 * This class is part of the de.mospace.swing library.
003 * Copyright (C) 2005-2006 Moritz Ringler
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019 package de.mospace.swing;
020
021 import java.awt.BorderLayout;
022 import java.awt.Component;
023 import java.awt.Container;
024 import java.awt.Dialog;
025 import java.awt.Dimension;
026 import java.awt.Frame;
027 import java.awt.Point;
028 import java.awt.event.ActionEvent;
029 import java.awt.event.ActionListener;
030 import java.util.Collection;
031 import java.util.Vector;
032 import javax.swing.DefaultComboBoxModel;
033 import javax.swing.JButton;
034 import javax.swing.JDialog;
035 import javax.swing.JList;
036 import javax.swing.JPanel;
037 import javax.swing.JScrollPane;
038 import javax.swing.AbstractAction;
039 import javax.swing.Action;
040 import javax.swing.KeyStroke;
041 import javax.swing.ListCellRenderer;
042 import javax.swing.ListModel;
043 import javax.swing.UIManager;
044
045 public class ListPicker extends JDialog{
046 private ListModel model;
047 private final JList list = new JList();
048 private Object value;
049 private final Container customButtons = new JPanel();
050
051 public ListPicker(Frame f, Object[] obj){
052 super(f);
053 model = new DefaultComboBoxModel(obj);
054 init(f);
055 }
056
057 public ListPicker(Dialog d, Object[] obj){
058 super(d);
059 model = new DefaultComboBoxModel(obj);
060 init(null);
061 }
062
063 public ListPicker(Frame f, Collection c){
064 super(f);
065 model = makeModel(c);
066 init(f);
067 }
068
069 public ListPicker(Dialog d, Collection c){
070 super(d);
071 model = makeModel(c);
072 init(null);
073 }
074
075 private ListModel makeModel(Collection c){
076 Vector v = null;
077 if(c instanceof Vector){
078 v = (Vector) c;
079 } else {
080 v = new Vector(c.size());
081 v.addAll(c);
082 }
083 return new DefaultComboBoxModel(v);
084 }
085
086 private void init(Frame f){
087 setModal(true);
088 JPanel panel = new JPanel(new BorderLayout());
089
090 list.setModel(model);
091 list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
092 Action okAction = new AbstractAction(UIManager.getString("OptionPane.okButtonText")){
093 public void actionPerformed(ActionEvent e){
094 value = list.getSelectedValue();
095 if(value != null){
096 ListPicker.this.setVisible(false);
097 }
098 }
099 };
100 list.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"commit");
101 list.getActionMap().put("commit", okAction);
102
103
104 JScrollPane sp = new JScrollPane(list);
105 panel.add(sp, BorderLayout.CENTER);
106
107 JButton buttonOK = new JButton(okAction);
108 JButton buttonCancel = new JButton();
109 buttonCancel.setText(UIManager.getString("OptionPane.cancelButtonText"));
110 buttonOK.addActionListener(new ActionListener(){
111 public void actionPerformed(ActionEvent e){
112 //do nothing
113 }
114 });
115 buttonCancel.addActionListener(new ActionListener(){
116 public void actionPerformed(ActionEvent e){
117 value = null;
118 ListPicker.this.setVisible(false);
119 }
120 });
121 JPanel mainButtons = new JPanel();
122 JPanel buttons = new JPanel(new BorderLayout());
123 buttons.add(mainButtons, BorderLayout.CENTER);
124 buttons.add(customButtons, BorderLayout.SOUTH);
125 mainButtons.add(buttonCancel);
126 mainButtons.add(buttonOK);
127 panel.add(buttons, BorderLayout.SOUTH);
128 panel.setPreferredSize(new Dimension(500,500));
129 setContentPane(panel);
130 pack();
131 Dimension dlgSize = getPreferredSize();
132 if(f != null){
133 Dimension frmSize = f.getSize();
134 Point loc = f.getLocation();
135 setLocation((frmSize.width - dlgSize.width) / 2 +
136 loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
137 }
138 if(model.getSize() > 0){
139 list.setSelectedIndex(0);
140 }
141 }
142
143 public void addButton(Component c){
144 customButtons.add(c);
145 }
146
147 public Object getValue(){
148 return value;
149 }
150
151
152 public void setValue(Object o){
153 value = o;
154 }
155
156 private static class PickerButton extends JButton{
157 final String bvalue;
158 final ListPicker picker;
159
160 public PickerButton(String title, String pvalue, ListPicker picker){
161 super(title);
162 this.bvalue = pvalue;
163 this.picker = picker;
164 addActionListener(new ActionListener(){
165 public void actionPerformed(ActionEvent e){
166 PickerButton.this.picker.setValue(bvalue);
167 PickerButton.this.picker.setVisible(false);
168 }
169 });
170 }
171 }
172
173 public void addPickerButton(String title, String pvalue){
174 JButton pb = new PickerButton(title, pvalue, this);
175 addButton(pb);
176 }
177
178 public void setVisible(boolean b){
179 super.setVisible(b);
180 if(b){
181 list.requestFocusInWindow();
182 }
183 }
184
185 public void setCellRenderer(ListCellRenderer renderer){
186 list.setCellRenderer(renderer);
187 }
188
189 private void setModel(){
190 list.setModel(model);
191 if(model.getSize() > 0){
192 list.setSelectedIndex(0);
193 }
194 }
195
196 public void setChoice(Object[] obj){
197 model = new DefaultComboBoxModel(obj);
198 setModel();
199 }
200
201 public void setChoice(Collection c){
202 model = makeModel(c);
203 setModel();
204 }
205 }