public void updateClientState(Node nodeUpdate) { for (int i = 0; i < rootNode.getChildCount(); i++) { TreeNode treeNode = rootNode.getChildAt(i); // System.out.println(treeNode.toString()); if (treeNode.toString().equals(nodeUpdate.getName())) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeNode; if (nodeUpdate.isOnline() && nodeUpdate.isValid()) { // 删除”状态:在线,可信“ 那一行 node.remove(2); node.add(new DefaultMutableTreeNode("状态:在线,可信")); Log.i(this.getClass().getName(), nodeUpdate.getName() + " 已登录,状态可信."); } else if (nodeUpdate.isOnline() && !nodeUpdate.isValid()) { node.remove(2); node.add(new DefaultMutableTreeNode("状态:在线,不可信")); Log.v( this.getClass().getName(), nodeUpdate.getName() + "状态不可信,IP地址为:" + nodeUpdate.getIP()); } else { node.remove(2); node.add(new DefaultMutableTreeNode("状态:离线")); Log.i( this.getClass().getName(), nodeUpdate.getName() + "不在线,IP地址为:" + nodeUpdate.getIP()); } this.jTree.updateUI(); return; } } }
public void removeMarker(LogMarker marker) { DefaultMutableTreeNode node; for (int i = 0; i < markersNode.getChildCount(); i++) { node = (DefaultMutableTreeNode) markersNode.getChildAt(i); if (node.getUserObject() == marker) { markersNode.remove(node); } } if (markersNode.getChildCount() == 0) { root.remove(markersNode); markersNode = null; } treeModel.nodeStructureChanged(root); expandAllTree(); }
public void removeTreeItem(Object current) { // Casting final File file = (File) current; // Information required final DefaultMutableTreeNode node = getFileNode(file); // if (node != null) { // final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent(); // Dont allow removal of root node. if (node == defaultRootNode) { return; } // Grab the index of it final int index = parent.getIndex(node); // Remove it parent.remove(index); // updateTree(); } }
public static void moveSelectedRow(final JTree tree, final int direction) { final TreePath selectionPath = tree.getSelectionPath(); final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) treeNode.getParent(); final int idx = parent.getIndex(treeNode); parent.remove(treeNode); parent.insert(treeNode, idx + direction); ((DefaultTreeModel) tree.getModel()).reload(parent); selectNode(tree, treeNode); }
private static void removePathFromActionsTree(JTree tree, ActionUrl url) { if (url.myComponent == null) return; final TreePath treePath = CustomizationUtil.getTreePath(tree, url); if (treePath == null) return; DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent(); final int absolutePosition = url.getAbsolutePosition(); if (node.getChildCount() > absolutePosition && absolutePosition >= 0) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(absolutePosition); if (child.getUserObject().equals(url.getComponent())) { node.remove(child); } } }
/** * Gets invoked when changes occur to the contents of the list, and updates the nodes of the tree * model accordingly. It the invokes <code>reload(node)</code> on the tree model for the affected * node so changes are reflected in the <code>JTree</code> * * @see ca.odell.glazedlists.event.ListEventListener#listChanged * (ca.odell.glazedlists.event.ListEvent) */ public void listChanged(ListEvent listChanges) { while (listChanges.next()) { int type = listChanges.getType(); int index = listChanges.getIndex(); if (type == ListEvent.INSERT) { Object element = listChanges.getSourceList().get(index); DefaultMutableTreeNode newNode = createChildNode(node, element); node.insert(newNode, index); } else { if (type == ListEvent.UPDATE) { node.remove(index); Object element = listChanges.getSourceList().get(index); DefaultMutableTreeNode newNode = createChildNode(node, element); node.insert(newNode, index); } else { if (type == ListEvent.DELETE) { node.remove(index); } } } } treeModel.reload(node); }
private void updateListOfMostRecentAlgorithms() { final boolean bShowMostRecent = new Boolean(SextanteGUI.getSettingParameterValue(SextanteGeneralSettings.SHOW_MOST_RECENT)) .booleanValue(); if (bShowMostRecent) { final GeoAlgorithm[] recent = History.getRecentlyUsedAlgs(); final DefaultMutableTreeNode recentNode = new DefaultMutableTreeNode(Sextante.getText("RecentAlgorithms")); for (int i = 0; i < recent.length; i++) { final DefaultMutableTreeNode node = new DefaultMutableTreeNode(recent[i]); recentNode.add(node); } final DefaultMutableTreeNode mainNode = (DefaultMutableTreeNode) new TreePath(jTree.getModel().getRoot()).getLastPathComponent(); mainNode.remove(0); mainNode.insert(recentNode, 0); } }
private void remove(Object obj, DefaultMutableTreeNode parent) { for (int i = 0; i < parent.getChildCount(); i++) { if (!(parent.getChildAt(i) instanceof DefaultMutableTreeNode)) continue; DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i); if (!node.isLeaf()) remove(obj, node); else if (node.isLeaf()) { if (node.getUserObject().equals(obj)) { parent.remove(node); if (parent.getChildCount() == 0 && parent.getParent() instanceof DefaultMutableTreeNode) { ((DefaultMutableTreeNode) parent.getParent()).remove(parent); } treeModel.nodeStructureChanged(root); expandAllTree(); return; } } } }
// filters out non-acceleratable menu entries @SuppressWarnings("rawtypes") private static void addAcceleratableChildrenRecursively( final DefaultMutableTreeNode target, final Enumeration sourceChildren) { while (sourceChildren.hasMoreElements()) { final DefaultMutableTreeNode sourceChild = (DefaultMutableTreeNode) sourceChildren.nextElement(); final MenuEntry menuEntry = (MenuEntry) sourceChild.getUserObject(); if (sourceChild.isLeaf()) { if (menuEntry.getKeyStroke() != null) { target.add(new DefaultMutableTreeNode(menuEntry)); } } else { final DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(menuEntry); target.add(newNode); MenuUtils.addAcceleratableChildrenRecursively(newNode, sourceChild.children()); if (newNode.isLeaf()) { target.remove(newNode); } } } }
/** * Removes a node from rule tree. * * @param dialog */ void removeRuleNode(final CustomSearchDialog dialog) { // Get selected node and parent DefaultTreeModel model = ((DefaultTreeModel) dialog.getComplexRulesTree().getModel()); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) dialog.getComplexRulesTree().getSelectionPath().getLastPathComponent(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectedNode.getParent(); // If it's root, remove it if (selectedNode.isRoot()) { model.setRoot(null); } else { parentNode.remove(selectedNode); } // Reload model model.reload(); // Expand complex rules tree to display full rule GuiUtils.expandTree(dialog.getComplexRulesTree()); }
private static void movePathInActionsTree(JTree tree, ActionUrl url) { final TreePath treePath = CustomizationUtil.getTreePath(tree, url); if (treePath != null) { if (treePath.getLastPathComponent() != null) { final DefaultMutableTreeNode parent = ((DefaultMutableTreeNode) treePath.getLastPathComponent()); final int absolutePosition = url.getAbsolutePosition(); final int initialPosition = url.getInitialPosition(); if (parent.getChildCount() > absolutePosition && absolutePosition >= 0) { if (parent.getChildCount() > initialPosition && initialPosition >= 0) { final DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent.getChildAt(initialPosition); if (child.getUserObject().equals(url.getComponent())) { parent.remove(child); parent.insert(child, absolutePosition); } } } } } }
/** * Performs a "live-update" to the original DefaultMutableTreeNode by reconsiling the differences * between it and a given updated DefaultMutableTreeNode. The resulting original * DefaultMutableTreeNode will look identical to the updated node. Appropriate events will fire to * keep the GUI consistant with any changes that were made to the model. * * @param original the model's existing node * @param updated the node representing the model node's new state */ private void reconcileNodes(DefaultMutableTreeNode original, DefaultMutableTreeNode updated) { SVNTreeNodeData originalData = (SVNTreeNodeData) original.getUserObject(); SVNTreeNodeData updatedData = (SVNTreeNodeData) updated.getUserObject(); if (!originalData.equals(updatedData)) { originalData.copyNodeValues(updatedData); } int originalChildCount = original.getChildCount(); int insertionPoint = 0; // loop through the original node's children for (int i = insertionPoint; i < originalChildCount; i++) { // loop through remaining updated node's children boolean found = false; DefaultMutableTreeNode originalChildNode = (DefaultMutableTreeNode) original.getChildAt(insertionPoint); SVNTreeNodeData originalChildData = (SVNTreeNodeData) originalChildNode.getUserObject(); while (updated.children().hasMoreElements()) { DefaultMutableTreeNode updatedChildNode = (DefaultMutableTreeNode) updated.children().nextElement(); SVNTreeNodeData updatedChildData = (SVNTreeNodeData) updatedChildNode.getUserObject(); if (originalChildData.getPath().equals(updatedChildData.getPath())) { // same resource, check equality insertionPoint++; found = true; // remove from updated list so we know we handled this node updated.remove(updatedChildNode); if (!originalChildData.equals(updatedChildData)) { // same resource, different values -- update originalChildData.copyNodeValues(updatedChildData); reconcileNodes(originalChildNode, updatedChildNode); ((DefaultTreeModel) this.getModel()).nodeChanged(originalChildNode); break; } else { // same values, do nothing reconcileNodes(originalChildNode, updatedChildNode); break; } } else { // different resources // does updated node's child need to be inserted before original node's child? if (updatedChildData.getPath().compareTo(originalChildData.getPath()) < 0) { original.insert(updatedChildNode, insertionPoint); ((DefaultTreeModel) this.getModel()) .nodesWereInserted(original, new int[] {insertionPoint}); insertionPoint++; // inserting the node into the original has the side-effect of removing it from the // updated // node, just keep going until we've handled all children, or until the remaining // children // come after the last original node } else { break; } } } if (!found) { original.remove(insertionPoint); ((DefaultTreeModel) this.getModel()) .nodesWereRemoved( original, new int[] {insertionPoint}, new Object[] {originalChildNode}); } } // add any remaining new child nodes while (updated.children().hasMoreElements()) { DefaultMutableTreeNode updatedChildNode = (DefaultMutableTreeNode) updated.children().nextElement(); original.add(updatedChildNode); // adding the new node to the original has the side-effect of removing it from the updated // node's children // so we just keep going until no children remain ((DefaultTreeModel) this.getModel()) .nodesWereInserted(original, new int[] {insertionPoint++}); } }
/** * @param Packet message - the message to be processed Process messages returned from the * ServerConnection by updating GUI objects. */ public synchronized void processMessage(Packet message) { System.out.println("Message received and processing in progress"); System.out.println( message.getCommand().name() + ": " + message.getChannelName() + " " + message.getMessageText()); Packet response; switch (message.getCommand()) { case REPLY_LIST_CHANNEL_USERS: if (message.getChannelName().equals(gui.roomLabel.getText())) { DefaultListModel userListModel = (DefaultListModel) gui.userList.getModel(); userListModel.setSize(0); String[] users = message.getMessageText().split(" "); for (String user : users) { userListModel.addElement(user); } } break; case REPLY_SUCCESS: System.out.print("Reply success"); break; case REPLY_FAILURE: break; case JOIN: if (message.getChannelName().equals(gui.roomLabel.getText())) { DefaultListModel userListModel = (DefaultListModel) gui.userList.getModel(); userListModel.addElement(message.getAuthor()); } break; case REPLY_LIST_USERS: JTree tree = gui.tree; DefaultMutableTreeNode root = new DefaultMutableTreeNode("All Users"); DefaultTreeModel model = new DefaultTreeModel(root); String[] text = message.getMessageText().split(" "); for (String name : text) { root.add(new DefaultMutableTreeNode(name)); } // Sleep until tree is available. treeLogic means we have to wait extra time for tree to get // setup before access while (!gui.visible) { try { tree = gui.tree; Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } tree.setModel(model); tree.repaint(); break; case MESSAGE: String mess = message.getAuthor() + ": " + message.getMessageText(); if (gui.roomLabel.getText().equals(message.getChannelName())) { synchronized (gui.chatList) { JList chatList = gui.chatList; DefaultListModel model2 = (DefaultListModel) chatList.getModel(); model2.add(0, mess); chatList.repaint(); } } else { DefaultTableModel roomModel = (DefaultTableModel) gui.roomTable.getModel(); synchronized (roomModel) { for (int i = 0; i < roomModel.getRowCount(); i++) { if (message.getChannelName() != gui.roomLabel.getText() && message.getChannelName().equals(roomModel.getValueAt(i, 2))) { String missed = (String) roomModel.getValueAt(i, 3); try { Integer numMissed = Integer.parseInt(missed); numMissed += 1; roomModel.setValueAt(numMissed.toString(), i, 3); } catch (Exception e) { roomModel.setValueAt("1", i, 3); } break; } } } } client.getMessages(message.getChannelName()).add(0, mess); break; case QUIT: JTree treeCopy = gui.tree; DefaultTreeModel treeModel = (DefaultTreeModel) treeCopy.getModel(); DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) treeModel.getRoot(); for (int i = 0; i < rootNode.getChildCount(); i++) { System.out.println(message.getAuthor()); System.out.println("child: " + rootNode.getChildAt(i)); if (rootNode.getChildAt(i).equals(message.getAuthor())) { rootNode.remove(i); break; } } break; default: System.out.println("Fell through"); break; } }