Previous | Next |
TileMoveCommand.java
TileMoveCommand drags a tile within the component view, resetting the position of the tile as it is moved. TileMoveCommand objects take a TileSelection object as their target, which they use to modify the position of the selected Tile. TileMoveCommand 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 mouse-handling methods when the user drags a tile.
Create a TileMoveCommand, getting the label from the TilesResources class | public class TileMoveCommand extends Command { TileMoveCommand(TileSelection selection) { ResourceBundle resources = ResourceBundle.getBundle("tiles.TilesResources"); setLabel(resources.getString("Move Tile")); this.selection = selection; } |
Called by the view while the user drags the mouse, having the effect of incrementally executing the command | public void moveBy(Point delta) { selection.moveBy(delta); } |
Return if at least one tile is selected | public boolean handleCanDo() { return selection.isNotEmpty(); } |
Save data needed for undo | protected void handleDo() { oldPositions = selection.getPositions(); } |
Reset tiles to their original positions | protected void handleUndo() { if (newPositions == null) newPositions = selection.getPositions(); moveTo(oldPositions); } |
Reset tiles back to the new positions | protected void handleRedo() { moveTo(newPositions); } |
Helper method for changing the position of one or more selected tiles | void moveTo(Hashtable positions) { for (Enumeration keys = positions.keys(); keys.hasMoreElements();) { Tile tile = (Tile)keys.nextElement(); tile.setPosition((Point)positions.get(tile)); } selection.getModel().setModelChanged(true); selection.getModel().notifyOfModelChange(null); } |
Data used for undo and redo | TileSelection selection; Hashtable oldPositions, newPositions; } |
Classes for Tiles sample:
Previous | Next |
Copyright ©
Taligent, Inc. 1996 - 1997.
Copyright © IBM Corporation 1996 - 1997.
All Rights Reserved.