simgui.java

来自「一个小型网络仿真器的实现」· Java 代码 · 共 1,854 行 · 第 1/5 页

JAVA
1,854
字号
        x=(minx+maxx)/2;
        y=(miny+maxy)/2;
        //get delta from origin
        x=lastActivate.x - x;
        y=lastActivate.y - y;
        //wheew!!
  
        for(i=0;i<copiedComps.size();i++) {
          SimComponent thisComp=(SimComponent)copiedComps.get(i);
          String name=thisComp.getName();
  
          //ask user for component name
          String newCompName;
          for(;;) {
            newCompName = JOptionPane.showInputDialog(mainFrame,
              "Name (for "+name+"):","Paste Component",JOptionPane.QUESTION_MESSAGE);
            if(newCompName==null) return;
  
            newCompName = newCompName.trim();
            if(newCompName.length()<1) {
              JOptionPane.showMessageDialog(mainFrame,
                "Name cannot be blank!",
                "Error",JOptionPane.INFORMATION_MESSAGE);
              continue;
            }
            if(theSim.isCompNameDuplicate(newCompName)) {
              JOptionPane.showMessageDialog(mainFrame,
                "Duplicate component name, try another one...",
                "Error",JOptionPane.INFORMATION_MESSAGE);
              continue;
            }
            break;
          }

          //got the new name now, so ...
          //go!!!
          SimComponent newComp=generateComponent(thisComp.getClass().getName(),
                                                  thisComp.getCompClass(),newCompName,
                                                  new Point(thisComp.getX()+x,thisComp.getY()+y));

          if(newComp==null) {
            JOptionPane.showMessageDialog(mainFrame,
              "Cannot generate new component!\nNot all components are copied and/or connected!",
              "Error",JOptionPane.ERROR_MESSAGE);
            return;
          }

          newComp.copy(thisComp);
          newcomps.add(newComp);
          newComp.setSelected(true);
          simArea.repaint();
        }

        //now connect the neighbors
        for(i=0;i<copiedComps.size();i++) {
          SimComponent thisComp=(SimComponent)copiedComps.get(i);
          SimComponent newComp=(SimComponent)newcomps.get(i);
          SimComponent [] neighbors=thisComp.getNeighbors();
          for(j=0;j<neighbors.length;j++) {
            int index=copiedComps.indexOf(neighbors[j]);
            if(index!=-1) {
              newComp.addNeighbor((SimComponent)newcomps.get(index));
            }
          }
        }
        connectionsChanged();

        //done!!!
        mainFrame.repaint();
      }
      else if(cmd.equals("Quick 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)
        x=(minx+maxx)/2;
        y=(miny+maxy)/2;
        //get delta from origin
        x=lastActivate.x - x;
        y=lastActivate.y - y;
        //wheew!!

        for(i=0;i<copiedComps.size();i++) {
          SimComponent thisComp=(SimComponent)copiedComps.get(i);
          String name=thisComp.getName();

          int index=0;
          if(Character.isDigit(name.charAt(name.length()-1))) {
            j=name.length()-2;
            while(j>=0 && Character.isDigit(name.charAt(j))) j--;
            index=Integer.parseInt(name.substring(j+1));
            name=name.substring(0,j+1);
          }
          else {
            name=name+" ";
          }
          String newCompName;
          do {
            index++;
            newCompName=name+String.valueOf(index);
          } while(theSim.isCompNameDuplicate(newCompName));

          //got the new name now, so ...
          //go!!!
          SimComponent newComp=generateComponent(thisComp.getClass().getName(),
                                                  thisComp.getCompClass(),newCompName,
                                                  new Point(thisComp.getX()+x,thisComp.getY()+y));
          if(newComp==null) {
            JOptionPane.showMessageDialog(mainFrame,
              "Cannot generate new component!\nNot all components are copied and/or connected!",
              "Error",JOptionPane.ERROR_MESSAGE);
            return;
          }

          newComp.copy(thisComp);
          newcomps.add(newComp);
          newComp.setSelected(true);
          simArea.repaint();
        }
  
        //now connect the neighbors
        for(i=0;i<copiedComps.size();i++) {
          SimComponent thisComp=(SimComponent)copiedComps.get(i);
          SimComponent newComp=(SimComponent)newcomps.get(i);
          SimComponent [] neighbors=thisComp.getNeighbors();
          for(j=0;j<neighbors.length;j++) {
            int index=copiedComps.indexOf(neighbors[j]);
            if(index!=-1) {
              newComp.addNeighbor((SimComponent)newcomps.get(index));
            }
          }
        }
        connectionsChanged();
  
        //done!!!
        mainFrame.repaint();
      }
      else if(cmd.equals("Delete")) {
        if(!checkRunning()) return;
  
        java.util.List components=theSim.getSimComponents();
        java.util.List sel=getSelected();
        int i,j,k;

        //disconnect all neighbors
        for(i=0;i<sel.size();i++) {
          ((SimComponent)sel.get(i)).removeNeighbors(components);
        }
        for(i=0;i<components.size();i++) {
          ((SimComponent)components.get(i)).removeNeighbors(sel);
        }

        //kill all associated dialogs as well
        java.util.ListIterator iter=openDialogs.listIterator();
        while(iter.hasNext()) {
          JDialog dialog=(JDialog)iter.next();
          for(j=0;j<sel.size();j++) {
            if(dialog.getTitle().equals(((SimComponent)sel.get(j)).getName()+" - Properties")) {
              dialog.dispose();
              iter.remove();
              break;
            }
          }
        }
        for(j=0;j<sel.size();j++) {
          SimComponent comp=(SimComponent)sel.get(j);
          SimParameter [] params=comp.getParameters();
          for(k=0;k<params.length;k++) {
            if(params[k].isMeterShowing()) {
              JDialog dialog=params[k].getMeterDialog();
              dialog.dispose();
              meterDialogs.remove(dialog);
            }
          }
        }

        theSim.removeComponents(sel);
        mainFrame.repaint();
      }
      else if(cmd.equals("Save")) {
        if(!allowSimulation) return;
        if(!checkRunning()) return;

        if(currentFile==null) doSaveAs();
        else {
          if(!theSim.saveSim(currentFile)) {
            JOptionPane.showMessageDialog(mainFrame,"Error saving to file "+currentFile.getName(),
                                            "Error",JOptionPane.ERROR_MESSAGE);
            mainFrame.repaint();
          }
        }
      }
      else if(cmd.equals("Save As...")) {
        if(!allowSimulation) return;
        if(!checkRunning()) return;

        doSaveAs();
      }
      else if(cmd.equals("Save Topology...")) {
        if(!allowSimulation) return;
        if(!checkRunning()) return;

        if(fileChooser==null) {
          fileChooser=new JFileChooser(System.getProperty("user.dir"));
        }
        else {
          fileChooser.rescanCurrentDirectory();
        }
        if(fileChooser.showSaveDialog(mainFrame) != JFileChooser.APPROVE_OPTION) {
          mainFrame.repaint();
          return;
        }
        File theFile=fileChooser.getSelectedFile();

        try {
          PrintWriter outfile=new PrintWriter(new BufferedWriter(new FileWriter(theFile)));

          java.util.List components=theSim.getSimComponents();
          outfile.println(components.size()); //record total number of components

          int i,j;
          for(i=0;i<components.size();i++) {
            SimComponent thiscomp=(SimComponent)components.get(i);
            
            outfile.println(thiscomp.getName()); //component name
            outfile.println(thiscomp.getCompClass()); //component class
            outfile.println(thiscomp.getClass().getName()); //component type
            outfile.println(thiscomp.getX()); //location
            outfile.println(thiscomp.getY());
          }

          //now record the neighbors
          for(i=0;i<components.size();i++) {
            SimComponent thiscomp=(SimComponent)components.get(i);
            SimComponent [] neighbors=thiscomp.getNeighbors();

            outfile.println(neighbors.length); //record number of neighbors
            for(j=0;j<neighbors.length;j++) {
              int index=components.indexOf(neighbors[j]);
              outfile.println(index); //neighbor's index
            }
          }

          outfile.close(); //done!
        } catch(Exception ex) {
          JOptionPane.showMessageDialog(mainFrame,"Error saving to file "+theFile.getName(),
                                          "Error",JOptionPane.ERROR_MESSAGE);
          mainFrame.repaint();
        }
      }
      else if(cmd.equals("Load Topology...")) {
        if(!allowSimulation) return;
        if(!checkRunning()) return;
  
        if(fileChooser==null) {
          fileChooser=new JFileChooser(System.getProperty("user.dir"));
        }
        else {
          fileChooser.rescanCurrentDirectory();
        }
        if(fileChooser.showOpenDialog(mainFrame) != JFileChooser.APPROVE_OPTION) {
          mainFrame.repaint();
          return;
        }
        File theFile=fileChooser.getSelectedFile();

        //execute a "New" command first...
        doNew();

        //read from the topology file...
        try {
          BufferedReader infile=new BufferedReader(new FileReader(theFile));
          String aline=infile.readLine();
          if(aline==null) throw new Exception();
          int num=Integer.parseInt(aline); //get total number of components
          int i,j,x,y;
          for(i=0;i<num;i++) {
            String name=infile.readLine();
            if(name==null) throw new Exception();
            String className=infile.readLine();
            if(className==null) throw new Exception();
            String comptype=infile.readLine();
            if(comptype==null) throw new Exception();
            aline=infile.readLine();
            if(aline==null) throw new Exception();
            x=Integer.parseInt(aline);
            aline=infile.readLine();
            if(aline==null) throw new Exception();
            y=Integer.parseInt(aline);

            //go!
            if(generateComponent(comptype,className,name,new Point(x,y))==null)
              throw new Exception();
          }

          //add neighbors
          java.util.List components=theSim.getSimComponents();
          for(i=0;i<num;i++) {
            SimComponent thiscomp=(SimComponent)components.get(i);
            aline=infile.readLine();
            if(aline==null) throw new Exception();
            int numneighbors=Integer.parseInt(aline); //get number of neighbors
            for(j=0;j<numneighbors;j++) {
              aline=infile.readLine();
              if(aline==null) throw new Exception();
              int index=Integer.parseInt(aline); //get neighbor index
              if(index==-1) continue; //should not happen, just in case...
              thiscomp.addNeighbor((SimComponent)components.get(index));
            }
          }

          infile.close(); //done!
        } catch(Exception ex) {
          JOptionPane.showMessageDialog(mainFrame,"Error opening file "+theFile.getName(),
                                          "Error",JOptionPane.ERROR_MESSAGE);
          doNew();
        }

        mainFrame.repaint();
      }
      else if(cmd.equals("Open...")) {
        if(!allowSimulation) return;
        if(!checkRunning()) return;
  
        if(fileChooser==null) {
          fileChooser=new JFileChooser(System.getProperty("user.dir"));
        }
        else {
          fileChooser.rescanCurrentDirectory();
        }
        if(fileChooser.showOpenDialog(mainFrame) != JFileChooser.APPROVE_OPTION) {
          mainFrame.repaint();
          return;
        }

        doOpen(fileChooser.getSelectedFile());
      }
      else if(cmd.equals("Reset Zoom")) {
        isZooming=false;
        zoomFactor=1.0;
        simArea.resetZooming();
        doFit();
      }
      else if(cmd.equals("Zoom In")) {
        isZooming=true;
        zoomFactor*=1.2;
        simArea.setZooming(zoomFactor);
        doFit();
      }
      else if(cmd.equals("Zoom Out")) {
        isZooming=true;
        zoomFactor*=0.8;
        simArea.setZooming(zoomFactor);
        doFit();
      }
      else if(cmd.equals("Zoom Tool")) {
        if(zoomTool==null) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?