View Javadoc

1   /***
2    * ListSelectionDialog.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2003 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.swing;
27  
28  import java.awt.Dialog;
29  import java.awt.FlowLayout;
30  import java.awt.Frame;
31  import java.awt.GraphicsConfiguration;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  
35  import javax.swing.Action;
36  import javax.swing.BoxLayout;
37  import javax.swing.DefaultComboBoxModel;
38  import javax.swing.JButton;
39  import javax.swing.JDialog;
40  import javax.swing.JList;
41  import javax.swing.JPanel;
42  import javax.swing.JScrollPane;
43  import javax.swing.ListCellRenderer;
44  
45  /***
46   * 
47   * @author $Author: dbregeon $
48   * @version $Revision: 1.1 $
49   */
50  public class ListSelectionDialog extends JDialog {
51      private final JList valuesList = new JList();
52  
53      private final JScrollPane valuesListPane = new JScrollPane();
54  
55      private final JButton okButton = new JButton("Ok");
56  
57      private final JButton cancelButton = new JButton("Cancel");
58  
59      private final JPanel buttonPanel = new JPanel();
60  
61      private final ActionListener actionListener = new ActionListener() {
62          public void actionPerformed(ActionEvent actionevent) {
63              setVisible(false);
64          }
65      };
66  
67      /***
68       * 
69       *  
70       */
71      public ListSelectionDialog() {
72          initComponents();
73      }
74  
75      /***
76       * 
77       * @param dialog
78       */
79      public ListSelectionDialog(Dialog dialog) {
80          super(dialog);
81          initComponents();
82      }
83  
84      /***
85       * 
86       * @param frame
87       */
88      public ListSelectionDialog(Frame frame) {
89          super(frame);
90          initComponents();
91      }
92  
93      /***
94       * 
95       * @param frame
96       * @param flag
97       */
98      public ListSelectionDialog(Frame frame, boolean flag) {
99          super(frame, flag);
100         initComponents();
101     }
102 
103     /***
104      * 
105      * @param frame
106      * @param s
107      */
108     public ListSelectionDialog(Frame frame, String s) {
109         super(frame, s);
110         initComponents();
111     }
112 
113     /***
114      * 
115      * @param frame
116      * @param s
117      * @param flag
118      */
119     public ListSelectionDialog(Frame frame, String s, boolean flag) {
120         super(frame, s, flag);
121         initComponents();
122     }
123 
124     /***
125      * 
126      * @param frame
127      * @param s
128      * @param flag
129      * @param graphicsconfiguration
130      */
131     public ListSelectionDialog(Frame frame, String s, boolean flag,
132             GraphicsConfiguration graphicsconfiguration) {
133         super(frame, s, flag, graphicsconfiguration);
134         initComponents();
135     }
136 
137     /***
138      * 
139      * @param dialog
140      * @param s
141      */
142     public ListSelectionDialog(Dialog dialog, String s) {
143         super(dialog, s);
144         initComponents();
145     }
146 
147     /***
148      * 
149      * @param dialog
150      * @param s
151      * @param flag
152      */
153     public ListSelectionDialog(Dialog dialog, String s, boolean flag) {
154         super(dialog, s, flag);
155         initComponents();
156     }
157 
158     /***
159      * 
160      * @param dialog
161      * @param s
162      * @param flag
163      * @param graphicsconfiguration
164      */
165     public ListSelectionDialog(Dialog dialog, String s, boolean flag,
166             GraphicsConfiguration graphicsconfiguration) {
167         super(dialog, s, flag, graphicsconfiguration);
168         initComponents();
169     }
170 
171     /***
172      * 
173      * @param dialog
174      * @param flag
175      */
176     public ListSelectionDialog(Dialog dialog, boolean flag) {
177         super(dialog, flag);
178         initComponents();
179     }
180 
181     protected final void initComponents() {
182         getContentPane().setLayout(
183                 new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
184         this.valuesList.setSelectionMode(2);
185         this.valuesListPane.setViewportView(this.valuesList);
186         getContentPane().add(this.valuesListPane);
187         this.buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
188         this.okButton.addActionListener(this.actionListener);
189         this.buttonPanel.add(this.okButton);
190         this.cancelButton.addActionListener(this.actionListener);
191         this.buttonPanel.add(this.cancelButton);
192         getContentPane().add(this.buttonPanel);
193         pack();
194     }
195 
196     /***
197      * 
198      * @param listcellrenderer
199      */
200     public void setRenderer(ListCellRenderer listcellrenderer) {
201         if (listcellrenderer != null) {
202             this.valuesList.setCellRenderer(listcellrenderer);
203         }
204     }
205 
206     /***
207      * 
208      * @param values
209      */
210     public void setValues(Object[] values) {
211         if (values != null) {
212             this.valuesList.setModel(new DefaultComboBoxModel(values));
213         }
214     }
215 
216     /***
217      * 
218      * @return the currently selected values.
219      */
220     public Object[] getSelectedValues() {
221         return this.valuesList.getSelectedValues();
222     }
223 
224     /***
225      * 
226      * @param action
227      */
228     public void setOKAction(Action action) {
229         if (action != null) {
230             this.okButton.setAction(action);
231         }
232     }
233 
234     /***
235      * 
236      * @param action
237      */
238     public void setCancelAction(Action action) {
239         if (action != null) {
240             this.cancelButton.setAction(action);
241         }
242     }
243 }