Previous Next

Tiles Sample - Classes

TilesGUIHandler.java

The TilesGUIHandler class builds and manages the user interface for a Tiles component. TilesGUIHandler add several features to the default UI provided by the framework:

TilesGUIHandler also implements action handling to handle actions performed against the menu items and tool buttons it adds to the component.

Create the TilesGUIHandler object
public class TilesGUIHandler extends GUIHandler {
public TilesGUIHandler(ComponentController controller) {
    super(controller);
}
Create menu items for the new menu, getting the menu labels from the TilesResources class
protected void handleCreateMenus(MenuBar menubar) {
    ResourceBundle resources = getComponentController().getResourceBundle();

    redMenuItem         = new MenuItem(resources.getString("Red"));
    greenMenuItem       = new MenuItem(resources.getString("Green"));
    blueMenuItem        = new MenuItem(resources.getString("Blue"));
    rockMenuItem        = new MenuItem(resources.getString("Rock"));
    paperMenuItem       = new MenuItem(resources.getString("Paper"));
    scissorMenuItem     = new MenuItem(resources.getString("Scissor"));
Set up each menu item to delegate actions to this handler
    redMenuItem.addActionListener(this);
    greenMenuItem.addActionListener(this);
    blueMenuItem.addActionListener(this);
    rockMenuItem.addActionListener(this);
    paperMenuItem.addActionListener(this);
    scissorMenuItem.addActionListener(this);
Build the menu and add it to the default menu bar
    Menu tileMenu = new Menu(resources.getString("TilesMenu"), true);
    tileMenu.add(redMenuItem);
    tileMenu.add(greenMenuItem);
    tileMenu.add(blueMenuItem);
    tileMenu.add(rockMenuItem);
    tileMenu.add(paperMenuItem);
    tileMenu.add(scissorMenuItem);
    menubar.add(tileMenu);
}
Build the new tool buttons, specifying the image directory, the image name, and the text to use for flyover help
protected void handleCreateToolButtons(TToolBar toolbar) {
    ResourceBundle resources = getComponentController().getResourceBundle();

    rockToolButton = new TToolButton(getClass(), "rock.gif", resources.getString("Rock"),
        resources.getString("RockText"));
    paperToolButton = new TToolButton(getClass(), "paper.gif", resources.getString("Paper"),
        resources.getString("PaperText"));
    scissorToolButton = new TToolButton(getClass(), "scissor.gif",resources.getString("Scissor"),
        resources.getString("ScissorText"));
Add the tool buttons to the default toolbar
    toolbar.add(rockToolButton);
    toolbar.add(paperToolButton);
    toolbar.add(scissorToolButton);
Set up the tool buttons to delegate actions and mouse events to this controller
    rockToolButton.addActionListener(this);
    paperToolButton.addActionListener(this);
    scissorToolButton.addActionListener(this);

    rockToolButton.addMouseListener(this);
    paperToolButton.addMouseListener(this);
    scissorToolButton.addMouseListener(this);
}
Handle actions on the Tiles menu or new tool buttons, and propagate other actions to the parent
public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof MenuItem) {
        MenuItem mi = (MenuItem)e.getSource();
        if (mi == redMenuItem)
            setTileColor(Color.red);
        else if (mi == greenMenuItem)
            setTileColor(Color.green);
        else if (mi == blueMenuItem)
            setTileColor(Color.blue);
        else if (mi == rockMenuItem)
            createTile(Tile.kRock);
        else if (mi == paperMenuItem)
            createTile(Tile.kPaper);
        else if (mi == scissorMenuItem)
            createTile(Tile.kScissor);
        else
        super.actionPerformed(e);
    }
    else if (e.getSource() instanceof TToolButton) {
        TToolButton tb = (TToolButton)e.getSource();
        if (tb == rockToolButton)
            createTile(Tile.kRock);
        else if (tb == paperToolButton)
            createTile(Tile.kPaper);
        else if (tb == scissorToolButton)
            createTile(Tile.kScissor);
        else
            super.actionPerformed(e);
        }
    else
        super.actionPerformed(e);
}
Methods called by the Component Controller
public void undo() {
    handleUndo();
}
public void redo() {
    handleRedo();
}
Helper methods for performing actions associated with Tiles menu items and tool buttons
void setTileColor(Color color) {
    TileSelection selection = (TileSelection)getModelSelection().clone();
    addAndDo(new TileColorCommand(selection, color));
}
void createTile(int tileType) {
    int count = ((TileModel)getModel()).getTileCount();
    int row = count / 7;
    int col = count % 7;
    Color color = null;

    switch (tileType) {
        case Tile.kRock:
            color = Color.red;
            break;
        case Tile.kPaper:
            color = Color.blue;
            break;
        case Tile.kScissor:
            color = Color.green;
            break;
    }

    addAndDo(new TileCreateCommand((TileSelection)getModelSelection(),
        tileType, color,new Point(col * 65 + 10, row * 65 + 10 ),
        new Dimension(50,50)));
} 
Data fields used in building the UI
private MenuItem redMenuItem;
private MenuItem greenMenuItem;
private MenuItem blueMenuItem;
private MenuItem rockMenuItem;
private MenuItem paperMenuItem;
private MenuItem scissorMenuItem;

private TToolButton rockToolButton;
private TToolButton paperToolButton;
private TToolButton scissorToolButton;

private transient Dialog aboutBox;
}

Classes for Tiles sample:


Previous Next

Copyright © Taligent, Inc. 1996 - 1997.
Copyright
© IBM Corporation 1996 - 1997.
All Rights Reserved.