Previous | Next |
TileView.java
TileView draws the tiles in the component model by calling the draw method on each tile. The TileView mouse event handling methods enable the user to interact with the tiles, including:
TileView uses a TileMoveCommand to actually reset the position of dragged tiles.
Receive mouse and mouse motion events | public class TileView extends ModelView implements MouseListener, MouseMotionListener { public TileView() { setBackground(Color.white); addMouseListener(this); addMouseMotionListener(this); } |
Establish a connection to the component model | public void initialize() { super.initialize(); model = (TileModel)getModel(); } |
Update local reference to model | public void setModel(IModel model) { super.setModel(model); this.model = (TileModel)model; } |
Handle mouse events, but ignore it if the third mouse button is pressed | public void mousePressed(MouseEvent event) { if ((event.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) { return; } int x = event.getX(); int y = event.getY(); lastX = x; lastY = y; |
Empty the current selection unless the control key is being pressed | selection = (TileSelection)getComponentController().getModelSelection(); if (!event.isControlDown()) selection.deselectAll(); |
Determine whether a tile is being clicked on and, if so, select it (or deselect it if it is currently selected) | Tile tile = model.findTile(new Point(x, y)); if (tile != null) { if (!event.isControlDown()) { selection.selectTile(tile); } else { if (tile.isSelected()) selection.deselectTile(tile); else selection.selectTile(tile); } }} |
If a tile is selected, set up a TileMoveCommand and add it to the command processor | public void mouseDragged(MouseEvent event) { if (selection != null && selection.isNotEmpty()) { if (command == null) { command = new TileMoveCommand((TileSelection)selection.clone()); getComponentController().getGUIHandler().addAndDo(command); } |
Continue executing the command as the user drags the mouse | int x = event.getX(); int y = event.getY(); if (x != lastX || y != lastY) { if (command != null) command.moveBy(new Point(x - lastX, y - lastY)); lastX = x; lastY = y; } } } |
Terminate the TileMoveCommand when the user releases the mouse | public void mouseReleased(MouseEvent event) { command = null; } |
Standard handler methods for events not specifically handled by this view | public void mouseClicked(MouseEvent event) { }public void mouseEntered(MouseEvent event) { }public void mouseExited(MouseEvent event) { }public void mouseMoved(MouseEvent event) { } |
Draw non-selected tiles, then selected tiles, calling the draw method on each tile | public void paint(Graphics g) { g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); for (int i = 0; i < model.getTileCount(); i++) { Tile tile = model.getTile(i); if (!tile.isSelected()) tile.draw(g); } for (int i = 0; i < model.getTileCount(); i++) { Tile tile = model.getTile(i); if (tile.isSelected()) tile.draw(g); } } |
Redraw the view whenever the model or model selection change | public void handleModelChange(ModelChangeEvent e) { super.handleModelChange(e); repaint(); }public void handleModelSelectionChange(ModelSelectionChangeEvent e) { repaint(); } |
Set the initial size for the view | public Dimension getPreferredSize() { return new Dimension(400, 300); } |
Access to the component model and selection, and data to help determine when to activate a TileMoveCommand | private transient TileModel model; private transient TileSelection selection; private transient TileMoveCommand command; private transient int lastX; private transient int lastY; } |
Classes for Tiles sample:
Previous | Next |
Copyright ©
Taligent, Inc. 1996 - 1997.
Copyright © IBM Corporation 1996 - 1997.
All Rights Reserved.