⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphvisualizer.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    final JLabel jLbNodeHeight = new JLabel("Height");
    
    jTfNodeWidth.setHorizontalAlignment(JTextField.CENTER);
    jTfNodeWidth.setText(""+nodeWidth);
    jTfNodeHeight.setHorizontalAlignment(JTextField.CENTER);
    jTfNodeHeight.setText(""+nodeHeight);
    jLbNodeWidth.setEnabled(false);
    jTfNodeWidth.setEnabled(false);
    jLbNodeHeight.setEnabled(false);
    jTfNodeHeight.setEnabled(false);
    
    jCbCustomNodeSize.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        if( ((JCheckBox)ae.getSource()).isSelected() ) {
          jLbNodeWidth.setEnabled(true);
          jTfNodeWidth.setEnabled(true);
          jLbNodeHeight.setEnabled(true);
          jTfNodeHeight.setEnabled(true);
        }
        else {
          jLbNodeWidth.setEnabled(false);
          jTfNodeWidth.setEnabled(false);
          jLbNodeHeight.setEnabled(false);
          jTfNodeHeight.setEnabled(false);
          setAppropriateNodeSize();
        }
      }
    });
    
    
    jBtLayout  = new JButton("Layout Graph");
    jBtLayout.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        int tmpW, tmpH;
        
        if(jCbCustomNodeSize.isSelected()) {
          try{ tmpW = Integer.parseInt(jTfNodeWidth.getText()); }
          catch(NumberFormatException ne) {
            JOptionPane.showMessageDialog(GraphVisualizer.this.getParent(),
            "Invalid integer entered for node width.",
            "Error",
            JOptionPane.ERROR_MESSAGE);
            tmpW = nodeWidth;
            jTfNodeWidth.setText(""+nodeWidth);
            
          }
          try{ tmpH = Integer.parseInt(jTfNodeHeight.getText()); }
          catch(NumberFormatException ne) {
            JOptionPane.showMessageDialog(GraphVisualizer.this.getParent(),
            "Invalid integer entered for node height.",
            "Error",
            JOptionPane.ERROR_MESSAGE);
            tmpH = nodeHeight;
            jTfNodeWidth.setText(""+nodeHeight);
          }
          
          if(tmpW!=nodeWidth || tmpH!=nodeHeight) {
            nodeWidth = tmpW; paddedNodeWidth = nodeWidth+8; nodeHeight = tmpH;
          }
        }
        JButton bt = (JButton)ae.getSource();
        bt.setEnabled(false);
        m_le.setNodeSize(paddedNodeWidth, nodeHeight);
        m_le.layoutGraph();
      }
    });
    
    
    GridBagConstraints gbc = new GridBagConstraints();
    
    final JPanel p = new JPanel(new GridBagLayout());
    gbc.gridwidth = gbc.REMAINDER;
    gbc.anchor = gbc.NORTHWEST;
    gbc.fill = gbc.NONE;
    p.add( m_le.getControlPanel(), gbc);
    gbc.gridwidth = 1;
    gbc.insets = new Insets(8,0,0,0);
    gbc.anchor = gbc.NORTHWEST;
    gbc.gridwidth = gbc.REMAINDER;
    
    p.add( jCbCustomNodeSize, gbc );
    gbc.insets = new Insets(0,0,0,0);
    gbc.gridwidth = gbc.REMAINDER;
    Container c = new Container();
    c.setLayout( new GridBagLayout() );
    gbc.gridwidth = gbc.RELATIVE;
    c.add(jLbNodeWidth, gbc);
    gbc.gridwidth = gbc.REMAINDER;
    c.add(jTfNodeWidth, gbc);
    gbc.gridwidth = gbc.RELATIVE;
    c.add(jLbNodeHeight, gbc);
    gbc.gridwidth = gbc.REMAINDER;
    c.add(jTfNodeHeight, gbc);
    gbc.fill = gbc.HORIZONTAL;
    p.add( c, gbc );
    
    gbc.anchor = gbc.NORTHWEST;
    gbc.insets = new Insets(8,0,0,0);
    gbc.fill = gbc.HORIZONTAL;
    p.add( jBtLayout, gbc );
    gbc.fill = gbc.NONE;
    p.setBorder(BorderFactory.createCompoundBorder(
    BorderFactory.createTitledBorder("ExtraControls"),
    BorderFactory.createEmptyBorder(4,4,4,4)
    ) );
    p.setPreferredSize( new Dimension(0, 0) );
    
    final JToolBar jTbTools = new JToolBar();
    jTbTools.setFloatable(false);
    jTbTools.setLayout( new GridBagLayout() );
    gbc.anchor = gbc.NORTHWEST;
    gbc.gridwidth = gbc.REMAINDER;
    gbc.insets = new Insets(0,0,0,0);
    jTbTools.add(p,gbc);
    gbc.gridwidth = 1;
    jTbTools.add(m_jBtSave, gbc);
    jTbTools.addSeparator(new Dimension(2,2));
    jTbTools.add(jBtZoomIn, gbc);
    
    gbc.fill = gbc.VERTICAL;
    gbc.weighty = 1;
    JPanel p2 = new JPanel(new BorderLayout());
    p2.setPreferredSize( jTfZoom.getPreferredSize() );
    p2.setMinimumSize( jTfZoom.getPreferredSize() );
    p2.add(jTfZoom, BorderLayout.CENTER);
    jTbTools.add(p2, gbc);
    gbc.weighty =0;
    gbc.fill = gbc.NONE;
    
    jTbTools.add(jBtZoomOut, gbc);
    jTbTools.addSeparator(new Dimension(2,2));
    jTbTools.add(jBtExtraControls, gbc);
    jTbTools.addSeparator(new Dimension(4,2));
    gbc.weightx = 1;
    gbc.fill = gbc.BOTH;
    jTbTools.add(m_le.getProgressBar(), gbc);
    
    jBtExtraControls.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        Dimension d = p.getPreferredSize();
        if(d.width==0 || d.height==0) {
          LayoutManager lm = p.getLayout();
          Dimension d2 = lm.preferredLayoutSize(p);
          p.setPreferredSize(d2); jTbTools.revalidate();
          /*
          // this piece of code adds in an animation
          // for popping out the extra controls panel
          Thread th = new Thread() {
            int h = 0, w = 0;
            LayoutManager lm = p.getLayout();
            Dimension d2 = lm.preferredLayoutSize(p);
           
            int tow = (int)d2.getWidth(), toh = (int)d2.getHeight();
            //toh = (int)d2.getHeight();
            //tow = (int)d2.getWidth();
           
            public void run() {
              while(h<toh || w<tow) {
                if((h+10)<toh)
                  h += 10;
                else if(h<toh)
                  h = toh;
                if((w+10)<tow)
                  w += 10;
                else if(w<tow)
                  w = tow;
                p.setPreferredSize(new Dimension(w, h));
                //p.invalidate();
                jTbTools.revalidate();
                //paint(Temp4.this.getGraphics());
                try {this.sleep(30);}
                catch(InterruptedException ie) {ie.printStackTrace(); break;}
              }
              p.setPreferredSize(new Dimension(tow,toh)); jTbTools.revalidate();
            }
          };
          th.start();
           */
        }
        else {
          p.setPreferredSize( new Dimension(0,0) );
          jTbTools.revalidate();
          /*
          Thread th = new Thread() {
            int h = p.getHeight(), w = p.getWidth();
            LayoutManager lm = p.getLayout();
            int tow = 0, toh = 0;
           
            public void run() {
              while(h>toh || w>tow) {
                if((h-10)>toh)
                  h -= 10;
                else if(h>toh)
                  h = toh;
                if((w-10)>tow)
                  w -= 10;
                else if(w>tow)
                  w = tow;
           
                p.setPreferredSize(new Dimension(w, h));
                //p.invalidate();
                jTbTools.revalidate();
                //paint(Temp4.this.getGraphics());
                try {this.sleep(30);}
                catch(InterruptedException ie) {ie.printStackTrace(); break;}
              }
              p.setPreferredSize(new Dimension(tow,toh)); jTbTools.revalidate();
            }
          };
          th.start();
           */
        }
      }
    });
    this.setLayout( new BorderLayout() );
    this.add(jTbTools, BorderLayout.NORTH);
    this.add(m_js, BorderLayout.CENTER);
  }
  
  
  /**
   * This method sets the node size that is appropriate
   * considering the maximum label size that is present.
   * It is used internally when custom node size checkbox
   * is unchecked.
   */
  protected void setAppropriateNodeSize() {
    int strWidth;
    if(maxStringWidth==0)
      for(int i=0; i<m_nodes.size(); i++) {
        strWidth = fm.stringWidth(((GraphNode)m_nodes.elementAt(i)).lbl);
        if(strWidth>maxStringWidth)
          maxStringWidth=strWidth;
      }
    nodeWidth = maxStringWidth+4;
    paddedNodeWidth = nodeWidth+8;
    jTfNodeWidth.setText(""+nodeWidth);
    
    nodeHeight = 2*fm.getHeight();
    jTfNodeHeight.setText(""+nodeHeight);
  }
  
  /**
   * Sets the preferred size for m_gp GraphPanel to the
   * minimum size that is neccessary to display the graph.
   */
  protected void setAppropriateSize() {
    int maxX=0, maxY=0;
    
    m_gp.setScale(scale, scale);
    
    for(int i=0; i<m_nodes.size(); i++) {
      GraphNode n = (GraphNode)m_nodes.elementAt(i);
      if(maxX<n.x)
        maxX=n.x;
      if(maxY<n.y)
        maxY=n.y;
    }
    //System.out.println("Scale: "+scale+" paddedWidth: "+paddedNodeWidth+
    //                   " nodeHeight: "+nodeHeight+"\nmaxX: "+maxX+" maxY: "+
    //                   maxY+" final: "+(int)((maxX+paddedNodeWidth+2)*scale)+
    //                   ","+(int)((maxY+nodeHeight+2)*scale) );
    m_gp.setPreferredSize(new Dimension((int)((maxX+paddedNodeWidth+2)*scale),
    (int)((maxY+nodeHeight+2)*scale)));
    //System.out.println("Size set to "+this.getPreferredSize());
  }
  
  
  /**
   * This method is an implementation for LayoutCompleteEventListener
   * class. It sets the size appropriate for m_gp GraphPanel and
   * and revalidates it's container JScrollPane once a
   * LayoutCompleteEvent is received from the LayoutEngine.
   */
  public void layoutCompleted(LayoutCompleteEvent le) {
    setAppropriateSize();
    //m_gp.clearBuffer();
    m_gp.invalidate();
    m_js.revalidate();
    m_gp.repaint();
    jBtLayout.setEnabled(true);
  }
  
  
  /**
   * This method lays out the graph by calling the
   * LayoutEngine's layoutGraph() method. This method
   * should be called to display the graph nicely, unless
   * the input XMLBIF03 already contains some layout
   * information (ie the x,y positions of nodes.
   */
  public void layoutGraph() {
    if(m_le!=null)
      m_le.layoutGraph();
    
  }
  
  /*********************************************************
   *
   *  BIF reader<br>
   *  Reads a graph description in XMLBIF03 from a string
   *
   *********************************************************
   */
  public void readBIF(String instring) throws BIFFormatException {
    BIFParser bp = new BIFParser(instring, m_nodes, m_edges);
    try {
      graphID = bp.parse();
    } catch(BIFFormatException bf) {
      System.out.println("BIF format error");
      bf.printStackTrace();
    }
    catch(Exception ex) { ex.printStackTrace(); return; }
    
    setAppropriateNodeSize();
    if(m_le!=null) {
      m_le.setNodeSize(paddedNodeWidth, nodeHeight);
    }
  } //end readBIF1
  
  /**
   *
   *  BIF reader<br>
   *  Reads a graph description in XMLBIF03 from an InputStrem
   *
   *
   */
  public void readBIF(InputStream instream) throws BIFFormatException {
    BIFParser bp = new BIFParser(instream, m_nodes, m_edges);
    try {
      graphID = bp.parse();
    } catch(BIFFormatException bf) {
      System.out.println("BIF format error");
      bf.printStackTrace();
    }
    catch(Exception ex) { ex.printStackTrace(); return; }
    
    setAppropriateNodeSize();
    if(m_le!=null) {
      m_le.setNodeSize(paddedNodeWidth, nodeHeight);
    }
    setAppropriateSize();
  } //end readBIF2
  
  
  /*********************************************************
   *
   *  Dot reader<br>
   *  Reads a graph description in DOT format from a string
   *
   *********************************************************
   */
  public void readDOT(Reader input) {
    DotParser dp = new DotParser(input, m_nodes, m_edges);
    graphID = dp.parse();
    
    setAppropriateNodeSize();
    if(m_le!=null) {
      m_le.setNodeSize(paddedNodeWidth, nodeHeight);
      jBtLayout.setEnabled(false);
      layoutGraph();

⌨️ 快捷键说明

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