Copyright © 1998-1999, Paul Banta (pbanta@yahoo.com)
/*
**   M o d e l V i e w C o n t r o l l e r . j a v a
**
**   Copyright (C) 1998-1999, Paul Banta (pbanta@yahoo.com)
**
**   javac ModelViewController.java
**   jar -cvf ModelViewController.jar *.class
*/

public class ModelViewController
   extends java.applet.Applet
{
   //   A t t r i b u t e s


   //   M a i n

   public static void main( String[ ] args )
   {
      ModelViewController model_view_controller ;

      model_view_controller = new ModelViewController( ) ;
      model_view_controller.init( ) ;
   }


   //   C o n s t r u c t o r s


   //   P u b l i c   M e t h o d s

   //   i n i t

   public void init( )
   {
      Controller controller ;

      Model model ;

      model = new Model( ) ;
      controller = new Controller( model ) ;
      controller.show( ) ;
   }


   //   P r i v a t e   M e t h o d s
}


class Controller
   extends java.awt.Frame
{
   //   A t t r i b u t e s

   private static int myControllerCount    = 0 ;
   private static int myControllerLocation = 0 ;

   private Model myModel = null ;


   //   C o n s t r u c t o r s

   public Controller( Model model )
   {
      ActionEventHandler aeh = new ActionEventHandler( ) ;

      WindowEventHandler weh = new WindowEventHandler( ) ;

      java.awt.Panel north_panel = new java.awt.Panel( ) ;
      java.awt.Panel south_panel = new java.awt.Panel( ) ;

      north_panel.setLayout( new java.awt.FlowLayout( ) ) ;
      south_panel.setLayout( new java.awt.FlowLayout( ) ) ;

      addButton( "Red"   , java.awt.Color.red    , aeh, north_panel ) ;
      addButton( "Orange", java.awt.Color.orange , aeh, north_panel ) ;
      addButton( "Yellow", java.awt.Color.yellow , aeh, north_panel ) ;
      addButton( "Green" , java.awt.Color.green  , aeh, north_panel ) ;
      addButton( "Blue"  , java.awt.Color.blue   , aeh, north_panel ) ;
      addButton( "Purple", java.awt.Color.magenta, aeh, north_panel ) ;

      addButton( "New Controller", java.awt.Color.lightGray, aeh, south_panel ) ;
      addButton( "New View"      , java.awt.Color.lightGray, aeh, south_panel ) ;
      addButton( "Exit"          , java.awt.Color.lightGray, aeh, south_panel ) ;

      setLayout( new java.awt.BorderLayout( ) ) ;
      add( "North", north_panel ) ;
      add( "South", south_panel ) ;
      addWindowListener( weh ) ;
      pack( ) ;
//      setSize( 325, 100 ) ;
      setTitle( "C o l o r   C o n t r o l l e r" ) ;
      myControllerCount++ ;
      myControllerLocation = ( ( myControllerCount - 1 ) % 10 ) * 25 ;
      myModel = model  ;
      setLocation( myControllerLocation, myControllerLocation ) ;
   }


   //   P u b l i c   M e t h o d s

   //   a d d B u t t o n

   private void addButton( java.lang.String   s,
                           java.awt.Color     c,
                           ActionEventHandler aeh,
                           java.awt.Panel     p    )
   {
      java.awt.Button b = new java.awt.Button( s ) ;

      b.addActionListener( aeh ) ;
      b.setActionCommand ( s   ) ;
      b.setBackground    ( c   ) ;

      p.add( b ) ;
   }


   //   c r e a t e N e w C o n t r o l l e r

   private void createNewController( )
   {
      Controller controller ;

      Model model ;

      model = new Model( ) ;
      controller = new Controller( model ) ;
      controller.show( ) ;
   }


   //   c r e a t e N e w V i e w

   private void createNewView( )
   {
      View view ;

      view = new View( myModel ) ;
      view.show( ) ;
      myModel.addObserver( view ) ;
   }


   //   d i s p o s e

   public void dispose( )
   {
      myControllerCount--        ;
      myModel.deleteObservers( ) ;

      super.dispose( ) ;

      if ( myControllerCount == 0 )
      {
         System.exit( 0 ) ;
      }
   }


   //   P r i v a t e   M e t h o d s


   //   N e s t e d   C l a s s e s

   //   A c t i o n E v e n t H a n d l e r

   private class ActionEventHandler
      implements java.awt.event.ActionListener
   {
      //   a c t i o n P e r f o r m e d

      public void actionPerformed( java.awt.event.ActionEvent ae )
      {
         String s = ae.getActionCommand( ) ;

         if ( s.equals( "Red"    ) ) myModel.setColor( java.awt.Color.red     ) ;
         if ( s.equals( "Orange" ) ) myModel.setColor( java.awt.Color.orange  ) ;
         if ( s.equals( "Yellow" ) ) myModel.setColor( java.awt.Color.yellow  ) ;
         if ( s.equals( "Green"  ) ) myModel.setColor( java.awt.Color.green   ) ;
         if ( s.equals( "Blue"   ) ) myModel.setColor( java.awt.Color.blue    ) ;
         if ( s.equals( "Purple" ) ) myModel.setColor( java.awt.Color.magenta ) ;

         if ( s.equals( "New Controller" ) ) createNewController( ) ;
         if ( s.equals( "New View"       ) ) createNewView      ( ) ;
         if ( s.equals( "Exit"           ) ) dispose            ( ) ;
      }
   }


   //   W i n d o w E v e n t H a n d l e r

   private class WindowEventHandler
      extends java.awt.event.WindowAdapter
   {
      //   w i n d o w C l o s i n g

      public void windowClosing( java.awt.event.WindowEvent we )
      {
         dispose( ) ;
      }
   }
}


class Model
   extends java.util.Observable
{
   //   A t t r i b u t e s

   private java.awt.Color myColor = new java.awt.Color( 255, 255, 255 ) ;


   //   C o n s t r u c t o r s


   //   P u b l i c   M e t h o d s

   //   d e l e t e O b s e r v e r s

   public void deleteObservers( )
   {
      setChanged( ) ;
      notifyObservers( "deleteObservers" ) ;
      super.deleteObservers( ) ;
   }


   //   g e t C o l o r

   public java.awt.Color getColor( )
   {
      return myColor ;
   }


   //   s e t C o l o r

   public void setColor( java.awt.Color new_color )
   {
      myColor = new_color ;
      setChanged( ) ;
      notifyObservers( myColor ) ;
   }


   //   P r i v a t e   M e t h o d s
}


class View
   extends    java.awt.Frame
   implements java.util.Observer
{
   //   A t t r i b u t e s

   private static int myViewCount    = 0 ;
   private static int myViewLocation = 0 ;

   private java.util.Observable myObservable = null ;


   //   C o n s t r u c t o r s

   public View( java.util.Observable o )
   {
      WindowEventHandler weh = new WindowEventHandler( ) ;

      setBackground( ( ( Model ) o ).getColor( ) ) ;
      addWindowListener( weh ) ;

      myObservable = o  ;

      myViewCount++ ;
      myViewLocation = ( ( myViewCount - 1 ) % 10 ) * 25 ;
      setBounds( myViewLocation + 325, myViewLocation, 200, 200 ) ;
      setTitle ( "C o l o r   V i e w"                          ) ;
   }


   //   P u b l i c   M e t h o d s

   //   d i s p o s e

   public void dispose( )
   {
      myViewCount-- ;
      myObservable.deleteObserver( this ) ;
      super.dispose( ) ;
   }


   //   u p d a t e

   public void update( java.util.Observable obs, java.lang.Object obj )
   {
      if ( obj instanceof java.awt.Color )
      {
         this.setBackground( ( java.awt.Color ) obj ) ;
         this.repaint      (                        ) ;
      }

      if ( obj instanceof java.lang.String )
      {
         if ( ( ( java.lang.String ) obj ).equals( "deleteObservers" ) )
         {
            dispose( ) ;
         }
      }
   }


   //   P r i v a t e   M e t h o d s


   //   N e s t e d   C l a s s ( e s )

   private class WindowEventHandler extends java.awt.event.WindowAdapter
   {
      public void windowClosing( java.awt.event.WindowEvent we )
      {
         dispose( ) ;
      }
   }
}
      
Copyright © 1998-1999, Paul Banta (pbanta@yahoo.com)
1