KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > client > beans > ListDialog


1 /*
2  * ListDialog.java
3  *
4  * Created on June 1, 2002, 4:32 PM
5  */

6
7 package com.quikj.client.beans;
8
9 import java.awt.*;
10
11 /**
12  *
13  * @author amit
14  */

15 public class ListDialog extends java.awt.Dialog JavaDoc
16 {
17     
18     /** Creates new form ListDialog */
19     public ListDialog(java.awt.Frame JavaDoc parent,
20     String JavaDoc label,
21     String JavaDoc[] list,
22     String JavaDoc ok_button_label,
23     String JavaDoc cancel_button_label,
24     ListListenerInterface listener)
25     {
26         super(parent);
27         
28         if (listener == null)
29         {
30             setModal(true);
31         }
32         else
33         {
34             setModal(false);
35         }
36         
37         this.listener = listener;
38         
39         initComponents();
40         
41         listHeaderWidget.setText(label);
42         
43         for (int i = 0; i < list.length; i++)
44         {
45             listWidget.add(list[i]);
46         }
47         
48         if (ok_button_label != null)
49         {
50             okButton.setLabel(ok_button_label);
51         }
52         
53         if (cancel_button_label != null)
54         {
55             cancelButton.setLabel(cancel_button_label);
56         }
57         
58         // display the dialog
59
Rectangle pbounds = parent.getBounds();
60         Point mid = new Point(pbounds.x + (pbounds.width/2), pbounds.y + (pbounds.height/2));
61         Rectangle bounds = getBounds();
62         int x = mid.x - (bounds.width/2);
63         int y = mid.y - (bounds.height/2);
64         if (x < 0) x = pbounds.x;
65         if (y < 0) y = pbounds.y;
66         setBounds(x, y, bounds.width, bounds.height);
67         
68         show();
69     }
70     
71     /** This method is called from within the constructor to
72      * initialize the form.
73      * WARNING: Do NOT modify this code. The content of this method is
74      * always regenerated by the Form Editor.
75      */

76     private void initComponents()//GEN-BEGIN:initComponents
77
{
78         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
79
80         listHeaderWidget = new java.awt.Label JavaDoc();
81         listWidget = new java.awt.List JavaDoc();
82         panel1 = new java.awt.Panel JavaDoc();
83         okButton = new java.awt.Button JavaDoc();
84         cancelButton = new java.awt.Button JavaDoc();
85
86         setLayout(new java.awt.GridBagLayout JavaDoc());
87
88         setForeground(java.awt.Color.black);
89         setBackground(java.awt.Color.white);
90         addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
91         {
92             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt)
93             {
94                 closeDialog(evt);
95             }
96         });
97
98         listHeaderWidget.setText("List Header");
99         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
100         gridBagConstraints.gridx = 0;
101         gridBagConstraints.gridy = 0;
102         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
103         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
104         gridBagConstraints.weightx = 100.0;
105         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 10, 0, 10);
106         add(listHeaderWidget, gridBagConstraints);
107
108         listWidget.setBackground(new java.awt.Color JavaDoc(255, 255, 255));
109         listWidget.setForeground(new java.awt.Color JavaDoc(0, 51, 51));
110         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
111         gridBagConstraints.gridx = 0;
112         gridBagConstraints.gridy = 1;
113         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
114         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
115         gridBagConstraints.weightx = 100.0;
116         gridBagConstraints.weighty = 100.0;
117         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 10, 0, 10);
118         add(listWidget, gridBagConstraints);
119
120         panel1.setLayout(new java.awt.GridBagLayout JavaDoc());
121
122         panel1.setBackground(new java.awt.Color JavaDoc(255, 255, 255));
123         okButton.setLabel(" OK ");
124         okButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
125         {
126             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
127             {
128                 okButtonActionPerformed(evt);
129             }
130         });
131
132         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
133         gridBagConstraints.gridx = 0;
134         gridBagConstraints.gridy = 0;
135         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 20);
136         panel1.add(okButton, gridBagConstraints);
137
138         cancelButton.setLabel("Cancel");
139         cancelButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
140         {
141             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
142             {
143                 cancelButtonActionPerformed(evt);
144             }
145         });
146
147         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
148         gridBagConstraints.gridx = 1;
149         gridBagConstraints.gridy = 0;
150         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 20, 0, 0);
151         panel1.add(cancelButton, gridBagConstraints);
152
153         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.gridx = 0;
155         gridBagConstraints.gridy = 2;
156         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
157         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
158         gridBagConstraints.weightx = 100.0;
159         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 10, 10, 10);
160         add(panel1, gridBagConstraints);
161
162         pack();
163         java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
164         setSize(new java.awt.Dimension JavaDoc(400, 300));
165         setLocation((screenSize.width-400)/2,(screenSize.height-300)/2);
166     }//GEN-END:initComponents
167

168     private void cancelButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_cancelButtonActionPerformed
169
{//GEN-HEADEREND:event_cancelButtonActionPerformed
170
// Add your handling code here:
171
dispose();
172     }//GEN-LAST:event_cancelButtonActionPerformed
173

174     private void okButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_okButtonActionPerformed
175
{//GEN-HEADEREND:event_okButtonActionPerformed
176
// Add your handling code here:
177
selectedIndex = listWidget.getSelectedIndex();
178         if (listener == null)
179         {
180             okSelected = true;
181             
182             dispose();
183         }
184         else
185         {
186             if (selectedIndex >= 0)
187             {
188                 listener.actionPerformed(selectedIndex);
189             }
190             // else, dont bother the action listener
191
}
192     }//GEN-LAST:event_okButtonActionPerformed
193

194     /** Closes the dialog */
195     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
196
// do nothing
197
}//GEN-LAST:event_closeDialog
198

199     /** Getter for property selectedIndex.
200      * @return Value of property selectedIndex.
201      */

202     public int getSelectedIndex()
203     {
204         return selectedIndex;
205     }
206     
207     /** Setter for property selectedIndex.
208      * @param selectedIndex New value of property selectedIndex.
209      */

210     public void setSelectedIndex(int selectedIndex)
211     {
212         this.selectedIndex = selectedIndex;
213     }
214     
215     public boolean isOkSelected()
216     {
217         return okSelected;
218     }
219     
220     // Variables declaration - do not modify//GEN-BEGIN:variables
221
private java.awt.Label JavaDoc listHeaderWidget;
222     private java.awt.Button JavaDoc okButton;
223     private java.awt.Button JavaDoc cancelButton;
224     private java.awt.List JavaDoc listWidget;
225     private java.awt.Panel JavaDoc panel1;
226     // End of variables declaration//GEN-END:variables
227

228     private ListListenerInterface listener = null;
229     private boolean okSelected = false;
230     private int selectedIndex = -1;
231 }
232
Popular Tags