📄 svgresourceimagemanager.java
字号:
/* * Created on 10 déc. 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;import java.awt.*;import java.awt.event.*;import java.text.*;import java.util.*;import javax.swing.*;import javax.swing.plaf.metal.*;import org.apache.batik.dom.svg.*;import org.apache.batik.swing.*;import org.apache.batik.swing.gvt.*;import org.apache.batik.swing.svg.*;import org.w3c.dom.*;import org.w3c.dom.svg.*;import fr.itris.glips.svgeditor.canvas.*;/** * the class used to create an outline of a resource * * @author Jordi SUC */public class SVGResourceImageManager{ /** * used to convert numbers into a string */ private DecimalFormat format; /** * the map associating a frame to a map associating the id of a resource to the imageRepresentation object representing this resource */ private final Map<SVGFrame, Map<String, ImageRepresentation>> frameToIdToImages= Collections.synchronizedMap(new HashMap<SVGFrame, Map<String, ImageRepresentation>>()); /** * the size of each image, and the size of the small images */ private final Dimension imageSize=new Dimension(20, 20), smallImageSize=new Dimension(16, 16); /** * the list containing runnables to execute, create, or update */ private java.util.List<Runnable> queue=Collections.synchronizedList(new LinkedList<Runnable>()); /** * the thread handling the queue */ private Thread queueManager=null; /** * the list of the resource representations that have been added */ private java.util.List<ResourceRepresentation> resourceRepresentationList= Collections.synchronizedList(new LinkedList<ResourceRepresentation>()); /** * the editor */ private SVGEditor editor=null; /** * the constructor of the class * @param editor the editor */ public SVGResourceImageManager(SVGEditor editor){ this.editor=editor; //the listener to the frame changes editor.getFrameManager().addSVGFrameChangedListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { Collection frames=getSVGEditor().getFrameManager().getFrames(); //removes the frames that have been closed from the map for(SVGFrame frame : new LinkedList<SVGFrame>(frameToIdToImages.keySet())){ if(frame!=null && ! frames.contains(frame)){ frameToIdToImages.remove(frame); } } //removes the image representations that belongs to disposed frames for(ResourceRepresentation rep : new HashSet<ResourceRepresentation>(resourceRepresentationList)) { if(rep!=null && ! frames.contains(rep.getFrame())) { rep.dispose(); resourceRepresentationList.remove(rep); } } } }); //sets the format used to convert numbers into a string DecimalFormatSymbols symbols=new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); format=new DecimalFormat("######.#",symbols); //the queue manager queueManager=new Thread(){ @Override public void run() { Runnable runnable=null; while(true){ while(queue.size()>0){ //getting the runnable runnable=queue.get(0); //removing it from the queue queue.remove(runnable); //running the runnable runnable.run(); } try{ sleep(100); }catch (Exception ex){} } } }; queueManager.start(); } /** * returns a representation of the resource given by its id * @param frame a frame * @param resourceId the id of a resource node * @param useSmallImage whether the returned representation should be small or large * @return a resource representation */ public ResourceRepresentation getResourceRepresentation(SVGFrame frame, String resourceId, boolean useSmallImage){ final SVGFrame fframe=frame; final String fresourceId=resourceId; if(frame!=null && resourceId!=null){ //creating the runnable Runnable runnable=new Runnable(){ public void run() { ImageRepresentation imageRepresentation=getResourceImage(fframe, fresourceId); if(imageRepresentation==null){ createNewImage(fframe, fresourceId); } } }; //enqueueing the runnable invokeLater(runnable); } return getRepresentation(frame, resourceId, useSmallImage); } /** * enqueues the given runnable * @param runnable a runnable */ protected void invokeLater(Runnable runnable){ if(runnable!=null){ queue.add(runnable); } } /** * @return Returns the editor. */ protected SVGEditor getSVGEditor() { return editor; } /** * invalidates the representation of a resource * @param frame a frame * @param resourceId the id of a resource node */ public void invalidateResourceRepresentation(SVGFrame frame, String resourceId){ if(resourceId!=null && frame!=null){ Map<String, ImageRepresentation> idToImageMap=null; if(frameToIdToImages.containsKey(frame)){ idToImageMap=frameToIdToImages.get(frame); if(idToImageMap!=null){ //removes the id in the map synchronized(this){idToImageMap.remove(resourceId);} } } } } /** * checks the consistency of the stored images * @param frame */ public void checkConsistency(SVGFrame frame){ if(frame!=null){ final SVGFrame fframe=frame; Runnable runnable=new Runnable(){ public void run() { checkResourceRepresentationsConsistency(fframe); } }; //enqueueing the runnable invokeLater(runnable); } } /** * checks if the map associating an id to an image is consistent * @param frame */ protected void checkResourceRepresentationsConsistency(SVGFrame frame){ if(frame!=null){ Map<String, ImageRepresentation> idToImageMap=null; if(frameToIdToImages.containsKey(frame)){ idToImageMap=frameToIdToImages.get(frame); if(idToImageMap!=null){ //getting the map associating the id of a resource to the resource node LinkedList<String> resourceNames=new LinkedList<String>(); resourceNames.add("linearGradient"); resourceNames.add("radialGradient"); resourceNames.add("pattern"); resourceNames.add("marker"); Map resources=frame.getResourcesFromDefs( frame.getScrollPane().getSVGCanvas().getDocument(), resourceNames); for(String id : new LinkedList<String>(idToImageMap.keySet())){ if(id!=null && ! id.equals("") && ! resources.containsKey(id)){ idToImageMap.remove(id); } } } } } } /** * creates a new image * @param frame a frame
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -