📄 svgspacing.java
字号:
if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(verticalSpacingListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //adding the popup items to the sub menu subMenu.addPopupItem(horizontalSpacingItem); subMenu.addPopupItem(verticalSpacingItem); return popupItems; } /** * allows to put equal spaces vertically or horizontally between the different nodes of a list * @param list the nodes * @param type the type of the distribution of spaces */ protected void spacing(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; //the map associating a node to an affine transform final Hashtable transformMap=new Hashtable(); final LinkedList ordered=new LinkedList(); Element current=null, ocurrent=null; Rectangle2D rect=null; double wsum=0, hsum=0; int i=0; //orders the nodes given their position for(Iterator it=list.iterator(); it.hasNext();){ try{ current=(Element)it.next(); }catch (Exception e){current=null;} if(current!=null){ rect=frame.getNodeGeometryBounds(current); if(rect!=null){ wsum+=rect.getWidth(); hsum+=rect.getHeight(); } if(ordered.size()>0){ for(i=0;i<ordered.size();i++){ try{ ocurrent=(Element)ordered.get(i); }catch(Exception ex){ocurrent=null;} if(ocurrent!=null){ if(isGreaterThan(ocurrent,current,type)){ if(i>0){ ordered.add(i,current); }else { ordered.addFirst(current); } break; }else if(i==ordered.size()-1){ ordered.addLast(current); break; } } } }else{ ordered.add(current); } } } final Rectangle2D frect=rect; final double fwsum=wsum; final double fhsum=hsum; Runnable runnable=new Runnable(){ public void run() { double wtotal=0, htotal=0, wsum=fwsum, hsum=fhsum; Node current=null; Rectangle2D rect1=null, rect2=null, rect=frect; if(ordered.size()>1 && ordered.get(0)!=null && ordered.get(ordered.size()-1)!=null){ try{ rect1=frame.getNodeGeometryBounds((Element)ordered.get(0)); rect2=frame.getNodeGeometryBounds((Element)ordered.get(ordered.size()-1)); }catch (Exception ex){rect1=null; rect2=null;} if(rect1!=null && rect2!=null){ //computes the space between the first and the last node wtotal=rect2.getWidth()+rect2.getX()-rect1.getX(); htotal=rect.getHeight()+rect2.getY()-rect1.getY(); if(wtotal>0 && htotal>0){ //computes the space that will be set between each node double spacew=(wtotal-wsum)/(ordered.size()-1), spaceh=(htotal-hsum)/(ordered.size()-1), e=0, f=0; Node lastnode=null; Rectangle2D lrect=null; SVGTransformMatrix matrix=null; AffineTransform af=null; //computes the new matrix for each node for(Iterator it=ordered.iterator(); it.hasNext();){ try{ current=(Node)it.next(); }catch (Exception ex){current=null;} if(current!=null){ rect=frame.getNodeGeometryBounds((Element)current); if(lastnode!=null){ lrect=frame.getNodeGeometryBounds((Element)lastnode); }else{ lrect=null; } e=0; f=0; //computes the translation values if(rect!=null && lrect!=null){ if(ftype==HORIZONTAL_SPACING){ e=(lrect==null?0:-rect.getX()+lrect.getX()+lrect.getWidth()+spacew); }else if(ftype==VERTICAL_SPACING){ f=(lrect==null?0:-rect.getY()+lrect.getY()+lrect.getHeight()+spaceh); } af=AffineTransform.getTranslateInstance(e, f); transformMap.put(current, af); //sets the transformation matrix if(! af.isIdentity()){ //gets, modifies and sets the transform matrix matrix=editor.getSVGToolkit().getTransformMatrix(current); matrix.concatenateTransform(af); editor.getSVGToolkit().setTransformMatrix(current, matrix); } } lastnode=current; } } frame.getScrollPane().getSVGCanvas().delayedRepaint(); //creates the undo/redo action and insert it into the undo/redo stack if(editor.getUndoRedo()!=null){ //sets the name of the undo/redo action String actionName=""; if(ftype==HORIZONTAL_SPACING){ actionName=undoredospacinghorizontal; }else if(ftype==VERTICAL_SPACING){ actionName=undoredospacingvertical; } SVGUndoRedoAction action=new SVGUndoRedoAction(actionName){ public void undo(){ //sets the nodes transformation matrix if(transformMap.size()>0){ Node current=null; SVGTransformMatrix matrix=null; AffineTransform af=null; for(Iterator it=transformMap.keySet().iterator(); it.hasNext();){ try{ current=(Node)it.next(); }catch (Exception ex){current=null;} if(current!=null){ try{ af=(AffineTransform)transformMap.get(current); }catch (Exception ex){af=null;} if(af!=null && ! af.isIdentity()){ //gets, modifies and sets the transform matrix matrix=editor.getSVGToolkit().getTransformMatrix(current); try{matrix.concatenateTransform(af.createInverse());}catch (Exception ex){} editor.getSVGToolkit().setTransformMatrix(current, matrix); } } } frame.getScrollPane().getSVGCanvas().delayedRepaint(); } } public void redo(){ //sets the nodes transformation matrix if(transformMap.size()>0){ Node current=null; SVGTransformMatrix matrix=null; AffineTransform af=null; for(Iterator it=transformMap.keySet().iterator(); it.hasNext();){ try{ current=(Node)it.next(); }catch (Exception ex){current=null;} if(current!=null){ try{ af=(AffineTransform)transformMap.get(current); }catch (Exception ex){af=null;} if(af!=null && ! af.isIdentity()){ //gets, modifies and sets the transform matrix matrix=editor.getSVGToolkit().getTransformMatrix(current); matrix.concatenateTransform(af); editor.getSVGToolkit().setTransformMatrix(current, matrix); } } } frame.getScrollPane().getSVGCanvas().delayedRepaint(); } } }; //gets or creates the undo/redo list and adds the action into it SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList(actionName); actionlist.add(action); editor.getUndoRedo().addActionList(frame, actionlist); } } } } } }; frame.enqueue(runnable); } } /** * tells if node1 is greater than node2 given the type of the comparison * @param node1 the first node * @param node2 the second node * @param type the type of the comparison * @return true if node1 is greater than node2 */ protected boolean isGreaterThan(Element node1, Element node2, int type){ final SVGFrame frame=editor.getFrameManager().getCurrentFrame(); Rectangle2D rect1=frame.getNodeGeometryBounds(node1), rect2=frame.getNodeGeometryBounds(node2); if(rect1!=null && rect2!=null){ if(type==HORIZONTAL_SPACING && rect1.getX()>=rect2.getX()){ return true; }else if(type==VERTICAL_SPACING && rect1.getY()>=rect2.getY()){ return true; } } return false; } /** * gets the name of the module * @return the name of the module */ public String getName(){ return idspacing; } /** * 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 + -