KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > Toolkit


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: Toolkit.java,v $
11    Revision 1.14 2004/05/05 23:30:45 laurentmartelli
12    Added getProperty(String,String) stub
13
14    Revision 1.13 2004/05/05 12:43:19 bobintetley
15    Patches/new files from Laurent Martell
16
17    Revision 1.12 2004/05/04 10:06:52 bobintetley
18    Fixes to byte[] pixel data - added getModifiers() to ActionEvent
19
20    Revision 1.11 2004/04/29 12:49:26 bobintetley
21    Additional JOptionePane constants, missing JTree methods and improved awt.Image support
22
23    Revision 1.10 2004/04/23 10:02:47 bobintetley
24    MouseEvent BUTTON_MASK constants, JSlider.createStandardLabels(), FontMetrics
25    thread safety, Component/Toolkit.getFontMetrics() and MouseMotionAdapter
26
27    Revision 1.9 2004/03/30 10:42:44 bobintetley
28    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
29
30    Revision 1.8 2004/01/26 14:05:23 bobintetley
31    Compatibility methods
32
33    Revision 1.7 2004/01/20 07:38:05 bobintetley
34    Bug fixes and compatibility methods
35
36    Revision 1.6 2004/01/16 09:35:46 bobintetley
37    Full event dispatch thread support!
38
39    Revision 1.5 2003/12/14 09:13:38 bobintetley
40    Added CVS log to source headers
41
42 */

43
44 package swingwt.awt;
45
46 import java.net.URL JavaDoc;
47 import java.util.Hashtable JavaDoc;
48
49 import swingwt.awt.datatransfer.Clipboard;
50 import swingwt.awt.event.KeyEvent;
51 import swingwt.awt.image.ColorModel;
52 import swingwt.awt.image.ImageConsumer;
53 import swingwt.awt.image.ImageProducer;
54
55 public class Toolkit {
56     
57     private static Toolkit toolkit = new Toolkit();
58     private static Object JavaDoc retval;
59     private static Hashtable JavaDoc desktopProperties = new Hashtable JavaDoc();
60     
61     protected Toolkit() {}
62     
63     public static Toolkit getDefaultToolkit() {
64         return toolkit;
65     }
66     
67     public static Toolkit getToolkit() {
68         return toolkit;
69     }
70     
71     public Image getImage(String JavaDoc filename) {
72         return new swingwtx.swing.ImageIcon(filename).getImage();
73     }
74     
75     public Image getImage(URL JavaDoc url) {
76         return new swingwtx.swing.ImageIcon(url).getImage();
77     }
78     
79     public int getScreenResolution() {
80         return swingwtx.swing.SwingWTUtils.getDisplay().getDepth();
81     }
82     
83     public Insets getScreenInsets(GraphicsConfiguration gc) throws HeadlessException {
84         if (this != Toolkit.getDefaultToolkit())
85             return Toolkit.getDefaultToolkit().getScreenInsets(gc);
86         else
87             return new Insets(0,0,0,0);
88     }
89     
90     public FontMetrics getFontMetrics(Font f) {
91         return new FontMetrics(f);
92     }
93     
94     public int getMenuShortcutKeyMask() throws HeadlessException {
95         return KeyEvent.CTRL_MASK;
96     }
97     
98     public Dimension getScreenSize() {
99         swingwtx.swing.SwingUtilities.invokeSync(new Runnable JavaDoc() {
100             public void run() {
101                 retval = new Dimension(
102                     swingwtx.swing.SwingWTUtils.getDisplay().getBounds().width,
103                     swingwtx.swing.SwingWTUtils.getDisplay().getBounds().height);
104             }
105         });
106         return (Dimension) retval;
107     }
108     
109     public Image createImage(ImageProducer producer) {
110         ToolkitImageConsumer tki = new ToolkitImageConsumer();
111         producer.startProduction(tki);
112         return tki.awtImage;
113         
114     }
115
116     public Image createImage(byte[] imagedata) {
117     return new swingwtx.swing.ImageIcon(imagedata).getImage();
118     }
119
120     public Image createImage(byte[] imagedata, int imageoffset, int imagelength) {
121         byte[] im2 = new byte[imagelength];
122     for (int i = imageoffset; i < imagelength; i++)
123         im2[i - imageoffset] = imagedata[i];
124         return new swingwtx.swing.ImageIcon(im2).getImage();
125     }
126     
127     public Image createImage(String JavaDoc filename) {
128         return getImage(filename);
129     }
130     
131     public Image createImage(URL JavaDoc url) {
132         return getImage(url);
133     }
134     
135     public Clipboard getSystemClipboard() {
136         return new swingwt.awt.datatransfer.Clipboard();
137     }
138     
139     public void beep() {
140     }
141     
142     public final Object JavaDoc getDesktopProperty(String JavaDoc propertyName) {
143         return desktopProperties.get(propertyName);
144     }
145     
146     protected final void setDesktopProperty(String JavaDoc propertyName, Object JavaDoc value) {
147         desktopProperties.put(propertyName, value);
148     }
149     
150     /**
151      * Used to create images from the pixel data created from
152      * an ImageProducer
153      * @author Robin Rawson-Tetley
154      */

155     private class ToolkitImageConsumer implements ImageConsumer {
156         
157         /** The SwingWT Image to be returned */
158         public Image awtImage = new Image();
159         public boolean imageComplete = false;
160         public void imageComplete(int status) { imageComplete = true; }
161         public void setColorModel(ColorModel model) {}
162         public void setDimensions(int width, int height) {
163             awtImage.image = new org.eclipse.swt.graphics.Image(swingwtx.swing.SwingWTUtils.getDisplay(), width, height);
164         }
165         public void setHints(int hintflags) {}
166         public void setProperties(java.util.Hashtable JavaDoc props) {}
167         public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
168             byte[] bytes = new byte[pixels.length];
169             for (int i = 0; i < pixels.length; i++)
170                 bytes[i] = (byte) pixels[i];
171             setPixels(x, y, w, h, model, bytes, off, scansize);
172         }
173         public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
174             // Load the pixel data into the image
175
awtImage.image =
176                 new org.eclipse.swt.graphics.Image(
177                     swingwtx.swing.SwingWTUtils.getDisplay(),
178                     new org.eclipse.swt.graphics.ImageData( w, h, 16,
179                         new org.eclipse.swt.graphics.PaletteData(255, 255, 255)
180                         , scansize, pixels));
181         }
182     }
183     
184     public static String JavaDoc getProperty(String JavaDoc key,
185                                      String JavaDoc defaultValue) {
186         return defaultValue;
187     }
188 }
189
Popular Tags