View Javadoc

1   /* ============================================================
2    * JRobin : Pure java implementation of RRDTool's functionality
3    * ============================================================
4    *
5    * Project Info:  http://www.jrobin.org
6    * Project Lead:  Sasa Markovic (saxon@jrobin.org);
7    *
8    * (C) Copyright 2003, by Sasa Markovic.
9    *
10   * Developers:    Sasa Markovic (saxon@jrobin.org)
11   *
12   *
13   * This library is free software; you can redistribute it and/or modify it under the terms
14   * of the GNU Lesser General Public License as published by the Free Software Foundation;
15   * either version 2.1 of the License, or (at your option) any later version.
16   *
17   * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19   * See the GNU Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public License along with this
22   * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23   * Boston, MA 02111-1307, USA.
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  		// fill controls
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  			// NEW
75  			xffField.setText("" + 0.5);
76  		}
77  		else {
78  			// EDIT
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  			// rowsField.setEnabled(false);
86  		}
87  
88  		// layout
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 		// actions
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 			// should not be hear ever!
201 			Util.error(this, e);
202 			return null;
203 		}
204 	}
205 
206 	ArcDef getArcDef() {
207 		return arcDef;
208 	}
209 }