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 EditLinkDialog extends JDialog { 35 static final String ADD_TITLE = "New interface"; 36 static final String EDIT_TITLE = "Edit interface data"; 37 static final String DEFAULT_SAMPLING_INTERVAL = "300"; 38 static final int MAX_SAMPLING_INTERVAL = 600; 39 static final int MIN_SAMPLING_INTERVAL = 10; 40 41 private LinkInfo[] linkInfo; 43 44 private String [] ifDescrs; 45 private RouterInfo routerInfo; 46 boolean insertMode = false; 47 48 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 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 [] ifDescrs) { 65 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 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 () { 96 public void actionPerformed(ActionEvent e) { ok(); } 97 }); 98 cancelButton.addActionListener(new ActionListener () { 99 public void actionPerformed(ActionEvent e) { cancel(); } 100 }); 101 content.add(box); 102 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 103 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 (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 (); 137 } 138 } catch(NumberFormatException 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 Object [] 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 )selectedLinks[i]); 155 linkInfo[i].setActive(activeBox.isSelected()); 156 linkInfo[i].setDescr(descrField.getText()); 157 linkInfo[i].setSamplingInterval(samplingInterval); 158 } 159 } 160 else { 161 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 |