📄 jgraph.java
字号:
firePropertyChange( SELECTION_MODEL_PROPERTY, oldValue, this.selectionModel); } /** * Returns the model for selections. This should always return a * non-<code>null</code> value. If you don't want to allow anything * to be selected * set the selection model to <code>null</code>, which forces an empty * selection model to be used. * @return the current selection model * @see #setSelectionModel * */ public GraphSelectionModel getSelectionModel() { return selectionModel; } /** * Clears the selection. */ public void clearSelection() { getSelectionModel().clearSelection(); } /** * Returns true if the selection is currently empty. * @return true if the selection is currently empty */ public boolean isSelectionEmpty() { return getSelectionModel().isSelectionEmpty(); } /** * Adds a listener for <code>GraphSelection</code> events. * @param tsl the <code>GraphSelectionListener</code> that will be notified * when a cell is selected or deselected (a "negative * selection") */ public void addGraphSelectionListener(GraphSelectionListener tsl) { listenerList.add(GraphSelectionListener.class, tsl); if (listenerList.getListenerCount(GraphSelectionListener.class) != 0 && selectionRedirector == null) { selectionRedirector = new GraphSelectionRedirector(); selectionModel.addGraphSelectionListener(selectionRedirector); } } /** * Removes a <code>GraphSelection</code> listener. * @param tsl the <code>GraphSelectionListener</code> to remove */ public void removeGraphSelectionListener(GraphSelectionListener tsl) { listenerList.remove(GraphSelectionListener.class, tsl); if (listenerList.getListenerCount(GraphSelectionListener.class) == 0 && selectionRedirector != null) { selectionModel.removeGraphSelectionListener(selectionRedirector); selectionRedirector = null; } } /** * Notifies all listeners that have registered interest for * notification on this event type. The event instance * is lazily created using the parameters passed into * the fire method. * @param e the <code>GraphSelectionEvent</code> generated by the * <code>GraphSelectionModel</code> * when a cell is selected or deselected * @see javax.swing.event.EventListenerList * */ protected void fireValueChanged(GraphSelectionEvent e) { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // 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) { ((GraphSelectionListener) listeners[i + 1]).valueChanged(e); } } } /** * Selects the specified cell. * @param cell the <code>Object</code> specifying the cell to select */ public void setSelectionCell(Object cell) { getSelectionModel().setSelectionCell(cell); } /** * Selects the specified cells. * @param cells an array of objects that specifies * the cells to select */ public void setSelectionCells(Object[] cells) { getSelectionModel().setSelectionCells(cells); } /** * Adds the cell identified by the specified <code>Object</code> * to the current selection. * @param cell the cell to be added to the selection */ public void addSelectionCell(Object cell) { getSelectionModel().addSelectionCell(cell); } /** * Adds each cell in the array of cells to the current selection. * @param cells an array of objects that specifies the cells to add */ public void addSelectionCells(Object[] cells) { getSelectionModel().addSelectionCells(cells); } /** * Removes the cell identified by the specified Object from the current * selection. * @param cell the cell to be removed from the selection */ public void removeSelectionCell(Object cell) { getSelectionModel().removeSelectionCell(cell); } /** * Returns the first selected cell. * @return the <code>Object</code> for the first selected cell, * or <code>null</code> if nothing is currently selected */ public Object getSelectionCell() { return getSelectionModel().getSelectionCell(); } /** * Returns all selected cells. * @return an array of objects representing the selected cells, * or <code>null</code> if nothing is currently selected */ public Object[] getSelectionCells() { return getSelectionModel().getSelectionCells(); } /** * Returns the number of cells selected. * @return the number of cells selected */ public int getSelectionCount() { return getSelectionModel().getSelectionCount(); } /** * Returns true if the cell is currently selected. * @param cell an object identifying a cell * @return true if the cell is selected */ public boolean isCellSelected(Object cell) { return getSelectionModel().isCellSelected(cell); } /** * Scrolls to the specified cell. Only works when this * <code>JGraph</code> is contained in a <code>JScrollPane</code>. * @param cell the object identifying the cell to bring into view */ public void scrollCellToVisible(Object cell) { Rectangle bounds = getCellBounds(cell); if (bounds != null) { bounds = new Rectangle(bounds); scrollRectToVisible(toScreen(bounds)); } } /** * Makes sure the specified point is visible. * @param p the point that should be visible */ public void scrollPointToVisible(Point p) { if (p != null) { Rectangle bounds = new Rectangle(p); if (bounds != null) scrollRectToVisible(bounds); } } /** * Returns true if the graph is being edited. The item that is being * edited can be obtained using <code>getEditingCell</code>. * @return true if the user is currently editing a cell * @see #getSelectionCell * */ public boolean isEditing() { GraphUI graph = getUI(); if (graph != null) return graph.isEditing(this); return false; } /** * Ends the current editing session. * (The <code>DefaultGraphCellEditor</code> * object saves any edits that are currently in progress on a cell. * Other implementations may operate differently.) * Has no effect if the tree isn't being edited. * <blockquote> * <b>Note:</b><br> * To make edit-saves automatic whenever the user changes * their position in the graph, use {@link #setInvokesStopCellEditing}. * </blockquote> * @return true if editing was in progress and is now stopped, * false if editing was not in progress */ public boolean stopEditing() { GraphUI graph = getUI(); if (graph != null) return graph.stopEditing(this); return false; } /** * Cancels the current editing session. Has no effect if the * graph isn't being edited. */ public void cancelEditing() { GraphUI graph = getUI(); if (graph != null) graph.cancelEditing(this); } /** * Selects the specified cell and initiates editing. * The edit-attempt fails if the <code>CellEditor</code> * does not allow * editing for the specified item. */ public void startEditingAtCell(Object cell) { GraphUI graph = getUI(); if (graph != null) graph.startEditingAtCell(this, cell); } /** * Returns the cell that is currently being edited. * @return the cell being edited */ public Object getEditingCell() { GraphUI graph = getUI(); if (graph != null) return graph.getEditingCell(this); return null; } /** * Messaged when the graph has changed enough that we need to resize * the bounds, but not enough that we need to remove the cells * (e.g cells were inserted into the graph). You should never have to * invoke this, the UI will invoke this as it needs to. (Note: This * is invoked by GraphUI, eg. after moving.) */ public void graphDidChange() { revalidate(); repaint(); } /** * Serialization support. */ private void writeObject(ObjectOutputStream s) throws IOException { Vector values = new Vector(); s.defaultWriteObject(); // Save the cellEditor, if its Serializable. if (graphModel instanceof Serializable) { values.addElement("graphModel"); values.addElement(graphModel); } // Save the graphModel, if its Serializable. if (graphLayoutCache instanceof Serializable) { values.addElement("graphLayoutCache"); values.addElement(graphLayoutCache); } // Save the selectionModel, if its Serializable. if (selectionModel instanceof Serializable) { values.addElement("selectionModel"); values.addElement(selectionModel); } s.writeObject(values); if (getUIClassID().equals(uiClassID)) { /*byte count = JComponent.getWriteObjCounter(this); JComponent.setWriteObjCounter(this, --count);*/ if (/*count == 0 && */ ui != null) { ui.installUI(this); } } } /** * Serialization support. */ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); Vector values = (Vector) s.readObject(); int indexCounter = 0; int maxCounter = values.size(); if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("graphModel")) { graphModel = (GraphModel) values.elementAt(++indexCounter); indexCounter++; } if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("graphLayoutCache")) { graphLayoutCache = (GraphLayoutCache) values.elementAt(++indexCounter); indexCounter++; } if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("selectionModel")) { selectionModel = (GraphSelectionModel) values.elementAt(++indexCounter); indexCounter++; } // Reinstall the redirector. if (listenerList.getListenerCount(GraphSelectionListener.class) != 0) { selectionRedirector = new GraphSelectionRedirector(); selectionModel.addGraphSelectionListener(selectionRedirector); } } /** * <code>EmptySelectionModel</code> is a <code>GraphSelectionModel</code> * that does not allow anything to be selected. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is appropriate * for short term storage or RMI between applications running the same * version of Swing. A future release of Swing will provide support for * long term persistence. */ public static class EmptySelectionModel extends DefaultGraphSelectionModel { /** Unique shared instance. */ protected static final EmptySelectionModel sharedInstance = new EmptySelectionModel(); /** A <code>null</code> implementation that constructs an * EmptySelectionModel. */ public EmptySelectionModel() { super(null); } /** Returns a shared instance of an empty selection model. */ static public EmptySelectionModel sharedInstance() { return sharedInstance; } /** A <code>null</code> implementation that selects nothing. */ public void setSelectionCells(Object[] cells) { } /** A <code>null</code> implementation that adds nothing. */ public void addSelectionCells(Object[] cells) { } /** A <code>null</code> implementation that removes nothing. */ public void removeSelectionCells(Object[] cells) { } } /** * Handles creating a new <code>GraphSelectionEvent</code> with the * <code>JGraph</code> as the * source and passing it off to all the listeners. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is appropriate * for short term storage or RMI between applications running the same * version of Swing. A future release of Swing will provide support for * long term persistence. */ protected class GraphSelectionRedirector implements Serializable, GraphSelectionListener { /** * Invoked by the <code>GraphSelectionModel</code> when the * selection changes. * * @param e the <code>GraphSelectionEvent</code> generated by the * <code>GraphSelectionModel</code> */ public void valueChanged(GraphSelectionEvent e) { GraphSelectionEvent newE; newE = (GraphSelectionEvent) e.cloneWithSource(JGraph.this); fireValueChanged(newE); } } // End of class JGraph.GraphSelectionRedirector // // Scrollable interface // /** * Returns the preferred display size of a <code>JGraph</code>. The height is * determined from <code>getPreferredWidth</code>. * @return the graph's preferred size */ public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } /** * Returns the amount to increment when scrolling. The amount is 4. * @param visibleRect the view area visible within the viewport * @param orientation either <code>SwingConstants.VERTICAL</code> * or <code>SwingConstants.HORIZONTAL</code> * @param direction less than zero to scroll up/left, * greater than zero for down/right * @return the "unit" increment for scrolling in the specified direction * @see javax.swing.JScrollBar#setUnitIncrement(int) * */ public int getScrollableUnitIncrement( Rectangle visibleRect, int orientation, int direction) { if (orientation == SwingConstants.VERTICAL) { return 2; } return 4; } /** * Returns the amount for a block increment, which is the height or * width of <code>visibleRect</code>, based on <code>orientation</code>. * @param visibleRect the view area visible within the viewport * @param orientation either <code>SwingConstants.VERTICAL</code> * or <code>SwingConstants.HORIZONTAL</code> * @param direction less than zero to scroll up/left, * greater than zero for down/right. * @return the "block" increment for scrolling in the specified direction * @see javax.swing.JScrollBar#setBlockIncrement(int) * */ public int getScrollableBlockIncrement( Rectangle visibleRect, int orientation, int direction) { return (orientation == SwingConstants.VERTICAL) ? visibleRect.height : visibleRect.width; } /** * Returns false to indicate that the width of the viewport does not * determine the width of the graph, unless the preferred width of * the graph is smaller than the viewports width. In other words: * ensure that the graph is never smaller than its viewport. * @return false * @see Scrollable#getScrollableTracksViewportWidth * */ public boolean getScrollableTracksViewportWidth() { if (getParent() instanceof JViewport) { return ( ((JViewport) getParent()).getWidth() > getPreferredSize().width); } return false; } /** * Returns false to indicate that the height of the viewport does not * determine the height of the graph, unless the preferred height * of the graph is smaller than the viewports height. In other words: * ensure that the graph is never smaller than its viewport. * @return false * @see Scrollable#getScrollableTracksViewportHeight * */ public boolean getScrollableTracksViewportHeight() { if (getParent() instanceof JViewport) { return ( ((JViewport) getParent()).getHeight() > getPreferredSize().height); } return false; } /** * Returns a string representation of this <code>JGraph</code>. * This method * is intended to be used only for debugging purposes, and the * content and format of the returned string may vary between * implementations. The returned string may be empty but may not * be <code>null</code>. * @return a string representation of this <code>JGraph</code>. */ protected String paramString() { String editableString = (editable ? "true" : "false"); String invokesStopCellEditingString = (invokesStopCellEditing ? "true" : "false"); return super.paramString() + ",editable=" + editableString + ",invokesStopCellEditing=" + invokesStopCellEditingString; } public static void main(String[] args) { System.out.println(VERSION); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -