View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001-2003 Bartek Teodorczyk <barteo@barteo.net>
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 org.microemu.app.ui.swt;
21  
22  import javax.microedition.lcdui.Displayable;
23  
24  import org.eclipse.swt.widgets.Canvas;
25  import org.microemu.DisplayAccess;
26  import org.microemu.DisplayComponent;
27  import org.microemu.MIDletAccess;
28  import org.microemu.MIDletBridge;
29  import org.microemu.app.ui.DisplayRepaintListener;
30  import org.microemu.device.Device;
31  import org.microemu.device.DeviceFactory;
32  import org.microemu.device.MutableImage;
33  import org.microemu.device.swt.SwtDeviceDisplay;
34  import org.microemu.device.swt.SwtDisplayGraphics;
35  import org.microemu.device.swt.SwtMutableImage;
36  
37  
38  //TODO extends Canvas like in swing version
39  public class SwtDisplayComponent implements DisplayComponent
40  {
41  	private Canvas deviceCanvas;
42  	private SwtMutableImage displayImage = null;
43  	private DisplayRepaintListener displayRepaintListener;
44  	
45  	private Runnable redrawRunnable = new Runnable()
46  	{
47  		public void run() 
48  		{
49  			if (!deviceCanvas.isDisposed()) {
50  				deviceCanvas.redraw();
51  			}
52  		}
53  	};
54  
55  
56  	SwtDisplayComponent(Canvas deviceCanvas)
57  	{
58  		this.deviceCanvas = deviceCanvas;
59  	}
60  	
61  	
62  	public void addDisplayRepaintListener(DisplayRepaintListener l)
63  	{
64  		displayRepaintListener = l;
65  	}
66  
67  
68  	public void removeDisplayRepaintListener(DisplayRepaintListener l)
69  	{
70  		if (displayRepaintListener == l) {
71  			displayRepaintListener = null;
72  		}
73  	}
74  	
75  	
76  	public MutableImage getDisplayImage()
77  	{
78  		return displayImage;
79  	}
80  
81  
82  	public void paint(SwtGraphics gc) 
83  	{
84  		synchronized (this) {
85  			if (displayImage != null) {
86  				gc.drawImage(displayImage.img, 0, 0);
87  			}
88  		}
89  	}
90  
91    
92  	public void repaintRequest(int x, int y, int width, int height) 
93  	{
94  		if (!deviceCanvas.isDisposed()) {			
95  			MIDletAccess ma = MIDletBridge.getMIDletAccess();
96  			if (ma == null) {
97  				return;
98  			}
99  			DisplayAccess da = ma.getDisplayAccess();
100 			if (da == null) {
101 				return;
102 			}
103 			Displayable current = da.getCurrent();
104 			if (current == null) {
105 				return;
106 			}
107 
108 			Device device = DeviceFactory.getDevice();
109 
110 			SwtMutableImage image = new SwtMutableImage(
111 					device.getDeviceDisplay().getFullWidth(), device.getDeviceDisplay().getFullHeight());
112 						
113 			SwtGraphics gc = ((SwtDisplayGraphics) image.getGraphics()).g;
114 			try {
115 				SwtDeviceDisplay deviceDisplay = (SwtDeviceDisplay) device.getDeviceDisplay();
116 				deviceDisplay.paintDisplayable(gc, x, y, width, height);
117 				if (!deviceDisplay.isFullScreenMode()) {
118 					deviceDisplay.paintControls(gc);
119 				}
120 			} finally {
121 				gc.dispose();
122 			}
123 
124 			synchronized (this) {
125 				if (displayImage != null) {
126 					displayImage.img.dispose();
127 				}
128 				displayImage = image;
129 			}
130 			
131 			fireDisplayRepaint(displayImage);	
132 	
133 			deviceCanvas.getDisplay().asyncExec(redrawRunnable);
134 		}
135 	}
136 	
137 	
138 	private void fireDisplayRepaint(MutableImage image)
139 	{
140 		if (displayRepaintListener != null) {
141 			displayRepaintListener.repaintInvoked(image);
142 		}
143 	}
144   
145 }