KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.*;
34
35 class HostDialog extends JDialog {
36     private static final String JavaDoc HOST_FILENAME = System.getProperty("user.home") +
37         System.getProperty("file.separator") + ".last-mrtg-host";
38     private static final String JavaDoc DEFAULT_HOST = "localhost";
39     private static final String JavaDoc TITLE = "Select JRobin-MRTG host";
40
41     private String JavaDoc host;
42
43     private JLabel hostLabel = Util.standardLabel("Host address:");
44     private JTextField hostField = Util.standardTextField();
45     private JButton okButton = Util.standardButton("OK");
46     private JButton cancelButton = Util.standardButton("Cancel");
47
48     HostDialog(Frame parent) {
49         super(parent, TITLE, true);
50         constructUserInterface();
51         pack();
52         setVisible(true);
53     }
54
55     private void constructUserInterface() {
56         JPanel content = (JPanel) getContentPane();
57         Box box = Box.createVerticalBox();
58         box.add(Util.getPanelFor(hostLabel, hostField));
59         box.add(Util.getPanelFor(Util.standardLabel(), okButton, cancelButton));
60         content.add(box);
61
62         String JavaDoc mrtgHost = MrtgData.getInstance().getMrtgHost();
63         if(mrtgHost != null) {
64             hostField.setText(mrtgHost);
65         }
66         else {
67             String JavaDoc savedHost = getHostFromFile();
68             hostField.setText(savedHost == null? DEFAULT_HOST: savedHost);
69         }
70         hostField.selectAll();
71         okButton.addActionListener(new ActionListener JavaDoc() {
72             public void actionPerformed(ActionEvent JavaDoc e) { ok(); }
73         });
74         cancelButton.addActionListener(new ActionListener JavaDoc() {
75             public void actionPerformed(ActionEvent JavaDoc e) { cancel(); }
76         });
77         okButton.setMnemonic(KeyEvent.VK_O);
78         cancelButton.setMnemonic(KeyEvent.VK_C);
79         getRootPane().setDefaultButton(okButton);
80
81         // finalzie
82
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
83         Util.centerOnScreen(this);
84     }
85
86     String JavaDoc getHost() {
87         return host;
88     }
89
90     private void ok() {
91         String JavaDoc hostEntered = hostField.getText();
92         if(hostEntered.length() == 0) {
93             Util.warn(this, "Please enter host address");
94         }
95         else {
96             host = hostEntered;
97             saveHostToFile();
98             close();
99         }
100     }
101
102     private void close() {
103         dispatchEvent(new WindowEvent JavaDoc(this, WindowEvent.WINDOW_CLOSING));
104     }
105
106     private void cancel() {
107         close();
108     }
109
110     private void saveHostToFile() {
111         PrintWriter pw = null;
112         try {
113             pw = new PrintWriter(new FileWriter(HOST_FILENAME, false));
114             pw.println(host);
115         }
116         catch (IOException e) {
117         }
118         finally {
119             if(pw != null) {
120                 pw.close();
121             }
122         }
123     }
124
125     private String JavaDoc getHostFromFile() {
126         BufferedReader reader = null;
127         try {
128             reader = new BufferedReader(new FileReader(HOST_FILENAME));
129             return reader.readLine();
130         }
131         catch (IOException e) {
132             return null;
133         }
134         finally {
135             if(reader != null) {
136                 try {
137                     reader.close();
138                 }
139                 catch(IOException e) { }
140             }
141         }
142     }
143 }
144
Popular Tags