📄 svgalign.java
字号:
return editor; } /** * @return a map associating a menu item id to its menu item object */ public Hashtable getMenuItems(){ Hashtable menuItems=new Hashtable(); menuItems.put(idalign,align); return menuItems; } /** * @return a map associating a tool item id to its tool item object */ public Hashtable getToolItems(){ return null; } /** * Returns the collection of the popup items * @return the collection of the popup items */ public Collection getPopupItems(){ LinkedList popupItems=new LinkedList(); SVGPopupSubMenu subMenu=new SVGPopupSubMenu(getSVGEditor(), idalign, labelalign, ""); popupItems.add(subMenu); //creating the align left popup item SVGPopupItem alignLeftItem=new SVGPopupItem(getSVGEditor(), idleft, labelalignleft, "AlignLeft"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(alignLeftListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //creating the align bottom popup item SVGPopupItem alignRightItem=new SVGPopupItem(getSVGEditor(), idright, labelalignright, "AlignRight"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(alignRightListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //creating the align top popup item SVGPopupItem alignTopItem=new SVGPopupItem(getSVGEditor(), idtop, labelaligntop, "AlignTop"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(alignTopListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //creating the align bottom popup item SVGPopupItem alignBottomItem=new SVGPopupItem(getSVGEditor(), idbottom, labelalignbottom, "AlignBottom"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(alignBottomListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //creating the align center popup item SVGPopupItem alignCenterItem=new SVGPopupItem(getSVGEditor(), idcenter, labelaligncenter, "AlignCenter"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(alignCenterListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //creating the horizontal align center popup item SVGPopupItem horizontalAlignCenterItem=new SVGPopupItem(getSVGEditor(), idhorizontalcenter, labelalignhorizontalcenter, "AlignHorizontalCenter"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(alignHorizontalCenterListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //creating the vertical align center popup item SVGPopupItem verticalAlignCenterItem=new SVGPopupItem(getSVGEditor(), idverticalcenter, labelalignverticalcenter, "AlignVerticalCenter"){ public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>1){ menuItem.setEnabled(true); //adds the action listeners if(menuItem.isEnabled()){ menuItem.addActionListener(alignVerticalCenterListener); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; //adding the popup items to the sub menu subMenu.addPopupItem(alignLeftItem); subMenu.addPopupItem(alignRightItem); subMenu.addPopupItem(alignTopItem); subMenu.addPopupItem(alignBottomItem); subMenu.addPopupItem(alignCenterItem); subMenu.addPopupItem(horizontalAlignCenterItem); subMenu.addPopupItem(verticalAlignCenterItem); return popupItems; } /** * aligns the nodes contained in the selection list horizontally with the node that is placed at the very left of the selected nodes * @param list a list of nodes * @param type the type of the alignment */ protected void align(LinkedList list, int type){ final SVGFrame frame=editor.getFrameManager().getCurrentFrame(); if(list!=null && list.size()>1 && frame!=null){ final LinkedList snodes=new LinkedList(list); final int ftype=type; //the map associating a node to its transform for the alignment final Hashtable transformMap=new Hashtable(); //computing the union of all the outlines of the selected nodes Node current=null; final Area area=new Area(); Shape outline=null; for(Iterator it=snodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception e){current=null;} if(current!=null){ outline=frame.getNodeGeometryBounds((Element)current); if(outline!=null){ area.add(new Area(outline)); } } } //computes the values of the translation depending on the type of the alignment Runnable runnable=new Runnable(){ public void run(){ AffineTransform af=null; SVGTransformMatrix matrix=null; Rectangle2D rect, rect0=area.getBounds(); Node current=null; double e=0, f=0; for(Iterator it=snodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception ex){current=null;} if(current!=null){ rect=frame.getNodeGeometryBounds((Element)current); if(ftype==ALIGN_LEFT){ if(rect0!=null && rect!=null){ e=rect0.getX()-rect.getX(); } }else if(ftype==ALIGN_RIGHT){ if(rect0!=null && rect!=null){ e=(rect0.getX()+rect0.getWidth())-(rect.getX()+rect.getWidth()); } }else if(ftype==ALIGN_TOP){ if(rect0!=null && rect!=null){ f=rect0.getY()-rect.getY(); } }else if(ftype==ALIGN_BOTTOM){ if(rect0!=null && rect!=null){ f=(rect0.getY()+rect0.getHeight())-(rect.getY()+rect.getHeight()); } }else if(ftype==ALIGN_CENTER){ if(rect0!=null && rect!=null){ e=(rect0.getX()+(rect0.getWidth()-rect.getWidth())/2)-rect.getX(); f=rect0.getY()+(rect0.getHeight()-rect.getHeight())/2-rect.getY(); } }else if(ftype==ALIGN_HORIZONTAL_CENTER){ if(rect0!=null && rect!=null){ e=(rect0.getX()+(rect0.getWidth()-rect.getWidth())/2)-rect.getX(); } }else if(ftype==ALIGN_VERTICAL_CENTER){ if(rect0!=null && rect!=null){ f=rect0.getY()+(rect0.getHeight()-rect.getHeight())/2-rect.getY(); } }else return; af=AffineTransform.getTranslateInstance(e, f); transformMap.put(current, af); if(! 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(); } }; frame.enqueue(runnable); //sets the name of the undo/redo action String actionName=""; if(type==ALIGN_LEFT){ actionName=undoredoalignleft; }else if(type==ALIGN_RIGHT){ actionName=undoredoalignright; }else if(type==ALIGN_TOP){ actionName=undoredoaligntop; }else if(type==ALIGN_BOTTOM){ actionName=undoredoalignbottom; }else if(type==ALIGN_CENTER){ actionName=undoredoaligncenter; }else if(type==ALIGN_HORIZONTAL_CENTER){ actionName=undoredoalignhorizontalcenter; }else if(type==ALIGN_VERTICAL_CENTER){ actionName=undoredoalignverticalcenter; } //creates the undo/redo action and insert it into the undo/redo stack if(editor.getUndoRedo()!=null){ 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()){ 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()){ 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); } } } /** * gets the name of the module * @return the name of the module */ public String getName(){ return idalign; } /** * 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 + -