1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
public class MyThreadDelay<E> extends Thread {
/**
* The listener where event is delaied
*/
private Listener myListener;
/**
* The event to delay
*/
private E myEvent;
/**
* The method name to call on listener to reload the event
*/
private String handleEventName;
/**
* The shell where popup will be shown : could be null
*/
private Shell myShell;
/**
* The delay time in millisecond : default value is 100.
*/
private Integer delay;
/**
* @param myListener : The listener where event is delaied (mandatory).
* @param myEvent : The event to delay (mandatory).
* @param handleEventName : The name of method to call on listener to reload the event (mandatory).
*/
public MyThreadDelay(Listener myListener, E myEvent, String handleEventName) {
this(myListener,myEvent,handleEventName,null,100);
}
/**
* @param myListener : The listener where event is delaied (mandatory).
* @param myEvent : The event to delay (mandatory).
* @param handleEventName : The name of method to call on listener to reload the event (mandatory).
* @param myShell : The shell of popup that could launch in {@link #synchronize(Runnable)} method (non mandatory), could be null.
*/
public MyThreadDelay(Listener myListener, E myEvent, Shell myShell,String handleEventName) {
this(myListener,myEvent,handleEventName,myShell,100);
}
/**
* @param myListener : The listener where event is delaied (mandatory).
* @param myEvent : The event to delay (mandatory).
* @param handleEventName : The name of method to call on listener to reload the event (mandatory).
* @param delay : The delay time in millisecond : default value is 100.
*/
public MyThreadDelay(Listener myListener, E myEvent, String handleEventName, Integer delay) {
this(myListener,myEvent,handleEventName,null,delay);
}
/**
* @param myListener : The listener where event is delaied (mandatory).
* @param myEvent : The event to delay (mandatory).
* @param handleEventName : The name of method to call on listener to reload the event (mandatory).
* @param myShell : The shell of popup that could launch in {@link #synchronize(Runnable)} method (non mandatory), could be null.
* @param delay : The delay time in millisecond : default value is 100.
*/
public MyThreadDelay(Listener myListener, E myEvent, String handleEventName, Shell myShell, Integer delay) {
super();
this.myListener = myListener;
this.myEvent = myEvent;
this.handleEventName = handleEventName;
this.delay = delay;
/*
* If no shell was specify, then we used the main shell
*/
initMyShell(myShell);
}
/**
* Init the shell with shell in paramters. If myShell is null, then the main shell will be used
* @param myShell
*/
private void initMyShell(Shell myShell){
if (myShell == null)
if (PlatformUI.isWorkbenchRunning())
myShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
this.myShell = myShell;
}
/* (non-Javadoc)
* @see java.lang.Thread#start()
*/
@Override
public synchronized void start() {
/*
* We launch a sleep in order to delay the runs of event
*/
try {
Thread.sleep(delay);
/*
* We reload the event on listener
*/
ClassMethod.getResultMethod(myListener
, new Object[]{myEvent}
, handleEventName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Synchronize SWT process with the thread
* @param runnable
*/
public void synchronize(Runnable runnable){
if (myShell == null)
initMyShell(null);
/*
* We synchronize the thread with opens of popup
*/
myShell.getDisplay().syncExec(runnable);
}
/**
* Asynchronize SWT process with the thread
* @param runnable
*/
public void asynchronize(Runnable runnable){
if (myShell == null)
initMyShell(null);
/*
* We synchronize the thread with opens of popup
*/
myShell.getDisplay().asyncExec(runnable);
}
} |
Partager