📄 defaultgraphselectionmodel.java
字号:
if (selection != null) return selection.toArray(); return null; } /** * Returns the number of paths that are selected. */ public int getSelectionCount() { return (selection == null) ? 0 : selection.size(); } /** * Returns true if the cell, <code>cell</code>, * is in the current selection. */ public boolean isCellSelected(Object cell) { int count = getSelectedChildCount(cell); return (count == SELECTED); } /** * Returns true if the cell, <code>cell</code>, * has selected children. */ public boolean isChildrenSelected(Object cell) { int count = getSelectedChildCount(cell); return (count > 0); } /** * Returns true if the selection is currently empty. */ public boolean isSelectionEmpty() { return (selection.isEmpty()); } /** * Empties the current selection. If this represents a change in the * current selection, the selection listeners are notified. */ public void clearSelection() { if (selection != null) { Vector change = new Vector(); Iterator it = cellStates.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); change.addElement(new CellPlaceHolder(entry.getKey(), false)); } selection.clear(); cellStates.clear(); if (change.size() > 0) notifyCellChange(change); } } // // Internal Datastructures // /** * Returns the number of selected childs for <code>cell</code>. */ protected int getSelectedChildCount(Object cell) { if (cell != null) { Integer state = (Integer) cellStates.get(cell); if (state == null) { state = UNSELECTED; cellStates.put(cell, state); } return state.intValue(); } return 0; } /** * Sets the number of selected childs for <code>cell</code> * to <code>count</code>. */ protected void setSelectedChildCount(Object cell, int count) { Integer i = new Integer(count); cellStates.put(cell, i); } /** * Selects a single cell and updates all datastructures. * No listeners are notified. Override this method to control * individual cell selection. */ protected boolean select(List list, Object cell) { if (!isCellSelected(cell) && graph.getGraphLayoutCache().isVisible(cell)) { GraphModel model = graph.getModel(); // Deselect and Update All Parents Object parent = model.getParent(cell); while (parent != null) { int count = getSelectedChildCount(parent); // Deselect Selected Parents if (count == SELECTED) count = 0; // Increase Child Count count++; setSelectedChildCount(parent, count); // Remove From Selection selection.remove(parent); // Next Parent parent = model.getParent(parent); } // Deselect All Children Object[] tmp = new Object[] { cell }; Set childs = DefaultGraphModel.getDescendants(model, tmp); // Remove Current Cell From Flat-View childs.remove(cell); Iterator it = childs.iterator(); while (it.hasNext()) { Object child = it.next(); if (child != null && !model.isPort(child)) { // Remove Child From Selection selection.remove(child); // Remove Child State cellStates.remove(child); } } // Set Selected State for Current setSelectedChildCount(cell, SELECTED); // Add Current To HashSet and Return return list.add(cell); } return false; } /** * Deselects a single cell and updates all datastructures. * No listeners are notified. */ protected boolean deselect(Object cell) { if (isCellSelected(cell)) { // Update All Parents Object parent = graph.getModel().getParent(cell); boolean firstParent = true; int change = -1; while (parent != null && change != 0) { int count = getSelectedChildCount(parent); count += change; // Select First Parent If No More Children if (count == 0 && firstParent) { change = 0; count = SELECTED; selection.add(parent); } // Update Selection Count setSelectedChildCount(parent, count); // Next Parent parent = graph.getModel().getParent(parent); firstParent = false; } // Remove State of Current Cell cellStates.remove(cell); // Remove Current from Selection and Return return selection.remove(cell); } return false; } // // Listeners // /** * Adds x to the list of listeners that are notified each time the * set of selected TreePaths changes. * * @param x the new listener to be added */ public void addGraphSelectionListener(GraphSelectionListener x) { listenerList.add(GraphSelectionListener.class, x); } /** * Removes x from the list of listeners that are notified each time * the set of selected TreePaths changes. * * @param x the listener to remove */ public void removeGraphSelectionListener(GraphSelectionListener x) { listenerList.remove(GraphSelectionListener.class, x); } /** * Notifies all listeners that are registered for * tree selection events on this object. * @see #addGraphSelectionListener * @see EventListenerList */ protected void fireValueChanged(GraphSelectionEvent e) { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // TreeSelectionEvent e = null; // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == GraphSelectionListener.class) { // Lazily create the event: // if (e == null) // e = new ListSelectionEvent(this, firstIndex, lastIndex); ((GraphSelectionListener) listeners[i + 1]).valueChanged(e); } } } /** * Returns an array of all the listeners of the given type that * were added to this model. * * @return all of the objects receiving <em>listenerType</em> notifications * from this model * * @since 1.3 */ public EventListener[] getListeners(Class listenerType) { return listenerList.getListeners(listenerType); } /** * Adds a PropertyChangeListener to the listener list. * The listener is registered for all properties. * <p> * A PropertyChangeEvent will get fired when the selection mode * changes. * * @param listener the PropertyChangeListener to be added */ public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { if (changeSupport == null) { changeSupport = new SwingPropertyChangeSupport(this); } changeSupport.addPropertyChangeListener(listener); } /** * Removes a PropertyChangeListener from the listener list. * This removes a PropertyChangeListener that was registered * for all properties. * * @param listener the PropertyChangeListener to be removed */ public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { if (changeSupport == null) { return; } changeSupport.removePropertyChangeListener(listener); } /** * Notifies listeners of a change in path. changePaths should contain * instances of PathPlaceHolder. */ protected void notifyCellChange(Vector changedCells) { int cCellCount = changedCells.size(); boolean[] newness = new boolean[cCellCount]; Object[] cells = new Object[cCellCount]; CellPlaceHolder placeholder; for (int counter = 0; counter < cCellCount; counter++) { placeholder = (CellPlaceHolder) changedCells.elementAt(counter); newness[counter] = placeholder.isNew; cells[counter] = placeholder.cell; } GraphSelectionEvent event = new GraphSelectionEvent(this, cells, newness); fireValueChanged(event); } /** * Returns a clone of this object with the same selection. * This method does not duplicate * selection listeners and property listeners. * * @exception CloneNotSupportedException never thrown by instances of * this class */ public Object clone() throws CloneNotSupportedException { DefaultGraphSelectionModel clone = (DefaultGraphSelectionModel) super.clone(); clone.changeSupport = null; if (selection != null) clone.selection = new ArrayList(selection); clone.listenerList = new EventListenerList(); return clone; } /** * Holds a path and whether or not it is new. */ protected class CellPlaceHolder { protected boolean isNew; protected Object cell; protected CellPlaceHolder(Object cell, boolean isNew) { this.cell = cell; this.isNew = isNew; } /** * Returns the cell. * @return Object */ public Object getCell() { return cell; } /** * Returns the isNew. * @return boolean */ public boolean isNew() { return isNew; } /** * Sets the cell. * @param cell The cell to set */ public void setCell(Object cell) { this.cell = cell; } /** * Sets the isNew. * @param isNew The isNew to set */ public void setNew(boolean isNew) { this.isNew = isNew; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -