📄 svgvisualresourcetoolkit.java
字号:
/* * Created on 21 janv. 2005 ============================================= GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 ============================================= GLIPS Graffiti Editor, a SVG Editor Copyright (C) 2003 Jordi SUC, Philippe Gil, SARL ITRIS This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 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 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact : jordi.suc@itris.fr; philippe.gil@itris.fr ============================================= */package fr.itris.glips.svgeditor.visualresources;import java.awt.geom.*;import org.apache.batik.dom.svg.*;import org.w3c.dom.*;import java.util.*;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.canvas.*;/** * the class providing methods to handle the resources * @author Jordi SUC */public class SVGVisualResourceToolkit { /** * the visual resources module */ private SVGVisualResources visualResources=null; /** * the constructor of the class * @param visualResources the visual resource module */ public SVGVisualResourceToolkit(SVGVisualResources visualResources){ this.visualResources=visualResources; } /** * @return Returns the visualResources. */ protected SVGVisualResources getVisualResources() { return visualResources; } /** * creates a resource node given its name * @param frame the current frame * @param parentNode the parentNode * @param nodeName the name of the node of the accurate resource * @param idShapeToBeAppended the id of the shape to be appended as a child of the resource * @return the element corresponding to the given name of the resource */ protected Element createVisualResource(SVGFrame frame, Element parentNode, String nodeName, String idShapeToBeAppended){ Element resource=null; ResourceBundle bundle=SVGEditor.getBundle(); if(frame!=null && parentNode!=null && nodeName!=null && ! nodeName.equals("")){ Element childElement=null; if(idShapeToBeAppended!=null && ! idShapeToBeAppended.equals("")){ Element elt=null; Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); if(doc!=null){ elt=doc.getElementById(idShapeToBeAppended); if(elt!=null){ childElement=(Element)doc.importNode(elt, true); childElement.setAttribute("id", ""); //setting a transform value to put the upper left corner of the document SVGTransformMatrix matrix=getVisualResources().getSVGEditor().getSVGToolkit().getTransformMatrix(childElement); //computing the translation values and adding them if(matrix!=null){ double e=0, f=0; Rectangle2D bounds=frame.getNodeBounds(elt); if(bounds!=null){ e=-bounds.getX(); f=-bounds.getY(); } matrix.concatenateTranslate(e, f); getVisualResources().getSVGEditor().getSVGToolkit().setTransformMatrix(childElement, matrix); } } } } //creates the resource node String svgNS=parentNode.getOwnerDocument().getDocumentElement().getNamespaceURI(); resource=parentNode.getOwnerDocument().createElementNS(svgNS, nodeName); if(nodeName.equals("linearGradient")){ resource.setAttributeNS(null, "x1", "0%"); resource.setAttributeNS(null, "y1", "0%"); resource.setAttributeNS(null, "x2", "100%"); resource.setAttributeNS(null, "y2", "0%"); resource.setAttributeNS(null, "spreadMethod", "pad"); resource.setAttributeNS(null, "gradientTransform", ""); resource.setAttributeNS(null, "gradientUnits", "objectBoundingBox"); Element child=parentNode.getOwnerDocument().createElementNS(svgNS, "stop"); child.setAttributeNS(null, "offset", "0%"); child.setAttributeNS(null, "style", "stop-color:#000000;stop-opacity:1.0;"); resource.appendChild(child); }else if(nodeName.equals("radialGradient")){ resource.setAttributeNS(null, "cx", "50%"); resource.setAttributeNS(null, "cy", "50%"); resource.setAttributeNS(null, "r", "50%"); resource.setAttributeNS(null, "fx", "50%"); resource.setAttributeNS(null, "fy", "50%"); resource.setAttributeNS(null, "spreadMethod", "pad"); resource.setAttributeNS(null, "gradientTransform", ""); resource.setAttributeNS(null, "gradientUnits", "objectBoundingBox"); Element child=parentNode.getOwnerDocument().createElementNS(svgNS, "stop"); child.setAttributeNS(null, "offset", "0%"); child.setAttributeNS(null, "style", "stop-color:#000000;stop-opacity:1.0;"); resource.appendChild(child); }else if(nodeName.equals("pattern")){ resource.setAttributeNS(null, "x", "0%"); resource.setAttributeNS(null, "y", "0%"); resource.setAttributeNS(null, "width", "100%"); resource.setAttributeNS(null, "height", "100%"); resource.setAttributeNS(null, "patternUnits", "objectBoundingBox"); resource.setAttributeNS(null, "patternContentUnits", "userSpaceOnUse"); }else if(nodeName.equals("marker")){ resource.setAttributeNS(null, "refX", "0"); resource.setAttributeNS(null, "refY", "0"); resource.setAttributeNS(null, "markerWidth", "0"); resource.setAttributeNS(null, "markerHeight", "0"); resource.setAttributeNS(null, "markerUnits", "strokeWidth"); resource.setAttributeNS(null, "orient", "auto"); } //appends the node to the parent node// //getting the base string for the id String baseId=resource.getNodeName(); if(bundle!=null){ try{ baseId="vresourcename_".concat(baseId); baseId=bundle.getString(baseId); baseId=baseId.replaceAll("\\s+", ""); }catch (Exception ex){baseId=resource.getNodeName();} } String id=frame.getId(baseId); resource.setAttributeNS(null, "id", id); if(childElement!=null){ //sets the child node, if it exists resource.appendChild(childElement); } } return resource; } /** * returns the duplicate resource of the given resource * @param frame a frame * @param resourceNode a resource node * @return the duplicated node */ protected Element duplicateVisualResource(SVGFrame frame, Element resourceNode){ Element resource=null; ResourceBundle bundle=getVisualResources().getBundle(); if(resourceNode!=null){ Element parentNode=(Element)resourceNode.getParentNode(); if(parentNode!=null){ //cloning the node resource=(Element)resourceNode.cloneNode(true); //getting the id of the resource node and creating the id for the cloned node String suffix=""; if(bundle!=null){ try{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -