1 /* 2 * This file is part of libbluray 3 * Copyright (C) 2012 libbluray 4 * Copyright (C) 2012-2014 Petri Hintukainen <phintuka@users.sourceforge.net> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library. If not, see 18 * <http://www.gnu.org/licenses/>. 19 */ 20 21 package java.awt; 22 23 import java.awt.image.ColorModel; 24 import java.awt.image.ImageObserver; 25 import java.awt.image.ImageProducer; 26 import java.io.File; 27 import java.net.URL; 28 import java.util.Collections; 29 import java.util.Hashtable; 30 import java.util.WeakHashMap; 31 import java.util.Map; 32 import java.util.Iterator; 33 34 import sun.awt.image.ByteArrayImageSource; 35 import sun.awt.image.FileImageSource; 36 import sun.awt.image.URLImageSource; 37 38 import org.videolan.BDJXletContext; 39 import org.videolan.Logger; 40 41 abstract class BDToolkitBase extends Toolkit { 42 private EventQueue eventQueue = new EventQueue(); 43 private BDGraphicsEnvironment localEnv = new BDGraphicsEnvironment(); 44 private BDGraphicsConfiguration defaultGC = (BDGraphicsConfiguration)localEnv.getDefaultScreenDevice().getDefaultConfiguration(); 45 private static Hashtable cachedImages = new Hashtable(); 46 private static final Logger logger = Logger.getLogger(BDToolkit.class.getName()); 47 48 // mapping of Components to AppContexts, WeakHashMap<Component,AppContext> 49 private static final Map contextMap = Collections.synchronizedMap(new WeakHashMap()); 50 51 BDToolkitBase()52 public BDToolkitBase () { 53 } 54 setFocusedWindow(Window window)55 public static void setFocusedWindow(Window window) { 56 } 57 shutdownDisc()58 public static void shutdownDisc() { 59 try { 60 Toolkit toolkit = getDefaultToolkit(); 61 if (toolkit instanceof BDToolkit) { 62 ((BDToolkit)toolkit).shutdown(); 63 } 64 } catch (Exception t) { 65 logger.error("shutdownDisc() failed: " + t + "\n" + Logger.dumpStack(t)); 66 } 67 } 68 shutdown()69 protected void shutdown() { 70 /* 71 if (eventQueue != null) { 72 BDJHelper.stopEventQueue(eventQueue); 73 eventQueue = null; 74 } 75 */ 76 cachedImages.clear(); 77 contextMap.clear(); 78 } 79 getScreenSize()80 public Dimension getScreenSize() { 81 Rectangle dims = defaultGC.getBounds(); 82 return new Dimension(dims.width, dims.height); 83 } 84 getGraphics(Window window)85 Graphics getGraphics(Window window) { 86 if (!(window instanceof BDRootWindow)) { 87 logger.error("getGraphics(): not BDRootWindow"); 88 throw new Error("Not implemented"); 89 } 90 return new BDWindowGraphics((BDRootWindow)window); 91 } 92 getLocalGraphicsEnvironment()93 GraphicsEnvironment getLocalGraphicsEnvironment() { 94 return localEnv; 95 } 96 getScreenResolution()97 public int getScreenResolution() { 98 return 72; 99 } 100 getColorModel()101 public ColorModel getColorModel() { 102 return defaultGC.getColorModel(); 103 } 104 getFontList()105 public String[] getFontList() { 106 return BDFontMetrics.getFontList(); 107 } 108 getFontMetrics(Font font)109 public FontMetrics getFontMetrics(Font font) { 110 return BDFontMetrics.getFontMetrics(font); 111 } 112 clearCache(BDImage image)113 static void clearCache(BDImage image) { 114 synchronized (cachedImages) { 115 Iterator i = cachedImages.entrySet().iterator(); 116 while (i.hasNext()) { 117 Map.Entry entry = (Map.Entry) i.next(); 118 if (entry.getValue() == image) { 119 i.remove(); 120 return; 121 } 122 } 123 } 124 } 125 getImage(String filename)126 public Image getImage(String filename) { 127 if (BDJXletContext.getCurrentContext() == null) { 128 logger.error("getImage(): no context " + Logger.dumpStack()); 129 } 130 131 if (cachedImages.containsKey(filename)) 132 return (Image)cachedImages.get(filename); 133 Image newImage = createImage(filename); 134 if (newImage != null) 135 cachedImages.put(filename, newImage); 136 return newImage; 137 } 138 getImage(URL url)139 public Image getImage(URL url) { 140 if (BDJXletContext.getCurrentContext() == null) { 141 logger.error("getImage(): no context " + Logger.dumpStack()); 142 } 143 144 if (cachedImages.containsKey(url)) 145 return (Image)cachedImages.get(url); 146 Image newImage = createImage(url); 147 if (newImage != null) 148 cachedImages.put(url, newImage); 149 return newImage; 150 } 151 createImage(String filename)152 public Image createImage(String filename) { 153 if (BDJXletContext.getCurrentContext() == null) { 154 logger.error("createImage(): no context " + Logger.dumpStack()); 155 } 156 157 if (!new File(filename).isAbsolute()) { 158 String home = BDJXletContext.getCurrentXletHome(); 159 if (home != null) { 160 String homeFile = home + filename; 161 if (new File(homeFile).exists()) { 162 logger.warning("resource translated to " + homeFile); 163 filename = homeFile; 164 } else { 165 logger.error("resource " + homeFile + " does not exist"); 166 } 167 } 168 } 169 170 ImageProducer ip = new FileImageSource(filename); 171 Image newImage = createImage(ip); 172 return newImage; 173 } 174 createImage(URL url)175 public Image createImage(URL url) { 176 if (BDJXletContext.getCurrentContext() == null) { 177 logger.error("createImage(): no context " + Logger.dumpStack()); 178 } 179 ImageProducer ip = new URLImageSource(url); 180 Image newImage = createImage(ip); 181 return newImage; 182 } 183 createImage(byte[] imagedata, int imageoffset, int imagelength)184 public Image createImage(byte[] imagedata, 185 int imageoffset, 186 int imagelength) { 187 188 if (BDJXletContext.getCurrentContext() == null) { 189 logger.error("createImage(): no context " + Logger.dumpStack()); 190 } 191 192 ImageProducer ip = new ByteArrayImageSource(imagedata, imageoffset, imagelength); 193 Image newImage = createImage(ip); 194 return newImage; 195 } 196 createImage(ImageProducer producer)197 public Image createImage(ImageProducer producer) { 198 if (BDJXletContext.getCurrentContext() == null) { 199 logger.error("createImage(): no context " + Logger.dumpStack()); 200 } 201 return new BDImageConsumer(producer); 202 } 203 createImage(Component component, int width, int height)204 public Image createImage(Component component, int width, int height) { 205 return new BDImage(component, width, height, defaultGC); 206 } 207 prepareImage(Image image, int width, int height, ImageObserver observer)208 public boolean prepareImage(Image image, int width, int height, ImageObserver observer) { 209 if (!(image instanceof BDImageConsumer)) 210 return true; 211 BDImageConsumer img = (BDImageConsumer)image; 212 return img.prepareImage(observer); 213 } 214 checkImage(Image image, int width, int height, ImageObserver observer)215 public int checkImage(Image image, int width, int height, 216 ImageObserver observer) { 217 if (!(image instanceof BDImageConsumer)) { 218 return ImageObserver.ALLBITS; 219 } 220 BDImageConsumer img = (BDImageConsumer)image; 221 return img.checkImage(observer); 222 } 223 beep()224 public void beep() { 225 } 226 addComponent(Component component)227 public static void addComponent(Component component) { 228 229 BDJXletContext context = BDJXletContext.getCurrentContext(); 230 if (context == null) { 231 logger.warning("addComponent() outside of app context"); 232 return; 233 } 234 contextMap.put(component, context); 235 } 236 getEventQueue(Component component)237 public static EventQueue getEventQueue(Component component) { 238 if (component != null) { 239 do { 240 BDJXletContext ctx = (BDJXletContext)contextMap.get(component); 241 if (ctx != null) { 242 EventQueue eq = ctx.getEventQueue(); 243 if (eq == null) { 244 logger.warning("getEventQueue() failed: no context event queue"); 245 } 246 return eq; 247 } 248 249 component = component.getParent(); 250 } while (component != null); 251 252 logger.warning("getEventQueue() failed: no context"); 253 return null; 254 } 255 256 logger.warning("getEventQueue() failed: no component"); 257 return null; 258 } 259 getSystemEventQueueImpl()260 protected EventQueue getSystemEventQueueImpl() { 261 BDJXletContext ctx = BDJXletContext.getCurrentContext(); 262 if (ctx != null) { 263 EventQueue eq = ctx.getEventQueue(); 264 if (eq != null) { 265 return eq; 266 } 267 } 268 269 logger.warning("getSystemEventQueue(): no context from:" + logger.dumpStack()); 270 return eventQueue; 271 } 272 } 273