KickJava   Java API By Example, From Geeks To Geeks.

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


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 EditRouterDialog extends JDialog {
35     static final String JavaDoc ADD_TITLE = "New router";
36     static final String JavaDoc EDIT_TITLE = "Edit router data";
37
38     private RouterInfo routerInfo;
39
40     private JLabel hostLabel = Util.standardLabel("Address: ");
41     private JLabel communityLabel = Util.standardLabel("Community: ");
42     private JLabel descrLabel = Util.standardLabel("Description: ");
43     private JLabel activeLabel = Util.standardLabel("Active: ");
44     private JTextField hostField = Util.standardTextField();
45     private JTextField communityField = Util.standardTextField();
46     private JTextField descrField = Util.standardTextField();
47     private JCheckBox activeBox = new JCheckBox("", true);
48     private JButton okButton = Util.standardButton("OK");
49     private JButton cancelButton = Util.standardButton("Cancel");
50
51     EditRouterDialog(Frame parent) {
52         this(parent, null);
53     }
54
55     EditRouterDialog(Frame parent, RouterInfo routerInfo) {
56         super(parent, routerInfo == null? ADD_TITLE: EDIT_TITLE, true);
57         this.routerInfo = routerInfo;
58         constructUserInterface();
59         pack();
60         setVisible(true);
61     }
62
63     private void constructUserInterface() {
64         JPanel content = (JPanel) getContentPane();
65         Box box = Box.createVerticalBox();
66         box.add(Util.getPanelFor(hostLabel, hostField));
67         box.add(Util.getPanelFor(communityLabel, communityField));
68         box.add(Util.getPanelFor(descrLabel, descrField));
69         box.add(Util.getPanelFor(activeLabel, activeBox));
70         box.add(Util.getPanelFor(Util.standardLabel(), okButton, cancelButton));
71         okButton.addActionListener(new ActionListener JavaDoc() {
72             public void actionPerformed(ActionEvent JavaDoc e) {
73                 ok();
74             }
75         });
76         cancelButton.addActionListener(new ActionListener JavaDoc() {
77             public void actionPerformed(ActionEvent JavaDoc e) {
78                 cancel();
79             }
80         });
81         content.add(box);
82         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
83         // populate controls if possible
84
if(routerInfo != null) {
85             hostField.setText(routerInfo.getHost());
86             hostField.setEnabled(false);
87             communityField.setText(routerInfo.getCommunity());
88             descrField.setText(routerInfo.getDescr());
89             activeBox.setSelected(routerInfo.isActive());
90         }
91
92         okButton.setMnemonic(KeyEvent.VK_O);
93         cancelButton.setMnemonic(KeyEvent.VK_C);
94         getRootPane().setDefaultButton(okButton);
95         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
96         Util.centerOnScreen(this);
97     }
98
99     private void close() {
100         dispatchEvent(new WindowEvent JavaDoc(this, WindowEvent.WINDOW_CLOSING));
101     }
102
103     private void ok() {
104         if(hostField.getText().length() == 0) {
105             Util.warn(this, "Please enter router address");
106         }
107         else if(communityField.getText().length() == 0) {
108             Util.warn(this, "Please eneter router community");
109         }
110         else {
111             routerInfo = new RouterInfo();
112             routerInfo.setHost(hostField.getText());
113             routerInfo.setCommunity(communityField.getText());
114             routerInfo.setDescr(descrField.getText());
115             routerInfo.setActive(activeBox.isSelected());
116             close();
117         }
118     }
119
120     private void cancel() {
121         routerInfo = null;
122         close();
123     }
124
125     RouterInfo getRouterInfo() {
126         return routerInfo;
127     }
128 }
129
Popular Tags