Previous Next

Tiles Sample - Classes

TileCreateCommand.java

TileCreateCommand adds a new tile to the component model. TileCreateCommand objects take a TileSelection object as their target, which they use to access the array of Tiles in the model. TileCreateCommand is undoable and saves the data needed for undo and redo operations.

TileCreateCommand objects are created and added to the command processor by the component controller when the user selects one of the Create Tile items in the Tiles menu or one of the Tiles tool buttons. TileCreateCommand resets the model selection to specify the new tile.

Create a TileCreateCommand to create a tile of the specifed type, color, and location, getting the label from the TilesResources class
public class TileCreateCommand extends Command {
public TileCreateCommand(TileSelection selection, int type, Color color, 
			Point position, Dimension size) {
    ResourceBundle resources = ResourceBundle.getBundle("tiles.TilesResources");
    setLabel(resources.getString("Create Tile"));
    this.selection = selection;
    this.type   = type;
    this.position = position;
    this.size   = size;
    this.color = color;
}
Create a new tile, add it to the model, and select it
public void handleDo() {
    newTile = new Tile(type, color, position, size);
    handleRedo();
}
Delete the tile just added and reset the selection
protected void handleUndo() {      
    ((TileModel)selection.getModel()).removeTile(newTile);
    selection.deselectAll();
}
Add the tile back to the model and select it, deselecting any currently selected tiles
protected void handleRedo() {
    ((TileModel)selection.getModel()).addTile(newTile);
    selection.deselectAll();
    selection.selectTile(newTile);
}
Data used for undo and redo
private TileSelection selection;
private int type;
private Color color;
private Point position;
private Dimension        size;
private Tile newTile;
}

Classes for Tiles sample:


Previous Next

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