Problem with a blocking thread [message #456189] |
Thu, 26 May 2005 13:32 |
Eclipse User |
|
|
|
Originally posted by: stephane.xxxxx.xx
Hi,
Here is a small application that shows my problem with a blocking thread.
Description of the application:
This application displays an icon that responds to 3 events:
- When you "mouseEnter(MouseEvent e)", the icon becomes red.
- When you "mouseExit(MouseEvent e)", the icon becomes blue.
- When you "mouseHover(MouseEvent e)", a balloon appears while 3 seconds
and prints a message.
The application's classes:
- IconTip : The icon that responds to the 3 events.
- BalloonTip : The balloon that shows the message
My problem:
When the balloon appears, the "mouseEnter(MouseEvent e)" and the
"mouseExit(MouseEvent e)" method can't be called. The thread I use to show
my balloon in the "mouseHover(MouseEvent e)" blocks the rest of the
application. However, it is started asynchronously...
Thanks by advance for answering,
Stéphane
************************************************************ ****************
The IconTip class
************************************************************ ****************
package steph.tips2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class IconTip extends Canvas {
private BalloonTip balloonTip = null;
private Image image = null;
private String lastMessage = "no message";
private int timeToShow = 3000; // MILLISECONDS
public IconTip(Composite parent, int style, Image image) {
super(parent, style);
this.setImage(image);
init();
}
private void init() {
getShell().setSize(image.getBounds().width*2,
image.getBounds().height*2);
getShell().setLocation(500,500);
this.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(image, 0, 0);
}
});
this.addMouseTrackListener(new IconMouseTrackListener());
this.redraw();
this.layout();
Point location = this.getShell().getLocation();
this.balloonTip = new BalloonTip(this.getParent().getDisplay());
this.balloonTip.setLocation(location.x - 210, location.y - 190);
}
public void showTip(String tipMessage) {
lastMessage = tipMessage;
this.getParent().getDisplay().asyncExec(new TipThread());
}
public void setImage(Image image) {
this.image = image;
this.setSize(image.getBounds().width, image.getBounds().height);
this.redraw();
}
class IconMouseTrackListener implements MouseTrackListener {
public void mouseEnter(MouseEvent e) {
Image image = new Image(getDisplay(), 16, 16);
Color color = getDisplay().getSystemColor (SWT.COLOR_RED);
GC gc = new GC (image);
gc.setBackground (color);
gc.fillRectangle (image.getBounds ());
gc.dispose ();
setImage(image);
}
public void mouseExit(MouseEvent e) {
Image image = new Image(getDisplay(), 16, 16);
Color color = getDisplay().getSystemColor (SWT.COLOR_BLUE);
GC gc = new GC (image);
gc.setBackground (color);
gc.fillRectangle (image.getBounds ());
gc.dispose ();
setImage(image);
}
public void mouseHover(MouseEvent e) {
showTip("This is a message.");
}
}
class TipThread implements Runnable {
int i = 0;
public void run() {
balloonTip.showMessage(lastMessage, timeToShow);
}
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.ON_TOP);
shell.setLayout(new GridLayout());
Image image = new Image(display, 16, 16);
Color color = display.getSystemColor (SWT.COLOR_BLUE);
GC gc = new GC (image);
gc.setBackground (color);
gc.fillRectangle (image.getBounds ());
gc.dispose ();
IconTip iconTip = new IconTip(shell, SWT.NO_BACKGROUND |
SWT.NO_REDRAW_RESIZE, image);
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
************************************************************ ****************
************************************************************ ****************
The BalloonTip class
************************************************************ ****************
package steph.tips2;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import fr.ill.ics.client.ui.display.swt.FontFactory;
public class BalloonTip {
private Display display = null;
private Shell balloonTipShell = null;
private Point location = null;
public BalloonTip(Display display) {
this.display = display;
init();
}
/**
* Initialize the shell that handle the BalloonTip instance
*
*/
private void init() {
if (this.balloonTipShell != null) {
this.balloonTipShell.dispose();
}
this.balloonTipShell = new Shell (this.display, SWT.NO_TRIM |
SWT.ON_TOP);
this.balloonTipShell.setLayout(new GridLayout());
if (this.location == null) {
this.location = new Point(0, 0);
}
this.balloonTipShell.setLocation(this.location);
}
/**
* This method makes the BalloonTip visible
* with the tipMessage during timeToShow milliseconds
*
* @param tipMessage The message to print in the BallonTip
* @param timeToShow The time spend to show the BalloonTip
*/
public void showMessage(String tipMessage, int timeToShow) {
final String message = tipMessage;
Canvas canvas = new Canvas(this.balloonTipShell, SWT.NO_BACKGROUND);
GridData gridData = new GridData();
canvas.setLayoutData(gridData);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent paintEvent) {
paintEvent.gc.setFont(FontFactory.getStandardFont());
paintEvent.gc.drawText(message, 10, 10);
}});
this.balloonTipShell.pack ();
this.balloonTipShell.open ();
/**
* Find something to do display.sleep(); while timeToShow milliseconds
* But I want to make the BalloonTip visible with the tipMessage
* during timeToShow milliseconds
* and the IconTip object keeps responding to the mouse events
*/
/**
* Currently, I use a Thread.sleep(timeToShow)
* But this solution wait timeToShow milliseconds before
* to execute display.sleep();
* So, the IconTip object can not keep responding to the mouse events
*/
try {
Thread.sleep(timeToShow);
} catch (InterruptedException e) {
e.printStackTrace();
}
display.sleep();
init();
}
/**
* Initialize the BalloonTip's location
*
* @param location
*/
public void setLocation(Point location) {
this.location = location;
init();
}
/**
* Initialize the BalloonTip's location
*
* @param x
* @param y
*/
public void setLocation(int x, int y) {
this.location = new Point(x, y);
init();
}
}
************************************************************ ****************
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02012 seconds