📄 jreepadview.java
字号:
{ setCurrentNode((JreepadNode)(node.getUserObject())); } */ private void setCurrentNode(JreepadNode n) { boolean isSame = currentNode!=null && n.equals(currentNode); //System.out.println("setCurrentNode() activated: sameness test = "+isSame); // // This "isSame" test should stop the caret jumping to the end of the text when we press Save. // if(isSame) { // Only update the node's stored content if it's a plaintext node if(currentNode.getArticleMode() == JreepadNode.ARTICLEMODE_ORDINARY) currentNode.setContent(getEditorPaneText()); return; } copyEditorPaneContentToNodeContent = false; // Deactivate the caret-listener, effectively - ALSO DEACTIVATES UNDO-STORAGE if(currentNode != null) { // Only update the node's stored content if it's a plaintext node if(currentNode.getArticleMode() == JreepadNode.ARTICLEMODE_ORDINARY) currentNode.setContent(getEditorPaneText()); } currentNode = n; setEditorPaneText(n.getContent());// editorPanePlainText.setText(n.getContent());// editorPaneHtml.setText(n.getContent()); ensureCorrectArticleRenderMode(); copyEditorPaneContentToNodeContent = true; // Reactivate the caret listener - ALSO REACTIVATES UNDO-STORAGE } public JTree getTree() { return tree; } public JreepadNode getRootJreepadNode() { setCurrentNode(getCurrentNode()); // Ensures any edits have been committed return root; } public JreepadNode getCurrentNode() { return currentNode; } public String getTreepadNodeUrl() { StringBuffer ret = new StringBuffer("\"node:/"); Object[] p = tree.getLeadSelectionPath().getPath(); for(int i=0; i<p.length; i++) ret.append("/" + ((JreepadNode)p[i]).getTitle()); return ret.toString() + "\""; } public void moveNode(JreepadNode node, JreepadNode newParent) { // First we need to make sure that the node is not a parent of the new parent // - otherwise things would go really wonky! if(node.isNodeInSubtree(newParent)) { return; } //DEL storeForUndo(); JreepadNode oldParent = node.getParentNode(); // Now make a note of the expanded/collapsed state of the subtree of the moving node boolean thisOnesExpanded = tree.isExpanded(tree.getSelectionPath()); Enumeration enumer; Vector expanded; if(thisOnesExpanded) { enumer = tree.getExpandedDescendants(tree.getSelectionPath()); expanded = new Vector(); while(enumer.hasMoreElements()) { expanded.add((TreePath)enumer.nextElement());// System.out.println(expanded.lastElement()); } } node.removeFromParent(); newParent.addChild(node); treeModel.reload(oldParent); treeModel.reload(newParent); // treeModel.reload((TreeNode)tree.getPathForRow(0).getLastPathComponent()); // If the destination node didn't previously have any children, then we'll expand it // if(newParent.getChildCount()==1) // Reapply the expanded/collapsed states } public void indentCurrentNode() { if(currentNode.equals(root)) { notForRootNode(); return; } int nodeRow = tree.getLeadSelectionRow(); TreePath parentPath = tree.getSelectionPath().getParentPath(); int pos = currentNode.getIndex(); if(pos<1) return; //DEL storeForUndo(); JreepadNode newParent = ((JreepadNode)currentNode.getParent().getChildAt(pos-1)); if(currentNode.indent()) { treeModel.reload(currentNode.getParent().getParent()); parentPath = parentPath.pathByAddingChild(newParent); TreePath myPath = parentPath.pathByAddingChild(currentNode); // Now use scrollPathToVisible() or scrollRowToVisible() to make sure it's visible tree.scrollPathToVisible(myPath); tree.setSelectionPath(myPath); } } public void outdentCurrentNode() { if(currentNode.equals(root)) { notForRootNode(); return; } TreePath parentPath = tree.getSelectionPath().getParentPath(); if(parentPath==null) return; TreePath parentParentPath = parentPath.getParentPath(); if(parentParentPath==null) return; //DEL storeForUndo(); if(currentNode.outdent()) { TreePath myPath = parentParentPath.pathByAddingChild(currentNode); treeModel.reload(currentNode.getParent()); // Now use scrollPathToVisible() or scrollRowToVisible() to make sure it's visible tree.scrollPathToVisible(myPath); tree.setSelectionPath(myPath); } } public void moveCurrentNodeUp() { TreePath nodePath = tree.getSelectionPath(); if(currentNode.equals(root)) { notForRootNode(); return; } //DEL storeForUndo(); currentNode.moveUp(); treeModel.reload(currentNode.getParent()); tree.setSelectionPath(nodePath); } public void moveCurrentNodeDown() { TreePath nodePath = tree.getSelectionPath(); if(currentNode.equals(root)) { notForRootNode(); return; } //DEL storeForUndo(); currentNode.moveDown(); treeModel.reload(currentNode.getParent()); tree.setSelectionPath(nodePath); } private void notForRootNode() { // FIXME: If there are no child nodes, assume the user needs some advice about adding nodes if(root.isLeaf()) JOptionPane.showMessageDialog(this, JreepadViewer.lang.getString("MSG_ONLY_ON_CHILDNODES"), JreepadViewer.lang.getString("TITLE_ONLY_ON_CHILDNODES") , JOptionPane.INFORMATION_MESSAGE); else return;// JOptionPane.showMessageDialog(this, // "The root node is currently selected - you can only perform this operation on child nodes.", "Root node is selected" , // JOptionPane.INFORMATION_MESSAGE); } protected String getContentForNewNode() { if(prefs.autoDateInArticles) return getCurrentDate(); // java.text.DateFormat.getDateInstance().format(new java.util.Date()); else return ""; } private java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(); private String getCurrentDate() { return dateFormat.format(new java.util.Date()); } public void insertDate() { if(currentNode.getArticleMode() != JreepadNode.ARTICLEMODE_ORDINARY) return; // May want to fix this later - allow other modes to have the date inserted... //DEL storeForUndo(); String theDate = getCurrentDate(); Document doc = editorPanePlainText.getDocument(); int here = editorPanePlainText.getCaretPosition(); try { editorPanePlainText.setText(doc.getText(0, here) + theDate + doc.getText(here, doc.getLength() - here)); editorPaneHtml.setText(doc.getText(0, here) + theDate + doc.getText(here, doc.getLength() - here)); editorPanePlainText.setCaretPosition(here + theDate.length()); } catch(BadLocationException e) { // Simply ignore this } } public JreepadNode addNodeAbove() { int index = currentNode.getIndex(); if(index==-1) { notForRootNode(); return null; } if(tree.getSelectionPath()==null) return null; //DEL storeForUndo(); TreePath parentPath = tree.getSelectionPath().getParentPath(); JreepadNode parent = currentNode.getParentNode(); JreepadNode ret = parent.addChild(index); ret.setContent(getContentForNewNode()); treeModel.nodesWereInserted(parent, new int[]{index}); TreePath newPath = (parentPath.pathByAddingChild(ret)); if(newPath!=null) tree.startEditingAtPath(newPath); return ret; } public JreepadNode addNodeBelow() { int index = currentNode.getIndex(); if(index==-1) { notForRootNode(); return null; } if(tree.getSelectionPath()==null) return null; //DEL storeForUndo(); TreePath parentPath = tree.getSelectionPath().getParentPath(); JreepadNode parent = currentNode.getParentNode(); JreepadNode ret = parent.addChild(index+1); ret.setContent(getContentForNewNode()); treeModel.nodesWereInserted(parent, new int[]{index+1}); tree.startEditingAtPath(parentPath.pathByAddingChild(ret)); return ret; } public JreepadNode addNode() { //DEL storeForUndo(); JreepadNode ret = currentNode.addChild(); ret.setContent(getContentForNewNode()); TreePath nodePath = tree.getSelectionPath(); treeModel.nodesWereInserted(currentNode, new int[]{currentNode.getIndex(ret)}); // tree.setSelectionPath(nodePath.pathByAddingChild(ret)); tree.scrollPathToVisible(nodePath.pathByAddingChild(ret)); tree.startEditingAtPath(nodePath.pathByAddingChild(ret)); return ret; } public JreepadNode removeNode() { JreepadNode parent = (JreepadNode)currentNode.getParent(); TreePath parentPath = tree.getSelectionPath().getParentPath(); if(parent != null) { //DEL storeForUndo(); int index = parent.getIndex(currentNode); JreepadNode ret = parent.removeChild(index); setCurrentNode(parent); tree.setSelectionPath(parentPath); treeModel.nodesWereRemoved(parent, new int[]{index}, new Object[]{ret}); repaint(); return ret; } else return null; } public void sortChildren() { //DEL storeForUndo(); currentNode.sortChildren(); treeModel.reload(currentNode); // System.out.println(currentNode.toFullString()); } public void sortChildrenRecursive() { //DEL storeForUndo(); currentNode.sortChildrenRecursive(); treeModel.reload(currentNode); // System.out.println(currentNode.toFullString()); } public void returnFocusToTree() { tree.requestFocus(); } public void expandAllCurrentNode() { expandAll(currentNode, tree.getLeadSelectionPath()); } public void expandAll(JreepadNode thisNode, TreePath tp) { // It's at this point that we expand the current element tree.expandPath(tp); Enumeration getKids = thisNode.children(); JreepadNode thisKid; while(getKids.hasMoreElements()) { thisKid = (JreepadNode)getKids.nextElement(); expandAll(thisKid, tp.pathByAddingChild(thisKid)); } } public void collapseAllCurrentNode() { collapseAll(currentNode, tree.getLeadSelectionPath()); } public void collapseAll(JreepadNode thisNode, TreePath tp) { Enumeration getKids = thisNode.children(); JreepadNode thisKid; while(getKids.hasMoreElements()) { thisKid = (JreepadNode)getKids.nextElement(); collapseAll(thisKid, tp.pathByAddingChild(thisKid)); } // It's at this point that we collapse the current element tree.collapsePath(tp); } public TreePath[] getAllExpandedPaths() { if(root.getChildCount()==0) return new TreePath[] {new TreePath(root)}; Enumeration getPaths = tree.getExpandedDescendants(new TreePath(root)); TreePath thisKid; Vector allPaths = new Vector(); while(getPaths.hasMoreElements()) { thisKid = (TreePath)getPaths.nextElement(); allPaths.add(thisKid); } TreePath[] ret = new TreePath[allPaths.size()]; for(int i=0; i<ret.length; i++) ret[i] = (TreePath)allPaths.get(i); return ret; } // THIS FUNCTION SEEMS TO HAVE NO EFFECT, ON MY MACHINE AT LEAST! WHAT'S GOING ON? public void expandPaths(TreePath[] paths) { for(int i=0; i<paths.length; i++) { tree.expandPath(paths[i]); } } // Functions and inner class for searching nodes public boolean performSearch(String inNodes, String inArticles, int searchWhat // 0=selected, 1=all , boolean orNotAnd, boolean caseSensitive, int maxResults) { switch(searchWhat) { case 0: // search selected node searcher.performSearch(inNodes, inArticles, tree.getSelectionPath(), orNotAnd, caseSensitive, maxResults); break; default: // case 1==search whole tree searcher.performSearch(inNodes, inArticles, new TreePath(root), orNotAnd, caseSensitive, maxResults); break; } return true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -