001: /*
002: * @(#)GraphSelectionEvent.java 1.0 03-JUL-04
003: *
004: * Copyright (c) 2001-2005 Gaudenz Alder
005: *
006: * See LICENSE file in distribution for licensing details of this source file
007: */
008: package org.jgraph.event;
009:
010: import java.util.EventObject;
011:
012: /**
013: * An event that characterizes a change in the current selection. The change is
014: * based on any number of cells. GraphSelectionListeners will generally query
015: * the source of the event for the new selected status of each potentially
016: * changed cell.
017: *
018: * @see GraphSelectionListener
019: * @see org.jgraph.graph.GraphSelectionModel
020: *
021: * @version 1.0 1/1/02
022: * @author Gaudenz Alder
023: */
024: public class GraphSelectionEvent extends EventObject {
025: /** Cells this event represents. */
026: protected Object[] cells;
027:
028: /**
029: * For each cell identifies whether or not that cell is newly selected.
030: */
031: protected boolean[] areNew;
032:
033: /**
034: * Represents a change in the selection of a GraphSelectionModel.
035: * <code>cells</code> identifies the cells that have been either added or
036: * removed from the selection.
037: *
038: * @param source
039: * source of event
040: * @param cells
041: * the paths that have changed in the selection
042: * @param areNew
043: * for each cell, defines whether or not that cell is newly
044: * selected
045: */
046: public GraphSelectionEvent(Object source, Object[] cells,
047: boolean[] areNew) {
048: super (source);
049: this .cells = cells;
050: this .areNew = areNew;
051: }
052:
053: /**
054: * Returns the cells that have been added or removed from the selection.
055: *
056: * @return added or removed cells
057: */
058: public Object[] getCells() {
059: int numCells;
060: Object[] retCells;
061:
062: numCells = cells.length;
063: retCells = new Object[numCells];
064: System.arraycopy(cells, 0, retCells, 0, numCells);
065: return retCells;
066: }
067:
068: /**
069: * Returns the first cell.
070: *
071: * @return the first selected cell
072: */
073: public Object getCell() {
074: return cells[0];
075: }
076:
077: /**
078: * Returns true if the first cell has been added to the selection, a return
079: * value of false means the first cell has been removed from the selection.
080: *
081: * @return whether or not the first cell has been added or removed
082: */
083: public boolean isAddedCell() {
084: return areNew[0];
085: }
086:
087: /**
088: * Returns true if the cell identified by cell was added to the selection. A
089: * return value of false means the cell was in the selection but is no
090: * longer in the selection. This will raise if cell is not one of the cells
091: * identified by this event.
092: *
093: * @param cell
094: * the cell that is to be indicated as newly selected or not
095: * @return <code>true</code> if the specified cell is newly selected
096: */
097: public boolean isAddedCell(Object cell) {
098: for (int counter = cells.length - 1; counter >= 0; counter--)
099: if (cells[counter].equals(cell))
100: return areNew[counter];
101: throw new IllegalArgumentException(
102: "cell is not a cell identified by the GraphSelectionEvent");
103: }
104:
105: /**
106: * Returns true if the cell identified by <code>index</code> was added to
107: * the selection. A return value of false means the cell was in the
108: * selection but is no longer in the selection. This will raise an exception
109: * if index < 0 || >=<code>getPaths</code> .length.
110: *
111: * @param index
112: * the index of <code>areNew</code> of the cell that is to be
113: * indicated as newly selected or not
114: * @return whether or not the cell is newly selected or not
115: */
116: public boolean isAddedCell(int index) {
117: if (cells == null || index < 0 || index >= cells.length) {
118: throw new IllegalArgumentException(
119: "index is beyond range of added cells identified by GraphSelectionEvent");
120: }
121: return areNew[index];
122: }
123:
124: /**
125: * Returns a copy of the receiver, but with the source being newSource.
126: *
127: * @param newSource
128: * the new event source
129: * @return the cloned event with the specified source
130: */
131: public Object cloneWithSource(Object newSource) {
132: // Fix for IE bug - crashing
133: return new GraphSelectionEvent(newSource, cells, areNew);
134: }
135: }
|