1 18 19 package org.apache.jmeter.protocol.jdbc.config.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.FocusEvent ; 23 import java.awt.event.FocusListener ; 24 25 import javax.swing.BorderFactory ; 26 import javax.swing.JLabel ; 27 import javax.swing.JOptionPane ; 28 import javax.swing.JPanel ; 29 import javax.swing.JTextField ; 30 31 import org.apache.jmeter.config.ConfigTestElement; 32 import org.apache.jmeter.config.gui.AbstractConfigGui; 33 import org.apache.jmeter.gui.util.VerticalPanel; 34 import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler; 35 import org.apache.jmeter.protocol.jdbc.util.JMeter19ConnectionPool; 36 import org.apache.jmeter.testelement.TestElement; 37 import org.apache.jmeter.util.JMeterUtils; 38 39 42 public class PoolConfigGui extends AbstractConfigGui implements FocusListener 43 { 44 private static String CONNECTIONS = "connections"; 45 private static String MAXUSE = "maxuse"; 46 private static String DEFAULT_MAX_USE = "50"; 47 private static String DEFAULT_NUM_CONNECTIONS = "1"; 48 private JTextField connField; 49 private JTextField maxUseField; 50 51 private boolean displayName; 52 53 public PoolConfigGui() 54 { 55 this(true); 56 } 57 58 public PoolConfigGui(boolean displayName) 59 { 60 this.displayName = displayName; 61 init(); 62 } 63 64 public void configure(TestElement element) 65 { 66 super.configure(element); 67 connField.setText( 68 element.getPropertyAsString(JMeter19ConnectionPool.CONNECTIONS)); 69 maxUseField.setText( 70 element.getPropertyAsString(JMeter19ConnectionPool.MAXUSE)); 71 } 72 73 public TestElement createTestElement() 74 { 75 ConfigTestElement element = new ConfigTestElement(); 76 modifyTestElement(element); 77 return element; 78 } 79 80 84 public void modifyTestElement(TestElement element) 85 { 86 configureTestElement(element); 87 element.setProperty( 88 JDBCSampler.CONNECTION_POOL_IMPL, 89 JMeter19ConnectionPool.class.getName()); 90 element.setProperty( 91 JMeter19ConnectionPool.CONNECTIONS, 92 connField.getText()); 93 element.setProperty( 94 JMeter19ConnectionPool.MAXUSE, 95 maxUseField.getText()); 96 } 97 98 public String getLabelResource() 99 { 100 return "database_conn_pool_title"; 101 } 102 103 public void focusGained(FocusEvent e) 104 { 105 } 106 107 public void focusLost(FocusEvent e) 108 { 109 String name = e.getComponent().getName(); 110 111 if (name.equals(CONNECTIONS)) 112 { 113 try 114 { 115 Integer.parseInt(connField.getText()); 116 } 117 catch (NumberFormatException nfe) 118 { 119 if (connField.getText().length() > 0) 120 { 121 JOptionPane.showMessageDialog( 122 this, 123 "You must enter a valid number", 124 "Invalid data", 125 JOptionPane.WARNING_MESSAGE); 126 } 127 connField.setText(DEFAULT_NUM_CONNECTIONS); 128 } 129 } 130 else if (name.equals(MAXUSE)) 131 { 132 try 133 { 134 Integer.parseInt(maxUseField.getText()); 135 } 136 catch (NumberFormatException nfe) 137 { 138 if (maxUseField.getText().length() > 0) 139 { 140 JOptionPane.showMessageDialog( 141 this, 142 "You must enter a valid number", 143 "Invalid data", 144 JOptionPane.WARNING_MESSAGE); 145 } 146 maxUseField.setText(DEFAULT_NUM_CONNECTIONS); 147 } 148 } 149 } 150 151 private void init() 152 { 153 setLayout(new BorderLayout (0, 5)); 154 155 if (displayName) 156 { 157 setBorder(makeBorder()); 158 add(makeTitlePanel(), BorderLayout.NORTH); 159 } 160 161 VerticalPanel poolPanel = new VerticalPanel(); 162 poolPanel.setBorder( 163 BorderFactory.createTitledBorder( 164 JMeterUtils.getResString("database_conn_pool_props"))); 165 166 poolPanel.add(createConnPanel()); 167 poolPanel.add(createMaxUsePanel()); 168 169 VerticalPanel mainPanel = new VerticalPanel(); 174 mainPanel.add(poolPanel); 175 176 add(mainPanel, BorderLayout.CENTER); 177 } 178 179 private JPanel createConnPanel() 180 { 181 connField = new JTextField (DEFAULT_NUM_CONNECTIONS, 5); 182 connField.setName(CONNECTIONS); 183 connField.addFocusListener(this); 184 185 JLabel label = 186 new JLabel (JMeterUtils.getResString("database_conn_pool_size")); 187 label.setLabelFor(connField); 188 189 JPanel panel = new JPanel (new BorderLayout (5, 0)); 190 panel.add(label, BorderLayout.WEST); 191 panel.add(connField, BorderLayout.CENTER); 192 return panel; 193 } 194 195 private JPanel createMaxUsePanel() 196 { 197 maxUseField = new JTextField (DEFAULT_MAX_USE, 5); 198 maxUseField.setName(MAXUSE); 199 maxUseField.addFocusListener(this); 200 201 JLabel label = 202 new JLabel ( 203 JMeterUtils.getResString("database_conn_pool_max_usage")); 204 label.setLabelFor(maxUseField); 205 206 JPanel panel = new JPanel (new BorderLayout (5, 0)); 207 panel.add(label, BorderLayout.WEST); 208 panel.add(maxUseField, BorderLayout.CENTER); 209 return panel; 210 } 211 } 212 | Popular Tags |