home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / OS Shell in Java / Facade / DesktopMenu.java < prev    next >
Encoding:
Java Source  |  1998-06-18  |  3.8 KB  |  131 lines  |  [TEXT/dosa]

  1. //    DesktopMenu.java : this is a Java source code file for the program Facade.
  2. //    Copyright 1998, Andrew S. Downs
  3. //    andrew.downs@tulane.edu
  4. //
  5. //    This source code is distributed as freeware.
  6. //    Just keep this author information in the file.  Enjoy!
  7.  
  8. import java.awt.*;
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. public class DesktopMenu extends DesktopComponent implements Serializable {
  13.     boolean enabled = false;
  14.     Color activeItemColor, inactiveItemColor;
  15.     Color highlightBackgroundColor, highlightForegroundColor;
  16.     DesktopMenuItem activeItem;
  17.     Rectangle itemBounds;
  18.     Vector vector;
  19.     
  20.     DesktopMenu() {
  21.         super();
  22.         this.vector = new Vector();
  23.         this.itemBounds = new Rectangle();
  24.     }
  25.  
  26.     public int getWidestItem( FontMetrics theFontMetrics ) {
  27.         // Determine widest menu item.
  28.         int tempWidth = 0;
  29.         Vector v = this.getVector();
  30.  
  31.         for ( int j = 0; j < v.size(); j++ ) {
  32.             int temp = theFontMetrics.stringWidth( ( ( DesktopMenuItem )v.elementAt( j ) ).getLabel() );
  33.             if ( temp > tempWidth )
  34.                 tempWidth = temp;
  35.         }
  36.         
  37.         return tempWidth;
  38.     }
  39.  
  40.     public void setItemLocations( FontMetrics theFontMetrics ) {
  41.         int tempWidth = this.getWidestItem( theFontMetrics );
  42.  
  43.         int newX = this.getX();
  44.         int newY = Global.defaultMenuBarHeight + theFontMetrics.getHeight();
  45.         int newWidth = tempWidth + ( 2 * Global.defaultMenuHSep );
  46.         int newHeight = theFontMetrics.getHeight() + Global.defaultMenuItemVSep;
  47.  
  48.         Vector v = this.getVector();
  49.  
  50.         for ( int j = 0; j < v.size(); j++ ) {
  51.             if ( ( ( DesktopMenuItem )v.elementAt( j ) ).getLabel().equals( Global.menuSep ) ) {
  52.                 ( ( DesktopMenuItem )v.elementAt( j ) ).setDrawPoint( newX, newY );
  53.                 ( ( DesktopMenuItem )v.elementAt( j ) ).setBounds( newX, newY - theFontMetrics.getHeight() + Global.defaultMenuItemVSep, newWidth, 3 * Global.defaultMenuItemVSep );
  54.                 newY += 4 * Global.defaultMenuItemVSep;
  55.                 continue;
  56.             }
  57.                         
  58.             ( ( DesktopMenuItem )v.elementAt( j ) ).setDrawPoint( newX, newY );
  59.             ( ( DesktopMenuItem )v.elementAt( j ) ).setBounds( newX, newY - theFontMetrics.getHeight(), newWidth, newHeight + Global.defaultMenuItemVSep );
  60.             newY += theFontMetrics.getHeight() + Global.defaultMenuItemVSep;
  61.         }
  62.         
  63.  
  64.         this.itemBounds.setBounds( this.getBounds().x - Global.defaultMenuHSep, Global.defaultMenuBarHeight, tempWidth + ( 2 * Global.defaultMenuHSep ), ( ( ( DesktopMenuItem )v.elementAt( v.size() - 1 ) ).getBounds().y ) + ( ( ( DesktopMenuItem )v.elementAt( v.size() - 1 ) ).getBounds().height ) - Global.defaultMenuBarHeight + ( Global.defaultMenuItemVSep ) );
  65.     }
  66.  
  67.     public void setVector( Vector v ) {
  68.         this.vector = v;
  69.     }
  70.  
  71.     public Vector getVector() {
  72.         return this.vector;
  73.     }
  74.  
  75.     public void setEnabled( boolean b ) {
  76.         this.enabled = b;
  77.     }
  78.  
  79.     public boolean getEnabled() {
  80.         return this.enabled;
  81.     }
  82.  
  83.     public void setActiveItemColor( Color c ) {
  84.         this.activeItemColor = c;
  85.     }
  86.  
  87.     public Color getActiveItemColor() {
  88.         return this.activeItemColor;
  89.     }
  90.  
  91.     public void setInactiveItemColor( Color c ) {
  92.         this.inactiveItemColor = c;
  93.     }
  94.  
  95.     public Color getInactiveItemColor() {
  96.         return this.inactiveItemColor;
  97.     }
  98.  
  99.     public void setHighlightBackgroundColor( Color c ) {
  100.         this.highlightBackgroundColor = c;
  101.     }
  102.  
  103.     public Color getHighlightBackgroundColor() {
  104.         return this.highlightBackgroundColor;
  105.     }
  106.  
  107.     public void setHighlightForegroundColor( Color c ) {
  108.         this.highlightForegroundColor = c;
  109.     }
  110.  
  111.     public Color getHighlightForegroundColor() {
  112.         return this.highlightForegroundColor;
  113.     }
  114.  
  115.     public void setActiveItem( DesktopMenuItem d ) {
  116.         this.activeItem = d;
  117.     }
  118.  
  119.     public DesktopMenuItem getActiveItem() {
  120.         return this.activeItem;
  121.     }
  122.  
  123.     public void setItemBounds( Rectangle r ) {
  124.         this.itemBounds = r;
  125.     }
  126.  
  127.     public Rectangle getItemBounds() {
  128.         return this.itemBounds;
  129.     }
  130. }
  131.