Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/barteo/emulator/app/ui/swing/SwingDeviceComponent.java


1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001,2002 Bartek Teodorczyk <barteo@it.pl>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package com.barteo.emulator.app.ui.swing;
21  
22  import java.awt.Dimension;
23  import java.awt.Graphics;
24  import java.awt.Image;
25  import java.awt.Rectangle;
26  import java.awt.event.KeyEvent;
27  import java.awt.event.MouseAdapter;
28  import java.awt.event.MouseEvent;
29  import java.awt.event.MouseMotionListener;
30  import java.util.Enumeration;
31  
32  import javax.microedition.lcdui.Command;
33  import javax.swing.JPanel;
34  import javax.swing.UIManager;
35  
36  import com.barteo.emulator.CommandManager;
37  import com.barteo.emulator.DisplayComponent;
38  import com.barteo.emulator.device.DeviceFactory;
39  import com.barteo.emulator.device.SoftButton;
40  import com.barteo.emulator.device.j2se.J2SEButton;
41  import com.barteo.emulator.device.j2se.J2SEDevice;
42  import com.barteo.emulator.device.j2se.J2SEDeviceDisplay;
43  import com.barteo.emulator.device.j2se.J2SEInputMethod;
44  
45  
46  public class SwingDeviceComponent extends JPanel
47  {
48    SwingDeviceComponent instance;
49    SwingDisplayComponent dc;
50  
51    J2SEButton prevOverButton;
52    J2SEButton overButton;
53    J2SEButton pressedButton;
54    
55    Image offi;
56    Graphics offg;
57        
58    MouseAdapter mouseListener = new MouseAdapter() 
59    {
60      
61      public void mousePressed(MouseEvent e) 
62      {
63        pressedButton = getButton(e.getX(), e.getY());
64        if (pressedButton != null) {
65          if (pressedButton instanceof SoftButton) {
66            Command cmd = ((SoftButton) pressedButton).getCommand();
67            if (cmd != null) {
68              CommandManager.getInstance().commandAction(cmd);
69            }
70          } else {
71            int key = pressedButton.getKey();
72            KeyEvent ev = new KeyEvent(instance, 0, 0, 0, key, KeyEvent.CHAR_UNDEFINED);
73            DeviceFactory.getDevice().getInputMethod().keyPressed(ev.getKeyCode());
74          }
75          repaint();
76        }
77      }
78  
79  
80      public void mouseReleased(MouseEvent e) 
81      {
82        J2SEButton prevOverButton = getButton(e.getX(), e.getY());
83        if (prevOverButton != null) {
84          int key = prevOverButton.getKey();
85          KeyEvent ev = new KeyEvent(instance, 0, 0, 0, key, KeyEvent.CHAR_UNDEFINED);
86  
87          DeviceFactory.getDevice().getInputMethod().keyReleased(ev.getKeyCode());
88        }
89        pressedButton = null;
90        repaint();      
91      }
92  
93    };
94    
95  
96    MouseMotionListener mouseMotionListener = new MouseMotionListener() 
97    {
98  
99      public void mouseDragged(MouseEvent e)
100     {
101       overButton = getButton(e.getX(), e.getY());
102     }
103 
104     
105     public void mouseMoved(MouseEvent e)
106     {
107       prevOverButton = overButton;
108       overButton = getButton(e.getX(), e.getY());
109       if (overButton != prevOverButton) {
110         repaint();
111       }
112     }
113     
114   };
115   
116   
117   public SwingDeviceComponent() 
118   {
119     instance = this;
120     
121     dc = new SwingDisplayComponent(this);    
122     
123     addMouseListener(mouseListener);
124     addMouseMotionListener(mouseMotionListener);
125   }
126   
127   
128   public DisplayComponent getDisplayComponent()
129   {
130     return dc;
131   }
132   
133   
134   public void init()
135   {
136     revalidate();
137   }
138   
139   
140   public void keyPressed(KeyEvent ev)
141   {
142     ((J2SEInputMethod) DeviceFactory.getDevice().getInputMethod()).keyboardKeyPressed(ev);
143     pressedButton = getButton(ev);
144     repaint();
145     if (pressedButton instanceof SoftButton) {
146       Command cmd = ((SoftButton) pressedButton).getCommand();
147       if (cmd != null) {
148         CommandManager.getInstance().commandAction(cmd);
149       }
150     }      
151   }
152    
153   
154   public void keyReleased(KeyEvent ev)
155   {
156     ((J2SEInputMethod) DeviceFactory.getDevice().getInputMethod()).keyboardKeyReleased(ev);
157     prevOverButton = pressedButton;
158     pressedButton = null;
159     repaint();      
160   }
161    
162   
163   public void paint(Graphics g) 
164   {
165     if (offg == null || 
166         offi.getWidth(null) != getSize().width || offi.getHeight(null) != getSize().height) {
167       offi = createImage(getSize().width, getSize().height);
168       offg = offi.getGraphics();
169     }
170 
171     Dimension size = getSize();
172     offg.setColor(UIManager.getColor("text"));
173     offg.fillRect(0, 0, size.width, size.height);
174     offg.drawImage(((J2SEDevice) DeviceFactory.getDevice()).getNormalImage(), 0, 0, this);
175     
176     Rectangle displayRectangle = 
177         ((J2SEDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getDisplayRectangle();
178     offg.translate(displayRectangle.x, displayRectangle.y);
179     dc.paint(offg);
180     offg.translate(-displayRectangle.x, -displayRectangle.y);
181 
182     Rectangle rect;
183     if (prevOverButton != null ) {
184       rect = prevOverButton.getRectangle();    
185       offg.drawImage(((J2SEDevice) DeviceFactory.getDevice()).getNormalImage(), 
186           rect.x, rect.y, rect.x + rect.width, rect.y + rect.height,
187           rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, null);
188       prevOverButton = null;
189     }
190     if (overButton != null) {
191       rect = overButton.getRectangle();    
192       offg.drawImage(((J2SEDevice) DeviceFactory.getDevice()).getOverImage(), 
193           rect.x, rect.y, rect.x + rect.width, rect.y + rect.height,
194           rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, null);
195     }
196     if (pressedButton != null) {
197       rect = pressedButton.getRectangle();    
198       offg.drawImage(((J2SEDevice) DeviceFactory.getDevice()).getPressedImage(), 
199           rect.x, rect.y, rect.x + rect.width, rect.y + rect.height,
200           rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, null);
201     }
202     
203      g.drawImage(offi, 0, 0, null);
204   }
205 
206 
207   public void update(Graphics g)
208   {
209     paint(g);
210   }
211  
212   
213   private J2SEButton getButton(int x, int y)
214   {
215     for (Enumeration e = ((J2SEDevice) DeviceFactory.getDevice()).getButtons().elements(); e.hasMoreElements(); ) {
216       J2SEButton button = (J2SEButton) e.nextElement();
217       Rectangle tmp = new Rectangle(button.getRectangle());
218       if (x >= tmp.x && x < tmp.x + tmp.width && y >= tmp.y && y < tmp.y + tmp.height) {
219         return button;
220       }
221     }        
222     return null;
223   }
224 
225   
226   private J2SEButton getButton(KeyEvent ev)
227   {
228     for (Enumeration e = ((J2SEDevice) DeviceFactory.getDevice()).getButtons().elements(); e.hasMoreElements(); ) {
229       J2SEButton button = (J2SEButton) e.nextElement();
230       if (ev.getKeyCode() == button.getKey()) {
231         return button;
232       }
233       if (button.isChar(ev.getKeyChar())) {
234         return button;
235       }
236     }        
237     return null;
238   }
239   
240 }