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

  1. // Faculte.java
  2. // 24.02.96
  3. //
  4. // the information for each faculte
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Graphics;
  9. import java.awt.image.ImageObserver;
  10. import java.awt.Image;
  11. import java.io.DataInputStream;
  12. import java.io.IOException;
  13. import java.util.Vector;
  14.  
  15. /**
  16.  * One of the basic objects, contains all the available info on a faculty.
  17.  */
  18. public class Faculte implements Literate, Paintable, Mapable {
  19.   private String name;
  20.   private String batiment;
  21.   private String telephone;
  22.   private String fax;
  23.   private String co;  // I don't know what this is, but it's in the data file
  24.   private int level;               
  25.  
  26.   public static final int clipY = Scaler.scale (30);
  27.   public static final int clipWidth = Scaler.scale (683);
  28.   public static final int clipHeight = Scaler.scale (35);
  29.   public static final int iconX = Scaler.scale (688);
  30.  
  31.   private static int indentWidth = Scaler.scale (40);
  32.  
  33.   private Faculte parent = null;
  34.   private FaculteVector children = null;                
  35.   
  36.   /** for reading in */
  37.   private static final int numLines = 5;
  38.   private static String lines[] = new String[numLines];
  39.  
  40.   /** for painting */
  41.   private Image icon;
  42.   private static Image leaf;
  43.   private static Image node;
  44.  
  45.   public Faculte getParent () { return parent; }
  46.   public FaculteVector getChildren () { return children; }
  47.   public int getLevel () { return level; }
  48.   public String getName () { return name; }
  49.   public String getBatiment () { return batiment; }
  50.   public String getTelephone () { return telephone; }
  51.   public String getFax () { return fax; }
  52.   public String getContact () { return co; }
  53.  
  54.  
  55.   public static void setIconImages (Image leaf, Image node) {
  56.     Faculte.leaf = leaf;
  57.     Faculte.node = node;
  58.   }
  59.   
  60.   public Faculte (String name, String batiment, String telephone,
  61.           String fax, String co, int level) {
  62.     this.name = name;
  63.     this.batiment = batiment;
  64.     this.telephone = telephone;
  65.     this.fax = fax;
  66.     this.co = co;
  67.     this.level = level;
  68.     icon = leaf;
  69.   }
  70.  
  71.   /** used for TY fields (the name of the groupe) */
  72.   public Faculte (String name) {
  73.     this (name, null, null, null, null, 0);
  74.   }
  75.   
  76.   private Faculte (String lines[]) {
  77.     this (lines[0].substring (3),
  78.       lines[1].substring (3),
  79.       lines[2].substring (3),
  80.       lines[3].substring (3),
  81.       lines[4].substring (3),
  82.       new Integer(lines[0].substring(1,2)).intValue ());
  83.   }
  84.   
  85.   /** used to make an object that will then read itself in */
  86.   public Faculte () {
  87.   }
  88.  
  89.   public String toString () {
  90.     StringBuffer theString = new StringBuffer ();
  91.     theString.append ("Faculte[" + name + ", " + level);
  92.     if (parent != null)
  93.       theString.append (", parent = " + parent.name);
  94.     theString.append ("]");
  95.     return (theString.toString ());
  96.   }
  97.  
  98.   public void paint (Graphics g, int x, int y, ImageObserver observer) {
  99.     Graphics line = g.create (0, y - clipY, clipWidth, clipHeight);
  100.  
  101.     line.drawString (name, x + ((level - 1) * indentWidth), clipY);
  102.     if (icon != null)
  103.       g.drawImage (icon, iconX, y - clipY, observer);
  104.   }
  105.   
  106.   /** adds a child Faculte to this Faculte */
  107.   private void addChild (Faculte child) {
  108.     if (children == null) {
  109.       children = new FaculteVector ();
  110.       icon = node;
  111.     }
  112.     children.addElement (child);
  113.   }
  114.  
  115.   /** reads in a Faculte from inStream */
  116.   public Object read (DataInputStream inStream) throws IOException {
  117.     Faculte theFaculte = read_one (inStream);
  118.  
  119.     if (theFaculte != null) {
  120.       read_helper (inStream, theFaculte);
  121.     }
  122.  
  123.     return theFaculte;
  124.   }
  125.   
  126.   /** recursively goes through the file, builds the tree */
  127.   private static void read_helper (DataInputStream inStream,
  128.                    Faculte parent) throws IOException {
  129.     Faculte tempFaculte = read_one (inStream);
  130.     
  131.     if (tempFaculte != null) {
  132.       if (tempFaculte.level == parent.level + 1) {
  133.     
  134.     tempFaculte.parent = parent;
  135.     parent.addChild (tempFaculte);
  136.     read_helper (inStream, parent);
  137.     
  138.       } else if (tempFaculte.level == parent.level + 2) {
  139.     
  140.     Faculte lastChild = parent.children.lastElement ();
  141.     tempFaculte.parent = lastChild;
  142.     lastChild.addChild (tempFaculte);
  143.     read_helper (inStream, lastChild);
  144.     
  145.       } else {
  146.     
  147.     Faculte originalParent = parent;
  148.     while (originalParent.level != tempFaculte.level - 1)
  149.       originalParent = originalParent.parent;
  150.     tempFaculte.parent = originalParent;
  151.     originalParent.addChild (tempFaculte);
  152.     read_helper (inStream, originalParent);
  153.     
  154.       }
  155.     }
  156.   }
  157.                    
  158.   /** reads one groupe of info */
  159.   private static Faculte read_one (DataInputStream inStream)
  160.     throws IOException {
  161.     
  162.     lines[0] = inStream.readLine();
  163.     
  164.     /* get rid of comments */
  165.     while (lines[0] != null && (lines[0].startsWith ("#") || lines[0] == ""))
  166.       lines[0] = inStream.readLine();
  167.     
  168.     if (lines[0] != null) {
  169.       if (lines[0].startsWith ("===")) 
  170.     return null;
  171.       else if (lines[0].startsWith ("TY:"))
  172.     return new Faculte (lines[0].substring(3));
  173.       else {
  174.     for (int i = 1; i < numLines; i++) {
  175.       lines[i] = inStream.readLine();
  176.     }
  177.     return new Faculte (lines);
  178.       }
  179.     }
  180.     return null;
  181.   }
  182.     
  183.   public String getMapName () {
  184.     if (batiment.startsWith ("BFSH2"))
  185.       return "BFSH2_2";
  186.     else
  187.       return batiment;
  188.   }
  189.  
  190.   public String getZoneName () { return null; }
  191.   
  192.   public MapInfoPanel getMapInfoPanel (Appletlike app) {
  193.     FaculteInfoPanel thePanel = new FaculteInfoPanel (app);
  194.     thePanel.setData (this);
  195.     return thePanel;
  196.   }
  197.  
  198. }
  199.