simgui.java
来自「一个小型网络仿真器的实现」· Java 代码 · 共 1,854 行 · 第 1/5 页
JAVA
1,854 行
}
iter=meterDialogs.listIterator();
while(iter.hasNext()) {
JDialog dialog=(JDialog)iter.next();
dialog.dispose();
iter.remove();
}
iter=customDialogs.listIterator();
while(iter.hasNext()) {
SimDialog dialog=(SimDialog)iter.next();
dialog.dispose();
//note: a custom dialog will remove itself from the list
}
}
private JComponent createNewPropertySheet(SimComponent thisComp) {
SimParameter [] params=thisComp.getParameters();
JPanel base=new JPanel();
GridBagLayout gl=new GridBagLayout();
GridBagConstraints gc=new GridBagConstraints();
gc.fill=GridBagConstraints.BOTH;
gc.insets=new Insets(0,2,0,2);
base.setLayout(gl);
int j;
for(j=0;j<params.length;j++) {
JCheckBox isLog=new JCheckBox("",params[j].isLogging());
isLog.setEnabled(params[j].isLoggable());
isLog.setBorder(BorderFactory.createEmptyBorder());
JCheckBox isMeter=new JCheckBox("",params[j].isMeterShowing());
isMeter.setEnabled(params[j].isLoggable());
isMeter.setBorder(BorderFactory.createEmptyBorder());
if(!params[j].isMeterShowing() && params[j].gotMeter())
isMeter.setBackground(Color.white);
if(params[j].isLoggable()) {
isLog.addItemListener(params[j].getLogListener());
isMeter.addItemListener(params[j].getMeterListener(isMeter));
}
JLabel name=new JLabel(params[j].getName());
name.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(0,5,0,5)));
JComponent comp=params[j].getComponent(this);
gc.gridx=0; gc.gridy=j; gc.weightx=0;
gl.setConstraints(isLog,gc);
base.add(isLog);
gc.gridx=1;
gl.setConstraints(isMeter,gc);
base.add(isMeter);
gc.gridx=2;
gl.setConstraints(name,gc);
base.add(name);
gc.gridx=3; gc.weightx=1;
gl.setConstraints(comp,gc);
base.add(comp);
}
JScrollPane scroller=new JScrollPane(base,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroller.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
return scroller;
}
private JDialog createPropertyDialog(SimComponent thisComp) {
JDialog dialog=new JDialog(mainFrame,thisComp.getName()+" - Properties",false);
dialog.getContentPane().add(createNewPropertySheet(thisComp));
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
openDialogs.remove((JDialog)e.getSource());
}
public void windowClosed(WindowEvent e) {
mainFrame.repaint();
}
});
dialog.pack();
return dialog;
}
private void doFit() {
int i;
java.util.List components=theSim.getSimComponents();
if(components.isEmpty()) {
simArea.setPreferredSize(new Dimension(10,10));
simArea.revalidate();
mainFrame.repaint();
GUIChanged();
return;
}
int minx=Integer.MAX_VALUE,miny=Integer.MAX_VALUE;
int maxx=Integer.MIN_VALUE,maxy=Integer.MIN_VALUE;
for(i=0;i<components.size();i++) {
SimComponent comp=(SimComponent)components.get(i);
int x=comp.getX();
int y=comp.getY();
Image img=comp.getImage(simArea);
int w=img.getWidth(simArea);
int h=img.getHeight(simArea);
if(minx > (x-w/2)) minx=x-w/2;
if(miny > (y-h/2)) miny=y-h/2;
if(maxx < (x+w/2)) maxx=x+w/2;
if(maxy < (y+h/2)) maxy=y+h/2;
}
minx-=5; miny-=5; maxx+=5; maxy+=5; //adjust border
Dimension areaDim=mainScrollPane.getSize();
if(isZooming) {
areaDim.width=(int)(areaDim.width/zoomFactor);
areaDim.height=(int)(areaDim.height/zoomFactor);
}
if(areaDim.width>(maxx-minx)) {
int shift=(areaDim.width-(maxx-minx))/2;
minx-=shift;
maxx-=shift;
}
if(areaDim.height>(maxy-miny)) {
int shift=(areaDim.height-(maxy-miny))/2;
miny-=shift;
maxy-=shift;
}
for(i=0;i<components.size();i++) {
SimComponent comp=(SimComponent)components.get(i);
comp.setLocation(comp.getX()-minx,comp.getY()-miny);
}
Dimension newDim=new Dimension(maxx-minx,maxy-miny);
if(isZooming) {
newDim.width=(int)(newDim.width*zoomFactor);
newDim.height=(int)(newDim.height*zoomFactor);
}
simArea.setPreferredSize(newDim);
simArea.revalidate();
mainFrame.repaint();
GUIChanged();
}
private boolean checkRunning() {
if(theSim.isRunning()) {
JOptionPane.showMessageDialog(mainFrame,"Simulation is running!",
"Command not allowed",JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
private SimComponent generateComponent(String className,String compClass,
String newCompName,Point location) {
try {
Class compDef = Class.forName(className);
Class[] args = new Class[] { String.class,String.class,Sim.class,int.class,int.class };
java.lang.reflect.Constructor constr = compDef.getDeclaredConstructor(args);
Integer locx=new Integer(location.x);
Integer locy=new Integer(location.y);
Object[] argvals = new Object[] { newCompName,compClass,theSim,locx,locy };
SimComponent newComp=(SimComponent)constr.newInstance(argvals);
theSim.addNewComponent(newComp);
return newComp;
} catch(Exception e) {
JOptionPane.showMessageDialog(mainFrame,e.toString(),
"Component Creation Error",JOptionPane.ERROR_MESSAGE);
}
return null;
}
/////////////////////////// end helpers //////////////////////////////////
/////////////////////////////// services /////////////////////////////////
long getYieldPeriod() {
return yieldPeriod;
}
void runProbing(Runnable r) {
SwingUtilities.invokeLater(r);
}
void setClockTick(long tick) {
evthread_tick = tick;
Runnable r = new Runnable() {
public void run() {
clockLabel.setText(SimClock.getClockString(evthread_tick));
}
};
SwingUtilities.invokeLater(r);
}
void resume() {
simArea.startAnim(); //ask simArea to check whether to start animation
}
void pause() {
simArea.stopAnim();
}
void reset() {
simArea.stopAnim();
simArea.repaint();
}
boolean isLinkTagging() {
return linkTaggingDrag;
}
boolean isAppTagging() {
return appTaggingDrag;
}
void setUserState(String str) {
startButton.setText(str);
}
void connectionsChanged() {
for(int i=0;i<customDialogs.size();i++) {
((SimDialog)customDialogs.get(i)).connectionsChanged();
}
}
void componentsChanged() {
for(int i=0;i<customDialogs.size();i++) {
((SimDialog)customDialogs.get(i)).componentsChanged();
}
}
void GUIChanged() {
for(int i=0;i<customDialogs.size();i++) {
((SimDialog)customDialogs.get(i)).GUIChanged();
}
}
void notifyParamsChange(SimComponent comp) {
//kill existing meter dialog if owner lost
int j,k;
java.util.List components=theSim.getSimComponents();
java.util.ListIterator iter=meterDialogs.listIterator();
while(iter.hasNext()) {
JDialog dialog=(JDialog)iter.next();
//must ask everybody to ensure ownership
outer: for(j=0;j<components.size();j++) {
SimParameter [] params=((SimComponent)components.get(j)).getParameters();
for(k=0;k<params.length;k++) {
if(params[k].isMeterShowing()) {
if(dialog==params[k].getMeterDialog()) break outer; //still got owner
}
}
}
if(j==components.size()) { //no owner, kill it
dialog.dispose();
iter.remove();
}
}
//renew the properties dialog if needed
iter=openDialogs.listIterator();
while(iter.hasNext()) {
JDialog dialog=(JDialog)iter.next();
if(dialog.getTitle().equals(comp.getName()+" - Properties")) {
dialog.getContentPane().removeAll();
dialog.getContentPane().add(createNewPropertySheet(comp));
dialog.validate();
break;
}
}
}
void addMeterDialog(JDialog dialog) {
if(!meterDialogs.contains(dialog)) meterDialogs.add(dialog);
}
void removeMeterDialog(JDialog dialog) {
meterDialogs.remove(dialog);
}
void addCustomDialog(SimDialog dialog) {
if(!customDialogs.contains(dialog)) customDialogs.add(dialog);
}
void removeCustomDialog(SimDialog dialog) {
customDialogs.remove(dialog);
}
void doOpen(File theFile) {
windowCloseAll();
if(theSim.openSim(theFile)) {
currentFile = theFile;
mainFrame.setTitle(Sim.SIM_NAME+" - "+currentFile.getName());
}
else {
JOptionPane.showMessageDialog(mainFrame,"Error opening file "+theFile.getName(),
"Error",JOptionPane.ERROR_MESSAGE);
doNew();
}
mainFrame.repaint();
}
public JFrame getMainFrame() {
return mainFrame;
}
public java.util.List getSelected() {
java.util.List sel=new java.util.ArrayList();
java.util.List components=theSim.getSimComponents();
for(int i=0;i<components.size();i++) {
SimComponent thisComp=(SimComponent)components.get(i);
if(thisComp.isSelected()) {
sel.add(thisComp);
}
}
return sel;
}
void endConnectMode() {
connectButton.setText("Connect Mode");
simArea.changeMode(SimPanel.NORMAL);
allowSimulation=true;
}
boolean registerAnimation(SimComponent acomp,int compinfo_id) {
simArea.registerAnimation(acomp,compinfo_id);
return true;
}
void unregisterAnimation(SimComponent acomp,int compinfo_id) {
simArea.unregisterAnimation(acomp,compinfo_id);
}
void showPopupMenu(MouseEvent e) {
lastActivate=new Point(e.getPoint());
JPopupMenu popup=new JPopupMenu();
JMenuItem menuItem;
menuItem=new JMenuItem("Cancel");
popup.add(menuItem);
popup.addSeparator();
java.util.List sel=getSelected();
if(sel.size()==0) {
popup.add(compMenu);
if(copiedComps!=null) {
popup.addSeparator();
menuItem=new JMenuItem("Quick Paste");
popup.add(menuItem);
menuItem.addActionListener(cmdListener);
menuItem=new JMenuItem("Paste");
popup.add(menuItem);
menuItem.addActionListener(cmdListener);
}
}
if(sel.size()>0) {
menuItem=new JMenuItem("Copy");
popup.add(menuItem);
menuItem.addActionListener(cmdListener);
menuItem=new JMenuItem("Delete");
popup.add(menuItem);
menuItem.addActionListener(cmdListener);
if(sel.size()==1) {
menuItem=new JMenuItem("Select By Type");
popup.add(menuItem);
menuItem.addActionListener(cmdListener);
menuItem=new JMenuItem("Select By Class");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?