📄 svggroupungroup.java
字号:
try{p=((Node)snodes.getFirst()).getParentNode();}catch (Exception ex){p=null;} final Node parent=p; if(doc!=null && parent!=null){ //the list of the nodes to be grouped final LinkedList<Node> toBeGrouped=new LinkedList<Node>(); //orders the selected nodes Node cur=null; for(cur=parent.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(snodes.contains(cur)){ toBeGrouped.add(cur); } } //creates the g element final Element g=doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"g"); String id=frame.getId(""); g.setAttributeNS(null,"id",id); //appends the selected nodes as children of the g element Runnable runnable=new Runnable(){ public void run(){ Node current=null; for(Iterator it=toBeGrouped.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception ex){current=null;} if(current!=null){ try{ parent.removeChild(current); }catch(Exception ex){} g.appendChild(current); } } //appends the g element to the root element parent.appendChild(g); frame.getScrollPane().getSVGCanvas().delayedRepaint(); } }; frame.enqueue(runnable); //creates the undo/redo action and insert it into the undo/redo stack if(editor.getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction(undoredogroupug){ @Override public void undo(){ //removes the g element and appends the children of the g element to the root element for(Node current : toBeGrouped){ if(current!=null){ try{g.removeChild(current);}catch(Exception ex){} parent.appendChild(current); } } parent.removeChild(g); } @Override public void redo(){ //appends the g element to the root element for(Node current : toBeGrouped){ //removes the selected nodes from the root element and adds them to the g element if(current!=null){ try{parent.removeChild(current);}catch(Exception ex){} g.appendChild(current); } } parent.appendChild(g); } }; SVGSelection selection=editor.getSVGSelection(); if(selection!=null){ //manages the selections and the undo/redo action list selection.deselectAll(frame, false, false); selection.addUndoRedoAction(frame, action); selection.handleNodeSelection(frame, g); selection.addUndoRedoAction(frame, new SVGUndoRedoAction(undoredogroupug){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(undoredogroupug); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); } } } } } /** * ungroups the nodes in the given list * @param list the nodes to be grouped */ protected void ungroup(LinkedList<Element> list){ final SVGFrame frame=editor.getFrameManager().getCurrentFrame(); if(list!=null && list.size()>0 && frame!=null){ final Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); //the selected nodes final LinkedList<Element> snodes=new LinkedList<Element>(list); //getting the parent node Node p=null; try{p=((Node)snodes.getFirst()).getParentNode();}catch (Exception ex){p=null;} final Node parent=p; if(doc!=null && parent!=null){ //the list of the g nodes final LinkedList<Node> groupNodes=new LinkedList<Node>(); //orders the selected nodes Node cur=null; for(cur=parent.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(snodes.contains(cur)){ groupNodes.add(cur); } } //the map associating a group node to the list of its children before the ungroup action final LinkedHashMap<Node, Collection<Node>> gOldNodes=new LinkedHashMap<Node, Collection<Node>>(); //the map assocating a g element to the list of its linked nodes final LinkedHashMap<Node, Collection<Node>> gNewNodes=new LinkedHashMap<Node, Collection<Node>>(); //the action SVGUndoRedoAction urAction=null; //create the undo/redo action if(editor.getUndoRedo()!=null){ urAction=new SVGUndoRedoAction(undoredogungroup){ @Override public void undo(){ Iterator it=null, it2=null; Node g=null, child=null; LinkedList children=null; LinkedList newChildren=null; for(it=gOldNodes.keySet().iterator(); it.hasNext();){ try{ g=(Node)it.next(); children=(LinkedList)gOldNodes.get(g); newChildren=(LinkedList)gNewNodes.get(g); }catch (Exception ex){g=null; children=null; newChildren=null;} if(g!=null && children!=null && newChildren!=null){ //removes all the children from the g element while(g.hasChildNodes()){ g.removeChild(g.getFirstChild()); } //appends all the children to the g element for(it2=newChildren.iterator(); it2.hasNext();){ try{ child=(Node)it2.next(); }catch (Exception ex){child=null;} if(child!=null){ parent.removeChild(child); } } //appends all the children to the g element for(it2=children.iterator(); it2.hasNext();){ try{ child=(Node)it2.next(); }catch (Exception ex){child=null;} if(child!=null){ g.appendChild(child); } } parent.appendChild(g); } } } @Override public void redo(){ Iterator it=null, it2=null; Node g=null, child=null; LinkedList children=null; for(it=gNewNodes.keySet().iterator(); it.hasNext();){ try{ g=(Node)it.next(); children=(LinkedList)gNewNodes.get(g); }catch (Exception ex){g=null; children=null;} if(g!=null && children!=null){ //removes all the children from the g element while(g.hasChildNodes()){ g.removeChild(g.getFirstChild()); } //appends all the children to the root element for(it2=children.iterator(); it2.hasNext();){ try{ child=(Node)it2.next(); }catch (Exception ex){child=null;} if(child!=null){ parent.appendChild(child); } } parent.removeChild(g); } } } }; } final SVGUndoRedoAction action=urAction; //ungroups the g elements Runnable runnable=new Runnable(){ public void run(){ Iterator it=null, it2=null; Node g=null, current=null; //the list of the children of the g element LinkedList<Node> gchildren=null; //the list of the cloned children of the g element LinkedList<Node> clonedChildren=null; for(it=groupNodes.iterator(); it.hasNext();){ try{ g=(Node)it.next(); }catch (Exception ex){g=null;} if(g!=null && g.getNodeName().equals("g")){ gchildren=new LinkedList<Node>(); clonedChildren=new LinkedList<Node>(); for(current=g.getFirstChild(); current!=null; current=current.getNextSibling()){ if( ! current.getNodeName().startsWith(SVGToolkit.rtdaPrefix) || current.getNodeName().equals(SVGToolkit.jwidgetTagName)){ gchildren.add(current); } clonedChildren.add(current.cloneNode(true)); } gOldNodes.put(g, clonedChildren); gNewNodes.put(g, gchildren); it2=gchildren.iterator(); while(it2.hasNext()){ try{ current=(Node)it2.next(); }catch (Exception ex){current=null;} if(current!=null){ try{ g.removeChild(current); parent.appendChild(current); }catch(Exception ex){} } } } parent.removeChild(g); } //inserts the undo/redo action into the undo/redo stack if(editor.getUndoRedo()!=null && action!=null){ SVGSelection selection=editor.getSVGSelection(); if(selection!=null){ //manages the selections and the undo/redo action list selection.deselectAll(frame, false, false); selection.addUndoRedoAction(frame, action); //selects the nodes that were before children of the g elements Element child=null; LinkedList children=null; for(it=gNewNodes.keySet().iterator(); it.hasNext();){ try{ g=(Node)it.next(); children=(LinkedList)gNewNodes.get(g); }catch (Exception ex){g=null; children=null;} if(g!=null && children!=null){ //selects the nodes for(it2=children.iterator(); it2.hasNext();){ try{ child=(Element)it2.next(); }catch (Exception ex){child=null;} if(child!=null){ selection.handleNodeSelection(frame, child); } } } } selection.addUndoRedoAction(frame, new SVGUndoRedoAction(undoredogungroup){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(undoredogungroup); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); } } } }; frame.enqueue(runnable); } } } /** * gets the name of the module * @return the name of the module */ public String getName(){ return idgroupungroup; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -