KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > client > EditLinkDialog


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.mrtg.client;
26
27 import javax.swing.*;
28 import java.awt.*;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.awt.event.WindowEvent JavaDoc;
33
34 class EditLinkDialog extends JDialog {
35     static final String JavaDoc ADD_TITLE = "New interface";
36     static final String JavaDoc EDIT_TITLE = "Edit interface data";
37     static final String JavaDoc DEFAULT_SAMPLING_INTERVAL = "300";
38     static final int MAX_SAMPLING_INTERVAL = 600;
39     static final int MIN_SAMPLING_INTERVAL = 10;
40
41     // result comes here
42
private LinkInfo[] linkInfo;
43
44     private String JavaDoc[] ifDescrs;
45     private RouterInfo routerInfo;
46     boolean insertMode = false;
47
48     // labels
49
private JLabel routerLabel = Util.standardLabel("Router: ");
50     private JLabel linksListLabel = Util.standardLabel("Interface: ");
51     private JLabel descrLabel = Util.standardLabel("Description: ");
52     private JLabel samplingLabel = Util.standardLabel("Sampling interval: ");
53     private JLabel activeLabel = Util.standardLabel("Active: ");
54
55     // values
56
private JLabel routerValueLabel = new JLabel();
57     private JList linksList = new JList();
58     private JTextField descrField = Util.standardTextField();
59     private JTextField samplingField = Util.standardTextField();
60     private JCheckBox activeBox = new JCheckBox("", true);
61     private JButton okButton = Util.standardButton("OK");
62     private JButton cancelButton = Util.standardButton("Cancel");
63
64     EditLinkDialog(Frame parent, RouterInfo routerInfo, String JavaDoc[] ifDescrs) {
65         // add link
66
super(parent, ADD_TITLE, true);
67         this.insertMode = true;
68         this.routerInfo = routerInfo;
69         this.ifDescrs = ifDescrs;
70         constructUserInterface();
71         pack();
72         setVisible(true);
73     }
74
75     EditLinkDialog(Frame parent, RouterInfo routerInfo, LinkInfo linkInfo) {
76         // edit link
77
super(parent, EDIT_TITLE, true);
78         this.insertMode = false;
79         this.routerInfo = routerInfo;
80         this.linkInfo = new LinkInfo[] { linkInfo };
81         constructUserInterface();
82         pack();
83         setVisible(true);
84     }
85
86     private void constructUserInterface() {
87         JPanel content = (JPanel) getContentPane();
88         Box box = Box.createVerticalBox();
89         box.add(Util.getPanelFor(routerLabel, routerValueLabel));
90         box.add(Util.getPanelFor(linksListLabel, Util.standardScrollPane(linksList)));
91         box.add(Util.getPanelFor(descrLabel, descrField));
92         box.add(Util.getPanelFor(samplingLabel, samplingField));
93         box.add(Util.getPanelFor(activeLabel, activeBox));
94         box.add(Util.getPanelFor(Util.standardLabel(""), okButton, cancelButton));
95         okButton.addActionListener(new ActionListener JavaDoc() {
96             public void actionPerformed(ActionEvent JavaDoc e) { ok(); }
97         });
98         cancelButton.addActionListener(new ActionListener JavaDoc() {
99             public void actionPerformed(ActionEvent JavaDoc e) { cancel(); }
100         });
101         content.add(box);
102         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
103         // populate controls
104
routerValueLabel.setText(routerInfo.getHost());
105         if(insertMode) {
106             linksList.setModel(new DefaultComboBoxModel(ifDescrs));
107             linksList.setSelectedIndex(0);
108             samplingField.setText(DEFAULT_SAMPLING_INTERVAL);
109             activeBox.setSelected(true);
110         }
111         else {
112             linksList.setModel(new DefaultComboBoxModel(routerInfo.getInterfaces()));
113             linksList.setSelectedValue(linkInfo[0].getIfDescr(), true);
114             linksList.setEnabled(false);
115             descrField.setText(linkInfo[0].getDescr());
116             samplingField.setText("" + linkInfo[0].getSamplingInterval());
117             activeBox.setSelected(linkInfo[0].isActive());
118         }
119         okButton.setMnemonic(KeyEvent.VK_O);
120         cancelButton.setMnemonic(KeyEvent.VK_C);
121         getRootPane().setDefaultButton(okButton);
122         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
123         Util.centerOnScreen(this);
124     }
125
126     private void close() {
127         dispatchEvent(new WindowEvent JavaDoc(this, WindowEvent.WINDOW_CLOSING));
128     }
129
130     private void ok() {
131         int samplingInterval;
132         try {
133             samplingInterval = Integer.parseInt(samplingField.getText());
134             if(samplingInterval < MIN_SAMPLING_INTERVAL ||
135                 samplingInterval > MAX_SAMPLING_INTERVAL) {
136                 throw new NumberFormatException JavaDoc();
137             }
138         } catch(NumberFormatException JavaDoc nfe) {
139             Util.error(this, "Sampling interval must be a number between " +
140                 MIN_SAMPLING_INTERVAL + " and " + MAX_SAMPLING_INTERVAL);
141             return;
142         }
143         if(insertMode) {
144             // new link
145
Object JavaDoc[] selectedLinks = linksList.getSelectedValues();
146             int count = selectedLinks.length;
147             if(count == 0) {
148                 Util.error(this, "Select at least one interface to add");
149                 return;
150             }
151             linkInfo = new LinkInfo[count];
152             for(int i = 0; i < count; i++) {
153                 linkInfo[i] = new LinkInfo();
154                 linkInfo[i].setIfDescr((String JavaDoc)selectedLinks[i]);
155                 linkInfo[i].setActive(activeBox.isSelected());
156                 linkInfo[i].setDescr(descrField.getText());
157                 linkInfo[i].setSamplingInterval(samplingInterval);
158             }
159         }
160         else {
161             // update link
162
linkInfo[0].setActive(activeBox.isSelected());
163             linkInfo[0].setDescr(descrField.getText());
164             linkInfo[0].setSamplingInterval(samplingInterval);
165         }
166         close();
167     }
168
169     private void cancel() {
170         linkInfo = null;
171         close();
172     }
173
174     LinkInfo[] getLinkInfo() {
175         return linkInfo;
176     }
177 }
178
Popular Tags