simgui.java
来自「一个小型网络仿真器的实现」· Java 代码 · 共 1,854 行 · 第 1/5 页
JAVA
1,854 行
/*
JaNetSim --- Java Network Simulator
-------------------------------------
This software was developed at the Network Research Lab, Faculty of
Computer Science and Information Technology (FCSIT), University of Malaya.
This software may be used and distributed freely. FCSIT assumes no responsibility
whatsoever for its use by other parties, and makes no guarantees, expressed or
implied, about its quality, reliability, or any other characteristic.
We would appreciate acknowledgement if the software is used.
FCSIT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
FROM THE USE OF THIS SOFTWARE.
*/
package janetsim;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SimGUI {
private Sim theSim=null; //reference to the Sim
private JFrame mainFrame=null; //the main window
private SimPanel simArea=null; //simulation area
private JScrollPane mainScrollPane=null; //for fitting in scrollpane
private JViewport mainViewport=null; //for fast scroll test
private CommandListener cmdListener=null; //main command handler
private JMenu compMenu=null; //the component creation menu
private JLabel clockLabel=null; //label for clock
private JButton startButton=null; //start button
private JButton connectButton=null; //connect button
private JCheckBoxMenuItem fastScrollCB=null; //the fast scroll checkbox
private JCheckBoxMenuItem linktagCB=null;
private JCheckBoxMenuItem apptagCB=null;
private JMenuItem clearCopyBuffer=null;
private boolean linkTaggingDrag=true; //straight link with others
private boolean appTaggingDrag=true; //move apps with others
private Point lastActivate=null; //location of last popup menu activation
private long yieldPeriod=Sim.DEFAULT_YIELD; //GUI priority
private JDialog GUIDialog=null; //GUI priority dialog
private JSlider GUISlider=null; //GUI priority slider
private boolean allowSimulation=true; //(false when in connect mode)
private boolean isZooming=false; //whether it's non-default size currently
private double zoomFactor=1.0; //the zooming factor
private SimDialog zoomTool=null; //the zoom tool dialog
private JSlider zoomSlider=null; //the zoom tool slider
private java.util.List copiedComps=null; //copy 'buffer'
private java.util.List openDialogs=null; //opened property dialogs
private java.util.List meterDialogs=null; //opened meter dialogs
private java.util.List customDialogs=null; //opened custom dialogs
private JFileChooser fileChooser=null; //file open dialog
private File currentFile=null;
//variables used for swing invokeLater event threads...
private long evthread_tick;
///////////////////////// end fields //////////////////////////////////////
private class CommandListener implements ActionListener,ItemListener {
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
if(cmd.equals("Close All")) {
windowCloseAll();
mainFrame.repaint();
}
else if(cmd.equals("Select All")) {
if(!allowSimulation) return;
java.util.List components=theSim.getSimComponents();
for(int i=0;i<components.size();i++) {
((SimComponent)components.get(i)).setSelected(true);
}
mainFrame.repaint();
}
else if(cmd.equals("Select By Class")) {
if(!allowSimulation) return;
java.util.List sel=getSelected();
if(sel.isEmpty()) {
JOptionPane.showMessageDialog(mainFrame,"I need a reference component, select one first.",
"Select By Class",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(sel.size()>1) {
JOptionPane.showMessageDialog(mainFrame,"Please select ONE component only as the reference.",
"Select By Class",JOptionPane.INFORMATION_MESSAGE);
return;
}
String theclass=((SimComponent)sel.get(0)).getCompClass();
java.util.List components=theSim.getSimComponents();
for(int i=0;i<components.size();i++) {
SimComponent thiscomp=(SimComponent)components.get(i);
if(thiscomp.getCompClass().equals(theclass)) thiscomp.setSelected(true);
}
mainFrame.repaint();
}
else if(cmd.equals("Select By Type")) {
if(!allowSimulation) return;
java.util.List sel=getSelected();
if(sel.isEmpty()) {
JOptionPane.showMessageDialog(mainFrame,"I need a reference component, select one first.",
"Select By Type",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(sel.size()>1) {
JOptionPane.showMessageDialog(mainFrame,"Please select ONE component only as the reference.",
"Select By Type",JOptionPane.INFORMATION_MESSAGE);
return;
}
Class thetype=sel.get(0).getClass();
java.util.List components=theSim.getSimComponents();
for(int i=0;i<components.size();i++) {
SimComponent thiscomp=(SimComponent)components.get(i);
if(thiscomp.getClass().equals(thetype)) thiscomp.setSelected(true);
}
mainFrame.repaint();
}
else if(cmd.equals("Select By Name Prefix")) {
if(!allowSimulation) return;
String prefix=JOptionPane.showInputDialog(mainFrame,"Name Prefix:",
"Select By Name Prefix",JOptionPane.QUESTION_MESSAGE);
if(prefix==null) return;
prefix=prefix.trim();
java.util.List components=theSim.getSimComponents();
for(int i=0;i<components.size();i++) {
SimComponent thiscomp=(SimComponent)components.get(i);
if(thiscomp.getName().startsWith(prefix)) thiscomp.setSelected(true);
else thiscomp.setSelected(false);
}
mainFrame.repaint();
}
else if(cmd.equals("Reset Log File")) {
if(!allowSimulation) return;
if(!checkRunning()) return;
if(currentFile!=null)
theSim.resetLogFile(currentFile.getName()+".log");
else
theSim.resetLogFile(Sim.SIM_PACKAGE+".log");
JOptionPane.showMessageDialog(mainFrame,"Done!","Reset Log File",JOptionPane.INFORMATION_MESSAGE);
mainFrame.repaint();
}
else if(cmd.equals("Global Set Value")) {
if(!checkRunning()) return;
java.util.List sel=getSelected();
if(sel.isEmpty()) return;
int i;
Class thetype=sel.get(0).getClass();
for(i=1;i<sel.size();i++) {
if(!sel.get(i).getClass().equals(thetype)) {
JOptionPane.showMessageDialog(mainFrame,"Not all selected components are of the same type!",
"Error",JOptionPane.INFORMATION_MESSAGE);
mainFrame.repaint();
return;
}
}
//generate the parameter list
SimParameter [] params=((SimComponent)sel.get(0)).getParameters();
if(params.length==0) return; //safety check, very rare...
String [] paramnames=new String[params.length];
for(i=0;i<params.length;i++) paramnames[i]=params[i].getName();
new GSDialogListener(sel,paramnames);
mainFrame.repaint();
}
else if(cmd.equals("Properties")) {
java.util.List sel=getSelected();
int i,j;
outer: for(i=0;i<sel.size();i++) {
SimComponent thisComp=(SimComponent)sel.get(i);
//look for opened dialog
for(j=0;j<openDialogs.size();j++) {
JDialog dialog=(JDialog)openDialogs.get(j);
if(dialog.getTitle().equals(thisComp.getName()+" - Properties")) {
dialog.requestFocus();
continue outer;
}
}
//if not found, open a new one
JDialog dialog=createPropertyDialog(thisComp);
//position it tagged on that component
Point locscr=simArea.getLocationOnScreen();
Dimension ddim=dialog.getPreferredSize();
Dimension sdim=Toolkit.getDefaultToolkit().getScreenSize();
int x,y;
if(isZooming) {
x=(int)(locscr.x+thisComp.getX()*zoomFactor);
y=(int)(locscr.y+thisComp.getY()*zoomFactor);
}
else {
x=locscr.x+thisComp.getX();
y=locscr.y+thisComp.getY();
}
int h=ddim.height;
if(h>sdim.height/2) h=sdim.height/2;
if(x+ddim.width>sdim.width) x=sdim.width-ddim.width;
if(y+h>sdim.height) y=sdim.height-h;
dialog.setBounds(x,y,ddim.width,h);
openDialogs.add(dialog);
dialog.show();
}
}
else if(cmd.equals("Fit All")) {
doFit();
}
else if(cmd.equals("End Connect")) {
endConnectMode();
}
else if(cmd.equals("Connect Mode")) {
if(!checkRunning()) return;
connectButton.setText("End Connect");
allowSimulation=false;
simArea.changeMode(SimPanel.CONNECT);
}
else if(cmd.equals("Flip Horizontally")) {
java.util.List sel=getSelected();
if(sel.isEmpty()) return;
int i,x,minx=Integer.MAX_VALUE,maxx=Integer.MIN_VALUE;
for(i=0;i<sel.size();i++) {
SimComponent thisComp=(SimComponent)sel.get(i);
x=thisComp.getX();
if(x<minx) minx=x;
if(x>maxx) maxx=x;
}
x=(maxx+minx)/2;
for(i=0;i<sel.size();i++) {
SimComponent thisComp=(SimComponent)sel.get(i);
thisComp.setLocation(x+x-thisComp.getX(),thisComp.getY());
}
GUIChanged();
mainFrame.repaint();
}
else if(cmd.equals("Flip Vertically")) {
java.util.List sel=getSelected();
if(sel.isEmpty()) return;
int i,y,miny=Integer.MAX_VALUE,maxy=Integer.MIN_VALUE;
for(i=0;i<sel.size();i++) {
SimComponent thisComp=(SimComponent)sel.get(i);
y=thisComp.getY();
if(y<miny) miny=y;
if(y>maxy) maxy=y;
}
y=(miny+maxy)/2;
for(i=0;i<sel.size();i++) {
SimComponent thisComp=(SimComponent)sel.get(i);
thisComp.setLocation(thisComp.getX(),y+y-thisComp.getY());
}
GUIChanged();
mainFrame.repaint();
}
else if(cmd.equals("Disconnect")) {
if(!checkRunning()) return;
java.util.List sel=getSelected();
for(int i=0;i<sel.size();i++) {
((SimComponent)sel.get(i)).removeNeighbors(sel);
}
connectionsChanged();
mainFrame.repaint();
}
else if(cmd.equals("Copy")) {
java.util.List sel=getSelected();
if(sel.isEmpty()) return;
copiedComps=getSelected();
clearCopyBuffer.setEnabled(true);
}
else if(cmd.equals("Clear Copy Buffer")) {
copiedComps=null;
clearCopyBuffer.setEnabled(false);
}
else if(cmd.equals("GUI Priority...")) {
if(!checkRunning()) return;
GUIDialog=new JDialog(mainFrame,"GUI Priority",true);
GUISlider=new JSlider(JSlider.HORIZONTAL,0,10,
10-(int)(Math.log(yieldPeriod)/Math.log(3)));
GUISlider.setMinorTickSpacing(1);
GUISlider.setPaintTicks(true);
java.util.Hashtable labelTable=new java.util.Hashtable();
labelTable.put(new Integer(0),new JLabel("Lowest (!)"));
labelTable.put(new Integer(50),new JLabel("Highest"));
GUISlider.setLabelTable(labelTable);
GUISlider.setPaintLabels(true);
GUISlider.setSnapToTicks(true);
JPanel pane=new JPanel();
ActionListener listener=new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("OK")) {
yieldPeriod = (long)Math.pow(3,10-GUISlider.getValue());
}
GUIDialog.dispose();
}
};
JButton btn=new JButton("OK");
btn.addActionListener(listener);
pane.add(btn);
btn=new JButton("Cancel");
btn.addActionListener(listener);
pane.add(btn);
JPanel basepane=new JPanel(new BorderLayout());
basepane.add(GUISlider,BorderLayout.CENTER);
basepane.add(pane,BorderLayout.SOUTH);
GUIDialog.setContentPane(basepane);
GUIDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
GUIDialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
GUIDialog=null;
GUISlider=null;
}
});
GUIDialog.pack();
GUIDialog.setResizable(false);
Dimension ddim=GUIDialog.getPreferredSize();
Dimension sdim=Toolkit.getDefaultToolkit().getScreenSize();
GUIDialog.setLocation((sdim.width-ddim.width)/2,(sdim.height-ddim.height)/2);
GUIDialog.show();
}
else if(cmd.equals("Paste")) {
if(!checkRunning()) return;
if(copiedComps.isEmpty()) return; //nothing copied
java.util.List newcomps=new java.util.ArrayList();
int i,j;
int x,y;
int minx=Integer.MAX_VALUE,maxx=Integer.MIN_VALUE;
int miny=Integer.MAX_VALUE,maxy=Integer.MIN_VALUE;
for(i=0;i<copiedComps.size();i++) {
SimComponent thisComp=(SimComponent)copiedComps.get(i);
x=thisComp.getX();
y=thisComp.getY();
if(x<minx) minx=x;
if(x>maxx) maxx=x;
if(y<miny) miny=y;
if(y>maxy) maxy=y;
}
//get average (center of region)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?