📄 pasteaction.java
字号:
.replaceAll("</[aA]>[\\s\\S]*", ""); String text = Tools.toXMLUnescapedText(textWithHtml.replaceAll( "\\n", "").replaceAll("<[^>]*>", "").trim()); if (text.equals("")) { text = link; } URL linkURL = null; try { linkURL = new URL(link); } catch (MalformedURLException ex) { try { // Either invalid URL or relative URL if (referenceURL == null && !baseUrlCanceled) { String referenceURLString = JOptionPane .showInputDialog(c .getText("enter_base_url")); if (referenceURLString == null) { baseUrlCanceled = true; } else { referenceURL = new URL(referenceURLString); } } linkURL = new URL(referenceURL, link); } catch (MalformedURLException ex2) { } } if (linkURL != null) { if (links.length == 2 & pastedNode != null) { // pastedNode != null iff the number of pasted lines is // one // The firts element in links[] array is never a link, therefore // the condition links.length == 2 actually says "there is one link". // Set link directly into node ((MindMapNodeModel) pastedNode).setLink(linkURL .toString()); break; } if (linkParentNode == null) { linkParentNode = c.newNode("Links"); // Here we cannot set bold, because linkParentNode.font is null insertNodeInto(linkParentNode, target); ((NodeAdapter) linkParentNode).setBold(true); } MindMapNode linkNode = c.newNode(text); linkNode.setLink(linkURL.toString()); insertNodeInto(linkNode, linkParentNode); } } } public long getNumberOfObjects(Object TransferData, Transferable transfer) throws UnsupportedFlavorException, IOException { return ((String) (String) transfer .getTransferData(DataFlavor.stringFlavor)).split("\n").length; } public DataFlavor getDataFlavor() { return MindMapNodesSelection.htmlFlavor; } } private class StringFlavorHandler implements DataFlavorHandler { public void paste(Object TransferData, MindMapNode target, boolean asSibling, boolean isLeft, Transferable t) throws UnsupportedFlavorException, IOException { //System.err.println("stringFlavor"); String textFromClipboard = (String) t .getTransferData(DataFlavor.stringFlavor); pasteStringWithoutRedisplay(textFromClipboard, target, asSibling); } public long getNumberOfObjects(Object TransferData, Transferable transfer) { return ((String) TransferData).split("\n").length; } public DataFlavor getDataFlavor() { return DataFlavor.stringFlavor; } } /* * */ private void _paste(Transferable t, MindMapNode target, boolean asSibling, boolean isLeft) { if (t == null) { return; } try { // Uncomment to print obtained data flavours /* DataFlavor[] fl = t.getTransferDataFlavors(); for (int i = 0; i < fl.length; i++) { System.out.println(fl[i]); } */ DataFlavorHandler[] dataFlavorHandlerList = getFlavorHandlers(); for (int i = 0; i < dataFlavorHandlerList.length; i++) { DataFlavorHandler handler = dataFlavorHandlerList[i]; DataFlavor flavor = handler.getDataFlavor(); if(t.isDataFlavorSupported(flavor)) { handler.paste(t.getTransferData(flavor), target, asSibling, isLeft, t); break; } } c.nodeStructureChanged((MindMapNode) (asSibling ? target.getParent() : target)); } catch (Exception e) { e.printStackTrace(); } c.getFrame().setWaitingCursor(false); } /** * @return */ private DataFlavorHandler[] getFlavorHandlers() { DataFlavorHandler[] dataFlavorHandlerList = new DataFlavorHandler[] { new FileListFlavorHandler(), new MindMapNodesFlavorHandler(), new HtmlFlavorHandler(), new StringFlavorHandler() }; return dataFlavorHandlerList; } private MindMapNodeModel pasteXMLWithoutRedisplay(String pasted, MindMapNode target) throws XMLParseException { return pasteXMLWithoutRedisplay(pasted, target, /*asSibling=*/false); } public MindMapNodeModel pasteXMLWithoutRedisplay(String pasted, MindMapNode target, boolean asSibling) throws XMLParseException { // Call nodeStructureChanged(target) after this function. try { MindMapXMLElement element = new MindMapXMLElement(c.getFrame()); element.parseFromReader(new StringReader(pasted)); MindMapNodeModel node = (MindMapNodeModel)element.getUserObject(); element.processUnfinishedLinks(c.getModel().getLinkRegistry()); // now, the import is finished. We can inform others about the new nodes: if (asSibling) { MindMapNode parent = target.getParentNode(); insertNodeInto(node, parent, parent.getChildPosition(target)); } else { insertNodeInto(node, target); } c.invokeHooksRecursively(node, c.getModel()); return node; } catch (IOException ee) { ee.printStackTrace(); return null; }} static final Pattern nonLinkCharacter = Pattern.compile("[ \n()'\",;]"); /** * Paste String (as opposed to other flavours) * * Split the text into lines; determine the new tree structure * by the number of leading spaces in lines. In case that * trimmed line starts with protocol (http:, https:, ftp:), * create a link with the same content. * * If there was only one line to be pasted, return the pasted node, null otherwise. */ private MindMapNode pasteStringWithoutRedisplay(String textFromClipboard, MindMapNode parent, boolean asSibling) { Pattern mailPattern = Pattern.compile("([^@ <>\\*']+@[^@ <>\\*']+)"); String[] textLines = textFromClipboard.split("\n"); if (textLines.length > 1) { c.getFrame().setWaitingCursor(true); } MindMapNode realParent = null; if (asSibling) { // When pasting as sibling, we use virtual node as parent. When the pasting to // virtual node is completed, we insert the children of that virtual node to // the parrent of real parent. realParent = parent; parent = new MindMapNodeModel(c.getFrame()); } ArrayList parentNodes = new ArrayList(); ArrayList parentNodesDepths = new ArrayList(); parentNodes.add(parent); parentNodesDepths.add(new Integer(-1)); String[] linkPrefixes = { "http://", "ftp://", "https://" }; MindMapNode pastedNode = null; for (int i = 0; i < textLines.length; ++i) { String text = textLines[i]; text = text.replaceAll("\t"," "); if (text.matches(" *")) { continue; } int depth = 0; while (depth < text.length() && text.charAt(depth) == ' ') { ++depth; } String visibleText = text.trim(); // If the text is a recognizable link (e.g. http://www.google.com/index.html), // make it more readable by look nicer by cutting off obvious prefix and other // transforamtions. if (visibleText.matches("^http://(www\\.)?[^ ]*$")) { visibleText = visibleText.replaceAll("^http://(www\\.)?",""). replaceAll("(/|\\.[^\\./\\?]*)$","").replaceAll("((\\.[^\\./]*\\?)|\\?)[^/]*$"," ? ...").replaceAll("_|%20"," "); String[] textParts = visibleText.split("/"); visibleText = ""; for (int textPartIdx = 0; textPartIdx < textParts.length; textPartIdx++) { if (textPartIdx > 0 ) { visibleText += " > "; } visibleText += textPartIdx == 0 ? textParts[textPartIdx] : Tools.firstLetterCapitalized(textParts[textPartIdx].replaceAll("^~*","")); }} MindMapNode node = c.newNode(visibleText); if (textLines.length == 1) { pastedNode = node; } // Heuristically determine, if there is a mail. Matcher mailMatcher = mailPattern.matcher(visibleText); if (mailMatcher.find()) { node.setLink("mailto:"+mailMatcher.group()); } // Heuristically determine, if there is a link. Because this is // heuristic, it is probable that it can be improved to include // some matches or exclude some matches. for (int j = 0; j < linkPrefixes.length; j++) { int linkStart = text.indexOf(linkPrefixes[j]); if (linkStart != -1) { int linkEnd = linkStart; while (linkEnd < text.length() && !nonLinkCharacter.matcher(text.substring(linkEnd,linkEnd+1)).matches()) { linkEnd++; } node.setLink(text.substring(linkStart,linkEnd)); }} // Determine parent among candidate parents // Change the array of candidate parents accordingly for (int j = parentNodes.size()-1; j >= 0; --j) { if (depth > ((Integer)parentNodesDepths.get(j)).intValue()) { for (int k = j+1; k < parentNodes.size(); ++k) { parentNodes.remove(k); parentNodesDepths.remove(k); } MindMapNode target = (MindMapNode)parentNodes.get(j); insertNodeIntoNoEvent(node, target); parentNodes.add(node); parentNodesDepths.add(new Integer(depth)); break; }}} if (asSibling) { for (Iterator i=parent.childrenUnfolded(); /*children.iterator()*/ i.hasNext(); ) { insertNodeIntoNoEvent((MindMapNode)i.next(), realParent, asSibling); } c.nodeStructureChanged(realParent.getParentNode()); } else { c.nodeStructureChanged(parent); } // ^ Do not fire any event when inserting single lines. Fire the event // when all the lines are inserted. return pastedNode; } /** * @param node * @param target */ private void insertNodeIntoNoEvent(MindMapNode node, MindMapNode target) { c.getModel().insertNodeIntoNoEvent(node, target); } /** * @param node * @param realParent * @param asSibling */ private void insertNodeIntoNoEvent(MindMapNode node, MindMapNode realParent, boolean asSibling) { c.getModel().insertNodeIntoNoEvent(node, realParent, asSibling); } /** * @param node * @param parent * @param i */ private void insertNodeInto(MindMapNodeModel node, MindMapNode parent, int i) { c.getModel().insertNodeInto(node, parent,i); } private void insertNodeInto(MindMapNode node, MindMapNode parent) { c.getModel().insertNodeInto(node, parent); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -