1 ////////////////////////////////////////////////////////////////////////////////
2 // Modification history:
3 // Date Author Description
4 // ============================================================================
5 // 17 Jul 2003 Cory Generated
6 ////////////////////////////////////////////////////////////////////////////////
7 package net.sourceforge.toejam.utils.threadPool.gui;
8
9 import javax.swing.JTextField;
10 import org.apache.log4j.Logger;
11
12 import net.sourceforge.toejam.utils.threadPool.ThreadManager;
13
14 /**
15 * A panel used for managing a thread pool
16 *
17 * @author Cory Prowse
18 * @version 3.0
19 */
20 public class PoolEditorDialog extends javax.swing.JDialog {
21 /** Logger */
22 public static final transient Logger LOG = Logger.getLogger(PoolEditorDialog.class);
23
24 private ThreadManager threadManager;
25
26 /** A return status code - returned if Cancel button has been pressed */
27 public static final int RET_CANCEL = 0;
28 /** A return status code - returned if OK button has been pressed */
29 public static final int RET_OK = 1;
30
31 private int returnStatus = RET_CANCEL;
32
33 /** Creates new form PoolEditorDialog */
34 public PoolEditorDialog(final java.awt.Frame parent, final boolean modal, final ThreadManager threadManager) {
35 super(parent, modal);
36 this.threadManager = threadManager;
37
38 initComponents();
39
40 minTextField.setText(Integer.toString(threadManager.getMinThreads()));
41 maxTextField.setText(Integer.toString(threadManager.getMaxThreads()));
42 chunkTextField.setText(Integer.toString(threadManager.getChunkSize()));
43 }
44
45 /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
46 public int getReturnStatus() {
47 return returnStatus;
48 }
49
50 /** This method is called from within the constructor to
51 * initialize the form.
52 * WARNING: Do NOT modify this code. The content of this method is
53 * always regenerated by the Form Editor.
54 */
55 private void initComponents() {//GEN-BEGIN:initComponents
56 java.awt.GridBagConstraints gridBagConstraints;
57
58 dialogLabel = new javax.swing.JLabel();
59 editorPanel = new javax.swing.JPanel();
60 minLabel = new javax.swing.JLabel();
61 minTextField = new javax.swing.JTextField();
62 maxLabel = new javax.swing.JLabel();
63 maxTextField = new javax.swing.JTextField();
64 chunkLabel = new javax.swing.JLabel();
65 chunkTextField = new javax.swing.JTextField();
66 buttonPanel = new javax.swing.JPanel();
67 okButton = new javax.swing.JButton();
68 cancelButton = new javax.swing.JButton();
69
70 addWindowListener(new java.awt.event.WindowAdapter() {
71 public void windowClosing(java.awt.event.WindowEvent evt) {
72 closeDialog(evt);
73 }
74 });
75
76 dialogLabel.setText("Edit Thread Pool");
77 dialogLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
78 getContentPane().add(dialogLabel, java.awt.BorderLayout.NORTH);
79
80 editorPanel.setLayout(new java.awt.GridBagLayout());
81
82 minLabel.setText("Min");
83 gridBagConstraints = new java.awt.GridBagConstraints();
84 gridBagConstraints.gridx = 0;
85 gridBagConstraints.gridy = 0;
86 editorPanel.add(minLabel, gridBagConstraints);
87
88 minTextField.setText("0");
89 gridBagConstraints = new java.awt.GridBagConstraints();
90 gridBagConstraints.gridx = 1;
91 gridBagConstraints.gridy = 0;
92 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
93 gridBagConstraints.weightx = 1.0;
94 editorPanel.add(minTextField, gridBagConstraints);
95
96 maxLabel.setText("Max");
97 gridBagConstraints = new java.awt.GridBagConstraints();
98 gridBagConstraints.gridx = 0;
99 gridBagConstraints.gridy = 1;
100 editorPanel.add(maxLabel, gridBagConstraints);
101
102 maxTextField.setText("1");
103 gridBagConstraints = new java.awt.GridBagConstraints();
104 gridBagConstraints.gridx = 1;
105 gridBagConstraints.gridy = 1;
106 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
107 gridBagConstraints.weightx = 1.0;
108 editorPanel.add(maxTextField, gridBagConstraints);
109
110 chunkLabel.setText("Chunk");
111 gridBagConstraints = new java.awt.GridBagConstraints();
112 gridBagConstraints.gridx = 0;
113 gridBagConstraints.gridy = 2;
114 gridBagConstraints.ipady = 2;
115 editorPanel.add(chunkLabel, gridBagConstraints);
116
117 chunkTextField.setText("1");
118 gridBagConstraints = new java.awt.GridBagConstraints();
119 gridBagConstraints.gridx = 1;
120 gridBagConstraints.gridy = 2;
121 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
122 gridBagConstraints.weightx = 1.0;
123 editorPanel.add(chunkTextField, gridBagConstraints);
124
125 getContentPane().add(editorPanel, java.awt.BorderLayout.CENTER);
126
127 buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
128
129 okButton.setText("OK");
130 okButton.addActionListener(new java.awt.event.ActionListener() {
131 public void actionPerformed(java.awt.event.ActionEvent evt) {
132 okButtonActionPerformed(evt);
133 }
134 });
135
136 buttonPanel.add(okButton);
137
138 cancelButton.setText("Cancel");
139 cancelButton.addActionListener(new java.awt.event.ActionListener() {
140 public void actionPerformed(java.awt.event.ActionEvent evt) {
141 cancelButtonActionPerformed(evt);
142 }
143 });
144
145 buttonPanel.add(cancelButton);
146
147 getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);
148
149 pack();
150 }//GEN-END:initComponents
151
152 private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
153
154 if (validateAll()) {
155
156 // Update Thread Manager
157 try {
158 LOG.debug("Setting new thread pool constraints");
159
160 threadManager.init(Integer.parseInt(minTextField.getText()), Integer.parseInt(maxTextField.getText()), Integer.parseInt(chunkTextField.getText()));
161 } catch( NumberFormatException e ) {
162 throw new java.util.ConcurrentModificationException("Failed to parse all fields after a validation");
163 }
164
165 doClose(RET_OK);
166 }
167 }//GEN-LAST:event_okButtonActionPerformed
168
169 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
170 doClose(RET_CANCEL);
171 }//GEN-LAST:event_cancelButtonActionPerformed
172
173 /** Closes the dialog */
174 private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
175 doClose(RET_CANCEL);
176 }//GEN-LAST:event_closeDialog
177
178 private void doClose(int retStatus) {
179 returnStatus = retStatus;
180 setVisible(false);
181 dispose();
182 }
183
184 // Variables declaration - do not modify//GEN-BEGIN:variables
185 private javax.swing.JPanel buttonPanel;
186 private javax.swing.JButton cancelButton;
187 private javax.swing.JLabel chunkLabel;
188 private javax.swing.JTextField chunkTextField;
189 private javax.swing.JLabel dialogLabel;
190 private javax.swing.JPanel editorPanel;
191 private javax.swing.JLabel maxLabel;
192 private javax.swing.JTextField maxTextField;
193 private javax.swing.JLabel minLabel;
194 private javax.swing.JTextField minTextField;
195 private javax.swing.JButton okButton;
196 // End of variables declaration//GEN-END:variables
197
198 /**
199 * Indicates whether contents are valid
200 *
201 * @return indicates if contents of form are valid
202 */
203 private boolean validateAll() {
204 return (validateIntegerTextField(minTextField) && validateIntegerTextField(maxTextField) && validateIntegerTextField(chunkTextField));
205 }
206
207 /**
208 * Validate a Text field is an integer
209 *
210 */
211 public boolean validateIntegerTextField( JTextField jTextField ) {
212 try {
213 Integer.parseInt(jTextField.getText());
214 } catch (NumberFormatException e) {
215 // Focus on this Text field
216 jTextField.grabFocus();
217 return false;
218 }
219
220 return true;
221 }
222
223 }