📄 svgshape.java
字号:
/* * Created on 6 mai 2004 * ============================================= GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 =============================================GLIPS Graffiti Editor, a SVG EditorCopyright (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.shape;import fr.itris.glips.svgeditor.canvas.*;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.selection.*;import fr.itris.glips.svgeditor.undoredo.*;import org.w3c.dom.*;import javax.swing.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.*;import java.util.*;/** * @author Jordi SUC * * the class defining all the actions that can be done on a node */public class SVGShape extends SVGModuleAdapter{ /** * the variable describing the regular selection */ public static final int REGULAR_SELECTION=0; /** * the variable describing the rotate selection */ public static final int ROTATE_SELECTION=1; /** * the variable describing the do action */ public static final int DO_ACTION=0; /** * the variable describing the validate action */ public static final int VALIDATE_ACTION=1; /** * the variable describing the do translate action */ public static final int DO_TRANSLATE_ACTION=2; /** * the variable describing the validate translate action */ public static final int VALIDATE_TRANSLATE_ACTION=3; /** * the editor */ protected SVGEditor editor; /** * the menu item that will be inserted into the menubar */ protected JMenuItem menuitem=null; /** * the toggle button that will be displayed in the tool bar */ protected JToggleButton toolItem=null; /** * the icons that will be displayed in the menu item and the tool item */ protected ImageIcon icon=null, disabledIcon=null; /** * the map associating a frame to a paint listener */ protected final Map<SVGFrame,CanvasPaintListener> paintListenerTable=Collections.synchronizedMap(new Hashtable<SVGFrame,CanvasPaintListener>()); /** * the map associating a frame to hashtable associating a node to a list of selection squares */ protected Map<SVGFrame, Hashtable<Node, java.util.List<SVGSelectionSquare>>> selectionSquaresTable= Collections.synchronizedMap(new Hashtable<SVGFrame, Hashtable<Node, java.util.List<SVGSelectionSquare>>>()); /** * the map associating a frame to a paint listener painting the outline of the translated frame */ protected Map<SVGFrame,CanvasPaintListener> translateFrameTable=Collections.synchronizedMap(new Hashtable<SVGFrame,CanvasPaintListener>()); /** * the map associating a frame to a a paintListener */ protected Map<SVGFrame,CanvasPaintListener> resizeFrameTable=Collections.synchronizedMap(new Hashtable<SVGFrame,CanvasPaintListener>()); /** * the map associating a frame to a paint listener */ protected Map<SVGFrame,CanvasPaintListener> rotateFrameTable=Collections.synchronizedMap(new Hashtable<SVGFrame,CanvasPaintListener>()); /** * the map associating a frame to a paint listener */ protected Map<SVGFrame,CanvasPaintListener> modifyPointFrameTable=Collections.synchronizedMap(new Hashtable<SVGFrame,CanvasPaintListener>()); /** * the boolean telling if the selection can be repainted or not */ protected boolean canRepaintSelection=true; /** * the hashtable associating a key to its label */ protected Hashtable<String, String> labels=new Hashtable<String, String>(); /** * the hashtable associating a key to its id */ protected Hashtable<String, String> ids=new Hashtable<String, String>(); /** * the array describing the accurate order for the selection types */ protected String[] selectionsOrder=new String[4]; /** * the used colors */ protected static final Color SQUARE_SELECTION_COLOR1=new Color(141, 168, 255), SQUARE_SELECTION_COLOR2=new Color(38, 76, 135), LINE_SELECTION_COLOR=new Color(75, 100, 200), LOCK_COLOR=new Color(100,100,100), GHOST_COLOR=new Color(0,0,0), GHOST_COLOR_HIGHLIGHT=new Color(255,255,255), OUTLINE_COLOR=new Color(75, 75, 255), OUTLINE_FILL_COLOR=new Color(75, 75, 255, 100); /** * the stroke for the ghost outlines */ protected static final BasicStroke ghostStroke=new BasicStroke( 1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL, 0, new float[]{8, 8}, 0); /** * the constructor of the class * @param editor the editor of the class */ public SVGShape(SVGEditor editor){ this.editor=editor; selectionsOrder[0]="level1"; selectionsOrder[1]="level2"; selectionsOrder[2]="level3"; selectionsOrder[3]="lock"; //a listener that listens to the changes of the SVGFrames final ActionListener svgframeListener=new ActionListener(){ public void actionPerformed(ActionEvent e) { SVGFrame frame=null; Collection currentFrames=getSVGEditor().getFrameManager().getFrames(); //creating the list of all the frames stored in one of the maps HashSet<SVGFrame> frames=new HashSet<SVGFrame>(); frames.addAll(paintListenerTable.keySet()); frames.addAll(selectionSquaresTable.keySet()); frames.addAll(translateFrameTable.keySet()); frames.addAll(resizeFrameTable.keySet()); frames.addAll(rotateFrameTable.keySet()); frames.addAll(modifyPointFrameTable.keySet()); //adds the new mouse motion and key listeners for(Iterator it=frames.iterator(); it.hasNext();){ try{frame=(SVGFrame)it.next();}catch (Exception ex){frame=null;} if(frame!=null && ! currentFrames.contains(frame)){ paintListenerTable.remove(frame); selectionSquaresTable.remove(frame); translateFrameTable.remove(frame); resizeFrameTable.remove(frame); rotateFrameTable.remove(frame); modifyPointFrameTable.remove(frame); } } frame=getSVGEditor().getFrameManager().getCurrentFrame(); if(frame==null){ getSVGEditor().cancelActions(true); } } }; //adds the SVGFrame change listener editor.getFrameManager().addSVGFrameChangedListener(svgframeListener); } /** * @return the editor */ public SVGEditor getSVGEditor(){ return editor; } /** * selects the nodes and renders them graphically * @param frame the current SVGFrame * @param table the nodes to be selected */ public void select(SVGFrame frame, Hashtable table){ //gets the hashtables associated with the frame Hashtable<Node, java.util.List<SVGSelectionSquare>> selectionSquares=selectionSquaresTable.get(frame); if(selectionSquares==null){ selectionSquares=new Hashtable<Node, java.util.List<SVGSelectionSquare>>(); selectionSquaresTable.put(frame, selectionSquares); } LinkedList currentList=null; for(Iterator it=selectionSquares.keySet().iterator(); it.hasNext();){ currentList=(LinkedList)selectionSquares.get(it.next()); if(currentList!=null){ currentList.clear(); } } //removes the paint listener CanvasPaintListener paintListener=paintListenerTable.get(frame); if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); paintListenerTable.remove(frame); } if(table!=null && table.size()>0){ handleSelection(frame, table, frame.getScrollPane().getSVGCanvas().getGraphics()); final SVGFrame fframe=frame; final Hashtable ftable=new Hashtable(table); paintListener=new CanvasPaintListener(){ public void paintToBeDone(Graphics g) { if(canRepaintSelection()){ handleSelection(fframe, ftable, g); if(getSVGEditor().getSVGSelection()!=null){ getSVGEditor().getSVGSelection().addSelectionSquares(fframe, getSelectionSquares(fframe)); } } } }; frame.getScrollPane().getSVGCanvas().addLayerPaintListener(SVGCanvas.SELECTION_LAYER, paintListener, false); paintListenerTable.put(frame, paintListener); } } /** * calls the selection methods for each node in the hashtable * @param frame the current SVGFrame * @param table the table containing the nodes * @param g the graphics used to paint */ protected void handleSelection(SVGFrame frame, Hashtable table, Graphics g){ //gets the hashtable associated with the frame Hashtable<Node, java.util.List<SVGSelectionSquare>> selectionSquares=null; try{ selectionSquares=selectionSquaresTable.get(frame); }catch (Exception ex){selectionSquares=null;} if(selectionSquares!=null){ selectionSquares.clear(); }else{ selectionSquares=new Hashtable<Node, java.util.List<SVGSelectionSquare>>(); selectionSquaresTable.put(frame, selectionSquares); } if(table!=null && table.size()>0){ Node node=null; LinkedList<SVGSelectionSquare> squares=null; String type=""; //for each node in the table, the accurate "drawSelection" method is called given the type of the selection for(Iterator it=table.keySet().iterator(); it.hasNext();){ try{node=(Node)it.next();}catch (Exception e){node=null;} if(node!=null){ type=(String)table.get(node); if(type!=null && ! type.equals("")){ if(type.equals(selectionsOrder[0])){ squares=drawSelection(frame, g, node); }else if(type.equals(selectionsOrder[1])){ squares=drawRotateSelection(frame, g, node); }else if(type.equals(selectionsOrder[2])){ squares=drawModifyPointsSelection(frame, g, node); }else if(type.equals(selectionsOrder[3])){ squares=drawLockedSelection(frame, g, node); } if(squares!=null && squares.size()>0){ selectionSquares.put(node, squares); } } } } } } /** * @return the boolean telling if the selection can be repainted or not */ public boolean canRepaintSelection(){ return canRepaintSelection; } /** * sets the canRepaintSelection boolean * @param canRepaintSelection true to enable the selections to be repainted */ public synchronized void setCanRepaintSelection(boolean canRepaintSelection){ if(this.canRepaintSelection!=canRepaintSelection){ this.canRepaintSelection=canRepaintSelection; getSVGEditor().getFrameManager().getCurrentFrame().getScrollPane().getSVGCanvas().delayedRepaint(); } } /** * gets the nexts level after the given selection level * @param type a selection level * @return the next selection level */ public String getNextLevel(String type){ if(type!=null){ if(type.equals("level1")){ return "level2"; }else if(type.equals("level2")){ return "level1"; }else if(type.equals("default")){ return "level1"; } } return "level1"; } /** * clears the table of the selected nodes * @param frame the current SVGFrame * @param makeRepaint the Boolean telling if a repaint action should be done or not */ public void deselectAll(SVGFrame frame, boolean makeRepaint){ //removes the paint listener CanvasPaintListener paintListener=paintListenerTable.get(frame); if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); paintListenerTable.remove(paintListener); } //removing the paint listeners of the translate actions paintListener=translateFrameTable.get(frame); if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); translateFrameTable.remove(paintListener); } //removing the paint listeners of the resize actions paintListener=resizeFrameTable.get(frame); if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); resizeFrameTable.remove(paintListener); } //removing the paint listeners of the rotate actions paintListener=rotateFrameTable.get(frame); if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); rotateFrameTable.remove(paintListener); } //removing the paint listeners of the modify point actions paintListener=modifyPointFrameTable.get(frame); if(paintListener!=null){ frame.getScrollPane().getSVGCanvas().removePaintListener(paintListener, false); modifyPointFrameTable.remove(paintListener); } if(makeRepaint){ frame.getScrollPane().getSVGCanvas().delayedRepaint(); } } /** * does the action that are linked with a special selection * @param frame the current SVGFrame * @param type the type of the selection * @param action the type of the action (do or validate) * @param args the array of the arguments */ public void doActions(SVGFrame frame, String type, int action, Object[] args){ final SVGFrame fframe=frame; final Object[] fargs=args; frame.getScrollPane().getSVGCanvas().setEnableWaitCursor(true); if(type!=null && ! type.equals("")){ //if it is a translation action if(action==DO_TRANSLATE_ACTION || action==VALIDATE_TRANSLATE_ACTION){ if(action==DO_TRANSLATE_ACTION){ setCanRepaintSelection(false); try{ translateNodes(frame, (LinkedList)args[0], (Point2D.Double)args[1]); }catch (Exception ex){return;} }else if(action==VALIDATE_TRANSLATE_ACTION){ Runnable runnable=new Runnable(){ public void run() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -