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