1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.awt; 19 20 import java.awt.im.InputMethodHighlight; 21 import java.awt.image.ColorModel; 22 import java.awt.image.ImageObserver; 23 import java.awt.image.ImageProducer; 24 import java.awt.peer.*; 25 import java.io.Serializable; 26 import java.net.URL; 27 import java.util.Hashtable; 28 import java.util.Map; 29 import org.apache.harmony.awt.gl.image.*; 30 import org.apache.harmony.awt.wtk.GraphicsFactory; 31 32 class ToolkitImpl extends Toolkit { 33 34 static final Hashtable<Serializable, Image> imageCache = new Hashtable<Serializable, Image>(); 35 36 @Override 37 public void sync() { 38 lockAWT(); 39 try { 40 } finally { 41 unlockAWT(); 42 } 43 } 44 45 @Override 46 public int checkImage(Image image, int width, int height, ImageObserver observer) { 47 lockAWT(); 48 try { 49 if (width == 0 || height == 0) { 50 return ImageObserver.ALLBITS; 51 } 52 if (!(image instanceof OffscreenImage)) { 53 return ImageObserver.ALLBITS; 54 } 55 OffscreenImage oi = (OffscreenImage) image; 56 return oi.checkImage(observer); 57 } finally { 58 unlockAWT(); 59 } 60 } 61 62 @Override 63 public Image createImage(ImageProducer producer) { 64 lockAWT(); 65 try { 66 return new OffscreenImage(producer); 67 } finally { 68 unlockAWT(); 69 } 70 } 71 72 @Override 73 public Image createImage(byte[] imagedata, int imageoffset, int imagelength) { 74 lockAWT(); 75 try { 76 return new OffscreenImage(new ByteArrayDecodingImageSource(imagedata, imageoffset, 77 imagelength)); 78 } finally { 79 unlockAWT(); 80 } 81 } 82 83 @Override 84 public Image createImage(URL url) { 85 lockAWT(); 86 try { 87 return new OffscreenImage(new URLDecodingImageSource(url)); 88 } finally { 89 unlockAWT(); 90 } 91 } 92 93 @Override 94 public Image createImage(String filename) { 95 lockAWT(); 96 try { 97 return new OffscreenImage(new FileDecodingImageSource(filename)); 98 } finally { 99 unlockAWT(); 100 } 101 } 102 103 @Override 104 public ColorModel getColorModel() { 105 lockAWT(); 106 try { 107 return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() 108 .getDefaultConfiguration().getColorModel(); 109 } finally { 110 unlockAWT(); 111 } 112 } 113 114 @SuppressWarnings("deprecation") 115 @Override 116 @Deprecated 117 public FontMetrics getFontMetrics(Font font) { 118 lockAWT(); 119 try { 120 GraphicsFactory gf = getGraphicsFactory(); 121 return gf.getFontMetrics(font); 122 } finally { 123 unlockAWT(); 124 } 125 } 126 127 @Override 128 public boolean prepareImage(Image image, int width, int height, ImageObserver observer) { 129 lockAWT(); 130 try { 131 if (width == 0 || height == 0) { 132 return true; 133 } 134 if (!(image instanceof OffscreenImage)) { 135 return true; 136 } 137 OffscreenImage oi = (OffscreenImage) image; 138 return oi.prepareImage(observer); 139 } finally { 140 unlockAWT(); 141 } 142 } 143 144 @Override 145 public void beep() { 146 lockAWT(); 147 try { 148 // ???AWT: is there nothing to be implemented here? 149 } finally { 150 unlockAWT(); 151 } 152 } 153 154 @SuppressWarnings("deprecation") 155 @Override 156 @Deprecated 157 public String[] getFontList() { 158 lockAWT(); 159 try { 160 } finally { 161 unlockAWT(); 162 } 163 return null; 164 } 165 166 @SuppressWarnings("deprecation") 167 @Override 168 @Deprecated 169 protected FontPeer getFontPeer(String a0, int a1) { 170 lockAWT(); 171 try { 172 return null; 173 } finally { 174 unlockAWT(); 175 } 176 } 177 178 @Override 179 public Image getImage(String filename) { 180 return getImage(filename, this); 181 } 182 183 static Image getImage(String filename, Toolkit toolkit) { 184 synchronized (imageCache) { 185 Image im = (filename == null ? null : imageCache.get(filename)); 186 187 if (im == null) { 188 try { 189 im = toolkit.createImage(filename); 190 imageCache.put(filename, im); 191 } catch (Exception e) { 192 } 193 } 194 195 return im; 196 } 197 } 198 199 @Override 200 public Image getImage(URL url) { 201 return getImage(url, this); 202 } 203 204 static Image getImage(URL url, Toolkit toolkit) { 205 synchronized (imageCache) { 206 Image im = imageCache.get(url); 207 if (im == null) { 208 try { 209 im = toolkit.createImage(url); 210 imageCache.put(url, im); 211 } catch (Exception e) { 212 } 213 } 214 return im; 215 } 216 } 217 218 @Override 219 public int getScreenResolution() throws HeadlessException { 220 lockAWT(); 221 try { 222 return 62; 223 } finally { 224 unlockAWT(); 225 } 226 } 227 228 @Override 229 public Dimension getScreenSize() { 230 lockAWT(); 231 try { 232 DisplayMode dm = GraphicsEnvironment.getLocalGraphicsEnvironment() 233 .getDefaultScreenDevice().getDisplayMode(); 234 return new Dimension(dm.getWidth(), dm.getHeight()); 235 } finally { 236 unlockAWT(); 237 } 238 } 239 240 @Override 241 public Map<java.awt.font.TextAttribute, ?> mapInputMethodHighlight( 242 InputMethodHighlight highlight) throws HeadlessException { 243 lockAWT(); 244 try { 245 return mapInputMethodHighlightImpl(highlight); 246 } finally { 247 unlockAWT(); 248 } 249 } 250 251 @Override 252 protected EventQueue getSystemEventQueueImpl() { 253 return getSystemEventQueueCore().getActiveEventQueue(); 254 } 255 } 256