📄 svgorder.java
字号:
} }; //creating the down popup item SVGPopupItem toDownItem=new SVGPopupItem(getSVGEditor(), idorderdown, labelorderdown, "OrderDown"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>0){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(toDownListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //creating the bottom popup item SVGPopupItem toBottomItem=new SVGPopupItem(getSVGEditor(), idorderbottom, labelorderbottom, "OrderBottom"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>0){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(toBottomListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //adding the popup items to the sub menu subMenu.addPopupItem(toTopItem); subMenu.addPopupItem(toUpItem); subMenu.addPopupItem(toDownItem); subMenu.addPopupItem(toBottomItem); return popupItems; } /** * sets the nodes contained in the linked list at the top of the document * @param list the list of the selected nodes * @param type the type of the order action */ protected void order(LinkedList list, int type){ final SVGFrame frame=editor.getFrameManager().getCurrentFrame(); if(list!=null && list.size()>0 && frame!=null){ final LinkedList snodes=new LinkedList(list); final int ftype=type; final Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); //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){ NodeList allChildren=parent.getChildNodes(); //the list of the shape elements in the svg document final LinkedList childrenList=new LinkedList(); final LinkedList orderedSelectedNodes=new LinkedList(); Node current=null; //orders the selected nodes according to their place in the dom for(int i=0;i<allChildren.getLength();i++){ current=allChildren.item(i); if(current!=null && current instanceof Element && ! current.getNodeName().equals("defs")){ childrenList.add(current); if(snodes.contains(current)){ orderedSelectedNodes.add(current); } } } //tests whether the order action should be done or not if(isOrderActionNeeded(type, orderedSelectedNodes, childrenList)){ //put the selected nodes in the proper place final Runnable orderAction=new Runnable(){ public void run(){ Node current=null; Iterator it; if(ftype==TO_BOTTOM){ //getting the first child element in the dom that is not selected Element firstChild=null; for(it=childrenList.iterator(); it.hasNext();){ current=(Element)it.next(); if(current!=null && ! orderedSelectedNodes.contains(current)){ firstChild=(Element)current; break; } } if(firstChild!=null){ //inserts each selected element at the beginning of the dom for(it=orderedSelectedNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception ex){current=null;} if(current!=null){ parent.insertBefore(current, firstChild); } } } }else if(ftype==TO_DOWN){ int ind=0, i; Element previousElement=null; //puts each selected element to the previous step for(it=orderedSelectedNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception ex){current=null;} if(current!=null){ //getting the index of the current element in the children list ind=childrenList.indexOf(current); if(ind>0){ //getting the node that lays before the current node in the list and that is not selected //and inserting the current element before this node for(i=ind-1; i>=0; i--){ previousElement=(Element)childrenList.get(i); if(previousElement!=null && ! orderedSelectedNodes.contains(previousElement)){ parent.insertBefore(current, previousElement); break; } } } } } }else if(ftype==TO_UP){ //reverses the list of the ordered selected nodes LinkedList reversedSelectedNodes=new LinkedList(orderedSelectedNodes); Collections.reverse(reversedSelectedNodes); int ind=0, i; Element nextElement=null; //puts each selected element to the previous step for(it=reversedSelectedNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception ex){current=null;} if(current!=null){ //getting the index of the current element in the children list ind=childrenList.indexOf(current); if(ind>=0 && ind<childrenList.size()-1){ //getting the node that lays after the current node in the list and that is not selected //and inserting the current element after this node for(i=ind+1; i<childrenList.size(); i++){ nextElement=(Element)childrenList.get(i); if(nextElement!=null && ! orderedSelectedNodes.contains(nextElement)){ if(nextElement.getNextSibling()!=null){ parent.insertBefore(current, nextElement.getNextSibling()); }else{ parent.removeChild(current); parent.appendChild(current); } break; } } } } } }else if(ftype==TO_TOP){ //reverses the list of the ordered selected nodes LinkedList reversedSelectedNodes=new LinkedList(orderedSelectedNodes); Collections.reverse(reversedSelectedNodes); //getting the last child element in the dom that is not selected Element lastElement=null; for(int i=childrenList.size()-1; i>=0; i--){ current=(Element)childrenList.get(i); if(current!=null && ! orderedSelectedNodes.contains(current)){ lastElement=(Element)current; break; } } if(lastElement!=null){ //inserts each selected element at the beginning of the dom for(it=reversedSelectedNodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception ex){current=null;} if(current!=null){ if(lastElement.getNextSibling()!=null){ parent.insertBefore(current, lastElement.getNextSibling()); }else{ parent.removeChild(current); parent.appendChild(current); } } } } } } }; frame.enqueue(orderAction); //sets the label for the undo/redo action String undoredo=""; if(type==TO_TOP){ undoredo=undoredoordertop; }else if(type==TO_UP){ undoredo=undoredoorderup; }else if(type==TO_DOWN){ undoredo=undoredoorderdown; }else if(type==TO_BOTTOM){ undoredo=undoredoorderbottom; } //creates the undo/redo action and insert it into the undo/redo stack if(editor.getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction(undoredo){ public void undo(){ NodeList newChildren=parent.getChildNodes(); Node current=null; int i; for(i=0;i<newChildren.getLength();i++){ try{ current=newChildren.item(i); if(current!=null){ parent.removeChild(current); } }catch (Exception ex){current=null;} } for(i=0;i<childrenList.size();i++){ try{ current=(Node)childrenList.get(i); }catch (Exception ex){current=null;} if(current!=null){ parent.appendChild(current); } } } public void redo(){ orderAction.run(); } }; //gets or creates the undo/redo list and adds the action into it SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(undoredo); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); //sets that the svg has been modified getSVGEditor().getFrameManager().getCurrentFrame().setModified(true); } } } } } /** * checks whether an order action should be done or not * @param type the type of the action * @param nodesToBeModified the list of the nodes to be modified * @param children the list of the children of the parent node * @return whether an order action should be done or not */ protected boolean isOrderActionNeeded(int type, LinkedList nodesToBeModified, LinkedList children){ boolean isOrderActionNeeded=false; if(nodesToBeModified!=null && nodesToBeModified.size()>0 && children!=null && children.size()>0){ //for each order action type, checks if this action is needed if(type==TO_TOP || type==TO_UP){ //getting the list of the nodes that lay at the end of the children list, the list having the size of the list //of the nodes to be modified List endList=children.subList(children.size()-nodesToBeModified.size(), children.size()); if(! endList.containsAll(nodesToBeModified)){ isOrderActionNeeded=true; } }else if(type==TO_BOTTOM || type==TO_DOWN){ //getting the list of the nodes that lay at the beginning of the children list, the list having the size of the list //of the nodes to be modified List beginningList=children.subList(0, nodesToBeModified.size()); if(! beginningList.containsAll(nodesToBeModified)){ isOrderActionNeeded=true; } } } return isOrderActionNeeded; } /** * gets the name of the module * @return the name of the module */ public String getName(){ return idorder; } /** * cancels all the actions that could be running */ public void cancelActions(){ } /** * layout some elements in the module */ public void initialize(){ }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -