001 /*
002 * $Id: URLPainter.java 1839 2007-03-15 22:56:16Z joshy $
003 *
004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020 */
021
022
023 package org.jdesktop.swingx;
024
025 import java.awt.Graphics2D;
026 import java.io.File;
027 import java.net.MalformedURLException;
028 import java.net.URL;
029
030 import org.jdesktop.swingx.editors.PainterUtil;
031 import org.jdesktop.swingx.painter.*;
032
033 /**
034 *
035 * @author joshy
036 */
037 public class URLPainter extends CompoundPainter {
038 URL url;
039 /**
040 * Creates a new instance of URLPainter
041 */
042 public URLPainter() {
043 this.url = null;
044 }
045
046 public URLPainter(URL url) {
047 this.url = url;
048 }
049
050 public URLPainter(File file) {
051 try {
052 this.url = file.toURI().toURL();
053 } catch (MalformedURLException exception) {
054 exception.printStackTrace();
055 this.url = null;
056 }
057 }
058
059 public URLPainter(String url) {
060 try {
061 this.url = new URL(url);
062 } catch (MalformedURLException ex) {
063 ex.printStackTrace();
064 this.url = null;
065 }
066 }
067
068 public URLPainter(Class<?> baseClass, String resource) {
069 url = baseClass.getResource(resource);
070 }
071
072 public void setURL(URL url) {
073 URL old = this.url;
074 this.url = url;
075 firePropertyChange("file", old, this.url);
076 }
077
078 public URL getURL() {
079 return this.url;
080 }
081
082 private boolean loaded = false;
083
084 private void load() {
085 try {
086 Painter painter = PainterUtil.loadPainter(url);
087 this.setPainters(painter);
088 loaded = true;
089 } catch (Exception ex) {
090 ex.printStackTrace();
091 }
092 }
093
094 @Override
095 public void doPaint(Graphics2D g, Object component, int width, int height) {
096 if(!loaded) {
097 load();
098 }
099 super.doPaint(g, component, width, height);
100 }
101
102
103 }