Source Code Cross Referenced for ImageLoader.java in  » GIS » GeoTools-2.4.1 » org » geotools » renderer » style » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoTools 2.4.1 » org.geotools.renderer.style 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *    GeoTools - OpenSource mapping toolkit
003:         *    http://geotools.org
004:         *    (C) 2003-2006, Geotools Project Managment Committee (PMC)
005:         *
006:         *    This library is free software; you can redistribute it and/or
007:         *    modify it under the terms of the GNU Lesser General Public
008:         *    License as published by the Free Software Foundation; either
009:         *    version 2.1 of the License, or (at your option) any later version.
010:         *
011:         *    This library is distributed in the hope that it will be useful,
012:         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         *    Lesser General Public License for more details.
015:         */
016:        package org.geotools.renderer.style;
017:
018:        import java.awt.Canvas;
019:        import java.awt.Graphics2D;
020:        import java.awt.Image;
021:        import java.awt.MediaTracker;
022:        import java.awt.Toolkit;
023:        import java.awt.image.BufferedImage;
024:        import java.net.URL;
025:        import java.util.HashMap;
026:        import java.util.Map;
027:        import java.util.logging.Level;
028:        import java.util.logging.Logger;
029:
030:        /**
031:         * $Id: ImageLoader.java 27862 2007-11-12 19:51:19Z desruisseaux $
032:         *
033:         * @author Ian Turton
034:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/render/src/main/java/org/geotools/renderer/style/ImageLoader.java $
035:         */
036:        class ImageLoader implements  Runnable {
037:            /** The logger for the rendering module. */
038:            private static final Logger LOGGER = org.geotools.util.logging.Logging
039:                    .getLogger("org.geotools.rendering");
040:
041:            /** The images managed by the loader */
042:            private static Map images = new HashMap();
043:
044:            /** A canvas used as the image observer on the tracker */
045:            private static Canvas obs = new Canvas();
046:
047:            /** Used to track the images loading status */
048:            private static MediaTracker tracker = new MediaTracker(obs);
049:
050:            /** Currently loading image */
051:            private static int imageID = 1;
052:
053:            /** A maximum time to wait for the image to load */
054:            private static long timeout = 10000;
055:
056:            /** Location of the loading image */
057:            private URL location;
058:
059:            /** Still waiting for the image? */
060:            private boolean waiting = true;
061:
062:            /**
063:             * Returns the timeout for aborting an image loading sequence
064:             *
065:             * @return the timeout in milliseconds
066:             */
067:            public static long getTimeout() {
068:                return timeout;
069:            }
070:
071:            /**
072:             * Sets the maximum time to wait for getting an external image. Set it to -1 to wait
073:             * undefinitely
074:             *
075:             * @param newTimeout the new timeout value in milliseconds
076:             */
077:            public static void setTimeout(long newTimeout) {
078:                timeout = newTimeout;
079:            }
080:
081:            /**
082:             * Add an image to be loaded by the ImageLoader
083:             *
084:             * @param location the image location
085:             * @param interactive if true the methods returns immediatly, otherwise waits for the image to
086:             *        be loaded
087:             */
088:            private void add(URL location, boolean interactive) {
089:                int imgId = imageID;
090:                this .location = location;
091:                LOGGER.finest("adding image, interactive? " + interactive);
092:
093:                Thread t = new Thread(this );
094:                t.start();
095:
096:                if (interactive) {
097:                    LOGGER.finest("fast return");
098:
099:                    return;
100:                } else {
101:                    waiting = true;
102:
103:                    long elapsed = 0;
104:                    final long step = 500;
105:
106:                    while (waiting && (elapsed < timeout || timeout < 0)) {
107:                        LOGGER.finest("waiting..." + waiting);
108:
109:                        try {
110:                            Thread.sleep(step);
111:                            elapsed += step;
112:
113:                            if (LOGGER.isLoggable(Level.FINEST)) {
114:                                LOGGER.finest("Waiting for image " + location
115:                                        + ", elapsed " + elapsed
116:                                        + " milliseconds");
117:                            }
118:                        } catch (InterruptedException e) {
119:                            LOGGER.warning(e.toString());
120:                        }
121:                    }
122:
123:                    if (LOGGER.isLoggable(Level.FINEST)) {
124:                        LOGGER.finest(imgId + " complete?: "
125:                                + (isFlagUp(imgId, MediaTracker.COMPLETE)));
126:                        LOGGER.finest(imgId + " abort?: "
127:                                + (isFlagUp(imgId, MediaTracker.ABORTED)));
128:                        LOGGER.finest(imgId + " error?: "
129:                                + (isFlagUp(imgId, MediaTracker.ERRORED)));
130:                        LOGGER.finest(imgId + " loading?: "
131:                                + (isFlagUp(imgId, MediaTracker.LOADING)));
132:                        LOGGER.finest(imgId + "slow return " + waiting);
133:                    }
134:
135:                    return;
136:                }
137:            }
138:
139:            /**
140:             * Checks the state of the current tracker against a flag
141:             *
142:             * @param id the image id
143:             * @param flag the flag to be checked
144:             *
145:             * @return true if the flag is up
146:             */
147:            private boolean isFlagUp(int id, int flag) {
148:                return (tracker.statusID(id, true) & flag) == flag;
149:            }
150:
151:            /**
152:             * Fetch a buffered image from the loader, if interactive is false then the loader will wait
153:             * for  the image to be available before returning, used by printers and file output
154:             * renderers. If interactive is true and the image is ready then return, if image is not ready
155:             * start loading it  and return null. The renderer is responsible for finding an alternative
156:             * to use.
157:             *
158:             * @param location the url of the image to be fetched
159:             * @param interactive boolean to signal if the loader should wait for the image to be ready.
160:             *
161:             * @return the buffered image or null
162:             */
163:            public BufferedImage get(URL location, boolean interactive) {
164:                if (images.containsKey(location)) {
165:                    LOGGER.finest("found it");
166:
167:                    return (BufferedImage) images.get(location);
168:                } else {
169:                    if (!interactive) {
170:                        images.put(location, null);
171:                    }
172:
173:                    LOGGER.finest("adding " + location);
174:                    add(location, interactive);
175:
176:                    return (BufferedImage) images.get(location);
177:                }
178:            }
179:
180:            /**
181:             * Runs the loading thread
182:             */
183:            public void run() {
184:                int myID = 0;
185:                Image img = null;
186:
187:                try {
188:                    img = Toolkit.getDefaultToolkit().createImage(location);
189:                    myID = imageID++;
190:                    tracker.addImage(img, myID);
191:                } catch (Exception e) {
192:                    LOGGER.warning("Exception fetching image from " + location
193:                            + "\n" + e);
194:                    images.remove(location);
195:                    waiting = false;
196:
197:                    return;
198:                }
199:
200:                try {
201:                    while ((tracker.statusID(myID, true) & MediaTracker.LOADING) != 0) {
202:                        tracker.waitForID(myID, 500);
203:                        LOGGER.finest(myID + "loading - waiting....");
204:                    }
205:                } catch (InterruptedException ie) {
206:                    LOGGER.warning(ie.toString());
207:                }
208:
209:                int state = tracker.statusID(myID, true);
210:
211:                if (state == MediaTracker.ERRORED) {
212:                    LOGGER.finer("" + myID + " Error loading");
213:                    // images.remove(location);
214:                    waiting = false;
215:
216:                    return;
217:                }
218:
219:                if ((state & MediaTracker.COMPLETE) == MediaTracker.COMPLETE) {
220:                    LOGGER.finest("" + myID + "completed load");
221:
222:                    int iw = img.getWidth(obs);
223:                    int ih = img.getHeight(obs);
224:                    BufferedImage bi = new BufferedImage(iw, ih,
225:                            BufferedImage.TYPE_INT_ARGB);
226:                    Graphics2D big = bi.createGraphics();
227:                    big.drawImage(img, 0, 0, obs);
228:                    images.put(location, bi);
229:
230:                    waiting = false;
231:
232:                    return;
233:                }
234:
235:                LOGGER.finer("" + myID + " whoops - some other outcome "
236:                        + state);
237:                waiting = false;
238:
239:                return;
240:            }
241:
242:            /**
243:             * Resets the image cache
244:             */
245:            public void reset() {
246:                images.clear();
247:            }
248:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.