Previous | Next |
TileSelection.java
TileSelection is used to encapsulate a specification of what tiles are currently selected. TileSelection stores a vector that contains copies of all the currently selected tiles. Commands in the Tiles component use a TileSelection object to identify the target data for the command.
TileSelection also supports clipboard operations (cut, copy, paste, and delete) on selected tiles. This is provided by implementing ModelSelection methods inherited from the Transferable interface, part of the java.awt.datatransfer package. TileSelection overrides Transferable methods that:
TileSelection can only provide selected tiles as an array of Tile objects, a data format it calls TilesFlavor.
TileSelection also overrides standard ModelSelection methods that are called to delete selected data or paste data into the selected objects. Methods are also provided that undo these operations, and that are called to query whether the operation is currently available.
public class TileSelection extends ModelSelection { |
|
Define the data types this selection can provide for clipboard operations | public static DataFlavor tilesFlavor = new DataFlavor(tiles.TileSelection.class, "TilesFlavor"); |
Default selection is empty, that is, doesn't specify any tiles | public TileSelection(ModelSelectionOwner owner, TileModel model) { super(owner, model); this.model = model; setEmpty(); } |
Methods for copying TileSelection objects | public TileSelection(TileSelection selection) { super(selection); model = selection.model; selectedTiles = (Vector)selection.selectedTiles.clone(); }public Object clone() { return new TileSelection(this); } |
Called to specify the model containing the selected data | public void setModel(IModel model) { super.setModel(model); this.model = (TileModel)model; } |
Called to query the data types that can be provided for a clipboard operation | public synchronized DataFlavor[] getTransferDataFlavors() { DataFlavor[] flavors = { tilesFlavor }; return flavors; } |
Called to query whether selected data can be returned as a specified data type | public boolean isDataFlavorSupported(DataFlavor dataFlavor) { return dataFlavor.equals(tilesFlavor); } |
Called to get an object containing the selected data, to be used to transfer it to the clipboard | public synchronized Object getTransferData(DataFlavor dataFlavor) throws IOException, UnsupportedFlavorException { if (dataFlavor.equals(tilesFlavor)) { return selectedTiles; } else throw new UnsupportedFlavorException(dataFlavor); } |
Methods to control delete operations | public boolean canDelete() { return isNotEmpty(); }public Object delete() { Vector deletedTiles = (Vector)selectedTiles.clone(); model.removeTiles(selectedTiles); deselectAll(); return deletedTiles; }public void undoDelete(Object undoData) { Vector deletedTiles = (Vector)undoData; deselectAll(); model.addTiles(deletedTiles); selectTiles(deletedTiles); } |
Methods to control paste operations | public boolean canPasteFrom(Transferable contents) { if (contents != null) { DataFlavor flavors[] = contents.getTransferDataFlavors(); if (flavors != null) { for (int i=0; i < flavors.length; i++) { if (flavors[i].equals(tilesFlavor)) return true; } } } return false; }public Object pasteFrom(Transferable contents) { if (contents == null) return null; try { Vector deletedTiles = (Vector)delete(); Vector pastedTiles = (Vector)contents.getTransferData(tilesFlavor); model.addTiles(pastedTiles); selectTiles(pastedTiles); return new ObjectPair(deletedTiles, pastedTiles); } catch (Exception e) { System.err.println(e); } return null; }public void undoPaste(Object undoData) { if (undoData instanceof ObjectPair) { ObjectPair pair = (ObjectPair)undoData; Vector deletedTiles = (Vector)pair.getObject1(); Vector pastedTiles = (Vector)pair.getObject2(); model.removeTiles(pastedTiles); model.addTiles(deletedTiles); deselectAll(); selectTiles(deletedTiles); } } |
Standard methods to select or deselect all tiles in the model, plus special methods to select or deselect specific tiles | public void selectAll() { selectedTiles = (Vector)model.getTiles().clone(); if (!selectedTiles.isEmpty()) { for (int i = 0; i < selectedTiles.size(); i++) ((Tile)selectedTiles.elementAt(i)).setSelected(true); setNotEmpty(); } }public void deselectAll() { if (!selectedTiles.isEmpty()) { for (int i = 0; i < selectedTiles.size(); i++) ((Tile)selectedTiles.elementAt(i)).setSelected(false); selectedTiles.removeAllElements(); setEmpty(); } }public void selectTile(Tile tile) { tile.setSelected(true); selectedTiles.addElement(tile); setNotEmpty(); }public void selectTiles(Vector tiles) { if (tiles != null && !tiles.isEmpty()) { for (int i = 0; i < tiles.size(); i++) { Tile tile = (Tile)tiles.elementAt(i); tile.setSelected(true); selectedTiles.addElement(tile); } setNotEmpty(); } }public void deselectTile(Tile tile) { tile.setSelected(false); selectedTiles.removeElement(tile); if (selectedTiles.isEmpty()) setEmpty(); else setNotEmpty(); } |
Methods used by commands to change data in the selected tiles | public void setColor(Color color) { model.setColor(selectedTiles, color); }public void moveBy(Point delta) { model.moveBy(selectedTiles, delta); } |
Methods to access data fields of one or more selected tiles | public Hashtable getColors() { Hashtable table = new Hashtable(); for (int i = 0; i < selectedTiles.size(); i++) { Tile tile = (Tile)selectedTiles.elementAt(i); table.put(tile, tile.getColor()); } return table; }public Hashtable getPositions() { Hashtable table = new Hashtable(); for (int i = 0; i < selectedTiles.size(); i++) { Tile tile = (Tile)selectedTiles.elementAt(i); table.put(tile, tile.getPosition()); } return table; } |
Access to the component model and copies of currently selected tiles | private TileModel model; private Vector selectedTiles = new Vector(); } |
Convenience class used while pasting to encapsulate both the currently selected tiles and the tiles that replace them during the paste operation | class ObjectPair { public ObjectPair(Object a, Object b) { object1 = a; object2 = b; }public Object getObject1() { return object1; }public Object getObject2() { return object2; }private Object object1; private Object object2; } |
Classes for Tiles sample:
Previous | Next |
Copyright ©
Taligent, Inc. 1996 - 1997.
Copyright © IBM Corporation 1996 -
1997.
All Rights Reserved.