1 25 package org.jrobin.mrtg.client; 26 27 import javax.swing.*; 28 import java.awt.*; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.KeyEvent ; 32 import java.awt.event.WindowEvent ; 33 import java.io.*; 34 35 class HostDialog extends JDialog { 36 private static final String HOST_FILENAME = System.getProperty("user.home") + 37 System.getProperty("file.separator") + ".last-mrtg-host"; 38 private static final String DEFAULT_HOST = "localhost"; 39 private static final String TITLE = "Select JRobin-MRTG host"; 40 41 private String 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 mrtgHost = MrtgData.getInstance().getMrtgHost(); 63 if(mrtgHost != null) { 64 hostField.setText(mrtgHost); 65 } 66 else { 67 String savedHost = getHostFromFile(); 68 hostField.setText(savedHost == null? DEFAULT_HOST: savedHost); 69 } 70 hostField.selectAll(); 71 okButton.addActionListener(new ActionListener () { 72 public void actionPerformed(ActionEvent e) { ok(); } 73 }); 74 cancelButton.addActionListener(new ActionListener () { 75 public void actionPerformed(ActionEvent e) { cancel(); } 76 }); 77 okButton.setMnemonic(KeyEvent.VK_O); 78 cancelButton.setMnemonic(KeyEvent.VK_C); 79 getRootPane().setDefaultButton(okButton); 80 81 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 83 Util.centerOnScreen(this); 84 } 85 86 String getHost() { 87 return host; 88 } 89 90 private void ok() { 91 String 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 (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 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 |