Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * TracesCanvas.java
   *
   * Created on April 16, 2002, 5:40 PM
   */
  
  /* This class displays the trace logged by the server
   * Here is a description of the properties
   *
  *   -------------------------------------------------------  ^
  *   |                                                     |  |
  *   |       Actor 1          Actor 2         Actor 3      |  | 3
  *   |                           |               |
  *   |------------------------------------------------------  v ^
  *   |           |               |               |         |    | 4
  *   |           |-------------->|               |         |    v
  *   |           |               |               |         |
  *   |           |               |               |         |
  *   |           |               |-------------->|         |  ^
  *   |           |               |               |         |  | 5
  *   |           |               |               |         |  |
  *   |           |               |<--------------|         |  v
  *   |           |               |               |         |
  *   |           |               |               |         |
  *   |           |<--------------|               |         |  ^
  *   |           |               |               |         |  | 6
  *   -------------------------------------------------------  v
  *                                                    7
  *         1                             2       <--------->
  *   <----------->               <--------------->
  *
  *
  *  1 : FIRST_ACTOR_GAP
  *  2 : HORIZONTAL_GAP
  *  3 : ACTORS_STRIPE
  *  4 : FIRST_ARROW_GAP
  *  5 : VERTICAL_GAP
  *  6 : LAST_ARROW_GAP
  *  7 : LAST_ACTOR_GAP
  */
 
 package tools.tracesviewer;
 
 import java.awt.*;
 import java.util.*;

Author(s):
deruelle, chazeau
Version:
1.0
 
 public class TracesCanvas
     extends Canvas
     implements MouseListenerMouseMotionListener {
 
     public static int FIRST_ACTOR_GAP = 100;
     public int HORIZONTAL_GAP = 350;
     public static int ACTORS_STRIPE = 120;
     public static int FIRST_ARROW_GAP = 50;
     public static int VERTICAL_GAP = 60;
     public static int LAST_ARROW_GAP = 30;
     public static int LAST_ACTOR_GAP = 100;
 
     // This color is the color of the current selected SIP message
     public Color SELECTED_COLOR = .;
     public Color[] ARROWS_COLOR =
         {
             .,
             new Color(94, 151, 185),
             .,
             new Color(55, 223, 131),
             .,
             new Color(209, 177, 69),
             .,
             new Color(187, 91, 185),
             .,
             new Color(199, 239, 39)};
 
     public String selectedArrowName;
     public Arrow newArrow;
     public Arrow oldArrow;
 
     public Image actorsImage;
     public Image backgroundImage;
 
     public TracesSession tracesSession = null;
     public TextArea messageContentTextArea = null;
     public String tracesOrigin;
 
     public Hashtable actors = new Hashtable();
     public Hashtable arrows = new Hashtable();
     public Hashtable arrowsColors = new Hashtable();
 
     public boolean isAnimated = false;
     public Arrow arrowTipTool = null;
     public TracesViewer tracesViewer;
 
 
    public Dimension getPreferredSize() {
        int width =
            
                + 
                +  * (.size() - 1);
        int height =
            
                + 
                + 
                +  * (.size() - 1);
        return new Dimension(widthheight);
    }
    public TracesCanvas(
        TracesSession tracesSession,
        TextArea messageContentTextArea,
        String trueName,
        TracesViewer tracesViewer) {
        this. = tracesViewer;
        this. = tracesSession;
        this. = messageContentTextArea;
        refreshTracesCanvas(tracesSessiontrueName);
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    // Reserved constructor for the AboutFrame
    public TracesCanvas(
        TracesSession tracesSession,
        Image backgroundImage,
        String trueName,
        int horizontalGap,
        TracesViewer tracesViewer) {
        this. = tracesViewer;
        this. = backgroundImage;
         = horizontalGap;
        this. = tracesSession;
        this. = null;
        refreshTracesCanvas(tracesSessiontrueName);
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    public void refreshTracesCanvas(
        TracesSession tracesSession,
        String tracesOrigin) {
        this. = tracesSession;
        // All of this has to called when the trace is refreshed or a new Session:
        // The actors are the hosts
        this. = tracesOrigin;
        constructActors();
        // All the SIP messages from the session
        constructArrows();
        // We select the first message
        selectMessage(
             +  / 2,
             + );
    }
    public void drawTop(Graphics g) {
        int widthCanvas = getSize().;
        int heightCanvas = getSize().;
        // System.out.println(widthCanvas+","+heightCanvas);
        // purple
        g.setColor(new Color(0, 0, 125));
        // light purple
        //g.setColor(new Color(102,102,153));
        // x, y, width, height
        g.fillRect(0, 0, widthCanvas - 5);
        // Draw the String using the current Font and color
        g.setColor(.);
        // display the label
        Font f = g.getFont();
        Font newFont = new Font(f.getName(), . | ., 17);
        g.setFont(newFont);
        // String, x,y
        g.drawString("Trace retrieved from " + , 40, 25);
        // draw a separation line:
        g.setColor(.);
        // x,y -> x,y
        g.drawLine(0, widthCanvas);
        // draw the actors above the separation line and their vertical line:
        Enumeration e = .keys();
        while (e.hasMoreElements()) {
            String origin = (Stringe.nextElement();
            int positionX = ((Integer.get(origin)).intValue();
            // if we have an image for the actors display it
            // otherwise just do nothing
            // Draw the name of the actor and its address below its icon:
            // String, x,y
            g.setColor(.);
            //  if (origin.equals(TracesViewerLauncher.stackId) ){
            //       g.drawString(tracesOrigin,positionX - getFontMetrics(g.getFont()).stringWidth(tracesOrigin) / 2 ,
            // ACTORS_STRIPE -30) ;
            // }
            // else {
            //     g.drawString("user agent",positionX - getFontMetrics(g.getFont()).stringWidth("user agent") / 2 ,
            // ACTORS_STRIPE - 30) ;
            // }
            f = g.getFont();
            newFont = new Font(f.getName(), . | ., 14);
            g.setFont(newFont);
            g.drawString(
                origin,
                positionX - getFontMetrics(g.getFont()).stringWidth(origin) / 2,
                 - 15);
            if ( != null) {
                g.drawImage(
                    ,
                    positionX - .getWidth(this) / 2,
                     / 3,
                    this);
            } else {
                //System.out.println("The actors icon is null!!");
            }
            // Vertical line
            g.setColor(.);
            g.drawLine(positionXpositionXheightCanvas);
        }
    }
    public void constructActors() {
        try {
            // We clean the table
             = new Hashtable();
            for (int i = 0; i < .size(); i++) {
                TracesMessage tracesMessage =
                    (TracesMessage.elementAt(i);
                String from = tracesMessage.getFrom().trim();
                String to = tracesMessage.getTo().trim();
                //  System.out.println("consructorActors():"+tracesMessage.getMessageString() );
                //System.out.println("from:"+from);
                //System.out.println("to:"+from);
                //System.out.println();
                //fromActorName = getActorName(from) ;
                //to = getActorName(to) ;
                // We have to stock the actor and its position in the canvas
                int sizeActors = .size();
                if (.get(from) == null) {
                    .put(
                        from,
                        new Integer(
                            sizeActors *  + ));
                }
                sizeActors = .size();
                if (.get(to) == null) {
                    .put(
                        to,
                        new Integer(
                            sizeActors *  + ));
                }
            }
        } catch (Exception e) {
            ..println("Error in trying to construct the actors");
        }
    }
    public void constructArrows() {
         = new Hashtable();
        String from = null;
        String to = null;
        // Assign different colors to each arrows
        assignColors();
        //System.out.println("Construct arrows");
        // Setup the first selected arrow:
         = "arrow1";
        //System.out.println("tracesSession size:"+tracesSession.size());
        for (int i = 0; i < .size(); i++) {
            TracesMessage tracesMessage =
                (TracesMessage.elementAt(i);
            from = tracesMessage.getFrom();
            to = tracesMessage.getTo();
            int positionXFrom = ((Integer.get(from)).intValue();
            int positionXTo = ((Integer.get(to)).intValue();
            int positionY = i *  +  + ;
            boolean info = tracesMessage.getStatusInfo() != null;
            String arrowName = "arrow" + (i + 1);
            boolean selected = false;
            // Set up the color of this arrow
            String transactionId = tracesMessage.getTransactionId();
            Color color;
            if (transactionId != null) {
                color = (Color.get(transactionId);
                if (color == null)
                    color = .;
            } else
                color = .;
            if (positionXFrom == positionXTo) {
                // This is a loop !!
                CircleArrow circleArrow =
                    new CircleArrow(
                        selected,
                        arrowName,
                        positionXFrom,
                        positionY - 20,
                        positionY + 20,
                        40,
                        true,
                        info);
                circleArrow.setColor(color);
                circleArrow.setTracesMessage(tracesMessage);
                circleArrow.setTracesCanvas(this);
                .put(arrowNamecircleArrow);
            } else {
                // This is a straight arrow
                //StraightArrow straightArrow=new StraightArrow(selected,arrowName,
                //positionXFrom,positionXTo,positionY-15, positionY+15, true,info);
                StraightArrow straightArrow =
                    new StraightArrow(
                        selected,
                        arrowName,
                        positionXFrom,
                        positionXTo,
                        positionY - 31,
                        positionY + 31,
                        true,
                        info);
                straightArrow.setColor(color);
                straightArrow.setTracesMessage(tracesMessage);
                straightArrow.setTracesCanvas(this);
                .put(arrowNamestraightArrow);
                // System.out.println("color:"+ straightArrow.color);
                // System.out.println("arrow name:"+straightArrow.arrowName);
                // System.out.println("transactionId:"+tracesMessage.getTransactionId());
                // System.out.println();
            }
        }
        // System.out.println("arrows size:"+arrows.size());
    }
    public void assignColors() {
         = new Hashtable();
        TracesMessage tracesMessage = null;
        int colorIndex = 0;
        for (int i = 0; i < .size(); i++) {
            tracesMessage = (TracesMessage.elementAt(i);
            String transactionId = tracesMessage.getTransactionId();
            if (transactionId != null) {
                Color color = (Color.get(transactionId);
                if (color == null) {
                    if (colorIndex >= 10)
                        color = .;
                    else
                        color = [colorIndex];
                    .put(transactionIdcolor);
                    colorIndex++;
                } else {
                    // the Color is already attribuated with this transaction id.
                }
            }
        }
    }
    public Arrow getArrow(int xint y) {
        //System.out.println("getArrow: x:"+x+" y:"+y);
        Enumeration e = .keys();
        while (e.hasMoreElements()) {
            String arrowName = (Stringe.nextElement();
            Arrow arrow = (Arrow.get(arrowName);
            if (arrow == null)
                return null;
            if (arrow.isCollisionArrow(xy))
                return arrow;
        }
        return null;
    }
    public Arrow getArrowInfo(int xint y) {
        //System.out.println("getArrow: x:"+x+" y:"+y);
        Enumeration e = .keys();
        while (e.hasMoreElements()) {
            String arrowName = (Stringe.nextElement();
            Arrow arrow = (Arrow.get(arrowName);
            if (arrow == null)
                return null;
            else if (arrow.statusInfo) {
                if (arrow.isCollisionInfo(xy))
                    return arrow;
            }
        }
        return null;
    }
    public void selectMessage(int xint y) {
        // x,y: position of the cursor
         = getArrow(xy);
        if ( == null) {
            // big problem
        } else {
            // We have to update the content:
            TracesMessage tracesMessage = .getTracesMessage();
            if ( != null) {
                .setText(
                    tracesMessage.getMessageString());
                if (tracesMessage.debugLine != null
                    && !tracesMessage.debugLine.equals("")) {
                    ..setText(
                        "SIP message: (debug log line: "
                            + tracesMessage.debugLine
                            + " )");
                }
            }
             = tracesMessage;
            // We have to change the color of the old message and the new one:
             = (Arrow.get();
            . = false;
            . = true;
             = .;
            repaint();
        }
    }
    public void showTipTool(int xint y) {
        Arrow oldArrowTipTool = ;
         = getArrow(xy);
        if (oldArrowTipTool != null) {
            if (oldArrowTipTool.arrowName.equals(.)) {
                // we do nothing because it is the same arrow
            } else {
                // A new arrow
                oldArrowTipTool.displayTipTool = false;
                . = true;
                repaint();
            }
        } else {
            // The very first arrow selected!!!
            . = true;
            repaint();
        }
    }
    public void unShowTipTool() {
        if ( != null) {
            . = false;
            repaint();
             = null;
        }
    }
    public void drawArrows(Graphics g) {
        // draw the arrows and information
        // Set up the Font
        Enumeration e = .keys();
        while (e.hasMoreElements()) {
            String arrowName = (Stringe.nextElement();
            Arrow arrow = (Arrow.get(arrowName);
            if (arrow.visible) {
                //System.out.println("arrow:"+arrow.ymin);
                arrow.draw(g);
            }
        }
        if (getParent() != null) {
            getParent().doLayout();
            getParent().validate();
        }
    }
    public void unvisibleAllArrows() {
        Enumeration e = .keys();
        while (e.hasMoreElements()) {
            String arrowName = (Stringe.nextElement();
            Arrow arrow = (Arrow.get(arrowName);
            arrow.visible = false;
        }
    }
    public void unselectAllArrows() {
        Enumeration e = .keys();
        while (e.hasMoreElements()) {
            String arrowName = (Stringe.nextElement();
            Arrow arrow = (Arrow.get(arrowName);
            arrow.selected = false;
        }
    }
    public boolean isOnArrow(int xint y) {
        Arrow arrow = getArrow(xy);
        if (arrow == null)
            return false;
        else
            return true;
    }
    public boolean isOnInfo(int xint y) {
        Arrow arrow = getArrowInfo(xy);
        if (arrow == null)
            return false;
        else
            return true;
    }
    public void displayInfo(int xint y) {
        Arrow arrow = getArrow(xy);
        arrow.displayInfo = true;
        repaint();
    }
    public void unDisplayInfo() {
        //System.out.println("getArrow: x:"+x+" y:"+y);
        Enumeration e = .keys();
        boolean repaint = false;
        while (e.hasMoreElements()) {
            String arrowName = (Stringe.nextElement();
            Arrow arrow = (Arrow.get(arrowName);
            if (arrow != null)
                if (arrow.displayInfo) {
                    arrow.displayInfo = false;
                    repaint = true;
                }
        }
        repaint();
    }
    public void setBackground(Graphics g) {
        // if we have a background image, fill the back with it
        // otherwise black by default
        if ( != null
            && .getWidth(this) != -1
            && .getHeight(this) != -1) {
            int widthImage = .getWidth(this);
            int heightImage = .getHeight(this);
            int nbImagesX = getSize(). / widthImage + 1;
            int nbImagesY = getSize(). / heightImage + 1;
            // we don't draw the image above the top
            for (int i = 0; i < nbImagesXi++)
                for (int j = 0; j < nbImagesYj++)
                    g.drawImage(
                        ,
                        i * widthImage,
                        j * heightImage + 95,
                        this);
        } else {
            g.setColor(.);
            g.fillRect(0, 95, getSize().getSize().);
        }
    }
    /*
     // Without this method, the canvas is clippping!!!!!!
     public void update(Graphics g){
         try{
             // Update is called only by selectMessage:
             //System.out.println("update method called");
             setBackground(g);
             drawTop(g);
             drawArrows(g);
         }
         catch(Exception exception) {}
     }*/
    public void paint(Graphics g) {
        try {
            // Paint is called each time:
            //  - resize of the window
            // System.out.println();
            // System.out.println("paint method called");
            // We draw the title and some decorations:
            setBackground(g);
            drawTop(g);
            drawArrows(g);
            /*
            Graphics2D g2 = (Graphics2D) g;
            // The width and height of the canvas
            int w = getSize().width;
            int h = getSize().height;
            // Create an ellipse and use it as the clipping path
            Ellipse2D e = new Ellipse2D.Float(w/4.0f,h/4.0f,
                               w/2.0f,h/2.0f);
            g2.setClip(e);
            // Fill the canvas. Only the area within the clip is rendered
            g2.setColor(Color.cyan);
            g2.fillRect(0,0,w,h);
            // Change the clipping path, setting it to the intersection of
            // the current clip and a new rectangle.
            Rectangle r = new Rectangle(w/4+10,h/4+10,w/2-20,h/2-20);
            g2.clip(r);
            // Fill the canvas. Only the area within the new clip
            // is rendered
            g2.setColor(Color.magenta);
            g2.fillRect(0,0,w,h);
            */
        } catch (Exception exception) {
        }
    }
    public void mousePressed(MouseEvent e) {
        // System.out.println("Mouse pressed in the canvas!!!");
        try {
            int x = e.getX();
            int y = e.getY();
            if ((e.getModifiers() & .)
                == .) {
                // The right button is pressed
                if (isOnArrow(xy)) {
                    //unShowTipTool();
                    . = false;
                     = null;
                    displayInfo(xy);
                }
            } else {
                // The left button is pressed:
                if (isOnArrow(xy)) {
                    //System.out.println("click on Arrow!!!");
                    selectMessage(xy);
                }
                //if (isOnInfo(x,y)){
                //    displayInfo(x,y);
                //}
            }
        } catch (Exception ex) {
        }
    }
    public void mouseReleased(MouseEvent p1) {
        unDisplayInfo();
    }
    public void mouseEntered(MouseEvent p1) {
    }
    public void mouseClicked(MouseEvent p1) {
    }
    public void mouseExited(MouseEvent p1) {
    }
    public void mouseDragged(MouseEvent p1) {
    }
    public void mouseMoved(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        if (isOnArrow(xy)) {
            showTipTool(xy);
        } else
            unShowTipTool();
    }
New to GrepCode? Check out our FAQ X