Link to home
Start Free TrialLog in
Avatar of castello
castello

asked on

mediator design question

re this applet I've been working on

Thanks to everybody's help I finally got this thing halfway working.

My problem is how to refer to the enclosing Applet when the class doesn't know ahead of time that an instance is going to be a member of an Applet.

My Applet has a mediator that is just an Object, not a Component (so I can't put a menu on it).  The Applet also has a popup menu that I activate by the right click.  The menu options choose activity modules to load into the applet, so actionPerformed() calls the proper method in the mediator to create the module (Canvas) and add it to the Applet.  

But then, if I right click again to see the menu and load a different module, the menu doesn't come up.  I'm assuming that the Applet MouseListener can no longer "hear" the mouse click because the module is "in the way".

So, I gave the module a mouse listener, and when there's a right click I send x and y to the mediator.  I would like the mediator to send x and y to the proper Applet method, which  will be showMenu(int x,int y){popup.show(instance,x,y);}

But I don't see how to refer to the Applet from the mediator.
I tried the following in the Applet:

MediatorClass myMediator;
public void init()
{
  myMediator = new MediatorClass();
  myMediator.setApl(this);
}

which didn't work, I assume because 'this' is not available in the middle of the initialization process.

So, the bottom line is I would like to be able to show the popup menu on top of the modules after they're loaded. How do I do that?
Avatar of vladi21
vladi21

can u post ur code?
Avatar of castello

ASKER


The applet starts blank.  I right click, then choose
Pitch practice/play the keyboard from the popup.
The keyboard draws itself in the applet.

Now, I need to be able to right click and get the menu again.
The following doesn't compile.  I'm trying to pass the coordinates of the click on sglNteKbd to METAplActDir then to
the shwMnu() method in ETApl.  But maybe I'm going about it all wrong....

// ETApl.java


/*
<applet
code="ETApl" width=300 height=300>
  </applet>
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class ETApl extends Applet implements
ActionListener  
{

   ETApl instance;
   /*
   * Width of applet.
   */
   private final int WID = 300;

   /*
   * Height of applet.
   */
   private final int HGT = 300;

   /*
   * Music ear-training applet activity director.
   */
   private AplActDir METAplActDir;

   private String manMnuItm = "See note names";
   private String manMnuItmCom = "names";
   private String subMnuTtl = "Pitch Practice";
   private String subMnuItmA = "Play the keyboard";
   private String subMnuItmB = "Listen and respond";
   private String subMnuItmACom = "play";
   private String subMnuItmBCom = "listen";
   private int manMnuItmSrt = KeyEvent.VK_N;
   private int subMnuItmSrtA = KeyEvent.VK_P;
   private int subMnuItmSrtB = KeyEvent.VK_L;

   PopupMenu popup;
   MenuItem mi;
   MenuShortcut ms;
   Menu subMnu;
   String curCom = "";
   String curAct = "";

   
   public void init()
   {
      instance=this;
      METAplActDir = new AplActDir(WID,HGT);
      METAplActDir.setApl(this);
   
      popup = new PopupMenu();
      ms = new MenuShortcut(manMnuItmSrt);
      mi = new MenuItem(manMnuItm,ms);
      mi.setActionCommand(manMnuItmCom);
      mi.addActionListener(this);
      popup.add(mi);
      this.add(popup);
   
      subMnu = new Menu(subMnuTtl);
      popup.add(subMnu);
      ms = new MenuShortcut(subMnuItmSrtA);
      mi = new MenuItem(subMnuItmA,ms);
      mi.setActionCommand(subMnuItmACom);
      mi.addActionListener(this);
      subMnu.add(mi);
   
      ms = new MenuShortcut(subMnuItmSrtB);
      mi = new MenuItem(subMnuItmB,ms);
      mi.setActionCommand(subMnuItmACom);
      mi.addActionListener(this);
      subMnu.add(mi);
   
      this.addMouseListener(new MouseAdapter(){
         public void mousePressed(MouseEvent e){
            int x = e.getX();
            int y = e.getY();    
            if ( ( e.getModifiers() & InputEvent.BUTTON3_MASK ) ==
                  InputEvent.BUTTON3_MASK )
            {
               popup.show(instance,x,y);            
            }
            else
            {
               // nothing
            }              
         }
      });
   
      setLayout(new BorderLayout());
                 
   }
 
   public void actionPerformed(ActionEvent ae)
   {
      curCom = ae.getActionCommand();
      if (curAct == curCom)
         return;
      if (curCom.equals("play"))
      {
         if (METAplActDir.sglNteKbd == null)
         {
            Canvas keyboardimg=METAplActDir.crtPblKbd(WID,HGT,12,Color.black);
            keyboardimg.setSize(WID - 1,HGT -1);
            this.add(keyboardimg);
            this.validate();
         }
       
         curAct = curCom;
      }            
      else
      {
      }                  
   }

   public void shwMnu(int x,int y)
   {
      popup.show(instance,x,y);
   }
   
}

// AplActDir.java

import java.awt.*;

public class AplActDir
{
   int wid;
   int hgt;

   PblKbd sglNteKbd = null;
   // this will be the other module: DumAct notNam = null;
   Applet apl = null;



   public AplActDir(int w,int h)
   {
      wid = w;
      hgt = h;
 
   }

   public Canvas crtPblKbd(int wid,int hgt,int num,Color color)
   {
      sglNteKbd = new PblKbd(wid,hgt,num,color);
      sglNteKbd.setDir(this);
      return sglNteKbd;
   }

   public void setApl(Applet a)
   {
      apl = a;  
   }

   public void proRhtClk(int x,int y)
   {
      apl.shwMnu(x,y);
 
   }  


}

// PblKbd.java
import java.awt.*;

public class PblKbd extends Canvas
{    
   /**
   * The keyboard diagram.
   */
   private RszKbdDgm kbdDgm;
   private AplActDir pblKbdDir;

   //(...)

   public PblKbd(int wid,int hgt,int num,Color color)
   {
      //(...)
      kbdDgm=new RszKbdDgm(wid,num,color);
 
      this.addMouseListener(new MouseAdapter(){
         public void mousePressed(MouseEvent e){
            int x = e.getX();
            int y = e.getY();                        
            if ( ( e.getModifiers() & InputEvent.BUTTON3_MASK ) ==
                      InputEvent.BUTTON3_MASK )
            {
               pblKbdDir.proRhtClk(x,y);          
            }
            else
            {
               // nothing
            }            
         }
      });
   }
   
   public void paint(Graphics g)
   {    
      // Draw the diagram.
     kbdDgm.draw(g);        
   }

   public void setDir(AplActDir aad);
   {
      pblKbdDir = aad;
   }




// RszKbdDgm.java
import java.awt.*;

public class RszKbdDgm
{  //
   public RszKbdDgm(int w,int numKey,Color barC)
   { //    
   }
   public void draw(Graphics g)
   {
      // draws the diagram
   }
}



                     
ASKER CERTIFIED SOLUTION
Avatar of sgoms
sgoms

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial