📄 oyoahafilethemeloader.java
字号:
/* ==================================================================== * Copyright (c) 2001-2003 OYOAHA. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. The names "OYOAHA" must not be used to endorse or promote products * derived from this software without prior written permission. * For written permission, please contact email@oyoaha.com. * * 3. Products derived from this software may not be called "OYOAHA", * nor may "OYOAHA" appear in their name, without prior written * permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL OYOAHA OR ITS CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package com.oyoaha.swing.plaf.oyoaha;import java.awt.*;import java.io.*;import java.net.*;import java.util.*;import javax.swing.*;import java.util.zip.*;import java.lang.reflect.*;import com.microstar.xml.*;public class OyoahaFileThemeLoader extends ClassLoader implements OyoahaThemeLoader{ protected DebugOyoahaConsole debug; protected File file; protected ZipFile zip; protected ClassLoader parent; protected Properties idProperties; protected Properties schemeProperties; public OyoahaFileThemeLoader(DebugOyoahaConsole _debug, ClassLoader _parent, File _file) { debug = _debug; parent = _parent; file = _file; }//------------------------------------------------------------------------------// OYOAHATHEMELOADER//------------------------------------------------------------------------------ public Hashtable loadTheme(OyoahaLookAndFeel _lnf, OyoahaTheme _theme) { Properties table = new Properties(); open(); try { InputStream in = getInputStream("OyoahaTheme.properties"); table.load(in); Reader reader = new InputStreamReader(getInputStream("OyoahaTheme.xml")); Hashtable t = readOyoahaTheme(reader, _lnf, _theme); in.close(); reader.close(); Enumeration e = table.keys(); while(e.hasMoreElements()) { Object s = e.nextElement(); Object o = table.get(s); if(t.containsKey(o)) { Object tmp = t.get(o); if(tmp instanceof NullTag) table.remove(s); else table.put(s, tmp); } /*else { //try the old slaf theme way... Object tmp = readProperty((String)s, (String)o); if(tmp==null) table.remove(s); else table.put(s, tmp); }*/ } } catch(Exception ex) { /*if(debug!=null) { debug.println("ERROR loadTheme: " + ex.toString()); }*/ } idProperties = new Properties(); try { idProperties.load(getInputStream("id.properties")); } catch (Exception ex) { /*if(debug!=null) { debug.println("ERROR load id properties: " + ex.toString()); }*/ } schemeProperties = new Properties(); try { idProperties.load(getInputStream("scheme.properties")); } catch (Exception ex) { /*if(debug!=null) { debug.println("ERROR load scheme properties: " + ex.toString()); }*/ } close(); return table; } public Properties loadDefaultOyoahaThemeScheme() { return schemeProperties; } public String getProperty(String key) { if(idProperties!=null) return idProperties.getProperty(key, "???"); return "???"; } public void dispose() { close(); }//------------------------------------------------------------------------------// CLASSLOADER//------------------------------------------------------------------------------ /** * */ protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { Class c = findLoadedClass(name); if (c==null) { try { c = findSystemClass(name); } catch (ClassNotFoundException e1) { try { c = findClass(name); } catch (Exception e2) { if(parent!=null) { c = parent.loadClass(name); } else { c = super.loadClass(name); } } if (c==null) { throw e1; } } } if (resolve) { resolveClass(c); } return c; } /** * find class */ protected Class findClass(String name) throws ClassNotFoundException { byte data[] = loadClassData(name); if(data == null) { throw new ClassNotFoundException(); } Class result = defineClass(name, data, 0, data.length); if(result == null) { throw new ClassFormatError(); } return result; } protected byte[] loadClassData(String _name) { String name = _name.replace('.', '/') + ".class"; return loadBytes(name); } protected byte[] loadBytes(String _name) { try { InputStream in; if(file.isDirectory()) { in = new FileInputStream(new File(file, _name)); } else { ZipEntry entry = zip.getEntry(_name); if(entry!=null) { in = zip.getInputStream(entry); } else { return null; } } byte[] b = new byte[1024]; byte[] bytes = new byte[0]; while(true) { int i = in.read(b); if(i>0) { if(bytes.length==0) { bytes = new byte[i]; System.arraycopy(b, 0, bytes, 0, i); } else { byte[] newbytes = new byte[bytes.length + i]; System.arraycopy(bytes, 0, newbytes, 0, bytes.length); System.arraycopy(b, 0, newbytes, bytes.length, i); bytes = newbytes; } } else { break; } } return bytes; } catch (Exception ex) { return null; } } /** * Get an InputStream on a given resource. return null if no * resource with this name is found. */ public InputStream getResourceAsStream(String _name) { try { return getInputStream(_name); } catch(Exception e) { if(parent!=null) return parent.getResourceAsStream(_name); else return super.getResourceAsStream(_name); } } /** * Find a resource with a given name. The return is a URL to the resource. * Doing a getContent() on the URL may return an Image, an AudioClip, * or an InputStream. */ public URL getResource(String _name) { if(_name!=null) { try { if(file.isDirectory()) { StringBuffer buf = new StringBuffer(); buf.append("file:"); buf.append(file.getPath()); buf.append(File.separatorChar); buf.append(_name); /*if(debug!=null) { debug.println("> getResource from file: " + buf.toString()); }*/ return new URL(buf.toString()); } else { String path = file.getPath(); StringBuffer buf = new StringBuffer(); buf.append("jar:file:"); buf.append(path); buf.append("!/"); buf.append(_name); /*if(debug!=null) { debug.println("> getResource from jar: " + buf.toString()); }*/ return new URL(buf.toString()); } } catch(Exception e) { } } return super.getResource(_name); } public InputStream getInputStream(String _name) throws IOException { if(file.isDirectory()) { return new BufferedInputStream(new FileInputStream(new File(file, _name))); } else { try { return new BufferedInputStream(zip.getInputStream(zip.getEntry(_name))); } catch(Exception e) { return new BufferedInputStream(new FileInputStream(new File(file.getParent(), _name))); } } } protected void open() { if(file.isDirectory() || zip!=null) { return; } try { zip = new ZipFile(file); } catch(Exception e) { } } protected void close() { if(zip==null) { return; } try { zip.close(); zip = null; } catch(Exception e) { } }//------------------------------------------------------------------------------// PROTECTED OYOAHA THEME LOADER FUNCTION//------------------------------------------------------------------------------ protected Hashtable readOyoahaTheme(Reader reader, OyoahaLookAndFeel lnf, OyoahaTheme theme) throws Exception { OyoahaThemeHandler handler = new OyoahaThemeHandler(this, lnf, theme); XmlParser parser = new XmlParser(); parser.setHandler(handler); parser.parse(getClass().getResource("OyoahaTheme.dtd").toString(), null, reader); return handler.getHashtable(); } protected Image readImage(String _string) {System.out.println("readImage: " + _string); if(_string.endsWith(".png")) { //load and return a png image try { PNGImageProducer p = new PNGImageProducer(getInputStream(_string)); Image image = Toolkit.getDefaultToolkit().createImage(p); OyoahaUtilities.loadImage(image); p.dispose(); return image; } catch(Exception e) { } } Icon icon = readIcon(_string); if(icon!=null) return ((ImageIcon)icon).getImage(); return null; } protected Icon readIcon(String _string) { try { return new ImageIcon(getByteArray(_string)); } catch(Exception e) { /*if(debug!=null) { debug.println("ERROR readIcon: " + e.toString()); }*/ } return null; } public byte[] getByteArray(String _string) { try { InputStream in = getInputStream(_string); ByteArrayOutputStream out = new ByteArrayOutputStream(4096); int c = in.read(); while (c != -1) { out.write(c); c = in.read(); } in.close(); out.close(); return out.toByteArray(); } catch(Exception e) { /*if(debug!=null) { debug.println("ERROR return a byte array: " + e.toString()); }*/ } return null; } /** * create a object from a class name */ public Object createObject(String _name) { try { Class c = loadClass(_name); return c.newInstance(); } catch(Exception e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -