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