SWT MouseDown事件对于自定义选择控件太占优势
我已经制作了UI /编辑器,您可以在下面的图片中看到。文本显示为StyledText
s。黑色线条是自定义边框,实际上是Label
,其上绘制有线条。SWT MouseDown事件对于自定义选择控件太占优势
现在我的目标是提供一个选择,允许用户选择Control
s到删除或者添加一些给他们。第二张图显示了一个示例选择。所以从各种各样的MouseEvent
开始,这比我最初想象的更复杂。
当MouseDown
事件上的任何Control
解雇我不能够跟踪任何其他Control
s表示用户希望选择,因为MouseMove
事件包含引发该事件MouseDown
,直到鼠标得到释放相同的控制。我需要追踪到正在进行的选择,以便为选定的Control
提供视觉反馈。下面的代码显示了一个演示行为的最小示例。
public class Tester {
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Stackoverflow");
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout(1, true));
SelectionListener listener = new SelectionListener();
StyledText text = new StyledText(comp, SWT.NONE);
text.setText("first text");
attachListener(text, listener);
text = new StyledText(comp, SWT.NONE);
text.setText("second text");
attachListener(text, listener);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void attachListener(Control control, Listener listener) {
if(control != null && !control.isDisposed()){
control.addListener(SWT.MouseDown, listener);
control.addListener(SWT.MouseMove, listener);
control.addListener(SWT.MouseEnter, listener);
control.addListener(SWT.MouseUp, listener);
}
}
static class SelectionListener implements Listener {
Event lastEvent = null;
@Override
public void handleEvent(Event event) {
switch(event.type){
case SWT.MouseDown:
lastEvent = event;
break;
case SWT.MouseMove:
if(lastEvent != null){
if(event.widget == lastEvent.widget)
System.out.println("the same!!!");
else
System.out.println("not the same!!!");
}
break;
case SWT.MouseEnter:
//this also does not fire when MouseDown is fired
System.out.println("entering");
break;
case SWT.MouseUp:
lastEvent = null;
break;
}
}
}
}
因此,基本上我正在寻求帮助。也许有更好/更简单的方法来实现这一点。我还试图弄清楚swt是如何在支持多选的表格或其他控件中执行此操作。但是很难找到特定的代码,或者他们为诸如表格之类的nativ控件调用本地方法。所以如果有人有想法请分享。
我找到了我的问题的解决方案。您可以自行发布MouseUp
事件。之后,所有事件都会再次出现。唯一困难的部分是区分您自己的自定义事件和普通用户/系统事件。我能够创建/找到一些标准来识别自定义事件。下面的代码/文档更详细地解释了这一点:
/**
* Sets up a custom {@code SWT.MouseUp} event and fires it. This is needed because a {@code SWT.MouseDown} is consuming all
* other events until a {@code SWT.MouseUp} event is fired. This means that it is not possible to get a e.g.
* {@code SWT.MouseEnter} event when entering a certain StyledText which is needed for selection. Therefore a custom {@code SWT.MouseUp}
* event is fired to simulate the releasing of the button on system level so that all other events can come through again. The real problem here
* is to distinguish between the custom event for simulation and a normal event produced by the user. Firing the event via Display.post(Event)
* does not fire the handed over event parameter. The system actually creates a new event instance. Therefore 2 criteria are used to distinguish the custom event:
* <ul>
* <ol>1. The selection can only be started by dragging a border control.
* A {@code SWT.DragDetect} event starts the hole selection process. All events coming in before this event are ignored.</ol>
* <ol>2. The actual distinguishing of the {@code SWT.MouseUp} is performed on the cursor coordinates and the referenced/dragged {@code widget}.
* The dragging event has to be started on this widget.</ol>
* </ul>
* @param the starting {@code SWT.DragDetect} event
* @see #isCustomMouseUpEvent(Event)
*/
private void fireCustomMouseUpEvent(Event dragDetectEvent){
customMouseUpEvent = new Event();
customMouseUpEvent.type = SWT.MouseUp;
customMouseUpEvent.button = 1; //left mouse button
customMouseUpEvent.widget = dragDetectEvent.widget;
if(dragDetectEvent.widget instanceof Control){
startingControl = (Control) dragDetectEvent.widget;
//get cursor location relative to widget to be comparable later with the event fired by the system
Point cursorLocation = startingControl.toControl(startingControl.getDisplay().getCursorLocation());
customMouseUpEvent.x = cursorLocation.x;
customMouseUpEvent.y = cursorLocation.y;
}
/*
* note: set attributes like Event.data or Event.x are not present
* in the actually firing event. SWT or the system is creating a complete new
* event instance without those manually added information.
*/
// mouseUpEvent.data = SELECTION_START_EVENT_IDENTIFIER;
if(dragDetectEvent.widget.getDisplay().post(customMouseUpEvent))
System.out.println("custom MouseUp event fired!");
}
private boolean isCustomMouseUpEvent(Event event) {
return customMouseUpEvent != null && event.widget == customMouseUpEvent.widget &&
customMouseUpEvent.x == event.x && customMouseUpEvent.y == event.y;
}
只是想详细说明这一点。虽然它在Windows上工作,但它不适用于Linux(Ubuntu)。找到了一种在SWT中使用DragAndDrop(DND)机制实现自定义选择的方法。这也用于滚动实现(选择文本和自动滚动)的SWT(至少为StyledText)。在StyledText类中也有一些死代码,实现了鼠标事件的滚动,它首先让我感到困惑,直到我发现这个代码从未被调用(至少在Windows中不被调用)。 – lolung
想要通过将鼠标拖到段右侧来选择多个段? –
是的,通过指定的控件(标签和StyledTexts)。目前正在开发一种方法,当我离开某个控件时我自己发送一个MouseUp事件,之后所有其他事件再次通过。 – lolung