[英]Mouse Events on Application Window
I've an eclipse e4 application. 我有一个eclipse e4应用程序。 I'm trying to run some code say
xyz()
when the window is resized. 调整窗口大小时,我正在尝试运行一些代码,例如
xyz()
。 The issue is that while resizing the resize listener is called multiple times and I want to call xyz()
only once after the user has done resizing. 问题在于,调整大小的侦听器在调整大小时会被多次调用,而我只想在用户完成调整大小后才调用
xyz()
一次。 The only way that I can think is to capture the mouseUp event on the window itself, but couldn't figure an API to get the same. 我能想到的唯一方法是在窗口本身上捕获mouseUp事件,但无法找到一个能获得相同效果的API。
Sample code 样例代码
public class CTGHandler{
@Execute
public void execute(final EPartService partService, final EModelService modelService){
MPart mPart = modelService.createModelElement(MPart.class);
mPart.setLabel("CTGLive"); //$NON-NLS-1$
mPart.setContributionURI("bundleclass://test.ui.ctg/test.ui.ctg.CTGPart"); //$NON-NLS-1$
partService.showPart(mPart, PartState.ACTIVATE);
}
}
public class CTGPart {
@Inject
private IEventBroker eventBroker;
@Inject
public CTGPart(){
//Do something here...
}
@PostConstruct
public void init(final Composite parent){
Composite grandParent = parent.getParent().getParent().getParent();
System.out.println(grandParent); //Prints "Shell {CTGApp}"
grandParent.addControlListener(new ControlListener() {
@Override
public void controlResized(ControlEvent e){
System.out.println(e); // Prints "ControlEvent{Shell {CTGApp} time=9286942 data=null}" multiple times for a single resize event
// because there is no way the framework can understand if the resize was complete
xyz();// called each time, but I want this to be called only once
}
@Override
public void controlMoved(ControlEvent e)
{}
});
// MouseUp event on the application window so that xyz(); can be called once and we can get rid of grandParent.addControlListener(){...}
}
}
You might find it better to defer the processing of the resize event using a ScheduledFuture and a ScheduledExecutorService . 您可能会发现最好使用ScheduledFuture和ScheduledExecutorService来延迟调整大小事件的处理。
If you keep a reference to the Future you can cancel the previous one and schedule a new one. 如果您保留对Future的引用,则可以取消上一个,并计划一个新的。 This way you can collapse many events triggered in a short time period into a single slightly delayed event.
这样,您可以将短时间内触发的许多事件折叠为一个稍微延迟的事件。 You need to pick a good delay time to balance the number of events that will be swallowed against the delay that will occur after the last future has been scheduled.
您需要选择一个良好的延迟时间,以平衡吞下的事件数量与计划了最后一个将来之后发生的延迟。
grandParent.addControlListener(new ControlListener() {
ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(1);
ScheduledFuture scheduledFuture;
@Override
public void controlResized(ControlEvent e){
if(scheduledFuture != null) {
scheduledFuture.cancel();
}
scheduledFuture = scheduledExecutorService.schedule(new Callable() {
public Object call() throws Exception {
xyz();
return null;
}
}, 1, TimeUnit.SECONDS);
}
@Override
public void controlMoved(ControlEvent e)
{}
});
If you get a lot of events you should create a static Callable rather than create a new one each time. 如果收到很多事件,则应创建一个静态Callable而不是每次都创建一个新的Callable。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.