KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > inspector > EditDatasourceDialog


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  * Arne Vandamme (cobralord@jrobin.org)
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 JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35
36 class EditDatasourceDialog extends JDialog {
37     private static final int FIELD_SIZE = 20;
38     private static final String JavaDoc TITLE_NEW = "New datasource";
39     private static final String JavaDoc 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         // fill controls
68
String JavaDoc[] 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             // NEW
75
minField.setText("U");
76             maxField.setText("U");
77         }
78         else {
79             // EDIT
80
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         // layout
90
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         // actions
135
okButton.addActionListener(new ActionListener JavaDoc() {
136             public void actionPerformed(ActionEvent JavaDoc e) { ok(); }
137         });
138         cancelButton.addActionListener(new ActionListener JavaDoc() {
139             public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc(this, WindowEvent.WINDOW_CLOSING));
154     }
155
156     private void cancel() {
157         close();
158     }
159
160     private DsDef createDsDef() {
161         String JavaDoc 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 JavaDoc type = (String JavaDoc) typeCombo.getSelectedItem();
167         long heartbeat;
168         try {
169             heartbeat = Long.parseLong(heartbeatField.getText());
170             if(heartbeat <= 0) {
171                 throw new NumberFormatException JavaDoc();
172             }
173         }
174         catch(NumberFormatException JavaDoc 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 JavaDoc nfe) {
183             // NOP, leave NaN
184
}
185         try {
186             max = Double.parseDouble(maxField.getText());
187         }
188         catch (NumberFormatException JavaDoc nfe) {
189             // NOP, leave NaN
190
}
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             // should not be hear ever!
200
e.printStackTrace();
201             return null;
202         }
203     }
204
205     DsDef getDsDef() {
206         return dsDef;
207     }
208 }
209
Popular Tags