SDSU CS 535 Object-Oriented Programming & Design
Spring Semester, 1999
Some UI Issues
Previous    Lecture Notes Index    Next    
© 1999, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 18-Feb-99

Contents of Doc 7, Some UI Issues


Doc 7, Some UI Issues Slide # 2

Questions about Menus


Doesn't the menu class need a switch statement?

public class Menu {
   ArrayList menusStrings = new ArrayList();
   ArrayList executers = new ArrayList();
   ArrayList methods = new ArrayList();
   
   public void add( String menuItem, 
                  Object executee, Method aMethod ) {
      menusStrings.add( menuItem);
      executers.add( executee );
      methods.add( aMethod );
   }
   
   public void run(){
      int selection = selectMenuItem();
      
      Object executee = executers.get( selection);
      Method aMethod = (Method) methods.get( selection);
      Object[] noArguments = {};
      aMethod.invoke( executee, noArguments );   
   }
}
Why is this better than a switch statement?

Doc 7, Some UI Issues Slide # 3
If so how is the switch statement better than what happens in Strawman 1 & 2 solutions?

Which is worst option A or Option B?

Option A
public class Menu {
   public void displayMenuItems(){
      blah
   }
   public int readUserResponse(){
      return blah;
   }
   public void run(){
      displayMenuItems();
      int response = readUserResponse();
      
      switch ( response ) {
         case 1:
            blah;
            break
         case 2:
            blah;
      }
   }
}

Doc 7, Some UI Issues Slide # 4
Option B
public class Menu {
   private void displayMenuItems() { blah }
   private int readUserResponse() { return blah; }
   pubic int display() {
      displayMenuItems();
      return readUserResponse();
      }
}
public class GUIClass {
   Menu selectLangauge;
   
   pubilc void aMethod() {
      int selection = selectLanguage.display();
      switch ( response ) {
         case 1:
            blah;
            break
         case 2:
            blah;
      }
   }
}   

Doc 7, Some UI Issues Slide # 5

Different Ways to Operate UI Widgets

The goal here is to show conceptually different ways UI widgets operate, not the details of how a particular widget works. Some widgets use more than one method.

Direct - Push

 public class UIWidget {
   private void displayMenuItems() {blah }
   pubic Value display() {
      displayStuff();
      return readUserResponse()
   }
   private Value readUserResponse() {
      return blah;
   }
}
public class GUIClass {
   UIWidget selectLangauge;
   
   public void aMethod() {
      Value selection = selectLanguage.display();
   }
}   

Doc 7, Some UI Issues Slide # 6

Direct - Pull

public class UIWidget {
   private Value userResponse;
   private void displayMenuItems() {blah; }
   private Value readUserResponse() {return blah; }
   
   pubic void display() {
      displayStuff();
      userResponse = readUserResponse();
   }
   public Value getValue() {
      return userResponse;
}
public class GUIClass {
   UIWidget selectLangauge;
   public void aMethod() {
      selectLanguage.display();
   }
   
   public void anotherMethod() {
      Value selection = selectLanguage.getValue();
   }
}   

Doc 7, Some UI Issues Slide # 7

Callback


public class UIWidget {
   Object executee;
   Method toExecute;
   
   public UIWidget( Object executee, String methodName ) {
      assign executee & toExecute here;
   }
   
   pubic void display() {
      displayStuff();
      userResponse = readUserResponse();
      Object[] arguments = { userResponse };
      toExecute.invoke( executee, arguments );
   }
}
public class GUIClass {
   public void aMethod() {
      UIWidget selectLangauge = new UIWidget( this, "respond");
      selectLanguage.display();
   }
   
   public void respond( Value answer ) {
      do it here
   }
}      

Doc 7, Some UI Issues Slide # 8

Callback using Interface

Java’s AWT uses listeners. A different listener interface is defined for each type of event. Interfaces can define more than on method. Event objects contain information about the event that occurred. Usually they contain a reference to the widget that generated the event. Some sample AWT listener interfaces.

interface ItemListener {
   public abstract void itemStateChanged(ItemEvent e);
}
interface KeyListener {
   public abstract void keyTyped(KeyEvent e);
   public abstract void keyPressed(KeyEvent e);
   public abstract void keyReleased(KeyEvent e);
}

Doc 7, Some UI Issues Slide # 9
Listener Example
public class UIWidget {
   ArrayList listeners = new ArrayList();
   
   public void addItemListener( ItemListener aListener) {
      listeners.add( aListener );
   }
   
   private void notifyListeners() {
      ArrayList localCopy;
      synchronized ( this ) {
      localCopy = listeners.clone();
      }
      
      ItemEvent thisEvent = new ItemEvent( this );//fake code
      Iterator items = localCopy.iterator();
      
      while ( items.hasNext() ) {
         ItemListener aListener = (ItemListener) items.next();
         aListener.itemStateChanged( thisEvent );
      }
   }
         
   pubic void display() {
      blah;
      notifyListeners();
   }
}
public class AClass implements ItemListener  {
   public void aMethod() {
      UIWidget selectLangauge = new UIWidget( );
      selectLanguage.addItemListener();
   }
   
   public void itemStateChanged(ItemEvent e) {
      do it here
   }
}

Copyright © 1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

Previous    visitors since 18-Feb-99    Next