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 34 class EditRouterDialog extends JDialog { 35 static final String ADD_TITLE = "New router"; 36 static final String 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 () { 72 public void actionPerformed(ActionEvent e) { 73 ok(); 74 } 75 }); 76 cancelButton.addActionListener(new ActionListener () { 77 public void actionPerformed(ActionEvent e) { 78 cancel(); 79 } 80 }); 81 content.add(box); 82 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 83 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 (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 |