001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.awt;
019:
020: import java.awt.datatransfer.Clipboard;
021: import java.awt.dnd.DragGestureEvent;
022: import java.awt.dnd.InvalidDnDOperationException;
023: import java.awt.dnd.peer.DragSourceContextPeer;
024: import java.awt.im.InputMethodHighlight;
025: import java.awt.image.ColorModel;
026: import java.awt.image.ImageObserver;
027: import java.awt.image.ImageProducer;
028: import java.awt.peer.*;
029: import java.io.Serializable;
030: import java.net.URL;
031: import java.util.Hashtable;
032: import java.util.Map;
033: import java.util.Properties;
034: import org.apache.harmony.awt.gl.font.FontMetricsImpl;
035: import org.apache.harmony.awt.datatransfer.DTK;
036: import org.apache.harmony.awt.gl.*;
037: import org.apache.harmony.awt.gl.image.*;
038: import java.awt.datatransfer.StringSelection;
039:
040: class ToolkitImpl extends Toolkit {
041: static final Hashtable<Serializable, Image> imageCache = new Hashtable<Serializable, Image>();
042:
043: static final FontMetrics cacheFM[] = new FontMetrics[10];
044:
045: @Override
046: public void sync() {
047: lockAWT();
048: try {
049: } finally {
050: unlockAWT();
051: }
052: }
053:
054: @Override
055: protected TextAreaPeer createTextArea(TextArea a0) {
056: lockAWT();
057: try {
058: return null;
059: } finally {
060: unlockAWT();
061: }
062: }
063:
064: @Override
065: public int checkImage(Image image, int width, int height,
066: ImageObserver observer) {
067: lockAWT();
068: try {
069: if (width == 0 || height == 0) {
070: return ImageObserver.ALLBITS;
071: }
072: if (!(image instanceof OffscreenImage)) {
073: return ImageObserver.ALLBITS;
074: }
075: OffscreenImage oi = (OffscreenImage) image;
076: return oi.checkImage(observer);
077: } finally {
078: unlockAWT();
079: }
080: }
081:
082: @Override
083: public Image createImage(ImageProducer producer) {
084: lockAWT();
085: try {
086: return new OffscreenImage(producer);
087: } finally {
088: unlockAWT();
089: }
090: }
091:
092: @Override
093: public Image createImage(byte[] imagedata, int imageoffset,
094: int imagelength) {
095: lockAWT();
096: try {
097: return new OffscreenImage(new ByteArrayDecodingImageSource(
098: imagedata, imageoffset, imagelength));
099: } finally {
100: unlockAWT();
101: }
102: }
103:
104: @Override
105: public Image createImage(URL url) {
106: lockAWT();
107: try {
108: return new OffscreenImage(new URLDecodingImageSource(url));
109: } finally {
110: unlockAWT();
111: }
112: }
113:
114: @Override
115: public Image createImage(String filename) {
116: lockAWT();
117: try {
118: return new OffscreenImage(new FileDecodingImageSource(
119: filename));
120: } finally {
121: unlockAWT();
122: }
123: }
124:
125: @Override
126: public ColorModel getColorModel() {
127: lockAWT();
128: try {
129: return GraphicsEnvironment.getLocalGraphicsEnvironment()
130: .getDefaultScreenDevice().getDefaultConfiguration()
131: .getColorModel();
132: } finally {
133: unlockAWT();
134: }
135: }
136:
137: /**
138: * Returns FontMetrics object that keeps metrics of the specified font.
139: *
140: * @param font specified Font
141: * @return FontMetrics object corresponding to the specified Font object
142: */
143: @SuppressWarnings("deprecation")
144: @Override
145: @Deprecated
146: public FontMetrics getFontMetrics(Font font) {
147: lockAWT();
148: try {
149: FontMetrics fm;
150: for (FontMetrics element : cacheFM) {
151: fm = element;
152: if (fm == null) {
153: break;
154: }
155:
156: if (fm.getFont().equals(font)) {
157: return fm;
158: }
159: }
160: fm = new FontMetricsImpl(font);
161:
162: System
163: .arraycopy(cacheFM, 0, cacheFM, 1,
164: cacheFM.length - 1);
165: cacheFM[0] = fm;
166:
167: return fm;
168:
169: // return getGraphicsFactory().getFontMetrics(font);
170: } finally {
171: unlockAWT();
172: }
173: }
174:
175: @Override
176: public boolean prepareImage(Image image, int width, int height,
177: ImageObserver observer) {
178: lockAWT();
179: try {
180: if (width == 0 || height == 0) {
181: return true;
182: }
183: if (!(image instanceof OffscreenImage)) {
184: return true;
185: }
186: OffscreenImage oi = (OffscreenImage) image;
187: return oi.prepareImage(observer);
188: } finally {
189: unlockAWT();
190: }
191: }
192:
193: @Override
194: public void beep() {
195: lockAWT();
196: try {
197: } finally {
198: unlockAWT();
199: }
200: }
201:
202: @Override
203: protected ButtonPeer createButton(Button a0) {
204: lockAWT();
205: try {
206: return null;
207: } finally {
208: unlockAWT();
209: }
210: }
211:
212: @Override
213: protected CanvasPeer createCanvas(Canvas a0) {
214: lockAWT();
215: try {
216: return null;
217: } finally {
218: unlockAWT();
219: }
220: }
221:
222: @Override
223: protected CheckboxPeer createCheckbox(Checkbox a0) {
224: lockAWT();
225: try {
226: return null;
227: } finally {
228: unlockAWT();
229: }
230: }
231:
232: @Override
233: protected CheckboxMenuItemPeer createCheckboxMenuItem(
234: CheckboxMenuItem a0) throws HeadlessException {
235: lockAWT();
236: try {
237: return null;
238: } finally {
239: unlockAWT();
240: }
241: }
242:
243: @Override
244: protected ChoicePeer createChoice(Choice a0) {
245: lockAWT();
246: try {
247: return null;
248: } finally {
249: unlockAWT();
250: }
251: }
252:
253: @Override
254: protected DialogPeer createDialog(Dialog a0) {
255: lockAWT();
256: try {
257: return null;
258: } finally {
259: unlockAWT();
260: }
261: }
262:
263: @Override
264: public DragSourceContextPeer createDragSourceContextPeer(
265: DragGestureEvent dge) throws InvalidDnDOperationException {
266: return dtk.createDragSourceContextPeer(dge);
267: }
268:
269: @Override
270: protected FileDialogPeer createFileDialog(FileDialog a0) {
271: lockAWT();
272: try {
273: return null;
274: } finally {
275: unlockAWT();
276: }
277: }
278:
279: @Override
280: protected FramePeer createFrame(Frame a0) {
281: lockAWT();
282: try {
283: return null;
284: } finally {
285: unlockAWT();
286: }
287: }
288:
289: @Override
290: protected LabelPeer createLabel(Label a0) {
291: lockAWT();
292: try {
293: return null;
294: } finally {
295: unlockAWT();
296: }
297: }
298:
299: @Override
300: protected ListPeer createList(List a0) {
301: lockAWT();
302: try {
303: return null;
304: } finally {
305: unlockAWT();
306: }
307: }
308:
309: @Override
310: protected MenuPeer createMenu(Menu a0) {
311: lockAWT();
312: try {
313: return null;
314: } finally {
315: unlockAWT();
316: }
317: }
318:
319: @Override
320: protected MenuBarPeer createMenuBar(MenuBar a0) {
321: lockAWT();
322: try {
323: return null;
324: } finally {
325: unlockAWT();
326: }
327: }
328:
329: @Override
330: protected MenuItemPeer createMenuItem(MenuItem a0) {
331: lockAWT();
332: try {
333: return null;
334: } finally {
335: unlockAWT();
336: }
337: }
338:
339: @Override
340: protected PanelPeer createPanel(Panel a0) {
341: lockAWT();
342: try {
343: return null;
344: } finally {
345: unlockAWT();
346: }
347: }
348:
349: @Override
350: protected PopupMenuPeer createPopupMenu(PopupMenu a0) {
351: lockAWT();
352: try {
353: return null;
354: } finally {
355: unlockAWT();
356: }
357: }
358:
359: @Override
360: protected ScrollPanePeer createScrollPane(ScrollPane a0) {
361: lockAWT();
362: try {
363: return null;
364: } finally {
365: unlockAWT();
366: }
367: }
368:
369: @Override
370: protected ScrollbarPeer createScrollbar(Scrollbar a0) {
371: lockAWT();
372: try {
373: return null;
374: } finally {
375: unlockAWT();
376: }
377: }
378:
379: @Override
380: protected TextFieldPeer createTextField(TextField a0) {
381: lockAWT();
382: try {
383: return null;
384: } finally {
385: unlockAWT();
386: }
387: }
388:
389: @Override
390: protected WindowPeer createWindow(Window a0) {
391: lockAWT();
392: try {
393: return null;
394: } finally {
395: unlockAWT();
396: }
397: }
398:
399: @SuppressWarnings("deprecation")
400: @Override
401: @Deprecated
402: public String[] getFontList() {
403: lockAWT();
404: try {
405: } finally {
406: unlockAWT();
407: }
408: return null;
409: }
410:
411: @SuppressWarnings("deprecation")
412: @Override
413: @Deprecated
414: protected FontPeer getFontPeer(String a0, int a1) {
415: lockAWT();
416: try {
417: return null;
418: } finally {
419: unlockAWT();
420: }
421: }
422:
423: @Override
424: public Image getImage(String filename) {
425: return getImage(filename, this );
426: }
427:
428: static Image getImage(String filename, Toolkit toolkit) {
429: synchronized (imageCache) {
430: Image im = (filename == null ? null : imageCache
431: .get(filename));
432:
433: if (im == null) {
434: try {
435: im = toolkit.createImage(filename);
436: imageCache.put(filename, im);
437: } catch (Exception e) {
438: }
439: }
440:
441: return im;
442: }
443: }
444:
445: @Override
446: public Image getImage(URL url) {
447: return getImage(url, this );
448: }
449:
450: static Image getImage(URL url, Toolkit toolkit) {
451: synchronized (imageCache) {
452: Image im = imageCache.get(url);
453: if (im == null) {
454: try {
455: im = toolkit.createImage(url);
456: imageCache.put(url, im);
457: } catch (Exception e) {
458: }
459: }
460: return im;
461: }
462: }
463:
464: @Override
465: public PrintJob getPrintJob(Frame a0, String a1, Properties a2) {
466: lockAWT();
467: try {
468: } finally {
469: unlockAWT();
470: }
471: return null;
472: }
473:
474: @Override
475: public int getScreenResolution() throws HeadlessException {
476: lockAWT();
477: try {
478: return ((GLGraphicsDevice) GraphicsEnvironment
479: .getLocalGraphicsEnvironment()
480: .getDefaultScreenDevice()).getResolution().width;
481: } finally {
482: unlockAWT();
483: }
484: }
485:
486: @Override
487: public Dimension getScreenSize() {
488: lockAWT();
489: try {
490: DisplayMode dm = GraphicsEnvironment
491: .getLocalGraphicsEnvironment()
492: .getDefaultScreenDevice().getDisplayMode();
493: return new Dimension(dm.getWidth(), dm.getHeight());
494: } finally {
495: unlockAWT();
496: }
497: }
498:
499: @Override
500: public Clipboard getSystemClipboard() {
501: lockAWT();
502: try {
503: SecurityManager security = System.getSecurityManager();
504: if (security != null) {
505: security.checkSystemClipboardAccess();
506: }
507: if (systemClipboard == null) {
508: systemClipboard = DTK.getDTK().getNativeClipboard();
509: }
510: if (systemClipboard.getContents(null) == null) {
511: systemClipboard.setContents(new StringSelection(""),
512: null);
513: }
514: return systemClipboard;
515: } finally {
516: unlockAWT();
517: }
518: }
519:
520: @Override
521: public Map<java.awt.font.TextAttribute, ?> mapInputMethodHighlight(
522: InputMethodHighlight highlight) throws HeadlessException {
523: lockAWT();
524: try {
525: return mapInputMethodHighlightImpl(highlight);
526: } finally {
527: unlockAWT();
528: }
529: }
530:
531: @Override
532: protected EventQueue getSystemEventQueueImpl() {
533: return getSystemEventQueueCore().getActiveEventQueue();
534: }
535: }
|