📄 jreepadview.java
字号:
} public JreepadSearcher.JreepadSearchResult[] getSearchResults() { return searcher.getSearchResults(); } public void addChildrenFromTextFiles(File[] inFiles) throws IOException { //DEL storeForUndo(); for(int i=0; i<inFiles.length; i++) getCurrentNode().addChildFromTextFile(new InputStreamReader(new FileInputStream(inFiles[i]), getPrefs().getEncoding()) , inFiles[i].getName()); treeModel.reload(currentNode); tree.expandPath(tree.getSelectionPath()); } public void addChild(JreepadNode newKid) { //DEL storeForUndo(); getCurrentNode().addChild(newKid); treeModel.reload(currentNode); tree.expandPath(tree.getSelectionPath()); } public void addChildrenFromListTextFile(InputStreamReader inFile) throws IOException { //DEL storeForUndo(); BufferedReader bReader = new BufferedReader(inFile); String curLine; while((curLine = bReader.readLine())!=null) if(curLine.trim().length() > 0) getCurrentNode().addChild(new JreepadNode(curLine.trim(), "", getCurrentNode())); treeModel.reload(currentNode); tree.expandPath(tree.getSelectionPath()); } public String getSelectedTextInArticle() { switch(currentNode.getArticleMode()) { case JreepadNode.ARTICLEMODE_CSV: int x = editorPaneCsv.getSelectedColumn(); int y = editorPaneCsv.getSelectedRow(); if(x==-1 || y ==-1) return ""; return editorPaneCsv.getValueAt(y,x).toString(); case JreepadNode.ARTICLEMODE_HTML: return editorPaneHtml.getSelectedText(); case JreepadNode.ARTICLEMODE_ORDINARY: default: return editorPanePlainText.getSelectedText(); } } public static JreepadPrefs getPrefs() { return prefs; } public static void setPrefs(JreepadPrefs thesePrefs) { prefs = thesePrefs; } /* DEPRECATED - I wrote this before discovering Java's UndoManager. // Stuff concerned with undo public void undoAction() { if(!canWeUndo()) return; // Swap the old root / selectionpath / expandedpaths for the current ones JreepadNode tempRoot = root; root = oldRootForUndo; oldRootForUndo = tempRoot; // Fire a tree-structure-changed event for the entire tree treeModel.setRoot(root); treeModel.reload(root); // Set the correct selection and expanded paths tree.setSelectionPath(oldSelectedPath); // I hope this ends up firing the setCurrentNode() function... editorPanePlainText.setText(currentNode.getContent()); editorPaneHtml.setText(currentNode.getContent()); repaint(); } public boolean canWeUndo() { return oldRootForUndo != null; } private void storeForUndo() { // Use JreepadNode.getCopy() on the root node to get a copy of the whole tree oldRootForUndo = root.getCopy(); // Also get the tree's entire set of open TreePaths and selected TreePaths oldSelectedPath = tree.getSelectionPath(); } void clearUndoCache() { oldRootForUndo = null; } // End of: stuff concerned with undo*/ // Stuff concerned with linking public void webSearchTextSelectedInArticle() { // JComponent treeOrArticle; String url = getSelectedTextInArticle(); if(url==null || url.length()==0) url = currentNode.getTitle(); if((url == null) && (currentNode.getArticleMode()==JreepadNode.ARTICLEMODE_ORDINARY)) { try { String text = getEditorPaneText(); int startpos = editorPanePlainText.getCaretPosition(); int endpos = startpos; if(text.length()>0) { // Select the character before/after the current position, and grow it until we hit whitespace... while(startpos>0 && !Character.isWhitespace(editorPanePlainText.getText(startpos-1,1).charAt(0))) startpos--; while(endpos<(text.length()) && !Character.isWhitespace(editorPanePlainText.getText(endpos,1).charAt(0))) endpos++; if(endpos>startpos) { editorPanePlainText.setSelectionStart(startpos); editorPanePlainText.setSelectionEnd(endpos); url = editorPanePlainText.getSelectedText(); } } } catch(BadLocationException err) {System.out.println(err); } } if(url==null || !(url.length()>0)) url = currentNode.getTitle(); webSearchText(url); } public void webSearchText(String text) { openURL("http://" + getPrefs().webSearchPrefix + text + getPrefs().webSearchPostfix); } public void openURLSelectedInArticle() { String url = getSelectedTextInArticle(); if((url == null) && (currentNode.getArticleMode()==JreepadNode.ARTICLEMODE_ORDINARY)) { try { String text = getEditorPaneText(); int startpos = editorPanePlainText.getCaretPosition(); int endpos = startpos; if(text != null) { // Select the character before/after the current position, and grow it until we hit whitespace... while(startpos>0 && !Character.isWhitespace(editorPanePlainText.getText(startpos-1,1).charAt(0))) startpos--; while(endpos<(text.length()) && !Character.isWhitespace(editorPanePlainText.getText(endpos,1).charAt(0))) endpos++; if(endpos>startpos) { editorPanePlainText.setSelectionStart(startpos); editorPanePlainText.setSelectionEnd(endpos); url = editorPanePlainText.getSelectedText(); } } } catch(BadLocationException err) { } } openURL(url); } public static boolean isPureWord(String in) { char[] c = in.toCharArray(); for(int i=0; i<c.length; i++) if(c[i]==':' || c[i]=='/' || c[i]=='[' || c[i]==']') return false; return true; } public static boolean isWikiWord(String in) { if(in.length()>4 && in.startsWith("[[") && in.endsWith("]]")) return true; char[] c = in.toCharArray(); int uppers = 0; boolean currentlyUpper = false; for(int i=0; i<c.length; i++) if(!Character.isLetter(c[i])) return false; else if(i==0 && !Character.isUpperCase(c[i])) return false; else if(currentlyUpper && Character.isLowerCase(c[i])) { currentlyUpper = false; uppers++; } else if(!currentlyUpper && Character.isUpperCase(c[i])) { currentlyUpper = true; } return uppers>1; } public void openURL(String url) { if(url==null || url=="") return; url = url.trim(); // Wiki-like links if(isWikiWord(url)) { followWikiLink(url, prefs.wikiBehaviourActive); return; }// if(url.length()>4 && url.startsWith("[[") && url.endsWith("]]"))// {// followWikiLink(url.substring(2, url.length()-2));// return;// } if(isPureWord(url)) { if(prefs.defaultSearchMode == 0) webSearchText(url); else followWikiLink(url, false); return; } // Strip quotes off if(url.length()>2 && url.startsWith("\"") && url.endsWith("\"")) url = url.substring(1, url.length()-1); // Treepad node:// links if(url.startsWith("node://")) { if(!followTreepadInternalLink(url)) JOptionPane.showMessageDialog(this, JreepadViewer.lang.getString("MSG_NODE_NOT_FOUND"), JreepadViewer.lang.getString("TITLE_NODE_NOT_FOUND"), JOptionPane.ERROR_MESSAGE); return; } // It's probably a web-link, so let's do something to it and then try and launch it /*// NOTE:// I haven't been able to get this file:// method to work, on Windows 2000 or on Mac OSX.// So I'm disactivating it for now. // Firstly we use Kami's method for attempting to open file:// links if(url.startsWith("file://")) { url = getPrefs().openLocation.getParentFile().getPath() + System.getProperty("file.separator")+ url.substring(7); try { BrowserLauncher.openURL(url.toString()); } catch(IOException err) { JOptionPane.showMessageDialog(this, "I/O error while opening URL:\n"+url+"\n\nThe \"BrowserLauncher\" used to open a URL is an open-source Java library \nseparate from Jreepad itself - i.e. a separate Sourceforge project. \nIt may be a good idea to submit a bug report to\nhttp://sourceforge.net/projects/browserlauncher\n\nIf you do, please remember to supply information about the operating system\nyou are using - which type, and which version.", "Error" , JOptionPane.ERROR_MESSAGE); } } else {*/ char[] curl = url.toCharArray(); StringBuffer surl = new StringBuffer(); for(int i=0; i<curl.length; i++) if(curl[i]==' ') surl.append("%20"); else surl.append(curl[i]); try { BrowserLauncher.openURL(surl.toString()); } catch(IOException err) { JOptionPane.showMessageDialog(this, "I/O error while opening URL:\n"+surl+"\n\nThe \"BrowserLauncher\" used to open a URL is an open-source Java library \nseparate from Jreepad itself - i.e. a separate Sourceforge project. \nIt may be a good idea to submit a bug report to\nhttp://sourceforge.net/projects/browserlauncher\n\nIf you do, please remember to supply information about the operating system\nyou are using - which type, and which version.", "Error" , JOptionPane.ERROR_MESSAGE); }// } } public boolean followTreepadInternalLink(String url) { url = url.substring(7); // Split it at slashes, and then add each one to the new TreePath object as we go Vector pathNames = new Vector(); StringBuffer buf = new StringBuffer(); char[] curl = url.toCharArray(); for(int i=0; i<curl.length; i++) if(curl[i]=='/') { pathNames.add(buf.toString()); buf = new StringBuffer(); } else buf.append(curl[i]); if(buf.length()>0) pathNames.add(buf.toString());// System.out.println(pathNames); // OK, so we've got the names into an array. Now how do we actually follow the path? if(pathNames.size()<1 || !((String)pathNames.get(0)).equals(root.getTitle())) return false; TreePath goTo = new TreePath(root); JreepadNode nextNode = root; for(int i=1; i<pathNames.size(); i++) { nextNode = nextNode.getChildByTitle((String)pathNames.get(i)); if(nextNode == null) return false; goTo = goTo.pathByAddingChild(nextNode); } tree.setSelectionPath(goTo); tree.scrollPathToVisible(goTo); return true; } // End of: stuff concerned with linking // Searching (for wikilike action) public void followWikiLink(String text, boolean noNeedToConfirm) { if(text.length()>4 && text.startsWith("[[") && text.endsWith("]]")) text = text.substring(2, text.length()-2); TreePath tp = findNearestNodeTitled(text); if(tp == null) { if(noNeedToConfirm || JOptionPane.showConfirmDialog(this, JreepadViewer.lang.getString("TITLE_NODE_NOT_FOUND_PROMPT_CREATE"), JreepadViewer.lang.getString("MSG_NODE_NOT_FOUND") , JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE) == JOptionPane.YES_OPTION) { JreepadNode newNode; TreePath newPath; newNode = new JreepadNode(text, "", currentNode); addChild(newNode); TreePath leadPath = tree.getLeadSelectionPath(); if(leadPath != null) newPath = leadPath.pathByAddingChild(newNode); else newPath = new TreePath(newNode); // Now we need to select it... how do we do that? tree.setSelectionPath(newPath); tree.scrollPathToVisible(newPath); } } else tree.setSelectionPath(tp); } public TreePath findNearestNodeTitled(String text) { TreePath curPath = tree.getLeadSelectionPath(); TreePath tp; while(curPath != null && curPath.getPathCount()>0) { tp = findChildTitled(text, curPath); if(tp!=null) return tp; // Else try again but using the parent... curPath = curPath.getParentPath(); } return null; } public TreePath findChildTitled(String text) { return findChildTitled(text, tree.getLeadSelectionPath()); } public TreePath findChildTitled(String text, TreePath pathToNode) { JreepadNode myNode = (JreepadNode)pathToNode.getLastPathComponent(); JreepadNode myChild; TreePath childPath; for(int i=0; i<myNode.getChildCount(); i++) { myChild = (JreepadNode)myNode.getChildAt(i); childPath = pathToNode.pathByAddingChild(myChild); if(myChild.getTitle().equals(text)) return childPath; else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -