📄 svgresource.java~1~
字号:
/* * Created on 2 juin 2004 * ============================================= 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.resources;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.colorchooser.*;import org.apache.batik.dom.svg.*;import org.w3c.dom.*;import javax.xml.parsers.*;import java.util.*;import java.io.*;import javax.swing.*;import java.net.*;import java.awt.*;import java.util.prefs.*;/** * @author Jordi SUC * * The class managing the resources */public class SVGResource { /** * the current directory for the system */ private File currentDirectory=null; /** * the hashtable associating a an icon's name to an icon */ private static final Hashtable<String, ImageIcon> icons=new Hashtable<String, ImageIcon>(); /** * the hashtable associating a an icon's name to a gray icon */ private static final Hashtable<String, ImageIcon> grayIcons=new Hashtable<String, ImageIcon>(); /** * the list of the recent files */ private final LinkedList<String> recentFiles=new LinkedList<String>(); /** * the map associating the name of a xml document to this document */ private final static Hashtable<String, Document> cachedXMLDocuments=new Hashtable<String, Document>(); /** * a user preference node */ private Preferences preferences=null; /** * the document of the xml properties */ private Document xmlProperties=null; /** * the document of the resource store */ private Document resourceStore=null; /** * the set of the style element names */ private final HashSet<String> styleProperties=new HashSet<String>(); /** * the list of the runnables that will be run when the editor is exited */ private final LinkedList<Runnable> exitRunnables=new LinkedList<Runnable>(); /** * the map associating the name of a w3c color to a svg w3c color object */ private LinkedHashMap<String, SVGW3CColor> w3cColorsMap=new LinkedHashMap<String, SVGW3CColor>(); /** * the editor */ private SVGEditor editor=null; /** * the constructor of the class * @param editor the editor */ public SVGResource(SVGEditor editor){ this.editor=editor; //creates the list of the recently opened files// //getting the preference node preferences=Preferences.userNodeForPackage(editor.getClass()); if(preferences!=null){ String[] keys=null; try{ keys=preferences.keys(); }catch (Exception ex){} if(keys!=null){ //filling the list of the recent files String val=""; for(int i=0; i<keys.length; i++){ val=preferences.get(keys[i], null); if(val!=null && ! val.equals("")){ recentFiles.add(val); } } } } //gets the W3C colors retrieveW3CColors(getXMLDocument("svgColors.xml")); } /** * @return the editor */ public SVGEditor getSVGEditor(){ return editor; } /** * retrieves all the colors in the given svg document and fills the list and the map of the colors * @param svgColorsDocument the document containing the name of each and its associated rgb value */ protected void retrieveW3CColors(Document svgColorsDocument){ if(svgColorsDocument!=null && svgColorsDocument.getDocumentElement()!=null){ String id="", value=""; Color color=null; SVGW3CColor svgColor=null; LinkedList<SVGW3CColor> colorsList=new LinkedList<SVGW3CColor>(); //for each svg colors, gets the name and the value and adds them to the list for(Node cur=svgColorsDocument.getDocumentElement().getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element && cur.getNodeName().equals("svgColor")){ id=((Element)cur).getAttribute("id"); value=((Element)cur).getAttribute("value"); if(id!=null && ! id.equals("") && value!=null && ! value.equals("")){ color=getColor(value); if(color!=null){ svgColor=new SVGW3CColor(id, color); colorsList.add(svgColor); } } } } //puts the colors and the ids to the map of the colors for(SVGW3CColor svgColor2 : colorsList){ if(svgColor2!=null){ w3cColorsMap.put(svgColor2.getId(), svgColor2); } } } } /** *@return the w3c color objects list */ public LinkedList<SVGW3CColor> getW3CColors(){ return new LinkedList<SVGW3CColor>(w3cColorsMap.values()); } /** *@return the w3c colors map */ public Map<String, SVGW3CColor> getW3CColorsMap(){ return new HashMap<String, SVGW3CColor>(w3cColorsMap); } /** * Returns the color corresponding to the given string * @param colorString a string representing a color * @return the color corresponding to the given string */ protected Color getColor(String colorString){ Color color=null; if(colorString==null){ colorString=""; } if(color==null){ try{color=Color.getColor(colorString);}catch (Exception ex){color=null;} if(color==null && colorString.length()==7){ int r=0, g=0, b=0; try{ r=Integer.decode("#"+colorString.substring(1,3)).intValue(); g=Integer.decode("#"+colorString.substring(3,5)).intValue(); b=Integer.decode("#"+colorString.substring(5,7)).intValue(); color=new Color(r,g,b); }catch (Exception ex){color=null;} }else if(color==null && colorString.indexOf("rgb(")!=-1){ String tmp=colorString.replaceAll("\\s*[rgb(]\\s*", ""); tmp=tmp.replaceAll("\\s*[)]\\s*", ""); tmp=tmp.replaceAll("\\s+", ","); tmp=tmp.replaceAll("[,]+", ","); int r=0, g=0, b=0; try{ r=new Integer(tmp.substring(0, tmp.indexOf(","))).intValue(); tmp=tmp.substring(tmp.indexOf(",")+1, tmp.length()); g=new Integer(tmp.substring(0, tmp.indexOf(","))).intValue(); tmp=tmp.substring(tmp.indexOf(",")+1, tmp.length()); b=new Integer(tmp).intValue(); color=new Color(r,g,b); }catch (Exception ex){color=null;} } } return color; } /** * computes the path of a resource given its name * @param resource the name of a resource * @return the full path of a resource */ public static String getPath(String resource){ String path=""; try{ path=SVGResource.class.getResource(resource).toExternalForm(); }catch (Exception ex){path="";} return path; } /** * gives an ImageIcon object given the name of it as it is witten in the SVGEditorIcons.properties file * @param name the name of an icon * @param isGrayIcon true if the icon should be used for a disabled widget * @return an image icon */ public static ImageIcon getIcon(String name, boolean isGrayIcon){ ImageIcon icon=null; if(name!=null && ! name.equals("")){ if(icons.containsKey(name)){ if(isGrayIcon){ icon=grayIcons.get(name); }else{ icon=icons.get(name); } }else{ //gets the name of the icons from the resources ResourceBundle bundle=null; try{ bundle=ResourceBundle.getBundle("fr.itris.glips.svgeditor.resources.properties.SVGEditorIcons"); }catch (Exception ex){bundle=null;} String path=""; if(bundle!=null){ try{path=bundle.getString(name);}catch (Exception ex){path="";} if(path!=null && ! path.equals("")){ try{ icon=new ImageIcon(new URL(getPath("icons/"+path))); }catch (Exception ex){icon=null;} if(icon!=null){ icons.put(name, icon); Image image=icon.getImage(); ImageIcon grayIcon=new ImageIcon(GrayFilter.createDisabledImage(image)); grayIcons.put(name, grayIcon); if(isGrayIcon){ icon=grayIcon; } } } } } } return icon; } /** * @return the current directory */ public File getCurrentDirectory(){ return currentDirectory; } /** * sets he current directory * @param directory a file representing a directory */ public void setCurrentDirectory(File directory){ currentDirectory=directory; } /** * create a document from tthe given file in the resource files * @param name the name of the xml file * @return the document */ public static Document getXMLDocument(String name){ Document doc=null; if(name!=null && ! name.equals("")){ if(cachedXMLDocuments.containsKey(name)){ doc=cachedXMLDocuments.get(name); }else{ DocumentBuilderFactory docBuildFactory=DocumentBuilderFactory.newInstance(); String path=""; try{ //parses the XML file DocumentBuilder docBuild=docBuildFactory.newDocumentBuilder(); path=getPath("xml/".concat(name)); doc=docBuild.parse(path); }catch (Exception ex){doc=null;} if(doc!=null){ cachedXMLDocuments.put(name, doc); } } } return doc; } /** * @return the resource store document */ public Document getResourceStore(){ if(resourceStore!=null){ SAXSVGDocumentFactory factory=new SAXSVGDocumentFactory("org.apache.xerces.parsers.SAXParser"); try{ resourceStore=factory.createDocument(getPath("xml/visualResourceStore.xml")); }catch (Exception ex){resourceStore=null;} } return resourceStore; } /** * writes a documents to a file * @param path the path of the file in which the document will be written * @param doc the document to be written */ public void writeXMLDocument(String path, Document doc){ if(doc!=null && path!=null && ! path.equals("")){ Element root=doc.getDocumentElement();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -