我正在尝试基于Ctrl键按下SWT ListViewer上的多个元素选择。我想区分使用Ctrl键的选择和ListViewer上的简单选择。我找不到如何查看是否按下或释放Ctrl键的方法。我没有看到有用的监听器。有没有办法在SWT ListViewer上使用按键和释放来了解选择。 这是我试过的示例代码。
public class DummyListViewer {
private ListViewer dummyViewer;
public DummyListViewer (Composite parent) {
dummyViewer = createListViewer(parent);
}
private ListViewer createListViewer(Composite parent) {
GridData data = new GridData(SWT.LEFT, SWT.LEFT, false, false);
ScrolledComposite scrolledParent = (ScrolledComposite) createScrolledComposite(parent, SWT.NONE);
final ListViewer dummyViewer= new ListViewer(scrolledParent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL| SWT.MULTI);
dummyViewer.getList().addKeyListener(new CtrlKeyListener());
dummyViewer.setContentProvider(new ListViewerContentProvider());
dummyViewer.setLabelProvider(new ListViewerLabelProvider());
scrolledParent.setContent(dummyViewer.getControl());
scrolledParent.setMinSize(dummyViewer.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT));
dummyViewer.getControl().setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
return dummyViewer;
}
private Composite createScrolledComposite(Composite parent, int style) {
ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setLayout(new GridLayout(1, false));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
gridData.widthHint = 400;
gridData.heightHint = 700;
scrolledComposite.setLayoutData(gridData);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setExpandHorizontal(true);
return scrolledComposite;
}
public void setInput(List<Object> input) {
filterViewer.setInput(input);
}
private class ListViewerContentProvider implements IStructuredContentProvider {
@Override
public void dispose() {
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List) {
return ((List)inputElement).toArray();
}
return null;
}
}
private ColumnViewerFilter filter;
private class ColumnViewerFilter extends ViewerFilter {
String value=""; //$NON-NLS-1$
/**
* Constructor for ColumnViewerFilter to set the String on which
* the viewer has to be filtered on.
* @param value - String on which
* the viewer has to be filtered on.
*/
public ColumnViewerFilter(String value) {
this.value = value;
}
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (value != null && value instanceof String && element instanceof String) {
if (value.equals(element))
return true;
}
return false;
}
}
private class ListViewerLabelProvider extends ColumnLabelProvider {
@Override
public String getText(Object element) {
if(element instanceof String)
return (String)element;
return null;
}
}
private boolean CtrlkeyPressed = false;
private class CtrlKeyListener implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == SWT.CTRL)
CtrlkeyPressed = true;
}
@Override
public void keyReleased(KeyEvent e) {
if (e.keyCode == SWT.CTRL)
CtrlkeyPressed = false;
}
}
}
答案 0 :(得分:1)
您可以使用addKeyListener
中包含的List
控件上的ListViewer
收听关键事件:
viewer.getList().addKeyListener(new KeyListener() {
@Override
public void keyPressed(final KeyEvent e)
{
if (e.keyCode == SWT.CTRL)
// TODO control pressed
}
@Override
public void keyReleased(final KeyEvent e)
{
if (e.keyCode == SWT.CTRL)
// TODO control release
}
});
请注意,在某些平台上,多个选择不使用 Ctrl 键。在Mac上,使用⌘键(SWT.COMMAND
)。