📄 simparamiprtable.java
字号:
private boolean checkRunning() {
if(getOwner().getSim().isRunning()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Simulation is running!",
"Command not allowed",JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
public void actionPerformed(ActionEvent e) {
String command=e.getActionCommand();
if(command.equals("Add")) {
if(!checkRunning()) return;
//handle IP
if(!ipOK()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Not a valid IP address!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtIP.requestFocus();
return;
}
if(!maskOK()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Not a valid net mask!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtMask.requestFocus();
return;
}
if(!nextIPOK()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Not a valid next hop IP address!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtNextIP.requestFocus();
return;
}
//ok, now add it
int insertpoint=addNewEntry(newIP,newMask,newNextIP,
(SimComponent)neighbors.get(comps.getSelectedIndex()),
"Static");
table.setRowSelectionInterval(insertpoint,insertpoint);
}
else if(command.equals("Update")) {
if(!checkRunning()) return;
int [] sels=table.getSelectedRows();
if(sels.length!=1) return; //update target not properly selected
//handle IP
if(!ipOK()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Not a valid IP address!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtIP.requestFocus();
return;
}
if(!maskOK()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Not a valid net mask!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtMask.requestFocus();
return;
}
if(!nextIPOK()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Not a valid next hop IP address!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtNextIP.requestFocus();
return;
}
//remove old entry, and add new one
rtable.remove(sels[0]);
int insertpoint=addNewEntry(newIP,newMask,newNextIP,
(SimComponent)neighbors.get(comps.getSelectedIndex()),
"Static");
table.setRowSelectionInterval(insertpoint,insertpoint);
}
else if(command.equals("Remove")) {
if(!checkRunning()) return;
int [] sels=table.getSelectedRows();
java.util.List selcomps=new java.util.ArrayList();
for(int i=0;i<sels.length;i++)
selcomps.add(rtable.get(sels[i]));
rtable.removeAll(selcomps);
table.getSelectionModel().clearSelection();
table.revalidate();
rtableDialog.repaint();
}
else if(command.equals("Close")) {
rtableDialog.dispose();
}
}
}
//helper for generating the combobox contents
// and fill in the neighbor list
private String [] getOutLinks(java.util.List outlinks) {
int i;
for(i=0;i<getOwner().neighborCount();i++) {
if(getOwner().neighbor(i).getCompClass().equals("Link"))
outlinks.add(getOwner().neighbor(i));
}
String [] names=new String[outlinks.size()];
for(i=0;i<names.length;i++) {
names[i]=((SimComponent)outlinks.get(i)).getName();
}
return names;
}
//helper for monitoring changes in the simulation (e.g. to refresh table)
private void checkLinks() {
int i;
//check if there are still neighbors...
if(getOwner().neighborCount()==0) {
//remove all entries
rtable.clear();
if(rtableDialog!=null) rtableDialog.dispose();
return;
}
//check for removal of neighbors...
java.util.Iterator it=rtable.iterator();
while(it.hasNext()) {
IPRouteEntry r=(IPRouteEntry)it.next();
for(i=0;i<getOwner().neighborCount();i++) {
if(getOwner().neighbor(i)==r.nexthop) break;
}
if(i==getOwner().neighborCount())
it.remove();
}
//refresh dialog box if it's there...
if(rtableDialog!=null) {
rtableDialog.getContentPane().removeAll();
JComponent newpane=createNewRTableDialog();
rtableDialog.getContentPane().add(newpane);
newpane.revalidate();
}
}
//helper for string ip/mask
private String getStringIP(int valueip) {
String ip=String.valueOf((valueip>>24)&0xff)+".";
ip+=String.valueOf((valueip>>16)&0xff)+".";
ip+=String.valueOf((valueip>>8)&0xff)+".";
ip+=String.valueOf(valueip&0xff);
return ip;
}
//the table model
private class MyTableModel extends javax.swing.table.AbstractTableModel {
public int getRowCount() {
return rtable.size();
}
public int getColumnCount() {
return 5;
}
public Object getValueAt(int row,int col) {
switch(col) {
case 0:
return getStringIP(((IPRouteEntry)rtable.get(row)).ip);
case 1:
return getStringIP(((IPRouteEntry)rtable.get(row)).mask);
case 2:
int nextip=((IPRouteEntry)rtable.get(row)).nexthopIP;
if(nextip==0) return new String("Direct");
else return getStringIP(nextip);
case 3:
return ((IPRouteEntry)rtable.get(row)).nexthop.getName();
case 4:
return ((IPRouteEntry)rtable.get(row)).type;
}
return null;
}
public String getColumnName(int col) {
switch(col) {
case 0: return "IP";
case 1: return "Mask";
case 2: return "Next Hop IP";
case 3: return "Out Link";
case 4: return "Type";
}
return "N/A";
}
}
private JComponent createNewRTableDialog() {
//prepare the input panel
JPanel inputPanel1=new JPanel();
inputPanel1.add(new JLabel("IP :"));
JTextField ip=new JTextField(12);
inputPanel1.add(ip);
inputPanel1.add(new JLabel("Mask:"));
JTextField mask=new JTextField(12);
inputPanel1.add(mask);
JPanel inputPanel2=new JPanel();
inputPanel2.add(new JLabel("Next Hop IP:"));
JTextField nextip=new JTextField(12);
inputPanel2.add(nextip);
inputPanel2.add(new JLabel("Out Links:"));
java.util.List outlinks=new java.util.ArrayList();
JComboBox comps=new JComboBox(getOutLinks(outlinks));
comps.setSelectedIndex(0);
inputPanel2.add(comps);
//the listener
DialogListener listener=new DialogListener(ip,mask,nextip,comps,outlinks);
//prepare the table
table=new JTable(new MyTableModel());
JScrollPane scrollPane=new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(500,100));
table.getSelectionModel().addListSelectionListener(listener);
//prepare buttons panel
JPanel compPanel=new JPanel();
JButton btnAdd=new JButton("Add");
btnAdd.addActionListener(listener);
compPanel.add(btnAdd);
JButton btnUpdate=new JButton("Update");
btnUpdate.addActionListener(listener);
compPanel.add(btnUpdate);
JButton btnRemove=new JButton("Remove");
btnRemove.addActionListener(listener);
compPanel.add(btnRemove);
JButton btnClose=new JButton("Close");
btnClose.addActionListener(listener);
compPanel.add(btnClose);
//now the outer panel
JPanel outerpane=new JPanel();
GridBagLayout gl=new GridBagLayout();
outerpane.setLayout(gl);
GridBagConstraints gc=new GridBagConstraints();
gc.fill=GridBagConstraints.BOTH;
gc.gridx=0; gc.gridy=0;
gl.setConstraints(inputPanel1,gc);
outerpane.add(inputPanel1);
gc.gridy=1;
gl.setConstraints(inputPanel2,gc);
outerpane.add(inputPanel2);
gc.gridy=2;
gl.setConstraints(compPanel,gc);
outerpane.add(compPanel);
gc.gridy=3; gc.weightx=1; gc.weighty=1;
gl.setConstraints(scrollPane,gc);
outerpane.add(scrollPane);
return outerpane;
}
public void actionPerformed(ActionEvent e) {
if(rtableDialog==null) {
rtableDialog=new SimDialog(theGUI.getMainFrame(),getOwner().getName()
+" - "+getName(),getOwner().getSim(),theGUI) {
public void connectionsChanged() { checkLinks(); }
public void componentsChanged() { checkLinks(); }
};
rtableDialog.getContentPane().add(createNewRTableDialog());
rtableDialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
rtableDialog=null;
}
});
rtableDialog.pack();
Dimension dsize=rtableDialog.getSize();
Dimension scrsize=Toolkit.getDefaultToolkit().getScreenSize();
rtableDialog.setLocation((scrsize.width-dsize.width)/2,(scrsize.height-dsize.height)/2);
rtableDialog.show();
}
else
rtableDialog.requestFocus();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -