KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Browser > StringListEditDialog


1 /*
2  * StringListEditDialog.java
3  *
4  * Created on 12. květen 2004, 22:35
5  */

6
7 package SOFA.SOFAnet.Browser;
8
9 import java.util.*;
10 import javax.swing.*;
11 import java.awt.Component JavaDoc;
12 import java.awt.Frame JavaDoc;
13
14 /**
15  *
16  * @author Ladislav Sobr
17  */

18 public class StringListEditDialog extends javax.swing.JDialog JavaDoc
19 {
20   private boolean isOK;
21   private List stringList;
22   private String JavaDoc itemDescription;
23   private DefaultListModel content;
24   
25   public static boolean showDialog(Component JavaDoc frameComp, List stringList, String JavaDoc itemDescription, String JavaDoc title)
26   {
27     Frame JavaDoc frame = JOptionPane.getFrameForComponent(frameComp);
28     StringListEditDialog dialog = new StringListEditDialog(frame, true, stringList, itemDescription, title);
29     dialog.setVisible(true);
30     return dialog.isOK();
31     
32   }
33   /** Creates new form StringListEditDialog */
34   public StringListEditDialog(java.awt.Frame JavaDoc parent, boolean modal, List stringList, String JavaDoc itemDescription, String JavaDoc title)
35   {
36     super(parent, modal);
37     this.stringList = stringList;
38     this.itemDescription = itemDescription;
39     isOK = false;
40     content = new DefaultListModel();
41     
42     content.ensureCapacity(stringList.size());
43     Iterator it = stringList.iterator();
44     while (it.hasNext()) content.addElement((String JavaDoc)it.next());
45     
46     initComponents();
47     setLocationRelativeTo(parent);
48     setTitle(title);
49     
50     jLabel.setText(itemDescription + " List:");
51   }
52   
53   public boolean isOK()
54   {
55     return isOK;
56   }
57   
58   
59   /** This method is called from within the constructor to
60    * initialize the form.
61    * WARNING: Do NOT modify this code. The content of this method is
62    * always regenerated by the Form Editor.
63    */

64   private void initComponents()//GEN-BEGIN:initComponents
65
{
66     jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
67     jList = new javax.swing.JList JavaDoc();
68     jPanel1 = new javax.swing.JPanel JavaDoc();
69     jPanel5 = new javax.swing.JPanel JavaDoc();
70     setButton = new javax.swing.JButton JavaDoc();
71     cancelButton = new javax.swing.JButton JavaDoc();
72     jPanel2 = new javax.swing.JPanel JavaDoc();
73     jPanel3 = new javax.swing.JPanel JavaDoc();
74     addButton = new javax.swing.JButton JavaDoc();
75     deleteSelectedButton = new javax.swing.JButton JavaDoc();
76     moveUpButton = new javax.swing.JButton JavaDoc();
77     moveDownButton = new javax.swing.JButton JavaDoc();
78     jPanel4 = new javax.swing.JPanel JavaDoc();
79     jLabel = new javax.swing.JLabel JavaDoc();
80
81     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
82     jScrollPane1.setMinimumSize(new java.awt.Dimension JavaDoc(260, 132));
83     jList.setModel(content);
84     jScrollPane1.setViewportView(jList);
85
86     getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
87
88     jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
89
90     jPanel1.setMinimumSize(new java.awt.Dimension JavaDoc(250, 35));
91     jPanel1.setPreferredSize(new java.awt.Dimension JavaDoc(250, 35));
92     setButton.setText("Set");
93     setButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
94     {
95       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
96       {
97         setButtonActionPerformed(evt);
98       }
99     });
100
101     jPanel5.add(setButton);
102
103     cancelButton.setText("Cancel");
104     cancelButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
105     {
106       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
107       {
108         cancelButtonActionPerformed(evt);
109       }
110     });
111
112     jPanel5.add(cancelButton);
113
114     jPanel1.add(jPanel5, java.awt.BorderLayout.EAST);
115
116     getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
117
118     jPanel2.setLayout(new java.awt.BorderLayout JavaDoc());
119
120     jPanel3.setLayout(new java.awt.GridLayout JavaDoc(4, 0));
121
122     jPanel3.setBorder(new javax.swing.border.TitledBorder JavaDoc("Edit"));
123     addButton.setText("Add");
124     addButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
125     {
126       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
127       {
128         addButtonActionPerformed(evt);
129       }
130     });
131
132     jPanel3.add(addButton);
133
134     deleteSelectedButton.setText("Delete Selected");
135     deleteSelectedButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
136     {
137       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
138       {
139         deleteSelectedButtonActionPerformed(evt);
140       }
141     });
142
143     jPanel3.add(deleteSelectedButton);
144
145     moveUpButton.setText("Move Up");
146     moveUpButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
147     {
148       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
149       {
150         moveUpButtonActionPerformed(evt);
151       }
152     });
153
154     jPanel3.add(moveUpButton);
155
156     moveDownButton.setText("Move Down");
157     moveDownButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
158     {
159       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
160       {
161         moveDownButtonActionPerformed(evt);
162       }
163     });
164
165     jPanel3.add(moveDownButton);
166
167     jPanel2.add(jPanel3, java.awt.BorderLayout.NORTH);
168
169     getContentPane().add(jPanel2, java.awt.BorderLayout.EAST);
170
171     jPanel4.setLayout(new java.awt.GridLayout JavaDoc(1, 0));
172
173     jPanel4.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(3, 1, 1, 1)));
174     jPanel4.setMinimumSize(new java.awt.Dimension JavaDoc(350, 19));
175     jPanel4.setPreferredSize(new java.awt.Dimension JavaDoc(350, 19));
176     jLabel.setText("String List:");
177     jPanel4.add(jLabel);
178
179     getContentPane().add(jPanel4, java.awt.BorderLayout.NORTH);
180
181     pack();
182   }//GEN-END:initComponents
183

184   private void cancelButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_cancelButtonActionPerformed
185
{//GEN-HEADEREND:event_cancelButtonActionPerformed
186
dispose();
187   }//GEN-LAST:event_cancelButtonActionPerformed
188

189   private void setButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_setButtonActionPerformed
190
{//GEN-HEADEREND:event_setButtonActionPerformed
191
stringList.clear();
192     Enumeration en = content.elements();
193     while (en.hasMoreElements()) stringList.add((String JavaDoc)en.nextElement());
194     isOK = true;
195     dispose();
196   }//GEN-LAST:event_setButtonActionPerformed
197

198   private void moveDownButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_moveDownButtonActionPerformed
199
{//GEN-HEADEREND:event_moveDownButtonActionPerformed
200
int i = jList.getSelectedIndex();
201     if (i == -1 || i >= content.size() - 1) return;
202     
203     String JavaDoc s1 = (String JavaDoc)content.getElementAt(i);
204     String JavaDoc s2 = (String JavaDoc)content.getElementAt(i + 1);
205     content.setElementAt(s2, i);
206     content.setElementAt(s1, i + 1);
207     jList.setSelectedIndex(i + 1);
208
209   }//GEN-LAST:event_moveDownButtonActionPerformed
210

211   private void moveUpButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_moveUpButtonActionPerformed
212
{//GEN-HEADEREND:event_moveUpButtonActionPerformed
213
int i = jList.getSelectedIndex();
214     if (i == -1 || i == 0) return;
215     
216     String JavaDoc s1 = (String JavaDoc)content.getElementAt(i);
217     String JavaDoc s2 = (String JavaDoc)content.getElementAt(i - 1);
218     content.setElementAt(s2, i);
219     content.setElementAt(s1, i - 1);
220     jList.setSelectedIndex(i - 1);
221   }//GEN-LAST:event_moveUpButtonActionPerformed
222

223   private void deleteSelectedButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_deleteSelectedButtonActionPerformed
224
{//GEN-HEADEREND:event_deleteSelectedButtonActionPerformed
225
Object JavaDoc[] selected = jList.getSelectedValues();
226     for (int i = 0; i < selected.length; i++)
227     {
228       if (selected[i] instanceof String JavaDoc) content.removeElement((String JavaDoc)selected[i]);
229     }
230   }//GEN-LAST:event_deleteSelectedButtonActionPerformed
231

232   private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_addButtonActionPerformed
233
{//GEN-HEADEREND:event_addButtonActionPerformed
234
String JavaDoc result = JOptionPane.showInputDialog(this, "New " + itemDescription + ":", "");
235     if (result != null) content.addElement(result);
236   }//GEN-LAST:event_addButtonActionPerformed
237

238   
239   // Variables declaration - do not modify//GEN-BEGIN:variables
240
private javax.swing.JButton JavaDoc addButton;
241   private javax.swing.JButton JavaDoc cancelButton;
242   private javax.swing.JButton JavaDoc deleteSelectedButton;
243   private javax.swing.JLabel JavaDoc jLabel;
244   private javax.swing.JList JavaDoc jList;
245   private javax.swing.JPanel JavaDoc jPanel1;
246   private javax.swing.JPanel JavaDoc jPanel2;
247   private javax.swing.JPanel JavaDoc jPanel3;
248   private javax.swing.JPanel JavaDoc jPanel4;
249   private javax.swing.JPanel JavaDoc jPanel5;
250   private javax.swing.JScrollPane JavaDoc jScrollPane1;
251   private javax.swing.JButton JavaDoc moveDownButton;
252   private javax.swing.JButton JavaDoc moveUpButton;
253   private javax.swing.JButton JavaDoc setButton;
254   // End of variables declaration//GEN-END:variables
255

256 }
257
Popular Tags