Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

Line.java

Go to the documentation of this file.
00001 /* Cyclops
00002  * FER - SPVP
00003  * Fakultet elektrotehnike i racunarstva (http://www.fer.hr/)
00004  * Unska 3, 10000 Zagreb, Hrvatska
00005  * (c) 2001 FER, Zagreb.
00006  */
00007 
00008 package hr.fer.zesoi.cyclops;
00009 
00010 import java.util.*;
00011 import java.awt.*;
00012 
00013 
00022 public class Line
00023 {
00024 
00029     protected boolean show_capacity = true;
00030 
00032     protected boolean deleted = false;
00033 
00038     public LineEnd left_end;
00039 
00044     public LineEnd right_end;
00045 
00049     protected Node left_node;
00050 
00054     protected Node right_node;
00055 
00061     protected int left_capacity;
00062 
00068     protected int right_capacity;
00069 
00078     public Line (Node left, Node right, int capacity)
00079     {
00080         left_end = new LineEnd(this);
00081         right_end = new LineEnd(this);
00082         left_node = left;
00083         right_node = right;
00084         left_node.attach(left_end, right_node);
00085         right_node.attach(right_end, left_node);
00086         left_capacity = capacity;
00087         right_capacity = capacity;
00088         left_end.setCapacity(left_capacity);
00089         right_end.setCapacity(right_capacity);
00090     }
00091     /* Line */
00092 
00102     public Line (Node left, Node right, int leftCapacity, int rightCapacity)
00103     {
00104         left_end = new LineEnd(this);
00105         right_end = new LineEnd(this);
00106         left_node = left;
00107         right_node = right;
00108         left_end.setCapacity(left_capacity);
00109         right_end.setCapacity(right_capacity);
00110         left_node.attach(left_end, right_node);
00111         right_node.attach(right_end, left_node);
00112         this.left_capacity = left_capacity;
00113         this.right_capacity = right_capacity;
00114     }
00115     /* Line */
00116 
00121     public void transmitAll ()
00122     {
00123         Object packet;
00124         while ( !left_end.isEmpty() )
00125             {
00126                 packet = left_end.get();
00127                 right_node.receive(packet);
00128             }
00129         while ( !right_end.isEmpty() )
00130             {
00131                 packet = right_end.get();
00132                 left_node.receive(packet);
00133             }
00134     }
00135     /* transmitAll */
00136 
00142     public int getCapacity ()
00143     {
00144         if (left_capacity < right_capacity)
00145             {
00146                 return left_capacity;
00147             }
00148         else
00149             {
00150                 return right_capacity;
00151             }
00152     }
00153     /* getCapacity */
00154 
00159     public int getLeftToRightCapacity ()
00160     {
00161         return left_capacity;
00162     }
00163     /* getLeftToRightCapacity */
00164 
00165 
00170     public int getRightToLeftCapacity ()
00171     {
00172         return right_capacity;
00173     }
00174     /* getRightToLeftCapacity */
00175 
00176 
00181     public void setCapacity (int capacity)
00182     {
00183         left_capacity = capacity;
00184         right_capacity = capacity;
00185         left_end.setCapacity(left_capacity);
00186         right_end.setCapacity(right_capacity);
00187     }
00188     /* setCapacity */
00189 
00194     public void setLeftToRightCapacity (int capacity)
00195     {
00196         left_capacity = capacity;
00197         left_end.setCapacity(left_capacity);
00198     }
00199     /* setLeftToRightCapacity */
00200 
00205     public void setRightToLeftCapacity (int capacity)
00206     {
00207         right_capacity = capacity;
00208         right_end.setCapacity(right_capacity);
00209     }
00210     /* setRightToLeftCapacity */
00211 
00215     public void delete()
00216     {
00217         right_node.detach(right_end);
00218         left_node.detach(left_end);
00219         deleted = true;
00220     }
00221     /* delete */
00222 
00226     public boolean isDisconnected ()
00227     {
00228         return deleted;
00229     }
00230     /* isDisconnected */
00231 
00238     public boolean areEndingNodes(Node first_node, Node second_node)
00239     {
00240         if (left_node == first_node)
00241             {
00242                 if (right_node == second_node)
00243                     {
00244                         return true;
00245                     }
00246             }
00247         else if (right_node == first_node)
00248             {
00249                 if (left_node == second_node)
00250                     {
00251                         return true;
00252                     }
00253             }
00254         return false;
00255     }
00256     /* areEndingNodes */
00257 
00264     public void paint (Graphics g, FontMetrics metrics, Color line_color)
00265     {
00266         int x1;
00267         int y1;
00268         int x2;
00269         int y2;
00270 
00271         x1 = left_node.getXCoordinate();
00272         y1 = left_node.getYCoordinate();
00273         x2 = right_node.getXCoordinate();
00274         y2 = right_node.getYCoordinate();
00275         g.setColor(line_color);
00276         g.drawLine(x1, y1, x2, y2);
00277         if (show_capacity)
00278             {
00279                 String capacity;
00280                 if (left_capacity < right_capacity)
00281                     {
00282                         capacity = String.valueOf(left_capacity);
00283                     }
00284                 else
00285                     {
00286                         capacity = String.valueOf(left_capacity);
00287                     }
00288                 g.setColor(Color.black);
00289                 g.drawString(capacity, x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2);
00290             }
00291     }
00292     /* paint */
00293 
00304     public void paint (Graphics g, FontMetrics metrics,
00305                        Color line_color, Color left_packet_color, Color right_packet_color,
00306                        Color traced_packet_color,
00307                        boolean show_line_capacity, boolean show_all_packets
00308                        )
00309     {
00310         int x1;
00311         int y1;
00312         int x2;
00313         int y2;
00314         int x;
00315         int y;
00316         int i;
00317         Packet packet;
00318 
00319         x1 = left_node.getXCoordinate();
00320         y1 = left_node.getYCoordinate();
00321         x2 = right_node.getXCoordinate();
00322         y2 = right_node.getYCoordinate();
00323         g.setColor(line_color);
00324         g.drawLine(x1, y1, x2, y2);
00325         x = x1 + (x2 - x1) / 2;
00326         y = y1 + (y2 - y1) / 2;
00327         if (show_line_capacity)
00328             {
00329                 String capacity;
00330                 if (left_capacity < right_capacity)
00331                     {
00332                         capacity = String.valueOf(left_capacity);
00333                     }
00334                 else
00335                     {
00336                         capacity = String.valueOf(left_capacity);
00337                     }
00338                 g.setColor(Color.black);
00339                 g.drawString(capacity, x, y);
00340             }
00341         /* if */ /* show_line_capacity */
00342         boolean axis = Math.abs(y2 - y1) > Math.abs(x2 - x1);
00343         if (show_all_packets)
00344             {
00345                 x = x1 + (x2 - x1) / 2;
00346                 y = y1 + (y2 - y1) / 2;
00347                 for (i=0; i<left_end.size(); i++)
00348                     {
00349                         if (axis) y = y + 6;
00350                         else x = x + 6;
00351                         packet = (Packet)left_end.elementAt(i);
00352                         if (packet.getDrawingFlag())
00353                             {
00354                                 packet.paint(g, metrics, traced_packet_color, x, y);
00355                             }
00356                         else
00357                             {
00358                                 packet.paint(g, metrics, left_packet_color, x, y);
00359                             }
00360                     }
00361                 /* for */
00362                 for (i=0; i<right_end.size(); i++)
00363                     {
00364                         if (axis) y = y + 6;
00365                         else x = x + 6;
00366                         packet = (Packet)right_end.elementAt(i);
00367                         if (packet.getDrawingFlag())
00368                             {
00369                                 packet.paint(g, metrics, traced_packet_color, x, y);
00370                             }
00371                         else
00372                             {
00373                                 packet.paint(g, metrics, right_packet_color, x, y);
00374                             }
00375                     }
00376                 /* for */
00377             }
00378         else
00379             {
00380                 x = x1 + (x2 - x1) / 2;
00381                 y = y1 + (y2 - y1) / 2;
00382                 for (i=0; i<left_end.size(); i++)
00383                     {
00384                         packet = (Packet)left_end.elementAt(i);
00385                         if (packet.getDrawingFlag())
00386                             {
00387                                 if (axis) y = y + 6;
00388                                 else x = x + 6;
00389                                 packet.paint(g, metrics, traced_packet_color, x, y);
00390                             }
00391                     }
00392                 /* for */
00393                 for (i=0; i<right_end.size(); i++)
00394                     {
00395                         packet = (Packet)right_end.elementAt(i);
00396                         if (packet.getDrawingFlag())
00397                             {
00398                                 if (axis) y = y + 6;
00399                                 else x = x + 6;
00400                                 packet.paint(g, metrics, traced_packet_color, x, y);
00401                             }
00402                     }
00403                 /* for */
00404             }
00405         /* if else */ /* show_all_packets */
00406     }
00407     /* paint */
00408 
00409 }
00410 /* Line */

Generated at Thu Jun 28 03:04:21 2001 for Cyclops Network Simulator by doxygen1.2.7 written by Dimitri van Heesch, © 1997-2001