下面列出了怎么用javax.swing.event.TreeModelEvent的API类实例代码及写法,或者点击链接到github查看源代码。
public void sortBranch(TreePath pathToBranch) {
BugSet bs = bugSet.query((BugAspects) pathToBranch.getLastPathComponent());
bs.sortList();
Debug.println("Data in sorted branch: " + pathToBranch.getLastPathComponent());
for (BugLeafNode b : bs) {
Debug.println(b);
}
Object[] children = new Object[getChildCount(pathToBranch.getLastPathComponent())];
int[] childIndices = new int[children.length];
for (int x = 0; x < children.length; x++) {
children[x] = getChild(pathToBranch.getLastPathComponent(), x);
childIndices[x] = x;
}
for (TreeModelListener l : listeners) {
TreeModelEvent event = new TreeModelEvent(this, pathToBranch, childIndices, children);
l.treeNodesChanged(event);
}
}
@Nonnull
public NodeProject addProjectFolder(@Nonnull final File folder) throws IOException {
NodeProject newProject = findForFolder(folder);
if (newProject == null) {
newProject = new NodeProject(this, folder);
final int index = this.children.size();
this.children.add(newProject);
final TreeModelEvent event = new TreeModelEvent(this, new Object[]{this}, new int[]{index}, new Object[]{newProject});
for (final TreeModelListener l : this.listeners) {
l.treeNodesInserted(event);
}
}
return newProject;
}
@Override
public void treeNodesChanged(TreeModelEvent e)
{
TreePath parentPath = e.getTreePath();
int leadingRow = Integer.MAX_VALUE;
int trailingRow = -1;
if (e.getChildren() != null)
{
for (Object node : e.getChildren())
{
TreePath childPath = parentPath.pathByAddingChild(node);
int row = tree.getRowForPath(childPath);
leadingRow = Math.min(leadingRow, row);
trailingRow = Math.max(trailingRow, row);
}
}
fireTableRowsUpdated(leadingRow, trailingRow);
}
public void fireNodeAdded(final GTreeNode parentNode, final GTreeNode newNode) {
if (!eventsEnabled) {
return;
}
SystemUtilities.assertThisIsTheSwingThread(
"GTreeModel.fireNodeAdded() must be " + "called from the AWT thread");
GTreeNode parent = convertToViewNode(parentNode);
if (parent == null) { // it will be null if filtered out
return;
}
int index = parent.getIndexOfChild(newNode);
if (index < 0) {
// the index will be -1 if filtered out
return;
}
TreeModelEvent event = new TreeModelEvent(this, parent.getTreePath(), new int[] { index },
new Object[] { newNode });
for (TreeModelListener listener : listeners) {
listener.treeNodesInserted(event);
}
}
private TreeModelEvent getChangedNodeEvent(GTreeNode changedNode) {
GTreeNode parentNode = changedNode.getParent();
if (parentNode == null) { // tree requires different event form when it is the root that changes
return new TreeModelEvent(this, root.getTreePath(), null, null);
}
GTreeNode node = convertToViewNode(changedNode);
if (node == null) {
return null;
}
int indexInParent = node.getIndexInParent();
if (indexInParent < 0) {
return null;
}
return new TreeModelEvent(this, node.getParent().getTreePath(), new int[] { indexInParent },
new Object[] { changedNode });
}
/**
* Method called when nodes are being inserted into the tree; update the treePath and the
* group path fields in the ProgramNode object.
*/
@Override
public void treeNodesInserted(TreeModelEvent e) {
Object[] path = e.getPath();
Object[] me = e.getChildren();
// build the tree path for the node being inserted
Object[] childPath = new Object[path.length + 1];
System.arraycopy(path, 0, childPath, 0, path.length);
childPath[childPath.length - 1] = me[0];
ProgramNode node = (ProgramNode) me[0];
node.setTreePath(new TreePath(childPath));
// set up GroupPath
tree.setGroupPath(node);
}
public void fireParameterRemoved( final ParameterDefinitionEntry parameter ) {
final TreePath pathForNode = new TreePath( new Object[] { getRoot(), getReportParametersNode() } );
final TreeModelListener[] treeModelListeners = getListeners();
final int index = findParameterInCache( parameter );
if ( index == -1 ) {
return;
}
final TreeModelEvent treeEvent = new TreeModelEvent( this, pathForNode,
new int[] { index }, new Object[] { parameter } );
for ( int i = treeModelListeners.length - 1; i >= 0; i -= 1 ) {
final TreeModelListener listener = treeModelListeners[ i ];
listener.treeNodesRemoved( treeEvent );
}
refreshParameterCache();
}
/**
* <p>Invoked after nodes have been removed from the tree. Note that
* if a subtree is removed from the tree, this method may only be
* invoked once for the root of the removed subtree, not once for
* each individual set of siblings removed.</p>
*
* <p>e.path() returns the former parent of the deleted nodes.</p>
*
* <p>e.childIndices() returns the indices the nodes had before they were deleted in ascending order.</p>
*/
public void treeNodesRemoved(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
int maxCounter;
TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
FHTreeStateNode changedParentNode = getNodeForPath
(parentPath, false, false);
changedIndexs = e.getChildIndices();
// PENDING(scott): make sure that changedIndexs are sorted in
// ascending order.
if(changedParentNode != null && changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
Object[] children = e.getChildren();
boolean isVisible =
(changedParentNode.isVisible() &&
changedParentNode.isExpanded());
for(int counter = maxCounter - 1; counter >= 0; counter--) {
changedParentNode.removeChildAtModelIndex
(changedIndexs[counter], isVisible);
}
if(isVisible) {
if(treeSelectionModel != null)
treeSelectionModel.resetRowSelection();
if (treeModel.getChildCount(changedParentNode.
getUserObject()) == 0 &&
changedParentNode.isLeaf()) {
// Node has become a leaf, collapse it.
changedParentNode.collapse(false);
}
visibleNodesChanged();
}
else if(changedParentNode.isVisible())
visibleNodesChanged();
}
}
}
protected void fireTreeNodesRemoved(Layer parentLayer) {
TreeModelEvent event = createTreeModelEvent(parentLayer);
if (event == null) {
return;
}
for (TreeModelListener treeModelListener : treeModelListeners.keySet()) {
treeModelListener.treeNodesRemoved(event);
}
}
private void fireRemove(ASTNavigatorNode node, int[] indices, ASTNavigatorNode[] children,
LinkedList<ASTNavigatorNode> nodePath) {
TreeModelListener[] listeners = listenerList.getListeners (TreeModelListener.class);
if (listeners.length == 0) return;
TreePath path = new TreePath(nodePath.toArray());
TreeModelEvent e = new TreeModelEvent (this, path, indices, children);
for (int i = 0; i < listeners.length; i++)
listeners [i].treeNodesRemoved (e);
}
/**
* <p>Invoked after nodes have been removed from the tree. Note that
* if a subtree is removed from the tree, this method may only be
* invoked once for the root of the removed subtree, not once for
* each individual set of siblings removed.</p>
*
* <p>e.path() returns the former parent of the deleted nodes.</p>
*
* <p>e.childIndices() returns the indices the nodes had before they were deleted in ascending order.</p>
*/
public void treeNodesRemoved(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
int maxCounter;
TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
FHTreeStateNode changedParentNode = getNodeForPath
(parentPath, false, false);
changedIndexs = e.getChildIndices();
// PENDING(scott): make sure that changedIndexs are sorted in
// ascending order.
if(changedParentNode != null && changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
Object[] children = e.getChildren();
boolean isVisible =
(changedParentNode.isVisible() &&
changedParentNode.isExpanded());
for(int counter = maxCounter - 1; counter >= 0; counter--) {
changedParentNode.removeChildAtModelIndex
(changedIndexs[counter], isVisible);
}
if(isVisible) {
if(treeSelectionModel != null)
treeSelectionModel.resetRowSelection();
if (treeModel.getChildCount(changedParentNode.
getUserObject()) == 0 &&
changedParentNode.isLeaf()) {
// Node has become a leaf, collapse it.
changedParentNode.collapse(false);
}
visibleNodesChanged();
}
else if(changedParentNode.isVisible())
visibleNodesChanged();
}
}
}
public boolean deleteNode(@Nonnull final NodeFileOrFolder node, final boolean notifyListeners) {
final NodeFileOrFolder parentNode = node.getNodeParent();
if (parentNode != null) {
final TreeModelEvent event = new TreeModelEvent(this, parentNode.makeTreePath(), new int[]{node.getIndexAtParent()}, new Object[]{node});
if (parentNode.deleteChild(node)) {
if (notifyListeners) {
for (final TreeModelListener l : this.listeners) {
l.treeNodesRemoved(event);
}
}
return true;
}
}
return false;
}
void fireDisplayChange(LookTreeNode source) {
if (listeners.isEmpty()) {
return;
}
LookTreeNode parent = source.getParent();
TreePath path = findPath(parent != null ? parent : source);
int[] childIndices = parent != null ? new int[] {getIndexOfChild(parent, source)} : null;
Object[] children = parent != null ? new Object[] {source} : null;
TreeModelEvent ev = new TreeModelEvent(this, path, childIndices, children);
for (TreeModelListener l : listeners) {
l.treeNodesChanged(ev);
}
}
public void removeWidget(Widget w)
{
Layout parent = w.getParent();
Object[] path = findPath(parent);
int index = parent.getWidgets().indexOf(w);
fireNodeRemovedEvent(new TreeModelEvent(this, path,
new int[] { index }, new Object[] { w }));
}
protected void fireTreeNodeChanged(Layer layer) {
TreeModelEvent event = createTreeModelEvent(layer);
if (event == null) {
return;
}
for (TreeModelListener treeModelListener : treeModelListeners.keySet()) {
treeModelListener.treeNodesChanged(event);
}
}
/** Creates an identical TreeModelEvent with the model we are proxying
* as the event source */
private TreeModelEvent translateEvent (TreeModelEvent e) {
//Create a new TreeModelEvent with us as the source
TreeModelEvent nue = new TreeModelEvent (getModel(), e.getPath(),
e.getChildIndices(), e.getChildren());
return nue;
}
/**
* <p>Invoked after nodes have been inserted into the tree.</p>
*
* <p>e.path() returns the parent of the new nodes
* <p>e.childIndices() returns the indices of the new nodes in
* ascending order.
*/
public void treeNodesInserted(TreeModelEvent e) {
if(e != null) {
int[] changedIndexs;
FHTreeStateNode changedParent = getNodeForPath
(SwingUtilities2.getTreePath(e, getModel()), false, false);
int maxCounter;
changedIndexs = e.getChildIndices();
/* Only need to update the children if the node has been
expanded once. */
// PENDING(scott): make sure childIndexs is sorted!
if(changedParent != null && changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
boolean isVisible =
(changedParent.isVisible() &&
changedParent.isExpanded());
for(int counter = 0; counter < maxCounter; counter++) {
changedParent.childInsertedAtModelIndex
(changedIndexs[counter], isVisible);
}
if(isVisible && treeSelectionModel != null)
treeSelectionModel.resetRowSelection();
if(changedParent.isVisible())
this.visibleNodesChanged();
}
}
}
/**
* <p>Invoked after nodes have been removed from the tree. Note that
* if a subtree is removed from the tree, this method may only be
* invoked once for the root of the removed subtree, not once for
* each individual set of siblings removed.</p>
*
* <p>e.path() returns the former parent of the deleted nodes.</p>
*
* <p>e.childIndices() returns the indices the nodes had before they were deleted in ascending order.</p>
*/
public void treeNodesRemoved(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
int maxCounter;
TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
FHTreeStateNode changedParentNode = getNodeForPath
(parentPath, false, false);
changedIndexs = e.getChildIndices();
// PENDING(scott): make sure that changedIndexs are sorted in
// ascending order.
if(changedParentNode != null && changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
Object[] children = e.getChildren();
boolean isVisible =
(changedParentNode.isVisible() &&
changedParentNode.isExpanded());
for(int counter = maxCounter - 1; counter >= 0; counter--) {
changedParentNode.removeChildAtModelIndex
(changedIndexs[counter], isVisible);
}
if(isVisible) {
if(treeSelectionModel != null)
treeSelectionModel.resetRowSelection();
if (treeModel.getChildCount(changedParentNode.
getUserObject()) == 0 &&
changedParentNode.isLeaf()) {
// Node has become a leaf, collapse it.
changedParentNode.collapse(false);
}
visibleNodesChanged();
}
else if(changedParentNode.isVisible())
visibleNodesChanged();
}
}
}
/**
* <p>Invoked after a node (or a set of siblings) has changed in some
* way. The node(s) have not changed locations in the tree or
* altered their children arrays, but other attributes have
* changed and may affect presentation. Example: the name of a
* file has changed, but it is in the same location in the file
* system.</p>
*
* <p>e.path() returns the path the parent of the changed node(s).</p>
*
* <p>e.childIndices() returns the index(es) of the changed node(s).</p>
*/
public void treeNodesChanged(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
FHTreeStateNode changedParent = getNodeForPath
(SwingUtilities2.getTreePath(e, getModel()), false, false);
int maxCounter;
changedIndexs = e.getChildIndices();
/* Only need to update the children if the node has been
expanded once. */
// PENDING(scott): make sure childIndexs is sorted!
if (changedParent != null) {
if (changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
Object parentValue = changedParent.getUserObject();
for(int counter = 0; counter < maxCounter; counter++) {
FHTreeStateNode child = changedParent.
getChildAtModelIndex(changedIndexs[counter]);
if(child != null) {
child.setUserObject(treeModel.getChild(parentValue,
changedIndexs[counter]));
}
}
if(changedParent.isVisible() && changedParent.isExpanded())
visibleNodesChanged();
}
// Null for root indicates it changed.
else if (changedParent == root && changedParent.isVisible() &&
changedParent.isExpanded()) {
visibleNodesChanged();
}
}
}
}
/**
* <p>Invoked after nodes have been inserted into the tree.</p>
*
* <p>e.path() returns the parent of the new nodes
* <p>e.childIndices() returns the indices of the new nodes in
* ascending order.
*/
public void treeNodesInserted(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
FHTreeStateNode changedParent = getNodeForPath
(SwingUtilities2.getTreePath(e, getModel()), false, false);
int maxCounter;
changedIndexs = e.getChildIndices();
/* Only need to update the children if the node has been
expanded once. */
// PENDING(scott): make sure childIndexs is sorted!
if(changedParent != null && changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
boolean isVisible =
(changedParent.isVisible() &&
changedParent.isExpanded());
for(int counter = 0; counter < maxCounter; counter++) {
changedParent.childInsertedAtModelIndex
(changedIndexs[counter], isVisible);
}
if(isVisible && treeSelectionModel != null)
treeSelectionModel.resetRowSelection();
if(changedParent.isVisible())
this.visibleNodesChanged();
}
}
}
/**
* <p>Invoked after a node (or a set of siblings) has changed in some
* way. The node(s) have not changed locations in the tree or
* altered their children arrays, but other attributes have
* changed and may affect presentation. Example: the name of a
* file has changed, but it is in the same location in the file
* system.</p>
*
* <p>e.path() returns the path the parent of the changed node(s).</p>
*
* <p>e.childIndices() returns the index(es) of the changed node(s).</p>
*/
public void treeNodesChanged(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
FHTreeStateNode changedParent = getNodeForPath
(SwingUtilities2.getTreePath(e, getModel()), false, false);
int maxCounter;
changedIndexs = e.getChildIndices();
/* Only need to update the children if the node has been
expanded once. */
// PENDING(scott): make sure childIndexs is sorted!
if (changedParent != null) {
if (changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
Object parentValue = changedParent.getUserObject();
for(int counter = 0; counter < maxCounter; counter++) {
FHTreeStateNode child = changedParent.
getChildAtModelIndex(changedIndexs[counter]);
if(child != null) {
child.setUserObject(treeModel.getChild(parentValue,
changedIndexs[counter]));
}
}
if(changedParent.isVisible() && changedParent.isExpanded())
visibleNodesChanged();
}
// Null for root indicates it changed.
else if (changedParent == root && changedParent.isVisible() &&
changedParent.isExpanded()) {
visibleNodesChanged();
}
}
}
}
/**
* <p>Invoked after nodes have been inserted into the tree.</p>
*
* <p>e.path() returns the parent of the new nodes
* <p>e.childIndices() returns the indices of the new nodes in
* ascending order.
*/
public void treeNodesInserted(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
FHTreeStateNode changedParent = getNodeForPath
(SwingUtilities2.getTreePath(e, getModel()), false, false);
int maxCounter;
changedIndexs = e.getChildIndices();
/* Only need to update the children if the node has been
expanded once. */
// PENDING(scott): make sure childIndexs is sorted!
if(changedParent != null && changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
boolean isVisible =
(changedParent.isVisible() &&
changedParent.isExpanded());
for(int counter = 0; counter < maxCounter; counter++) {
changedParent.childInsertedAtModelIndex
(changedIndexs[counter], isVisible);
}
if(isVisible && treeSelectionModel != null)
treeSelectionModel.resetRowSelection();
if(changedParent.isVisible())
this.visibleNodesChanged();
}
}
}
/**
* Informs all listeners, that the tree's contents have changed.
*/
private void fireTreeModelChanged() {
for ( int i = 0; i < listeners.size(); i++ ) {
final TreeModelListener l = listeners.get( i );
l.treeStructureChanged( new TreeModelEvent( this, new TreePath( root ) ) );
}
}
public void refreshAll ()
{
nodeMap.clear();
TreeModelEvent modelEvent = new TreeModelEvent(
this,
new Object[]{score});
for (TreeModelListener listener : listeners) {
listener.treeStructureChanged(modelEvent);
}
}
/**
* <p>Invoked after nodes have been removed from the tree. Note that
* if a subtree is removed from the tree, this method may only be
* invoked once for the root of the removed subtree, not once for
* each individual set of siblings removed.</p>
*
* <p>e.path() returns the former parent of the deleted nodes.</p>
*
* <p>e.childIndices() returns the indices the nodes had before they were deleted in ascending order.</p>
*/
public void treeNodesRemoved(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
int maxCounter;
TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
FHTreeStateNode changedParentNode = getNodeForPath
(parentPath, false, false);
changedIndexs = e.getChildIndices();
// PENDING(scott): make sure that changedIndexs are sorted in
// ascending order.
if(changedParentNode != null && changedIndexs != null &&
(maxCounter = changedIndexs.length) > 0) {
Object[] children = e.getChildren();
boolean isVisible =
(changedParentNode.isVisible() &&
changedParentNode.isExpanded());
for(int counter = maxCounter - 1; counter >= 0; counter--) {
changedParentNode.removeChildAtModelIndex
(changedIndexs[counter], isVisible);
}
if(isVisible) {
if(treeSelectionModel != null)
treeSelectionModel.resetRowSelection();
if (treeModel.getChildCount(changedParentNode.
getUserObject()) == 0 &&
changedParentNode.isLeaf()) {
// Node has become a leaf, collapse it.
changedParentNode.collapse(false);
}
visibleNodesChanged();
}
else if(changedParentNode.isVisible())
visibleNodesChanged();
}
}
}
public void refreshPath (TreePath path)
{
TreeModelEvent modelEvent = new TreeModelEvent(this, path);
for (TreeModelListener listener : listeners) {
listener.treeStructureChanged(modelEvent);
}
}
/**
* Invoked after a node (or a set of siblings) has changed in some
* way. The node(s) have not changed locations in the tree or
* altered their children arrays, but other attributes have
* changed and may affect presentation. Example: the name of a
* file has changed, but it is in the same location in the file
* system.
*
* <p><code>e.path</code> returns the path the parent of the
* changed node(s).
*
* <p><code>e.childIndices</code> returns the index(es) of the
* changed node(s).
*
* @param e the <code>TreeModelEvent</code> of interest
*/
public void treeNodesChanged(TreeModelEvent e) {
if(e != null) {
int changedIndexs[];
TreeStateNode changedNode;
changedIndexs = e.getChildIndices();
changedNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
if(changedNode != null) {
Object changedValue = changedNode.getValue();
/* Update the size of the changed node, as well as all the
child indexs that are passed in. */
changedNode.updatePreferredSize();
if(changedNode.hasBeenExpanded() && changedIndexs != null) {
int counter;
TreeStateNode changedChildNode;
for(counter = 0; counter < changedIndexs.length;
counter++) {
changedChildNode = (TreeStateNode)changedNode
.getChildAt(changedIndexs[counter]);
/* Reset the user object. */
changedChildNode.setUserObject
(treeModel.getChild(changedValue,
changedIndexs[counter]));
changedChildNode.updatePreferredSize();
}
}
else if (changedNode == root) {
// Null indicies for root indicates it changed.
changedNode.updatePreferredSize();
}
if(!isFixedRowHeight()) {
int aRow = changedNode.getRow();
if(aRow != -1)
this.updateYLocationsFrom(aRow);
}
this.visibleNodesChanged();
}
}
}
@Override
public void treeNodesRemoved(TreeModelEvent event) {
}
/**
* Invoked after the tree has drastically changed structure from a
* given node down. If the path returned by <code>e.getPath</code>
* is of length one and the first element does not identify the
* current root node the first element should become the new root
* of the tree.
*
* <p><code>e.path</code> holds the path to the node.
* <p><code>e.childIndices</code> returns <code>null</code>.
*
* @param e the <code>TreeModelEvent</code> of interest
*/
public void treeStructureChanged(TreeModelEvent e) {
if(e != null)
{
TreePath changedPath = SwingUtilities2.getTreePath(e, getModel());
TreeStateNode changedNode;
changedNode = getNodeForPath(changedPath, false, false);
// Check if root has changed, either to a null root, or
// to an entirely new root.
if(changedNode == root ||
(changedNode == null &&
((changedPath == null && treeModel != null &&
treeModel.getRoot() == null) ||
(changedPath != null && changedPath.getPathCount() == 1)))) {
rebuild(true);
}
else if(changedNode != null) {
int nodeIndex;
TreeStateNode newNode, parent;
boolean wasExpanded, wasVisible;
int newIndex;
wasExpanded = changedNode.isExpanded();
wasVisible = (changedNode.getRow() != -1);
/* Remove the current node and recreate a new one. */
parent = (TreeStateNode)changedNode.getParent();
nodeIndex = parent.getIndex(changedNode);
if(wasVisible && wasExpanded) {
changedNode.collapse(false);
}
if(wasVisible)
visibleNodes.removeElement(changedNode);
changedNode.removeFromParent();
createNodeAt(parent, nodeIndex);
newNode = (TreeStateNode)parent.getChildAt(nodeIndex);
if(wasVisible && wasExpanded)
newNode.expand(false);
newIndex = newNode.getRow();
if(!isFixedRowHeight() && wasVisible) {
if(newIndex == 0)
updateYLocationsFrom(newIndex);
else
updateYLocationsFrom(newIndex - 1);
this.visibleNodesChanged();
}
else if(wasVisible)
this.visibleNodesChanged();
}
}
}
@Override public void treeNodesRemoved(TreeModelEvent e) {
/* not needed */
}