KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36
37 class EditArchiveDialog extends JDialog {
38     private static final int FIELD_SIZE = 20;
39     private static final String JavaDoc TITLE_NEW = "New archive";
40     private static final String JavaDoc 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         // fill controls
67
String JavaDoc[] 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             // NEW
74
xffField.setText("" + 0.5);
75         }
76         else {
77             // EDIT
78
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             // rowsField.setEnabled(false);
85
}
86
87         // layout
88
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         // actions
127
okButton.addActionListener(new ActionListener JavaDoc() {
128             public void actionPerformed(ActionEvent JavaDoc e) { ok(); }
129         });
130         cancelButton.addActionListener(new ActionListener JavaDoc() {
131             public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc(this, WindowEvent.WINDOW_CLOSING));
146     }
147
148     private void cancel() {
149         close();
150     }
151
152     private ArcDef createArcDef() {
153         String JavaDoc consolFun = (String JavaDoc) consolFunCombo.getSelectedItem();
154         double xff;
155         try {
156             xff = Double.parseDouble(xffField.getText());
157             if(xff < 0 || xff >= 1D) {
158                 throw new NumberFormatException JavaDoc();
159             }
160         }
161         catch(NumberFormatException JavaDoc 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 JavaDoc();
170             }
171         }
172         catch (NumberFormatException JavaDoc 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 JavaDoc();
181             }
182         }
183         catch (NumberFormatException JavaDoc 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             // should not be hear ever!
192
e.printStackTrace();
193             return null;
194         }
195     }
196
197     ArcDef getArcDef() {
198         return arcDef;
199     }
200 }
201
Popular Tags