1 6 7 package SOFA.SOFAnet.Browser; 8 9 import java.util.*; 10 import javax.swing.*; 11 import java.awt.Component ; 12 import java.awt.Frame ; 13 14 18 public class StringListEditDialog extends javax.swing.JDialog 19 { 20 private boolean isOK; 21 private List stringList; 22 private String itemDescription; 23 private DefaultListModel content; 24 25 public static boolean showDialog(Component frameComp, List stringList, String itemDescription, String title) 26 { 27 Frame 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 34 public StringListEditDialog(java.awt.Frame parent, boolean modal, List stringList, String itemDescription, String 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 )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 64 private void initComponents() { 66 jScrollPane1 = new javax.swing.JScrollPane (); 67 jList = new javax.swing.JList (); 68 jPanel1 = new javax.swing.JPanel (); 69 jPanel5 = new javax.swing.JPanel (); 70 setButton = new javax.swing.JButton (); 71 cancelButton = new javax.swing.JButton (); 72 jPanel2 = new javax.swing.JPanel (); 73 jPanel3 = new javax.swing.JPanel (); 74 addButton = new javax.swing.JButton (); 75 deleteSelectedButton = new javax.swing.JButton (); 76 moveUpButton = new javax.swing.JButton (); 77 moveDownButton = new javax.swing.JButton (); 78 jPanel4 = new javax.swing.JPanel (); 79 jLabel = new javax.swing.JLabel (); 80 81 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 82 jScrollPane1.setMinimumSize(new java.awt.Dimension (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 ()); 89 90 jPanel1.setMinimumSize(new java.awt.Dimension (250, 35)); 91 jPanel1.setPreferredSize(new java.awt.Dimension (250, 35)); 92 setButton.setText("Set"); 93 setButton.addActionListener(new java.awt.event.ActionListener () 94 { 95 public void actionPerformed(java.awt.event.ActionEvent 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 () 105 { 106 public void actionPerformed(java.awt.event.ActionEvent 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 ()); 119 120 jPanel3.setLayout(new java.awt.GridLayout (4, 0)); 121 122 jPanel3.setBorder(new javax.swing.border.TitledBorder ("Edit")); 123 addButton.setText("Add"); 124 addButton.addActionListener(new java.awt.event.ActionListener () 125 { 126 public void actionPerformed(java.awt.event.ActionEvent 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 () 136 { 137 public void actionPerformed(java.awt.event.ActionEvent 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 () 147 { 148 public void actionPerformed(java.awt.event.ActionEvent 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 () 158 { 159 public void actionPerformed(java.awt.event.ActionEvent 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 (1, 0)); 172 173 jPanel4.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (3, 1, 1, 1))); 174 jPanel4.setMinimumSize(new java.awt.Dimension (350, 19)); 175 jPanel4.setPreferredSize(new java.awt.Dimension (350, 19)); 176 jLabel.setText("String List:"); 177 jPanel4.add(jLabel); 178 179 getContentPane().add(jPanel4, java.awt.BorderLayout.NORTH); 180 181 pack(); 182 } 184 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) { dispose(); 187 } 189 private void setButtonActionPerformed(java.awt.event.ActionEvent evt) { stringList.clear(); 192 Enumeration en = content.elements(); 193 while (en.hasMoreElements()) stringList.add((String )en.nextElement()); 194 isOK = true; 195 dispose(); 196 } 198 private void moveDownButtonActionPerformed(java.awt.event.ActionEvent evt) { int i = jList.getSelectedIndex(); 201 if (i == -1 || i >= content.size() - 1) return; 202 203 String s1 = (String )content.getElementAt(i); 204 String s2 = (String )content.getElementAt(i + 1); 205 content.setElementAt(s2, i); 206 content.setElementAt(s1, i + 1); 207 jList.setSelectedIndex(i + 1); 208 209 } 211 private void moveUpButtonActionPerformed(java.awt.event.ActionEvent evt) { int i = jList.getSelectedIndex(); 214 if (i == -1 || i == 0) return; 215 216 String s1 = (String )content.getElementAt(i); 217 String s2 = (String )content.getElementAt(i - 1); 218 content.setElementAt(s2, i); 219 content.setElementAt(s1, i - 1); 220 jList.setSelectedIndex(i - 1); 221 } 223 private void deleteSelectedButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] selected = jList.getSelectedValues(); 226 for (int i = 0; i < selected.length; i++) 227 { 228 if (selected[i] instanceof String ) content.removeElement((String )selected[i]); 229 } 230 } 232 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { String result = JOptionPane.showInputDialog(this, "New " + itemDescription + ":", ""); 235 if (result != null) content.addElement(result); 236 } 238 239 private javax.swing.JButton addButton; 241 private javax.swing.JButton cancelButton; 242 private javax.swing.JButton deleteSelectedButton; 243 private javax.swing.JLabel jLabel; 244 private javax.swing.JList jList; 245 private javax.swing.JPanel jPanel1; 246 private javax.swing.JPanel jPanel2; 247 private javax.swing.JPanel jPanel3; 248 private javax.swing.JPanel jPanel4; 249 private javax.swing.JPanel jPanel5; 250 private javax.swing.JScrollPane jScrollPane1; 251 private javax.swing.JButton moveDownButton; 252 private javax.swing.JButton moveUpButton; 253 private javax.swing.JButton setButton; 254 256 } 257 | Popular Tags |