9ca0366e0567f779ebdf599e950fd45d16ee7dbe
[org.ibex.core.git] / src / org / ibex / plat / AWT.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.plat;
6
7 import org.ibex.util.*;
8 import java.io.*;
9 import org.ibex.js.*;
10 import java.awt.*;
11 import java.awt.datatransfer.*;
12 import java.awt.image.*;
13 import java.awt.event.*;
14 import org.ibex.graphics.*;
15 import org.ibex.core.*;
16 import org.ibex.net.*;
17
18 /** Platform subclass for all VM's providing AWT 1.1 functionality */
19 public class AWT extends JVM {
20
21     protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; }
22     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new AWTPixelBuffer(w, h); }
23     protected Picture _createPicture(JS r) { return new AWTPicture(r); }
24     protected int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; }
25     protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; }
26     protected Surface _createSurface(Box b, boolean framed) { return new AWTSurface(b, framed); }
27
28     protected void postInit() {
29         if (Log.on) Log.diag(Platform.class, "               color depth = " +
30                             Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp");
31     }
32
33     protected void _criticalAbort(String message) {
34         if (Log.on) Log.info(this, message);
35         final Dialog d = new Dialog(new Frame(), "Ibex Cannot Continue");
36         d.setLayout(new BorderLayout());
37         TextArea ta = new TextArea("Ibex cannot continue because:\n\n" + message, 10, 80);
38         ta.setEditable(false);
39         d.add(ta, "Center");
40         Button b = new Button("OK");
41         b.addActionListener(new ActionListener() {
42                 public void actionPerformed(ActionEvent e) {
43                     d.dispose();
44                 }
45             });
46         d.add(b, "South");
47         d.setModal(true);
48         d.pack();
49         d.show();
50         new Semaphore().block();
51     }
52
53     protected String _getClipBoard() {
54         Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
55         if (cb == null) return null;
56         Transferable clipdata = cb.getContents(null);
57         try { return (String)clipdata.getTransferData(DataFlavor.stringFlavor); } catch (Exception ex) { return null; }
58     }
59
60     protected void _setClipBoard(String s) {
61         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
62         if (clipboard == null) return;
63         StringSelection clipString = new StringSelection(s);
64         clipboard.setContents(clipString, clipString);
65     }
66
67     /** some platforms (cough, cough, NetscapeVM) have totally broken modifier masks; they will need to override this */
68     protected static int modifiersToButtonNumber(int modifiers) {
69         if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1;
70         if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
71             // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
72             if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 2;
73             return 3;
74         }
75         if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
76             // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
77             if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 3;
78             return 2;
79         }
80         return 0;
81     }
82
83     static class FileDialogHelper extends FileDialog implements WindowListener, ComponentListener {
84         Semaphore s;
85         public FileDialogHelper(String suggestedFileName, Semaphore s, boolean write) {
86             super(new Frame(), write ? "Save" : "Open", write ? FileDialog.SAVE : FileDialog.LOAD);
87             this.s = s;
88             addWindowListener(this);
89             addComponentListener(this);
90             if (suggestedFileName.indexOf(File.separatorChar) == -1) {
91                 setFile(suggestedFileName);
92             } else {
93                 setDirectory(suggestedFileName.substring(0, suggestedFileName.lastIndexOf(File.separatorChar)));
94                 setFile(suggestedFileName.substring(suggestedFileName.lastIndexOf(File.separatorChar) + 1));
95             }
96             show();
97         }
98         public void windowActivated(WindowEvent e) { }
99         public void windowClosed(WindowEvent e) { s.release(); }
100         public void windowClosing(WindowEvent e) { }
101         public void windowDeactivated(WindowEvent e) { }
102         public void windowDeiconified(WindowEvent e) { }
103         public void windowIconified(WindowEvent e) { }
104         public void windowOpened(WindowEvent e) { }
105         public void componentHidden(ComponentEvent e) { s.release(); }
106         public void componentMoved(ComponentEvent e) { }
107         public void componentResized(ComponentEvent e) { }
108         public void componentShown(ComponentEvent e) { }
109     };
110
111     protected String _fileDialog(String suggestedFileName, boolean write) {
112         final Semaphore s = new Semaphore();
113         FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write);
114         s.block();
115         return fd.getDirectory() == null ? null : (fd.getDirectory() + File.separatorChar + fd.getFile());
116     }
117
118
119     // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
120
121     protected org.ibex.graphics.Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new AWTGlyph(f, c); }
122     protected static class AWTGlyph extends org.ibex.graphics.Font.Glyph {
123         Image i = null;
124         Image i2 = null;
125         private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
126
127         // this doesn't work on Win32 because the JVM is broken
128         /*
129         static final ColorModel cmodel = new ColorModel(8) {
130             public int getRed(int p) { return 0; }
131             public int getGreen(int p) { return 0; }
132             public int getBlue(int p) { return 0; }
133             public int getAlpha(int p) { return p & 0xFF; }
134         };
135         */
136
137         public AWTGlyph(org.ibex.graphics.Font f, char c) { super(f, c); }
138         Image getImage() {
139             if (i == null && isLoaded) {
140
141                 int[] data2 = new int[data.length];
142                 for(int i=0; i<data2.length; i++) data2[i] = ((data[i]) & 0xff) << 24;
143
144                 MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data2, 0, width);
145                 mis.setAnimated(true);
146                 i = Toolkit.getDefaultToolkit().createImage(mis);
147                 MediaTracker mediatracker = new MediaTracker(new Canvas());
148                 mediatracker.addImage(i, 1);
149                 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
150                 mediatracker.removeImage(i);
151                 synchronized(AWTPixelBuffer.class) { 
152                     if (AWTPixelBuffer.component == null) {
153                         AWTPixelBuffer.component = new Frame();
154                         AWTPixelBuffer.component.setVisible(false);
155                         AWTPixelBuffer.component.addNotify();
156                     }
157                 }
158                 data = null;
159             }
160             return i;
161         }
162     }
163
164     protected static class AWTPicture extends Picture {
165         public Image i = null;
166         private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
167         
168         boolean initialized = false;
169         public AWTPicture(JS r) { super(r); }
170         public void init() {
171             if (initialized) return;
172             initialized = true;
173             MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data, 0, width);
174             mis.setAnimated(true);
175             i = Toolkit.getDefaultToolkit().createImage(mis);
176             MediaTracker mediatracker = new MediaTracker(new Canvas());
177             mediatracker.addImage(i, 1);
178             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
179             mediatracker.removeImage(i);
180             synchronized(AWTPixelBuffer.class) { 
181                 if (AWTPixelBuffer.component == null) {
182                     AWTPixelBuffer.component = new Frame();
183                     AWTPixelBuffer.component.setVisible(false);
184                     AWTPixelBuffer.component.addNotify();
185                 }
186             }
187         }
188     }
189     
190     protected static class AWTPixelBuffer implements PixelBuffer {
191         
192         protected Image i = null;
193         protected Graphics g = null;
194         protected AWTSurface surface = null;
195
196         /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
197         static Component component = null;
198
199         public AWTPixelBuffer(Image i) { this.i = i; }
200         public AWTPixelBuffer(AWTSurface s) { this.surface = s; }
201         public AWTPixelBuffer(int w, int h) {
202             synchronized(AWTPixelBuffer.class) {
203                 if (component == null) {
204                     component = new Frame();
205                     component.setVisible(false);
206                     component.addNotify();
207                 }
208             }
209             i = component.createImage(w, h);
210         }
211         Graphics getGraphics() {
212             if (surface != null) return surface.getGraphics();
213             if (g != null) return g;
214             if (i != null) { g = i.getGraphics(); return g; }
215             return null;
216         }
217         
218         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
219             ((AWTPicture)source).init();
220             Graphics g = getGraphics();
221             g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
222             g.drawImage(((AWTPicture)source).i, dx, dy, null);
223             if (i != null) g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
224         }
225
226         public void drawLine(int x1, int y1, int x2, int y2, int rgb) {
227             Graphics g = getGraphics();
228             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
229             g.setColor(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
230             g.drawLine(x1, y1, x2, y2);
231         }
232
233         public void stroke(org.ibex.graphics.Polygon p, int color) { p.stroke(this, color); }
234         public void fill(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { p.fill(this, paint); }
235
236         private static int[] xa = new int[4];
237         private static int[] ya = new int[4];
238         public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
239             Graphics g = getGraphics();
240             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 
241             g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
242             xa[0]=x1;
243             xa[1]=x2;
244             xa[2]=x4;
245             xa[3]=x3;
246             ya[0]=y1;
247             ya[1]=y1;
248             ya[2]=y2;
249             ya[3]=y2;
250             g.fillPolygon(xa, ya, 4);
251         }
252
253         // this doens't seem to work on Windows
254         public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2,
255                               int rgb, int argb) {
256             Image i = ((AWTGlyph)source).getImage();
257             if (((AWTGlyph)source).i2 == null)
258                 ((AWTGlyph)source).i2 = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB);
259             Image i2 = ((AWTGlyph)source).i2;
260             Graphics g2 = i2.getGraphics();
261             g2.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
262             g2.fillRect(0, 0, i2.getWidth(null), i2.getHeight(null));
263             g2.drawImage(i, 0, 0, null);
264             Graphics g = getGraphics();
265             g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
266             g.drawImage(i2, dx, dy, i2.getWidth(null), i2.getHeight(null), null);
267             g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
268             g.fillRect(cx1, cy1, dx - cx1,  cy2 - cy1);
269             g.fillRect(cx1, cy1, cx2 - cx1, dy - cy1);
270             g.fillRect(dx+i2.getWidth(null), cy1, cx2 - (dx+i2.getWidth(null)), cy2 - cy1);
271             g.fillRect(cx1, dy+i2.getHeight(null), cx2 - cx1, cy2 - (dy+i2.getHeight(null)));
272             g.setClip(0, 0, 1000, 1000);
273         }
274     }
275     
276     
277     protected static class AWTSurface extends Surface.DoubleBufferedSurface
278         implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
279
280         protected AWTPixelBuffer pb = null;
281         private Graphics g = null;
282         Frame frame = null;
283         Window window = null;
284
285         public void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2) {
286             getGraphics().drawImage(((AWTPixelBuffer)source).i, sx, sy, sx+(dx2-dx), sy+(dy2-dy), dx, dy, dx2, dy2, null);
287         }
288         
289         /** our component's insets */
290         protected Insets insets = new Insets(0, 0, 0, 0);
291         Graphics getGraphics() { if (g==null) g=window==null ? frame.getGraphics() : window.getGraphics(); return g; }
292         public void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
293             if (pb ==null) pb = new AWTPixelBuffer(this);
294             pb.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
295         }
296         //public PixelBuffer getPixelBuffer() { return pb != null ? pb : (pb = new AWTPixelBuffer(this)); } 
297         public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); }
298         public void toBack() { if (window != null) window.toBack(); }
299         public void toFront() { if (window != null) window.toFront(); }
300         public void setLocation() { /*window.setLocation(root.x, root.y); FIXME */ }
301         public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
302         public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
303         public void _setSize(int width, int height) {
304             g = null;
305             window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom));
306         }
307         public void setInvisible(boolean b) { window.setVisible(!b); }
308         protected void _setMinimized(boolean b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
309         protected void _setMaximized(boolean b) {
310             if (!b) {
311                 if (Log.on) Log.info(this, "JDK 1.1 platforms cannot unmaximize windows");
312                 return;
313             }
314             window.setLocation(new Point(0, 0));
315             window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
316             g = null;
317         }
318
319         class InnerFrame extends Frame {
320             public InnerFrame() throws java.lang.UnsupportedOperationException { }
321             public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); }
322             public void update(Graphics gr) {
323                 Rectangle r = gr.getClipBounds();
324                 super.update(gr);
325                 dirtify(r);
326             }
327             public void paint(Graphics gr) {
328                 // Mac OS X Jdk1.4 Bug: after a componentResized(), you must wait for the paint() before you redraw
329                 Rectangle r = gr.getClipBounds();
330                 super.paint(gr);
331                 dirtify(r);
332             }
333             private void dirtify(java.awt.Rectangle r) {
334                 if (r != null) {
335                     Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
336                 } else {
337                     Dirty(0, 0,
338                           Math.max(getWidth() - insets.left - insets.right, root.width),
339                           Math.min(getHeight() - insets.top - insets.bottom, root.height));
340                 }
341                 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
342                 int newwidth = Math.max(getWidth() - insets.left - insets.right, root.width);
343                 int newheight = Math.max(getHeight() - insets.top - insets.bottom, root.height);
344                 if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
345             }
346         }
347
348         class InnerWindow extends Window {
349             public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
350             public Dimension getMinimumSize() { return new Dimension(root.minwidth, root.minheight); }
351             public void update(Graphics gr) { paint(gr); }
352             public void paint(Graphics gr) {
353                 g = null;
354                 Rectangle r = gr.getClipBounds();
355                 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
356             }
357         }
358
359         private int oldfill = 0x0;
360         public void render() {
361             // useful optimizatin;
362             if (oldfill != root.fillcolor) {
363                 oldfill = root.fillcolor;
364                 window.setBackground((root.fillcolor & 0xFF000000) == 0 ?
365                                      java.awt.Color.white :
366                                      new java.awt.Color((root.fillcolor >> 16) & 0xff,
367                                                (root.fillcolor >> 8) & 0xff,
368                                                (root.fillcolor) & 0xff));
369             }
370             super.render();
371         }
372
373         Insets getInsets() {
374             Insets ret = window.getInsets();
375             if (System.getProperty("os.name", "").equals("Mac OS X")) ret.bottom = -1 * ret.top;
376             return ret;
377         }
378
379         AWTSurface(Box root, boolean framed) {
380             super(root);
381             try {
382                 if (framed) window = frame = new InnerFrame();
383                 else window = new InnerWindow();
384
385             // this is here to catch HeadlessException on jdk1.4
386             } catch (java.lang.UnsupportedOperationException e) {
387                 if (Log.on) Log.info(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
388                 if (Log.on) Log.info(this, e);
389             }
390
391             insets = getInsets();
392             
393             window.addMouseListener(this);
394             window.addKeyListener(this);
395             window.addComponentListener(this);
396             window.addMouseMotionListener(this);
397             window.addWindowListener(this);
398
399             // IMPORTANT: this must be called before render() to ensure
400             // that our peer has been created
401             makeVisible();
402         }
403
404         protected void makeVisible() { window.setVisible(true); }
405         
406         public void _dispose() {
407             window.removeMouseListener(this);
408
409             // removed to work around a jdk1.3 bug
410             /* window.removeKeyListener(this); */
411
412             window.removeComponentListener(this);
413             window.removeMouseMotionListener(this);
414             window.removeWindowListener(this);
415             window.dispose();
416         }
417
418         public void syncCursor() {
419             if (cursor.equals("crosshair")) window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
420             else if (cursor.equals("east")) window.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
421             else if (cursor.equals("move")) window.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
422             else if (cursor.equals("north")) window.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
423             else if (cursor.equals("northeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
424             else if (cursor.equals("northwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
425             else if (cursor.equals("south")) window.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
426             else if (cursor.equals("southeast")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
427             else if (cursor.equals("southwest")) window.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
428             else if (cursor.equals("text")) window.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
429             else if (cursor.equals("west")) window.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
430             else if (cursor.equals("wait")) window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
431             else if (cursor.equals("hand")) window.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
432             else window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
433         }
434         
435         // AWT Message translation ////////////////////////////////////////////////////////////////
436         
437         // these functions are all executed in the AWT thread, not the
438         // MessageQueue thread. As a result, they must be *extremely*
439         // careful about invoking methods on instances of Box. Currently,
440         // they should only enqueue messages, use Box.whoIs()
441         // (unsynchronized but thought to be safe), and modify members of
442         // Surface.
443         
444         public void componentHidden(ComponentEvent e) { }
445         public void componentShown(ComponentEvent e) { }
446         public void windowOpened(WindowEvent e) { }
447         public void windowClosed(WindowEvent e) { }
448         public void windowClosing(WindowEvent e) { Close(); }
449         public void windowIconified(WindowEvent e) { Minimized(true); }
450         public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.width, root.height); Minimized(false); }
451         public void windowActivated(WindowEvent e) { Focused(true); }
452         public void windowDeactivated(WindowEvent e) { Focused(false); }
453         public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
454
455         public void componentResized(ComponentEvent e) {
456             // we have to periodically do this; I don't know why
457             insets = getInsets();
458             componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
459         }
460
461         public void componentResized(int newwidth, int newheight) {
462             SizeChange(newwidth, newheight);
463             if (newwidth > root.width) Dirty(root.width, 0, newwidth-root.width, newheight);
464             if (newheight > root.height) Dirty(0, root.height, newwidth, newheight-root.height);
465             Refresh();
466         }
467
468         public void keyTyped(KeyEvent k) { }
469         public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
470         public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
471         public void mouseExited(MouseEvent m) { mouseMoved(m); }
472         public void mouseEntered(MouseEvent m) { mouseMoved(m); }
473         public void mouseDragged(MouseEvent m) { mouseMoved(m); }
474         public void mouseMoved(MouseEvent m) {
475
476             // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
477             int newwidth = window.getWidth() - insets.left - insets.right;
478             int newheight = window.getHeight() - insets.top - insets.bottom;
479             if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
480             
481             Move(m.getX() - insets.left, m.getY() - insets.top);
482         }
483         public void mousePressed(MouseEvent m) { Press(modifiersToButtonNumber(m.getModifiers())); }
484         public void mouseReleased(MouseEvent m) { Release(modifiersToButtonNumber(m.getModifiers())); }
485         public void mouseClicked(MouseEvent m) {
486             if (m.getClickCount() == 2) DoubleClick(modifiersToButtonNumber(m.getModifiers()));
487             else Click(modifiersToButtonNumber(m.getModifiers()));
488         }
489         
490         String translateKey(KeyEvent k) {
491             switch (k.getKeyCode()) {
492             case KeyEvent.VK_ALT: return "alt";
493             case KeyEvent.VK_BACK_SPACE: return "back_space";
494             case KeyEvent.VK_CONTROL: return "control";
495             case KeyEvent.VK_DELETE: return "delete";
496             case KeyEvent.VK_DOWN: return "down";
497             case KeyEvent.VK_END: return "end";
498             case KeyEvent.VK_ENTER: return "enter";
499             case KeyEvent.VK_ESCAPE: return "escape";
500             case KeyEvent.VK_F1: return "f1";
501             case KeyEvent.VK_F10: return "f10";
502             case KeyEvent.VK_F11: return "f11";
503             case KeyEvent.VK_F12: return "f12";
504             case KeyEvent.VK_F2: return "f2";
505             case KeyEvent.VK_F3: return "f3";
506             case KeyEvent.VK_F4: return "f4";
507             case KeyEvent.VK_F5: return "f5";
508             case KeyEvent.VK_F6: return "f6"; 
509             case KeyEvent.VK_F7: return "f7";
510             case KeyEvent.VK_F8: return "f8";
511             case KeyEvent.VK_F9: return "f9";
512             case KeyEvent.VK_HOME: return "home";
513             case KeyEvent.VK_INSERT: return "insert";
514             case KeyEvent.VK_LEFT: return "left";
515             case KeyEvent.VK_META: return "alt";
516             case KeyEvent.VK_PAGE_DOWN: return "page_down";
517             case KeyEvent.VK_PAGE_UP: return "page_up";
518             case KeyEvent.VK_PAUSE: return "pause";
519             case KeyEvent.VK_PRINTSCREEN: return "printscreen";
520             case KeyEvent.VK_RIGHT: return "right";
521             case KeyEvent.VK_SHIFT: return "shift";
522             case KeyEvent.VK_TAB: return "tab";
523             case KeyEvent.VK_UP: return "up";
524             default:
525                 char c = k.getKeyChar();
526                 if (c >= 1 && c <= 26) c = (char)('a' + c - 1);
527                 return String.valueOf(c);
528             }
529         }
530     }
531
532     protected void _decodeJPEG(InputStream is, Picture p) {
533         try {
534             Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is));
535             MediaTracker mediatracker = new MediaTracker(new Canvas());
536             mediatracker.addImage(i, 1);
537             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
538             mediatracker.removeImage(i);
539             final int width = i.getWidth(null);
540             final int height = i.getHeight(null);
541             final int[] data = new int[width * height];
542             PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width);
543             pg.grabPixels();
544             if ((pg.getStatus() & ImageObserver.ABORT) != 0)
545                 Log.info(this, "PixelGrabber reported an error while decoding JPEG image");
546             p.width = width;
547             p.height = height;
548             p.data = data;
549         } catch (Exception e) {
550             Log.info(this, "Exception caught while decoding JPEG image");
551             Log.info(this, e);
552         }
553     }
554 }