📄 svgclipboard.java
字号:
final SVGSelection selection=editor.getSVGSelection(); Element parent=null; if(selection!=null){ parent=selection.getCurrentParentElement(frame); } if(parent!=null){ final Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); //the list of the pasted nodes final LinkedList pastedNodes=new LinkedList(); //clones the nodes final LinkedList pasteList=new LinkedList(clipboardContent); Node current=null; String sid=""; for(Iterator it=pasteList.iterator(); it.hasNext();){ current=(Node)it.next(); if(current!=null && current instanceof Element){ sid=frame.getId(""); ((Element)current).setAttribute("id", sid); } } final Element fparent=parent; //the runnable Runnable runnable=new Runnable(){ public void run(){ //the defs node final Element defs=frame.getDefsElement(); //the list of the resources used by the pasted nodes LinkedList resNodes=new LinkedList(); //the list of the resources imported into the current document final LinkedList usedResourceNodes=new LinkedList(); Iterator it, it2, it3; Element current=null; LinkedList resourceNodes=null; Element res=null; Node node=null; for(it=pasteList.iterator(); it.hasNext();){ try{ current=(Element)it.next(); }catch (Exception ex){current=null;} if(current!=null){ //getting all the resource nodes used by this node resourceNodes=getSVGEditor().getSVGToolkit().getResourcesUsedByNode((Element)current, true); //if the copied node does not belong to this svg document if(! current.getOwnerDocument().equals(doc)){ //for each resource node, check if it is contained in the list of the resources used by the copied nodes for(it2=resourceNodes.iterator(); it2.hasNext();){ try{res=(Element)it2.next();}catch (Exception ex){res=null;} if(res!=null && ! resNodes.contains(res)){ resNodes.add(res); } } } current=(Element)doc.importNode(current, true); pastedNodes.add(current); } } String resId="", newId="", style=""; //adds the resource nodes to the defs element for(it=resNodes.iterator(); it.hasNext();){ try{ res=(Element)doc.importNode((Element)it.next(), true); resId=res.getAttribute("id"); }catch (Exception ex){res=null; resId=null;} if(res!=null && resId!=null){ if(! frame.checkId(resId)){ //creating the new id newId=frame.getId(resId); //modifying the id of the resource res.setAttribute("id", newId); //for each pasted node, modifies the name of the resource they use for(it2=pastedNodes.iterator(); it2.hasNext();){ try{ current=(Element)it2.next(); }catch (Exception ex){current=null;} if(current!=null){ style=current.getAttribute("style"); if(style!=null && style.indexOf("#".concat(resId))!=-1){ style=style.replaceAll("#".concat(resId)+"[)]", "#".concat(newId)+")"); current.setAttribute("style", style); frame.addNodeUsingResource(newId, current); } //modifies the nodes of the subtree under the pasted node for(it3=new NodeIterator(current); it3.hasNext();){ node=(Node)it3.next(); if( node!=null && ! node.equals(current) && node instanceof Element && ((Element)node).hasAttribute("style")){ style=((Element)node).getAttribute("style"); if(style!=null && style.indexOf("#".concat(resId))!=-1){ style=style.replaceAll("#".concat(resId)+"[)]", "#".concat(newId)+")"); ((Element)node).setAttribute("style", style); frame.addNodeUsingResource(newId, node); } } } } } } //adding the resource node to the defs element and the list of the resource nodes defs.appendChild(res); usedResourceNodes.add(res); } } //appends the nodes to their parent and registers those which use resources //checks if the sub tree of the pasted nodes require a specific name space Node cur=null; NodeIterator nit=null; String prefix="", nsp=""; int pos=0; HashMap<String, String> nameSpaces=new HashMap<String, String>(SVGEditor.requiredNameSpaces); for(it=pastedNodes.iterator(); it.hasNext();){ try{ current=(Element)it.next(); }catch (Exception ex){current=null;} if(current!=null){ fparent.appendChild(current); frame.registerUsedResource(current); nit=new NodeIterator(current); //checks if the nodes under the pasted nodes use a specific name space do{ if(nit.hasNext()){ cur=nit.next(); }else{ cur=current; } if(cur!=null && cur instanceof Element){ pos=cur.getNodeName().indexOf(":"); if(pos!=-1){ prefix=cur.getNodeName().substring(0, pos); nsp=nameSpaces.get(prefix); if(nsp!=null){ SVGToolkit.checkXmlns(cur.getOwnerDocument(), prefix, nsp); nameSpaces.remove(prefix); } } } }while(nit.hasNext()); } } //adds the undo/redo action list if(editor.getUndoRedo()!=null){ final SVGFrame frm=frame; SVGUndoRedoAction action=new SVGUndoRedoAction(undoredopaste){ public void undo(){ Iterator it; Node current=null; //removes the added children from the parent node for(it=pastedNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception e){current=null;} if(current!=null){ fparent.removeChild(current); //unregister the current node to the used resources map if it uses a resource frame.unregisterAllUsedResource(current); } } //removes the added resources for(it=usedResourceNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception e){current=null;} if(current!=null){ defs.removeChild(current); } } //refreshing the properties and the resources frame getSVGEditor().getSVGToolkit().forceReselection(); getSVGEditor().getFrameManager().frameChanged(); } public void redo(){ Iterator it; Node current=null; //appends the resources for(it=usedResourceNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception e){current=null;} if(current!=null){ defs.appendChild(current); } } //appends the children for(it=pastedNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception e){current=null;} if(current!=null){ fparent.appendChild(current); //registers the current node to the used resources map if it uses a resource frame.registerUsedResource(current); } } //refreshing the properties and the resources frame getSVGEditor().getSVGToolkit().forceReselection(); getSVGEditor().getFrameManager().frameChanged(); } }; if(selection!=null && ! selection.isActing()){ selection.deselectAll(frame, false, true); selection.addUndoRedoAction(frame, action); for(it=pastedNodes.iterator(); it.hasNext();){ try{node=(Node)it.next();}catch (Exception ex){node=null;} if(node!=null && node instanceof Element){ selection.handleNodeSelection(frame, (Element)node); } } selection.addUndoRedoAction(frame, new SVGUndoRedoAction(undoredopaste){}); selection.refreshSelection(frame); }else{ //creates the undo/redo list and adds the action to it SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(undoredopaste); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); } } //refreshing the resources frame getSVGEditor().getFrameManager().frameChanged(); } }; //appends the nodes to the root element with the correct id frame.enqueue(runnable); } } } /** * cuts the current selection */ public void cut(){ //copies the nodes copy(); //removes the nodes delete(false); } /** * deletes the selected nodes * @param isDelete true if it is used in the delete action * false if it is used in the cut action */ public void delete(boolean isDelete){ final SVGFrame frame=editor.getFrameManager().getCurrentFrame(); if(frame!=null && selectedNodes.size()>0){ final LinkedList deletedlist=new LinkedList(selectedNodes); SVGSelection selection=editor.getSVGSelection(); Node parent=null; if(selection!=null){ parent=selection.getCurrentParentElement(frame); } Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); final Node fparent=parent; if(fparent!=null){ Node current=null; Runnable runnable=new Runnable(){ public void run(){ Node current=null; for(Iterator it=deletedlist.iterator(); it.hasNext();){ try{ current=(Node)it.next(); }catch (Exception e){current=null;} if(current!=null && current.getParentNode()!=null && current.getParentNode().equals(fparent)){ fparent.removeChild(current); //unregister the current node to the used resources map if it uses a resource frame.unregisterAllUsedResource(current); } } } }; frame.enqueue(runnable); //adds the undo/redo action list if(editor.getUndoRedo()!=null){ final SVGFrame frm=frame; String undoredo=""; if(isDelete){ undoredo=undoredodelete; }else{ undoredo=undoredocut; } SVGUndoRedoAction action=new SVGUndoRedoAction(undoredo){ public void undo(){ //appends the children to the root element Node current=null; for(Iterator it=deletedlist.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception e){current=null;} if(current!=null){ fparent.appendChild(current); //unregisters the current node to the used resources map if it uses a resource frame.registerUsedResource(current); } } } public void redo(){ //removes the children from the root element Node current=null; for(Iterator it=deletedlist.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception e){current=null;} if(current!=null && current.getParentNode()!=null && current.getParentNode().equals(fparent)){ fparent.removeChild(current); //register the current node to the used resources map if it uses a resource frame.unregisterAllUsedResource(current); } } } }; if(selection!=null){ selection.deselectAll(frame, false, true); selection.addUndoRedoAction(frame, action); selection.addUndoRedoAction(frame, new SVGUndoRedoAction(undoredo){}); selection.refreshSelection(frame); }else{ //creates the undo/redo list and adds the action to it SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(undoredo); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); } } } } } /** * gets the name of the module * @return the name of the module */ public String getName(){ return idclipboard; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -