1 25 26 package org.jrobin.inspector; 27 28 import org.jrobin.core.RrdException; 29 import org.jrobin.core.ArcDef; 30 31 import javax.swing.*; 32 import java.awt.*; 33 import java.awt.event.WindowEvent ; 34 import java.awt.event.ActionListener ; 35 import java.awt.event.ActionEvent ; 36 37 class EditArchiveDialog extends JDialog { 38 private static final int FIELD_SIZE = 20; 39 private static final String TITLE_NEW = "New archive"; 40 private static final String TITLE_EDIT = "Edit archive"; 41 42 private JLabel consolFunLabel = new JLabel("Consolidation function: "); 43 private JLabel xffLabel = new JLabel("X-files factor: "); 44 private JLabel stepsLabel = new JLabel("Steps: "); 45 private JLabel rowsLabel = new JLabel("Rows: "); 46 47 private JComboBox consolFunCombo = new JComboBox(); 48 private JTextField xffField = new JTextField(FIELD_SIZE); 49 private JTextField stepsField = new JTextField(FIELD_SIZE); 50 private JTextField rowsField = new JTextField(FIELD_SIZE); 51 52 private JButton okButton = new JButton("OK"); 53 private JButton cancelButton = new JButton("Cancel"); 54 55 private ArcDef arcDef; 56 57 EditArchiveDialog(Frame parent, ArcDef arcDef) { 58 super(parent, arcDef == null? TITLE_NEW: TITLE_EDIT, true); 59 constructUI(arcDef); 60 pack(); 61 Util.centerOnScreen(this); 62 setVisible(true); 63 } 64 65 private void constructUI(ArcDef arcDef) { 66 String [] funs = ArcDef.CONSOL_FUNS; 68 for (int i = 0; i < funs.length; i++) { 69 consolFunCombo.addItem(funs[i]); 70 } 71 consolFunCombo.setSelectedIndex(0); 72 if(arcDef == null) { 73 xffField.setText("" + 0.5); 75 } 76 else { 77 consolFunCombo.setSelectedItem(arcDef.getConsolFun()); 79 consolFunCombo.setEnabled(false); 80 xffField.setText("" + arcDef.getXff()); 81 stepsField.setText("" + arcDef.getSteps()); 82 stepsField.setEnabled(false); 83 rowsField.setText("" + arcDef.getRows()); 84 } 86 87 JPanel content = (JPanel) getContentPane(); 89 GridBagLayout layout = new GridBagLayout(); 90 content.setLayout(layout); 91 GridBagConstraints gbc = new GridBagConstraints(); 92 gbc.insets = new Insets(3, 3, 3, 3); 93 gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST; 94 layout.setConstraints(consolFunLabel, gbc); 95 content.add(consolFunLabel); 96 gbc.gridy = 1; 97 layout.setConstraints(xffLabel, gbc); 98 content.add(xffLabel); 99 gbc.gridy = 2; 100 layout.setConstraints(stepsLabel, gbc); 101 content.add(stepsLabel); 102 gbc.gridy = 3; 103 layout.setConstraints(rowsLabel, gbc); 104 content.add(rowsLabel); 105 gbc.gridy = 4; 106 layout.setConstraints(okButton, gbc); 107 okButton.setPreferredSize(cancelButton.getPreferredSize()); 108 content.add(okButton); 109 gbc.gridx = 1; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; 110 layout.setConstraints(consolFunCombo, gbc); 111 content.add(consolFunCombo); 112 gbc.gridy = 1; 113 layout.setConstraints(xffField, gbc); 114 content.add(xffField); 115 gbc.gridy = 2; 116 layout.setConstraints(stepsField, gbc); 117 content.add(stepsField); 118 gbc.gridy = 3; 119 layout.setConstraints(rowsField, gbc); 120 content.add(rowsField); 121 gbc.gridy = 4; 122 layout.setConstraints(cancelButton, gbc); 123 content.add(cancelButton); 124 getRootPane().setDefaultButton(okButton); 125 126 okButton.addActionListener(new ActionListener () { 128 public void actionPerformed(ActionEvent e) { ok(); } 129 }); 130 cancelButton.addActionListener(new ActionListener () { 131 public void actionPerformed(ActionEvent e) { cancel(); } 132 }); 133 134 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 135 } 136 137 private void ok() { 138 arcDef = createArcDef(); 139 if(arcDef != null) { 140 close(); 141 } 142 } 143 144 private void close() { 145 dispatchEvent(new WindowEvent (this, WindowEvent.WINDOW_CLOSING)); 146 } 147 148 private void cancel() { 149 close(); 150 } 151 152 private ArcDef createArcDef() { 153 String consolFun = (String ) consolFunCombo.getSelectedItem(); 154 double xff; 155 try { 156 xff = Double.parseDouble(xffField.getText()); 157 if(xff < 0 || xff >= 1D) { 158 throw new NumberFormatException (); 159 } 160 } 161 catch(NumberFormatException nfe) { 162 Util.error(this, "X-files factor must be a number not less than 0.0 and less than 1.0"); 163 return null; 164 } 165 int steps; 166 try { 167 steps = Integer.parseInt(stepsField.getText()); 168 if(steps <= 0) { 169 throw new NumberFormatException (); 170 } 171 } 172 catch (NumberFormatException nfe) { 173 Util.error(this, "Number of steps must be a positive integer"); 174 return null; 175 } 176 int rows; 177 try { 178 rows = Integer.parseInt(rowsField.getText()); 179 if(rows <= 0) { 180 throw new NumberFormatException (); 181 } 182 } 183 catch (NumberFormatException nfe) { 184 Util.error(this, "Number of rows must be a positive integer"); 185 return null; 186 } 187 try { 188 return new ArcDef(consolFun, xff, steps, rows); 189 } 190 catch(RrdException e) { 191 e.printStackTrace(); 193 return null; 194 } 195 } 196 197 ArcDef getArcDef() { 198 return arcDef; 199 } 200 } 201 | Popular Tags |