home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / faculte / facultepaper.java < prev   
Encoding:
Java Source  |  1996-08-14  |  4.5 KB  |  187 lines

  1. // FacultePaper.java
  2. // 01.03.96
  3. //
  4. // the paper on the faculte screen
  5.  
  6. package cybcerone.faculte;
  7.  
  8. import java.awt.Image;
  9. import java.awt.Graphics;
  10. import java.awt.Event;
  11. import java.util.Vector;
  12. import java.util.Enumeration;
  13.  
  14. import cybcerone.utils.Appletlike;
  15. import cybcerone.utils.Paper;
  16. import cybcerone.utils.FaculteVector;
  17. import cybcerone.utils.Faculte;
  18. import cybcerone.utils.Paintable;
  19. import cybcerone.utils.PaintableVector;
  20.  
  21. /**
  22.  * The paper on the facultes screen.
  23.  */
  24. class FacultePaper extends Paper implements Runnable {
  25.   private static final String id = "facultePaper";
  26.   private static final String statusText = "These are our faculties";
  27.  
  28.   private Thread updater;
  29.   private String updateName;
  30.  
  31.   protected Faculte currentFaculte;
  32.  
  33.   /** The number of items to show with a dark gray background. */
  34.   protected int darkLines;
  35.  
  36.   /** 
  37.    * The number of lines (not information, actual lines) that we've
  38.    * been asked to draw so far.  Used to make sure that we don't draw
  39.    * lines when writing information in the dark gray area.
  40.    */
  41.   protected int drawLinesCount;
  42.  
  43.   FacultePaper (Appletlike app) {
  44.     super (id, statusText, app);
  45.   }
  46.  
  47.   /**
  48.    * Search for the faculte with this name.
  49.    */
  50.   void update (String name) {
  51.     if (updater != null)
  52.       updater.stop ();
  53.  
  54.     updateName = name;
  55.     updater = new Thread (this);
  56.     updater.start ();
  57.   }
  58.  
  59.   public void stop () {
  60.     if (updater != null)
  61.       updater.stop ();
  62.     updater = null;
  63.   }
  64.  
  65.   public void run () {
  66.     boolean done = false;
  67.     Faculte oneFac = null;
  68.  
  69.     while (theData == null) {
  70.       try {
  71.     updater.sleep (1000);
  72.       } catch (InterruptedException e) {
  73.       }
  74.     }
  75.     
  76.     FaculteVector theFacs = new FaculteVector (theData.toVector ());
  77.     
  78.     for (Enumeration e = theFacs.elements (); 
  79.      e.hasMoreElements () && !done;) {
  80.       oneFac = (Faculte)e.nextElement ();
  81.       
  82.       if (oneFac.getName().equals (updateName)) {
  83.     done = true;
  84.     update (oneFac);
  85.       }
  86.     }
  87.   }
  88.   
  89.   /** Here's my data. */
  90.   public void setData (PaintableVector theData) {
  91.     this.theData = theData;
  92.     if (theData != null && theData.size () > 0)
  93.       update ((Faculte)theData.elementAt (0));
  94.   }
  95.  
  96.   /** 
  97.    * Show this new and exciting faculte.
  98.    */
  99.   void update (Faculte newFaculte) {
  100.     FaculteVector children = newFaculte.getChildren ();
  101.  
  102.     if (children != null) {
  103.  
  104.       currentFaculte = newFaculte;
  105.       clearPage ();
  106.       darkLines = newFaculte.getLevel ();
  107.       
  108.       addAncestors (newFaculte);
  109.       
  110.       if (newFaculte.getLevel () != 0)
  111.     addLine (newFaculte);
  112.       
  113.       if (children != null) {
  114.     for (Enumeration e = children.elements (); e.hasMoreElements ();)
  115.       addLine ((Paintable)e.nextElement ());
  116.       }
  117.       repaint ();
  118.  
  119.     } else if (newFaculte.getLevel () == 0) {
  120.       clearPage ();
  121.       repaint ();
  122.     } else {
  123.       app.update (newFaculte);
  124.     }
  125.   }
  126.  
  127.   private void addAncestors (Faculte fac) {
  128.     Faculte ancestor = fac.getParent ();
  129.  
  130.     if (ancestor != null && ancestor.getLevel () > 0) {
  131.       addAncestors (ancestor);
  132.       addLine (ancestor);
  133.     }
  134.   }
  135.  
  136.   /**
  137.    * We draw things a bit differently on this page.  There are light and
  138.    * dark gray backgrounds, and lines may be indented.
  139.    */
  140.   protected void paintLines (Graphics g, PaintableVector items) {
  141.     int yPos = border + (lineHeight * (darkLines + 1));
  142.     drawLinesCount = 0;
  143.     
  144.     g.clipRect (border, border,
  145.         placement.width - (border * 2),
  146.         placement.height - (border * 2));
  147.  
  148.     if (darkLines > 0) {
  149.       g.setColor (darkGray);
  150.       g.fillRect (0, 0, placement.width, placement.height);
  151.     }
  152.  
  153.     g.setColor (lightGray);
  154.     g.fillRect (border + darkLines * indentWidth + (darkLines == 0 ? 0 : 5),
  155.                 border + darkLines * lineHeight + (darkLines == 0 ? 0 : 5),
  156.                 placement.width, placement.height);
  157.     
  158.     g.setColor (black);
  159.     super.paintLines (g, items);
  160.   }
  161.  
  162.   /**
  163.    * Draw a line (not a piece of information, an actual line).
  164.    */
  165.   protected void drawLine (Graphics g, int yPos) {
  166.     if (++drawLinesCount > darkLines)
  167.       g.drawLine (border + (darkLines * indentWidth) + inset, yPos + inset,
  168.           placement.width, yPos + inset);
  169.   }
  170.   
  171.   /**
  172.    * If the user clicks on the current Faculte, show its parent,
  173.    * otherwise show the faculte that was clicked on.
  174.    */
  175.   public boolean mouseDown (Event evt, int x, int y) {
  176.     Faculte theFac = (Faculte)whichItem (x, y);
  177.     
  178.     if (theFac != null) {
  179.       if (theFac.equals (currentFaculte))
  180.     update (theFac.getParent ());
  181.       else
  182.     update (theFac);
  183.     }
  184.     return true;
  185.   }
  186. }
  187.