1 25 package org.jrobin.inspector; 26 27 import org.jrobin.core.DsDef; 28 import org.jrobin.core.RrdException; 29 30 import javax.swing.*; 31 import java.awt.*; 32 import java.awt.event.WindowEvent ; 33 import java.awt.event.ActionListener ; 34 import java.awt.event.ActionEvent ; 35 36 class EditDatasourceDialog extends JDialog { 37 private static final int FIELD_SIZE = 20; 38 private static final String TITLE_NEW = "New datasource"; 39 private static final String TITLE_EDIT = "Edit datasource"; 40 41 private JLabel nameLabel = new JLabel("Datasource name: "); 42 private JLabel typeLabel = new JLabel("Datasource type: "); 43 private JLabel heartbeatLabel = new JLabel("Heartbeat: "); 44 private JLabel minLabel = new JLabel("Min value: "); 45 private JLabel maxLabel = new JLabel("Max value: "); 46 47 private JTextField nameField = new JTextField(FIELD_SIZE); 48 private JComboBox typeCombo = new JComboBox(); 49 private JTextField heartbeatField = new JTextField(FIELD_SIZE); 50 private JTextField minField = new JTextField(FIELD_SIZE); 51 private JTextField maxField = new JTextField(FIELD_SIZE); 52 53 private JButton okButton = new JButton("OK"); 54 private JButton cancelButton = new JButton("Cancel"); 55 56 private DsDef dsDef; 57 58 EditDatasourceDialog(Frame parent, DsDef dsDef) { 59 super(parent, dsDef == null? TITLE_NEW: TITLE_EDIT, true); 60 constructUI(dsDef); 61 pack(); 62 Util.centerOnScreen(this); 63 setVisible(true); 64 } 65 66 private void constructUI(DsDef dsDef) { 67 String [] types = DsDef.DS_TYPES; 69 for (int i = 0; i < types.length; i++) { 70 typeCombo.addItem(types[i]); 71 } 72 typeCombo.setSelectedIndex(0); 73 if(dsDef == null) { 74 minField.setText("U"); 76 maxField.setText("U"); 77 } 78 else { 79 nameField.setText(dsDef.getDsName()); 81 nameField.setEnabled(false); 82 typeCombo.setSelectedItem(dsDef.getDsType()); 83 typeCombo.setEnabled(false); 84 heartbeatField.setText("" + dsDef.getHeartbeat()); 85 minField.setText("" + dsDef.getMinValue()); 86 maxField.setText("" + dsDef.getMaxValue()); 87 } 88 89 JPanel content = (JPanel) getContentPane(); 91 GridBagLayout layout = new GridBagLayout(); 92 content.setLayout(layout); 93 GridBagConstraints gbc = new GridBagConstraints(); 94 gbc.insets = new Insets(3, 3, 3, 3); 95 gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST; 96 layout.setConstraints(nameLabel, gbc); 97 content.add(nameLabel); 98 gbc.gridy = 1; 99 layout.setConstraints(typeLabel, gbc); 100 content.add(typeLabel); 101 gbc.gridy = 2; 102 layout.setConstraints(heartbeatLabel, gbc); 103 content.add(heartbeatLabel); 104 gbc.gridy = 3; 105 layout.setConstraints(minLabel, gbc); 106 content.add(minLabel); 107 gbc.gridy = 4; 108 layout.setConstraints(maxLabel, gbc); 109 content.add(maxLabel); 110 gbc.gridy = 5; 111 layout.setConstraints(okButton, gbc); 112 okButton.setPreferredSize(cancelButton.getPreferredSize()); 113 content.add(okButton); 114 gbc.gridx = 1; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; 115 layout.setConstraints(nameField, gbc); 116 content.add(nameField); 117 gbc.gridy = 1; 118 layout.setConstraints(typeCombo, gbc); 119 content.add(typeCombo); 120 gbc.gridy = 2; 121 layout.setConstraints(heartbeatField, gbc); 122 content.add(heartbeatField); 123 gbc.gridy = 3; 124 layout.setConstraints(minField, gbc); 125 content.add(minField); 126 gbc.gridy = 4; 127 layout.setConstraints(maxField, gbc); 128 content.add(maxField); 129 gbc.gridy = 5; 130 layout.setConstraints(cancelButton, gbc); 131 content.add(cancelButton); 132 getRootPane().setDefaultButton(okButton); 133 134 okButton.addActionListener(new ActionListener () { 136 public void actionPerformed(ActionEvent e) { ok(); } 137 }); 138 cancelButton.addActionListener(new ActionListener () { 139 public void actionPerformed(ActionEvent e) { cancel(); } 140 }); 141 142 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 143 } 144 145 private void ok() { 146 dsDef = createDsDef(); 147 if(dsDef != null) { 148 close(); 149 } 150 } 151 152 private void close() { 153 dispatchEvent(new WindowEvent (this, WindowEvent.WINDOW_CLOSING)); 154 } 155 156 private void cancel() { 157 close(); 158 } 159 160 private DsDef createDsDef() { 161 String name = nameField.getText(); 162 if(name == null || name.length() < 1 || name.length() > 20) { 163 Util.error(this, "Datasource name must be a non-empty string up to 20 chars long"); 164 return null; 165 } 166 String type = (String ) typeCombo.getSelectedItem(); 167 long heartbeat; 168 try { 169 heartbeat = Long.parseLong(heartbeatField.getText()); 170 if(heartbeat <= 0) { 171 throw new NumberFormatException (); 172 } 173 } 174 catch(NumberFormatException nfe) { 175 Util.error(this, "Heartbeat must be a positive integer number"); 176 return null; 177 } 178 double min = Double.NaN, max = Double.NaN; 179 try { 180 min = Double.parseDouble(minField.getText()); 181 } 182 catch (NumberFormatException nfe) { 183 } 185 try { 186 max = Double.parseDouble(maxField.getText()); 187 } 188 catch (NumberFormatException nfe) { 189 } 191 if(!Double.isNaN(min) && !Double.isNaN(max) && min >= max) { 192 Util.error(this, "Min value must be less than max value"); 193 return null; 194 } 195 try { 196 return new DsDef(name, type, heartbeat, min, max); 197 } 198 catch(RrdException e) { 199 e.printStackTrace(); 201 return null; 202 } 203 } 204 205 DsDef getDsDef() { 206 return dsDef; 207 } 208 } 209 | Popular Tags |