001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.designer;
042:
043: import java.net.URI;
044: import java.net.URISyntaxException;
045: import java.net.URL;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.Map;
049:
050: import javax.swing.ImageIcon;
051: import org.openide.ErrorManager;
052:
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.URLMapper;
055:
056: /**
057: * Image cache, to avoid repeated loading of the same image urls.
058: *
059: * @author Tor Norbye
060: */
061: public class ImageCache {
062: // HashMap images;
063: private Map<URL, ImageIcon> images;
064:
065: /** Construct a new image cache */
066: public ImageCache() {
067: }
068:
069: /** Get an image by a particular URL */
070: public ImageIcon get(URL url) {
071: if (images == null) {
072: return null;
073: }
074:
075: return images.get(url);
076: }
077:
078: /** Put an image into the cache */
079: public void put(URL url, ImageIcon image) {
080: if (images == null) {
081: images = new HashMap<URL, ImageIcon>(); // TODO - initial size?
082: }
083:
084: images.put(url, image);
085: }
086:
087: /** Clear out the cache */
088: public void flush() {
089: if (images != null) {
090: // Ensure that the files actually get reloaded from disk;
091: // NetBeans may be hanging on to old bits in its filesystem
092: // layer
093: Iterator<URL> it = images.keySet().iterator();
094:
095: while (it.hasNext()) {
096: URL url = it.next();
097:
098: // XXX Lame validity check, missing nice NB API.
099: try {
100: new URI(url.toExternalForm());
101: } catch (URISyntaxException ex) {
102: // XXX #6368790 It means the url is not valid URI and URLMapper.findFileObject
103: // would show an exception to the user. We just log it.
104: ErrorManager.getDefault().notify(
105: ErrorManager.INFORMATIONAL, ex);
106: continue;
107: }
108:
109: FileObject fo = URLMapper.findFileObject(url);
110:
111: if (fo != null) {
112: fo.refresh(false);
113: }
114: }
115:
116: images = null;
117: }
118: }
119:
120: @Override
121: public String toString() {
122: return super .toString() + "[images=" + images + "]"; // NOI18N
123: }
124: }
|