📄 svgselectionmanager.java
字号:
/* * Created on 7 avr. 2004 * ============================================= GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 =============================================Copyright (C) 2003 Jordi SUC, Philippe Gil, SARL ITRISThis library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USAContact : jordi.suc@itris.fr; philippe.gil@itris.fr ============================================= */package fr.itris.glips.svgeditor.selection;import java.awt.*;import java.awt.geom.*;import java.util.*;import javax.swing.*;import org.w3c.dom.*;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.canvas.*;import fr.itris.glips.svgeditor.shape.*;import fr.itris.glips.svgeditor.undoredo.*;/** * @author jordi * the class handling the selections within a canvas * * @author Jordi SUC */public class SVGSelectionManager{ /** * the selection module */ private SVGSelection selection=null; /** * the colors used to draw the area out of a parent group node */ private final Color outOfParentAreaColor=new Color(100, 100, 100, 50), outOfParentAreaBorderColor=new Color(100, 100, 100); /** * the frame linked with this selection manager */ private SVGFrame frame=null; /** * the undo/redo list that will be added to the undo/redo stack */ private SVGUndoRedoActionList undoRedoList=null; /** * the map associating the name of a module to a linked list of nodes that are handled by the module */ private Map selectedItemsByModule=new Hashtable(); /** * the map of the selected nodes associating a node to its type */ private Map currentSelectionType=new LinkedHashMap(); /** * the map associating a node to the linked list of the selection square objects */ private final Map selectionSquares=new Hashtable(); /** * the list of the locked nodes */ private LinkedList lockedNodes=new LinkedList(); /** * the boolean used to refresh the selection */ private boolean shouldRefresh=false; /** * the current cursor */ private Cursor currentCursor=null; /** * the selection listener */ private SVGSelectionListener selectionListener=null; /** * the parent of the nodes that could be selected */ private Element parentElement=null; /** * an instance of this class */ private SVGSelectionManager selectionManager=this; /** * the refresh manager */ private Thread refreshManager=null; /** * the paint listener for the selection */ private CanvasPaintListener paintListener=null; /** * whether the refresh manager should keep on running */ private boolean keepRunning=true; /** * the constructor of the class * @param selection the selection module * @param frame a frame */ public SVGSelectionManager(SVGSelection selection, SVGFrame frame){ this.selection=selection; this.frame=frame; //the parent Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); if(doc!=null){ parentElement=doc.getDocumentElement(); } //adding the paint listener that will paint the area paintListener=new CanvasPaintListener(){ public void paintToBeDone(Graphics g) { Graphics2D g2=(Graphics2D)g; if(getParentElement()!=null && getParentElement().getNodeName().equals("g")){ //the bounds of the parent element Rectangle2D parentBounds=selectionManager.frame.getNodeGeometryBounds(getParentElement()); Rectangle2D.Double dparentBounds=new Rectangle2D.Double(parentBounds.getX(), parentBounds.getY(), parentBounds.getWidth(), parentBounds.getHeight()), scaledParentBounds=getSVGFrame().getScaledRectangle(dparentBounds, false); //the size of the canvas Dimension canvasSize=selectionManager.frame.getScrollPane().getSVGCanvas().getScaledCanvasSize(); //the area to be painted Area area=new Area(new Rectangle(0, 0, canvasSize.width, canvasSize.height)); area.subtract(new Area(new Rectangle((int)scaledParentBounds.x, (int)scaledParentBounds.y, (int)scaledParentBounds.width, (int)scaledParentBounds.height))); //painting the area g.setColor(outOfParentAreaColor); g2.fill(area); g.setColor(outOfParentAreaBorderColor); g2.draw(area); } } }; frame.getScrollPane().getSVGCanvas().addLayerPaintListener(SVGCanvas.BOTTOM_LAYER, paintListener, false); //the mouse and key listeners selectionListener=new SVGSelectionListener(this); frame.getScrollPane().getSVGCanvas().addMouseListener(selectionListener); frame.getScrollPane().getSVGCanvas().addMouseMotionListener(selectionListener); frame.getScrollPane().getSVGCanvas().addKeyListener(selectionListener); //the refresh manager refreshManager=new Thread(){ @Override public void run() { while(keepRunning){ try{sleep(200);}catch (Exception ex){} if(shouldRefresh){ SwingUtilities.invokeLater(new Runnable(){ public void run() { redrawSelectionMethod(); } }); synchronized(this){shouldRefresh=false;} } } } }; refreshManager.start(); } /** * disposes this selection manager */ protected void dispose(){ if(selectionListener!=null){ synchronized(this){ keepRunning=false; } frame.getScrollPane().getSVGCanvas().removeMouseListener(selectionListener); frame.getScrollPane().getSVGCanvas().removeMouseMotionListener(selectionListener); frame.getScrollPane().getSVGCanvas().removeKeyListener(selectionListener); selectedItemsByModule.clear(); currentSelectionType.clear(); selectionSquares.clear(); lockedNodes.clear(); if(undoRedoList!=null){ undoRedoList.clear(); } } } /** * @return whether an action is currently being done or not */ public boolean isActing(){ return selectionListener.isActing(); } /** * @return the SVGFrame containing the canvas */ protected SVGFrame getSVGFrame(){ return frame; } /** * @return Returns the parentElement. */ public Element getParentElement() { return parentElement; } /** * @return Returns the selection. */ protected SVGSelection getSVGSelection() { return selection; } /** * sets the parent element * @param parentElement a parent element */ public synchronized void setParentElement(Element parentElement){ this.parentElement=parentElement; } /** * enters the group that is the single g selected node */ public void enterGroup(){ LinkedList currentSelection=new LinkedList(currentSelectionType.keySet()); if(currentSelection.size()==1){ final Element lastParent=getParentElement(); //getting the selected g node Element currentParent=null; try{ currentParent=(Element)currentSelection.getFirst(); }catch (Exception ex){currentParent=null;} if(currentParent!=null){ //sets the new parent setParentElement(currentParent); final Element newParentElement=currentParent; //adding the undo/redo action if(getSVGSelection().getSVGEditor().getUndoRedo()!=null){ if(undoRedoList==null){ undoRedoList=new SVGUndoRedoActionList(getSVGSelection().undoredogroupenter); } //adding the undo/redo action undoRedoList.addLast(new SVGUndoRedoAction(getSVGSelection().undoredogroupenter){ public void undo() { setParentElement(lastParent); redrawSelection(); } public void redo() { setParentElement(newParentElement); redrawSelection(); } }); undoRedoList.setName(getSVGSelection().undoredogroupenter); } //deselects all the nodes deselectAll(false, false); refreshSelection(); } } } /** * enters the group that is the single g selected node */ public void exitGroup(){ final Element lastParent=getParentElement(); if(lastParent!=null){ //getting the parent element of the last parent element Element currentParent=null; try{ currentParent=(Element)lastParent.getParentNode(); }catch (Exception ex){} if(currentParent!=null){ //sets the new parent setParentElement(currentParent); final Element newParentElement=currentParent; //adding the undo/redo action if(getSVGSelection().getSVGEditor().getUndoRedo()!=null){ if(undoRedoList==null){ undoRedoList=new SVGUndoRedoActionList(getSVGSelection().undoredogroupexit); } //adding the undo/redo action undoRedoList.addLast(new SVGUndoRedoAction(getSVGSelection().undoredogroupexit){ public void undo() { setParentElement(lastParent); redrawSelection(); } public void redo() { setParentElement(newParentElement); redrawSelection(); } }); undoRedoList.setName(getSVGSelection().undoredogroupexit); } //deselects all the nodes deselectAll(false, false); refreshSelection(); } } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -