1 6 7 package SOFA.SOFAnet.Browser; 8 9 import SOFA.SOFAnet.Repository.Licence; 10 import java.util.Date ; 11 import java.text.DateFormat ; 12 import java.text.ParseException ; 13 import javax.swing.*; 14 import java.awt.Component ; 15 import java.awt.Frame ; 16 17 21 public class LicenceEditDialog extends javax.swing.JDialog 22 { 23 private Licence licence; 24 private boolean edit; 25 private boolean isOK; 26 27 public static boolean showDialog(Component frameComp, Licence licence, boolean edit, String title) 28 { 29 Frame frame = JOptionPane.getFrameForComponent(frameComp); 30 LicenceEditDialog dialog = new LicenceEditDialog(frame, true, licence, edit, title); 31 dialog.setLocationRelativeTo(frameComp); 32 dialog.setVisible(true); 33 return dialog.isOK(); 34 } 35 36 37 public LicenceEditDialog(java.awt.Frame parent, boolean modal, Licence licence, boolean edit, String title) 38 { 39 super(parent, modal); 40 this.licence = licence; 41 this.edit = edit; 42 isOK = false; 43 44 initComponents(); 45 setTitle(title); 46 47 if (edit) 48 { 49 copiesTextField.setEditable(true); 50 validFromTextField.setEditable(true); 51 validToTextField.setEditable(true); 52 licenceTextTextArea.setEditable(true); 53 54 if (licence.getType() == Licence.TYPE_PHYSICAL) physicalRadioButton.setSelected(true); 55 else activeRadioButton.setSelected(true); 56 57 if (licence.withCopies()) copiesTextField.setText(Integer.toString(licence.getNumberOfCopies())); 58 else copiesTextField.setText(""); 59 60 Date validFrom = licence.getValidFrom(); 61 Date validTo = licence.getValidTo(); 62 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); 63 64 if (validFrom != null) validFromTextField.setText(dateFormat.format(validFrom)); 65 else validFromTextField.setText(""); 66 67 if (validTo != null) validToTextField.setText(dateFormat.format(validTo)); 68 else validToTextField.setText(""); 69 70 licenceTextTextArea.setText(licence.getText()); 71 } 72 else 73 { 74 copiesTextField.setEditable(false); 75 validFromTextField.setEditable(false); 76 validToTextField.setEditable(false); 77 licenceTextTextArea.setEditable(false); 78 79 if (licence.getType() == Licence.TYPE_PHYSICAL) physicalRadioButton.setSelected(true); 80 else activeRadioButton.setSelected(true); 81 82 if (licence.withCopies()) copiesTextField.setText(Integer.toString(licence.getNumberOfCopies())); 83 else copiesTextField.setText("without copies"); 84 85 Date validFrom = licence.getValidFrom(); 86 Date validTo = licence.getValidTo(); 87 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); 88 89 if (validFrom != null) validFromTextField.setText(dateFormat.format(validFrom)); 90 else validFromTextField.setText("no limitation"); 91 92 if (validTo != null) validToTextField.setText(dateFormat.format(validTo)); 93 else validToTextField.setText("no limitation"); 94 95 licenceTextTextArea.setText(licence.getText()); 96 } 97 } 98 99 public boolean isOK() 100 { 101 return isOK; 102 } 103 104 109 private void initComponents() { 111 typeButtonGroup = new javax.swing.ButtonGroup (); 112 jPanel2 = new javax.swing.JPanel (); 113 typeLabel = new javax.swing.JLabel (); 114 jPanel1 = new javax.swing.JPanel (); 115 physicalRadioButton = new javax.swing.JRadioButton (); 116 activeRadioButton = new javax.swing.JRadioButton (); 117 copiesLabel = new javax.swing.JLabel (); 118 copiesTextField = new javax.swing.JTextField (); 119 validFromLabel = new javax.swing.JLabel (); 120 validFromTextField = new javax.swing.JTextField (); 121 validToLabel = new javax.swing.JLabel (); 122 validToTextField = new javax.swing.JTextField (); 123 textLabel = new javax.swing.JLabel (); 124 jScrollPane1 = new javax.swing.JScrollPane (); 125 licenceTextTextArea = new javax.swing.JTextArea (); 126 jPanel3 = new javax.swing.JPanel (); 127 OKButton = new javax.swing.JButton (); 128 cancelButton = new javax.swing.JButton (); 129 130 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 131 jPanel2.setLayout(new java.awt.GridLayout (5, 0)); 132 133 jPanel2.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (10, 10, 1, 10))); 134 typeLabel.setText("Type of Licence:"); 135 jPanel2.add(typeLabel); 136 137 jPanel1.setLayout(new javax.swing.BoxLayout (jPanel1, javax.swing.BoxLayout.X_AXIS)); 138 139 physicalRadioButton.setSelected(true); 140 physicalRadioButton.setText("Physical"); 141 typeButtonGroup.add(physicalRadioButton); 142 physicalRadioButton.addActionListener(new java.awt.event.ActionListener () 143 { 144 public void actionPerformed(java.awt.event.ActionEvent evt) 145 { 146 typeRadioButtonActionPerformed(evt); 147 } 148 }); 149 150 jPanel1.add(physicalRadioButton); 151 152 activeRadioButton.setText("Active"); 153 typeButtonGroup.add(activeRadioButton); 154 activeRadioButton.addActionListener(new java.awt.event.ActionListener () 155 { 156 public void actionPerformed(java.awt.event.ActionEvent evt) 157 { 158 typeRadioButtonActionPerformed(evt); 159 } 160 }); 161 162 jPanel1.add(activeRadioButton); 163 164 jPanel2.add(jPanel1); 165 166 copiesLabel.setText("Number of Copies:"); 167 jPanel2.add(copiesLabel); 168 169 jPanel2.add(copiesTextField); 170 171 validFromLabel.setText("Valid From:"); 172 jPanel2.add(validFromLabel); 173 174 jPanel2.add(validFromTextField); 175 176 validToLabel.setText("Valid To:"); 177 jPanel2.add(validToLabel); 178 179 jPanel2.add(validToTextField); 180 181 textLabel.setText("Text of Licence:"); 182 jPanel2.add(textLabel); 183 184 getContentPane().add(jPanel2, java.awt.BorderLayout.NORTH); 185 186 jScrollPane1.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (1, 10, 1, 10))); 187 jScrollPane1.setMinimumSize(new java.awt.Dimension (400, 100)); 188 jScrollPane1.setPreferredSize(new java.awt.Dimension (400, 200)); 189 licenceTextTextArea.setBorder(new javax.swing.border.BevelBorder (javax.swing.border.BevelBorder.LOWERED)); 190 jScrollPane1.setViewportView(licenceTextTextArea); 191 192 getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); 193 194 OKButton.setText("OK"); 195 OKButton.addActionListener(new java.awt.event.ActionListener () 196 { 197 public void actionPerformed(java.awt.event.ActionEvent evt) 198 { 199 OKButtonActionPerformed(evt); 200 } 201 }); 202 203 jPanel3.add(OKButton); 204 205 cancelButton.setText("Cancel"); 206 cancelButton.addActionListener(new java.awt.event.ActionListener () 207 { 208 public void actionPerformed(java.awt.event.ActionEvent evt) 209 { 210 cancelButtonActionPerformed(evt); 211 } 212 }); 213 214 jPanel3.add(cancelButton); 215 216 getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH); 217 218 pack(); 219 } 221 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) { dispose(); 224 } 226 private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) { if (edit) 229 { 230 try 231 { 232 int type = Licence.TYPE_PHYSICAL; 233 234 if (activeRadioButton.isSelected()) type = Licence.TYPE_ACTIVE; 235 236 int copies = Licence.WITHOUT_COPIES; 237 String copiesText = copiesTextField.getText().trim().toLowerCase(); 238 if (copiesText.length() != 0 && copiesText.compareTo("without copies") != 0) 239 { 240 copies = Integer.parseInt(copiesText); 241 if (copies < 0) throw new NumberFormatException ("Negative 'Number of Copies' is not allowed"); 242 } 243 244 DateFormat dateFormat = DateFormat.getInstance(); 245 Date validFrom = null; 246 String validFromText = validFromTextField.getText().trim().toLowerCase(); 247 if (validFromText.length() != 0 && validFromText.compareTo("no limitation") != 0) validFrom = dateFormat.parse(validFromText); 248 249 Date validTo = null; 250 String validToText = validToTextField.getText().trim().toLowerCase(); 251 if (validToText.length() != 0 && validToText.compareTo("no limitation") != 0) validTo = dateFormat.parse(validToText); 252 253 String text = licenceTextTextArea.getText(); 254 255 licence.setType(type); 256 licence.setNumberOfCopies(copies); 257 licence.setValidFrom(validFrom); 258 licence.setValidTo(validTo); 259 licence.setText(text); 260 } 261 catch (NumberFormatException e) 262 { 263 JOptionPane.showMessageDialog(this, "Error occured while parsing 'Number of Copies':\n" + e, "Error", JOptionPane.ERROR_MESSAGE); 264 return; 265 } 266 catch (ParseException e) 267 { 268 JOptionPane.showMessageDialog(this, "Error occured while parsing 'Valid From' or 'Valid To':\n" + e, "Error", JOptionPane.ERROR_MESSAGE); 269 return; 270 } 271 272 } 273 274 isOK = true; 275 dispose(); 276 } 278 private void typeRadioButtonActionPerformed(java.awt.event.ActionEvent evt) { if (!edit) 281 { 282 if (physicalRadioButton.isSelected() && licence.getType() == Licence.TYPE_ACTIVE) activeRadioButton.setSelected(true); 283 if (activeRadioButton.isSelected() && licence.getType() == Licence.TYPE_PHYSICAL) physicalRadioButton.setSelected(true); 284 } 285 } 287 288 private javax.swing.JButton OKButton; 290 private javax.swing.JRadioButton activeRadioButton; 291 private javax.swing.JButton cancelButton; 292 private javax.swing.JLabel copiesLabel; 293 private javax.swing.JTextField copiesTextField; 294 private javax.swing.JPanel jPanel1; 295 private javax.swing.JPanel jPanel2; 296 private javax.swing.JPanel jPanel3; 297 private javax.swing.JScrollPane jScrollPane1; 298 private javax.swing.JTextArea licenceTextTextArea; 299 private javax.swing.JRadioButton physicalRadioButton; 300 private javax.swing.JLabel textLabel; 301 private javax.swing.ButtonGroup typeButtonGroup; 302 private javax.swing.JLabel typeLabel; 303 private javax.swing.JLabel validFromLabel; 304 private javax.swing.JTextField validFromTextField; 305 private javax.swing.JLabel validToLabel; 306 private javax.swing.JTextField validToTextField; 307 309 } 310 | Popular Tags |