KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * LicenceEditorDialog.java
3  *
4  * Created on 12. květen 2004, 15:12
5  */

6
7 package SOFA.SOFAnet.Browser;
8
9 import SOFA.SOFAnet.Repository.Licence;
10 import java.util.Date JavaDoc;
11 import java.text.DateFormat JavaDoc;
12 import java.text.ParseException JavaDoc;
13 import javax.swing.*;
14 import java.awt.Component JavaDoc;
15 import java.awt.Frame JavaDoc;
16
17 /**
18  *
19  * @author Ladislav Sobr
20  */

21 public class LicenceEditDialog extends javax.swing.JDialog JavaDoc
22 {
23   private Licence licence;
24   private boolean edit;
25   private boolean isOK;
26
27   public static boolean showDialog(Component JavaDoc frameComp, Licence licence, boolean edit, String JavaDoc title)
28   {
29     Frame JavaDoc 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   /** Creates new form LicenceEditorDialog */
37   public LicenceEditDialog(java.awt.Frame JavaDoc parent, boolean modal, Licence licence, boolean edit, String JavaDoc 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 JavaDoc validFrom = licence.getValidFrom();
61       Date JavaDoc validTo = licence.getValidTo();
62       DateFormat JavaDoc 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 JavaDoc validFrom = licence.getValidFrom();
86       Date JavaDoc validTo = licence.getValidTo();
87       DateFormat JavaDoc 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   /** This method is called from within the constructor to
105    * initialize the form.
106    * WARNING: Do NOT modify this code. The content of this method is
107    * always regenerated by the Form Editor.
108    */

109   private void initComponents()//GEN-BEGIN:initComponents
110
{
111     typeButtonGroup = new javax.swing.ButtonGroup JavaDoc();
112     jPanel2 = new javax.swing.JPanel JavaDoc();
113     typeLabel = new javax.swing.JLabel JavaDoc();
114     jPanel1 = new javax.swing.JPanel JavaDoc();
115     physicalRadioButton = new javax.swing.JRadioButton JavaDoc();
116     activeRadioButton = new javax.swing.JRadioButton JavaDoc();
117     copiesLabel = new javax.swing.JLabel JavaDoc();
118     copiesTextField = new javax.swing.JTextField JavaDoc();
119     validFromLabel = new javax.swing.JLabel JavaDoc();
120     validFromTextField = new javax.swing.JTextField JavaDoc();
121     validToLabel = new javax.swing.JLabel JavaDoc();
122     validToTextField = new javax.swing.JTextField JavaDoc();
123     textLabel = new javax.swing.JLabel JavaDoc();
124     jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
125     licenceTextTextArea = new javax.swing.JTextArea JavaDoc();
126     jPanel3 = new javax.swing.JPanel JavaDoc();
127     OKButton = new javax.swing.JButton JavaDoc();
128     cancelButton = new javax.swing.JButton JavaDoc();
129
130     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
131     jPanel2.setLayout(new java.awt.GridLayout JavaDoc(5, 0));
132
133     jPanel2.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(10, 10, 1, 10)));
134     typeLabel.setText("Type of Licence:");
135     jPanel2.add(typeLabel);
136
137     jPanel1.setLayout(new javax.swing.BoxLayout JavaDoc(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 JavaDoc()
143     {
144       public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc()
155     {
156       public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc(new java.awt.Insets JavaDoc(1, 10, 1, 10)));
187     jScrollPane1.setMinimumSize(new java.awt.Dimension JavaDoc(400, 100));
188     jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(400, 200));
189     licenceTextTextArea.setBorder(new javax.swing.border.BevelBorder JavaDoc(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 JavaDoc()
196     {
197       public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc()
207     {
208       public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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   }//GEN-END:initComponents
220

221   private void cancelButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_cancelButtonActionPerformed
222
{//GEN-HEADEREND:event_cancelButtonActionPerformed
223
dispose();
224   }//GEN-LAST:event_cancelButtonActionPerformed
225

226   private void OKButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_OKButtonActionPerformed
227
{//GEN-HEADEREND:event_OKButtonActionPerformed
228
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 JavaDoc 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 JavaDoc("Negative 'Number of Copies' is not allowed");
242         }
243         
244         DateFormat JavaDoc dateFormat = DateFormat.getInstance();
245         Date JavaDoc validFrom = null;
246         String JavaDoc validFromText = validFromTextField.getText().trim().toLowerCase();
247         if (validFromText.length() != 0 && validFromText.compareTo("no limitation") != 0) validFrom = dateFormat.parse(validFromText);
248
249         Date JavaDoc validTo = null;
250         String JavaDoc validToText = validToTextField.getText().trim().toLowerCase();
251         if (validToText.length() != 0 && validToText.compareTo("no limitation") != 0) validTo = dateFormat.parse(validToText);
252
253         String JavaDoc 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 JavaDoc 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 JavaDoc 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   }//GEN-LAST:event_OKButtonActionPerformed
277

278   private void typeRadioButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_typeRadioButtonActionPerformed
279
{//GEN-HEADEREND:event_typeRadioButtonActionPerformed
280
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   }//GEN-LAST:event_typeRadioButtonActionPerformed
286

287   
288   // Variables declaration - do not modify//GEN-BEGIN:variables
289
private javax.swing.JButton JavaDoc OKButton;
290   private javax.swing.JRadioButton JavaDoc activeRadioButton;
291   private javax.swing.JButton JavaDoc cancelButton;
292   private javax.swing.JLabel JavaDoc copiesLabel;
293   private javax.swing.JTextField JavaDoc copiesTextField;
294   private javax.swing.JPanel JavaDoc jPanel1;
295   private javax.swing.JPanel JavaDoc jPanel2;
296   private javax.swing.JPanel JavaDoc jPanel3;
297   private javax.swing.JScrollPane JavaDoc jScrollPane1;
298   private javax.swing.JTextArea JavaDoc licenceTextTextArea;
299   private javax.swing.JRadioButton JavaDoc physicalRadioButton;
300   private javax.swing.JLabel JavaDoc textLabel;
301   private javax.swing.ButtonGroup JavaDoc typeButtonGroup;
302   private javax.swing.JLabel JavaDoc typeLabel;
303   private javax.swing.JLabel JavaDoc validFromLabel;
304   private javax.swing.JTextField JavaDoc validFromTextField;
305   private javax.swing.JLabel JavaDoc validToLabel;
306   private javax.swing.JTextField JavaDoc validToTextField;
307   // End of variables declaration//GEN-END:variables
308

309 }
310
Popular Tags