1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90: import ;
91: import ;
92: import ;
93: import ;
94: import ;
95: import ;
96: import ;
97: import ;
98: import ;
99: import ;
100: import ;
101: import ;
102: import ;
103: import ;
104: import ;
105: import ;
106: import ;
107: import ;
108: import ;
109: import ;
110: import ;
111: import ;
112: import ;
113: import ;
114: import ;
115: import ;
116:
117: import ;
118:
119: import ;
120: import ;
121: import ;
122: import ;
123: import ;
124: import ;
125: import ;
126: import ;
127: import ;
128: import ;
129: import ;
130: import ;
131: import ;
132: import ;
133: import ;
134:
135: public class XToolkit
136: extends ClasspathToolkit
137: {
138:
139:
142: static boolean DEBUG = false;
143:
144:
147: HashMap colorMap = new HashMap();
148:
149:
152: private EventQueue eventQueue;
153:
154:
157: private ColorModel colorModel;
158:
159:
162: private HashMap imageCache = new HashMap();
163:
164:
167: private WeakHashMap<String,ClasspathFontPeer> fontCache =
168: new WeakHashMap<String,ClasspathFontPeer>();
169:
170: public XToolkit()
171: {
172: SystemProperties.setProperty("gnu.javax.swing.noGraphics2D", "true");
173: SystemProperties.setProperty("java.awt.graphicsenv",
174: "gnu.java.awt.peer.x.XGraphicsEnvironment");
175: }
176:
177: public GraphicsEnvironment getLocalGraphicsEnvironment()
178: {
179: return new XGraphicsEnvironment();
180: }
181:
182:
190: public ClasspathFontPeer getClasspathFontPeer(String name, Map attrs)
191: {
192: ClasspathFontPeer font;
193: if ("true".equals(System.getProperty("escherpeer.usexfonts")))
194: {
195: String canonical = XFontPeer.encodeFont(name, attrs);
196: if (!fontCache.containsKey(canonical))
197: {
198: font = new XFontPeer(name, attrs);
199: fontCache.put(canonical, font);
200: }
201: else
202: {
203: font = fontCache.get(canonical);
204: }
205: }
206: else
207: {
208: String canonical = OpenTypeFontPeer.encodeFont(name, attrs);
209: if (!fontCache.containsKey(canonical))
210: {
211: font = new OpenTypeFontPeer(name, attrs);
212: fontCache.put(canonical, font);
213: }
214: else
215: {
216: font = fontCache.get(canonical);
217: }
218: }
219: return font;
220: }
221:
222: public Font createFont(int format, InputStream stream)
223: {
224: return null;
225: }
226:
227: public RobotPeer createRobot(GraphicsDevice screen) throws AWTException
228: {
229:
230: throw new UnsupportedOperationException("Not yet implemented.");
231: }
232:
233: public EmbeddedWindowPeer createEmbeddedWindow(EmbeddedWindow w)
234: {
235:
236: throw new UnsupportedOperationException("Not yet implemented.");
237: }
238:
239: protected ButtonPeer createButton(Button target)
240: {
241: checkHeadLess("No ButtonPeer can be created in an headless" +
242: "graphics environment.");
243:
244: return new SwingButtonPeer(target);
245: }
246:
247: protected TextFieldPeer createTextField(TextField target)
248: {
249: checkHeadLess("No TextFieldPeer can be created in an headless " +
250: "graphics environment.");
251:
252: return new SwingTextFieldPeer(target);
253: }
254:
255: protected LabelPeer createLabel(Label target)
256: {
257: checkHeadLess("No LabelPeer can be created in an headless graphics " +
258: "environment.");
259: return new SwingLabelPeer(target);
260: }
261:
262: protected ListPeer createList(List target)
263: {
264:
265: throw new UnsupportedOperationException("Not yet implemented.");
266: }
267:
268: protected CheckboxPeer createCheckbox(Checkbox target)
269: {
270: checkHeadLess("No CheckboxPeer can be created in an headless graphics " +
271: "environment.");
272:
273: return new SwingCheckboxPeer(target);
274: }
275:
276: protected ScrollbarPeer createScrollbar(Scrollbar target)
277: {
278:
279: throw new UnsupportedOperationException("Not yet implemented.");
280: }
281:
282: protected ScrollPanePeer createScrollPane(ScrollPane target)
283: {
284:
285: throw new UnsupportedOperationException("Not yet implemented.");
286: }
287:
288: protected TextAreaPeer createTextArea(TextArea target)
289: {
290: checkHeadLess("No TextAreaPeer can be created in an headless graphics " +
291: "environment.");
292:
293: return new SwingTextAreaPeer(target);
294: }
295:
296: protected ChoicePeer createChoice(Choice target)
297: {
298:
299: throw new UnsupportedOperationException("Not yet implemented.");
300: }
301:
302: protected FramePeer createFrame(Frame target)
303: {
304: XFramePeer frame = new XFramePeer(target);
305: return frame;
306: }
307:
308: protected CanvasPeer createCanvas(Canvas target)
309: {
310: return new SwingCanvasPeer(target);
311: }
312:
313: protected PanelPeer createPanel(Panel target)
314: {
315: return new SwingPanelPeer(target);
316: }
317:
318: protected WindowPeer createWindow(Window target)
319: {
320: return new XWindowPeer(target);
321: }
322:
323: protected DialogPeer createDialog(Dialog target)
324: {
325: return new XDialogPeer(target);
326: }
327:
328: protected MenuBarPeer createMenuBar(MenuBar target)
329: {
330:
331: throw new UnsupportedOperationException("Not yet implemented.");
332: }
333:
334: protected MenuPeer createMenu(Menu target)
335: {
336:
337: throw new UnsupportedOperationException("Not yet implemented.");
338: }
339:
340: protected PopupMenuPeer createPopupMenu(PopupMenu target)
341: {
342:
343: throw new UnsupportedOperationException("Not yet implemented.");
344: }
345:
346: protected MenuItemPeer createMenuItem(MenuItem target)
347: {
348:
349: throw new UnsupportedOperationException("Not yet implemented.");
350: }
351:
352: protected FileDialogPeer createFileDialog(FileDialog target)
353: {
354:
355: throw new UnsupportedOperationException("Not yet implemented.");
356: }
357:
358: protected CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target)
359: {
360:
361: throw new UnsupportedOperationException("Not yet implemented.");
362: }
363:
364: protected FontPeer getFontPeer(String name, int style)
365: {
366:
367: throw new UnsupportedOperationException("Not yet implemented.");
368: }
369:
370: public Dimension getScreenSize()
371: {
372: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
373: GraphicsDevice gd = ge.getDefaultScreenDevice();
374: GraphicsConfiguration gc = gd.getDefaultConfiguration();
375: XGraphicsConfiguration xgc = (XGraphicsConfiguration) gc;
376:
377: return xgc.getSize();
378: }
379:
380: public int getScreenResolution()
381: {
382: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
383: GraphicsDevice gd = ge.getDefaultScreenDevice();
384: GraphicsConfiguration gc = gd.getDefaultConfiguration();
385: XGraphicsConfiguration xgc = (XGraphicsConfiguration) gc;
386:
387: return xgc.getResolution();
388: }
389:
390:
395: public ColorModel getColorModel()
396: {
397:
398: if (colorModel == null)
399: colorModel = new DirectColorModel(24, 0xFF0000, 0xFF00, 0xFF);
400: return colorModel;
401: }
402:
403: public String[] getFontList()
404: {
405: GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
406: return ge.getAvailableFontFamilyNames();
407: }
408:
409: public FontMetrics getFontMetrics(Font name)
410: {
411: ClasspathFontPeer peer = (ClasspathFontPeer) name.getPeer();
412: return peer.getFontMetrics(name);
413: }
414:
415: public void sync()
416: {
417:
418: throw new UnsupportedOperationException("Not yet implemented.");
419: }
420:
421:
430: public Image getImage(String name)
431: {
432: Image image;
433: try
434: {
435: File file = new File(name);
436: image = getImage(file.toURL());
437: }
438: catch (MalformedURLException ex)
439: {
440:
441: image = null;
442: }
443: return image;
444: }
445:
446:
455: public Image getImage(URL url)
456: {
457: Image image;
458: if (imageCache.containsKey(url))
459: {
460: image = (Image) imageCache.get(url);
461: }
462: else
463: {
464: image = createImage(url);
465: imageCache.put(url, image);
466: }
467: return image;
468: }
469:
470:
479: public Image createImage(String filename)
480: {
481: Image im;
482: try
483: {
484: File file = new File(filename);
485: URL url = file.toURL();
486: im = createImage(url);
487: }
488: catch (MalformedURLException ex)
489: {
490: im = createErrorImage();
491: }
492: return im;
493: }
494:
495:
504: public Image createImage(URL url)
505: {
506: Image image;
507: try
508: {
509: image = createImage(url.openStream());
510: }
511: catch (IOException ex)
512: {
513: image = createErrorImage();
514: }
515: return image;
516: }
517:
518:
525: private Image createErrorImage()
526: {
527:
528: return new XImage(1, 1);
529: }
530:
531: public boolean prepareImage(Image image, int width, int height, ImageObserver observer)
532: {
533: Image scaled = AbstractGraphics2D.prepareImage(image, width, height);
534: return checkImage(image, width, height, observer) == ImageObserver.ALLBITS;
535: }
536:
537: public int checkImage(Image image, int width, int height, ImageObserver observer)
538: {
539:
540: return ImageObserver.ALLBITS;
541: }
542:
543: public Image createImage(ImageProducer producer)
544: {
545: ImageConverter conv = new ImageConverter();
546: producer.startProduction(conv);
547: Image image = conv.getImage();
548: return image;
549: }
550:
551: public Image createImage(byte[] data, int offset, int len)
552: {
553: Image image;
554: try
555: {
556: ByteArrayInputStream i = new ByteArrayInputStream(data, offset, len);
557: image = createImage(i);
558: }
559: catch (IOException ex)
560: {
561: image = createErrorImage();
562: }
563: return image;
564: }
565:
566: private Image createImage(InputStream i)
567: throws IOException
568: {
569: Image image;
570: BufferedImage buffered = ImageIO.read(i);
571:
572:
573: if (buffered != null && buffered.getTransparency() == Transparency.OPAQUE)
574: {
575: ImageProducer source = buffered.getSource();
576: image = createImage(source);
577: }
578: else if (buffered != null)
579: {
580: image = buffered;
581: }
582: else
583: {
584: image = createErrorImage();
585: }
586: return image;
587: }
588:
589: public PrintJob getPrintJob(Frame frame, String title, Properties props)
590: {
591:
592: throw new UnsupportedOperationException("Not yet implemented.");
593: }
594:
595: public void beep()
596: {
597:
598: throw new UnsupportedOperationException("Not yet implemented.");
599: }
600:
601: public Clipboard getSystemClipboard()
602: {
603:
604: throw new UnsupportedOperationException("Not yet implemented.");
605: }
606:
607:
612: protected EventQueue getSystemEventQueueImpl()
613: {
614: if (eventQueue == null)
615: eventQueue = new EventQueue();
616: return eventQueue;
617: }
618:
619: public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent e)
620: {
621:
622: throw new UnsupportedOperationException("Not yet implemented.");
623: }
624:
625: public Map mapInputMethodHighlight(InputMethodHighlight highlight)
626: {
627:
628: throw new UnsupportedOperationException("Not yet implemented.");
629: }
630:
631:
636: static XGraphicsDevice getDefaultDevice()
637: {
638: XGraphicsEnvironment env = (XGraphicsEnvironment)
639: XGraphicsEnvironment.getLocalGraphicsEnvironment();
640: return (XGraphicsDevice) env.getDefaultScreenDevice();
641: }
642:
643: @Override
644: public boolean isModalExclusionTypeSupported(ModalExclusionType modalExclusionType)
645: {
646:
647: return false;
648: }
649:
650: @Override
651: public boolean isModalityTypeSupported(ModalityType modalityType)
652: {
653:
654: return false;
655: }
656:
657: private void checkHeadLess(String message) throws HeadlessException
658: {
659: if(GraphicsEnvironment.isHeadless())
660: {
661: if(message == null)
662: message = "This method cannot be called in headless mode.";
663:
664: throw new HeadlessException(message);
665: }
666: }
667: }