Previous Next

Tiles Sample - Classes

TileColorCommand.java

TileColorCommand changes the color of selected tiles. TileColorCommand objects take a TileSelection object as their target. TileColorCommand implements the standard command execution methods, and also implements handleCanDo. This is called by the component controller to determine when the color menu items should be enabled (at least one tile must be selected before the user can apply a color change command). TileColorCommand is undoable and saves the data needed for undo and redo operations.

TileColorCommand objects are created and added to the command processor by the component controller when the user selects one of the color items in the Tiles menu.

Create a TileColorCommand with the target selection and new color, getting the label from the TilesResources class
public class TileColorCommand extends Command {
TileColorCommand(TileSelection selection, Color color) {
    ResourceBundle resources = ResourceBundle.getBundle("tiles.TilesResources");
    setLabel(resources.getString("Color Tile"));
    this.selection = selection;
    newColor = color;
}
Return true if at least one tile is selected
public boolean handleCanDo() {	
    return selection.isNotEmpty(); 
}
Save the current color before applying the color change
protected void handleDo() {
    oldColors = selection.getColors();
    handleRedo();
}
Reset each tile to its previous color
protected void handleUndo() {
    for (Enumeration keys = oldColors.keys(); keys.hasMoreElements();) {
        Tile tile = (Tile)keys.nextElement();
        tile.setColor((Color)oldColors.get(tile));
    }
    selection.getModel().setModelChanged(true);
    selection.getModel().notifyOfModelChange(null);
}
Reset the colors of selected tiles
protected void handleRedo() {
    selection.setColor(newColor);
}
Data used for undo and redo
private TileSelection   selection;
private Color	        newColor;
private Hashtable	oldColors;
}

Classes for Tiles sample:


Previous Next

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