use of org.eclipse.swt.events.KeyAdapter in project eclipse.platform.swt by eclipse.
the class Tab method createListenersGroup.
/**
* Creates the "Listeners" group. The "Listeners" group
* goes below the "Example" and "Control" groups.
*/
void createListenersGroup() {
listenersGroup = new Group(tabFolderPage, SWT.NONE);
listenersGroup.setLayout(new GridLayout(4, false));
listenersGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
listenersGroup.setText(ControlExample.getResourceString("Listeners"));
/*
* Create the button to access the 'Listeners' dialog.
*/
Button listenersButton = new Button(listenersGroup, SWT.PUSH);
listenersButton.setText(ControlExample.getResourceString("Select_Listeners"));
listenersButton.addSelectionListener(widgetSelectedAdapter(e -> {
createListenerSelectionDialog();
recreateExampleWidgets();
}));
/*
* Create the checkbox to specify whether typed or untyped events are displayed in the log.
*/
final Button untypedEventsCheckbox = new Button(listenersGroup, SWT.CHECK);
untypedEventsCheckbox.setText(ControlExample.getResourceString("UntypedEvents"));
untypedEventsCheckbox.addSelectionListener(widgetSelectedAdapter(e -> untypedEvents = untypedEventsCheckbox.getSelection()));
/*
* Create the checkbox to add/remove listeners to/from the example widgets.
*/
final Button listenCheckbox = new Button(listenersGroup, SWT.CHECK);
listenCheckbox.setText(ControlExample.getResourceString("Listen"));
listenCheckbox.addSelectionListener(widgetSelectedAdapter(e -> {
logging = listenCheckbox.getSelection();
recreateExampleWidgets();
}));
/*
* Create the button to clear the text.
*/
Button clearButton = new Button(listenersGroup, SWT.PUSH);
clearButton.setText(ControlExample.getResourceString("Clear"));
clearButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
clearButton.addSelectionListener(widgetSelectedAdapter(e -> eventConsole.setText("")));
/* Initialize the eventsFilter to log all events. */
int customEventCount = getCustomEventNames().length;
eventsFilter = new boolean[EVENT_INFO.length + customEventCount];
for (int i = 0; i < EVENT_INFO.length + customEventCount; i++) {
eventsFilter[i] = true;
}
/* Create the event console Text. */
eventConsole = new Text(listenersGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
GridData data = new GridData(GridData.FILL_BOTH);
data.horizontalSpan = 4;
data.heightHint = 80;
eventConsole.setLayoutData(data);
createEventConsolePopup();
eventConsole.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if ((e.keyCode == 'A' || e.keyCode == 'a') && (e.stateMask & SWT.MOD1) != 0) {
eventConsole.selectAll();
e.doit = false;
}
}
});
}
use of org.eclipse.swt.events.KeyAdapter in project eclipse.platform.swt by eclipse.
the class AccessibleActionExample method main.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Accessible Action Example");
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
final Canvas customButton = new Canvas(shell, SWT.NONE) {
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
GC gc = new GC(this);
Point point = gc.stringExtent(buttonText);
gc.dispose();
point.x += MARGIN;
point.y += MARGIN;
return point;
}
};
customButton.addPaintListener(e -> {
Rectangle clientArea = customButton.getClientArea();
Point stringExtent = e.gc.stringExtent(buttonText);
int x = clientArea.x + (clientArea.width - stringExtent.x) / 2;
int y = clientArea.y + (clientArea.height - stringExtent.y) / 2;
e.gc.drawString(buttonText, x, y);
});
customButton.addMouseListener(MouseListener.mouseDownAdapter(e -> {
int actionIndex = (e.button == 1) ? 0 : 1;
customButtonAction(actionIndex);
}));
customButton.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int modifierKeys = e.stateMask & SWT.MODIFIER_MASK;
if (modifierKeys == SWT.CTRL || modifierKeys == 0) {
if (e.character == '1')
customButtonAction(0);
else if (e.character == '2')
customButtonAction(1);
}
}
});
Accessible accessible = customButton.getAccessible();
accessible.addAccessibleListener(new AccessibleAdapter() {
@Override
public void getName(AccessibleEvent e) {
e.result = buttonText;
}
@Override
public void getKeyboardShortcut(AccessibleEvent e) {
// default action is 'action 1'
e.result = "CTRL+1";
}
});
accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
@Override
public void getRole(AccessibleControlEvent e) {
e.detail = ACC.ROLE_PUSHBUTTON;
}
});
accessible.addAccessibleActionListener(new AccessibleActionAdapter() {
@Override
public void getActionCount(AccessibleActionEvent e) {
e.count = 2;
}
@Override
public void getName(AccessibleActionEvent e) {
if (0 <= e.index && e.index <= 1) {
if (e.localized) {
e.result = AccessibleActionExample.getResourceString("action" + e.index);
} else {
// $NON-NLS-1$
e.result = "Action" + e.index;
}
}
}
@Override
public void getDescription(AccessibleActionEvent e) {
if (0 <= e.index && e.index <= 1) {
e.result = AccessibleActionExample.getResourceString("action" + e.index + "description");
}
}
@Override
public void doAction(AccessibleActionEvent e) {
if (0 <= e.index && e.index <= 1) {
customButtonAction(e.index);
e.result = ACC.OK;
}
}
@Override
public void getKeyBinding(AccessibleActionEvent e) {
switch(e.index) {
case 0:
e.result = "1;CTRL+1";
break;
case 1:
e.result = "2;CTRL+2";
break;
default:
e.result = null;
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
use of org.eclipse.swt.events.KeyAdapter in project knime-core by knime.
the class StyledTextEditor method createStyledText.
private Control createStyledText(final Composite parent) {
m_styledText = new StyledText(parent, SWT.MULTI | SWT.WRAP | SWT.FULL_SELECTION);
// by default we are a workflow annotation editor
// can be changed by changing the default font (setDefaultFont(Font))
m_styledText.setFont(AnnotationEditPart.getWorkflowAnnotationDefaultFont());
m_styledText.setAlignment(SWT.LEFT);
m_styledText.setText("");
m_styledText.setTabs(TAB_SIZE);
m_styledText.addVerifyKeyListener(new VerifyKeyListener() {
@Override
public void verifyKey(final VerifyEvent event) {
if (event.character == SWT.CR && (event.stateMask & SWT.MOD1) != 0) {
event.doit = false;
}
}
});
// forward some events to the cell editor
m_styledText.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(final KeyEvent e) {
keyReleaseOccured(e);
}
});
m_styledText.addFocusListener(new FocusAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void focusLost(final org.eclipse.swt.events.FocusEvent e) {
// effect of an opening font editor, for instance)
if (m_allowFocusLost.get()) {
lostFocus();
}
}
});
m_styledText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(final ModifyEvent e) {
// super marks it dirty (otherwise no commit at the end)
fireEditorValueChanged(true, true);
}
});
m_styledText.addExtendedModifyListener(new ExtendedModifyListener() {
@Override
public void modifyText(final ExtendedModifyEvent event) {
if (event.length > 0) {
textInserted(event.start, event.length);
}
}
});
m_styledText.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent e) {
selectionChanged();
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
selectionChanged();
}
});
m_styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
addMenu(m_styledText);
// toolbar gets created first - enable its style buttons!
selectionChanged();
return m_styledText;
}
use of org.eclipse.swt.events.KeyAdapter in project yamcs-studio by yamcs.
the class NativeTextEditpart method doCreateFigure.
@Override
protected IFigure doCreateFigure() {
initFields();
int style = SWT.NONE;
NativeTextModel model = getWidgetModel();
if (model.isShowNativeBorder())
style |= SWT.BORDER;
if (model.isMultilineInput()) {
style |= SWT.MULTI;
if (model.isShowHScroll())
style |= SWT.H_SCROLL;
if (model.isShowVScroll())
style |= SWT.V_SCROLL;
if (model.isWrapWords())
style |= SWT.WRAP;
} else {
style |= SWT.SINGLE;
if (model.isPasswordInput())
style |= SWT.PASSWORD;
}
if (model.isReadOnly())
style |= SWT.READ_ONLY;
switch(model.getHorizontalAlignment()) {
case CENTER:
style |= SWT.CENTER;
break;
case LEFT:
style |= SWT.LEFT;
break;
case RIGHT:
style |= SWT.RIGHT;
default:
break;
}
final NativeTextFigure figure = new NativeTextFigure(this, style);
text = figure.getSWTWidget();
if (!model.isReadOnly()) {
if (model.isMultilineInput()) {
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent) {
if (keyEvent.character == '\r') {
// Return key
if (text != null && !text.isDisposed() && (text.getStyle() & SWT.MULTI) != 0) {
if ((keyEvent.stateMask & SWT.CTRL) != 0) {
outputText(text.getText());
keyEvent.doit = false;
text.getShell().setFocus();
}
}
}
}
});
} else {
text.addListener(SWT.DefaultSelection, new Listener() {
@Override
public void handleEvent(Event e) {
outputText(text.getText());
switch(getWidgetModel().getFocusTraverse()) {
case LOSE:
text.getShell().setFocus();
break;
case NEXT:
SingleSourceHelper.swtControlTraverse(text, SWT.TRAVERSE_TAB_PREVIOUS);
break;
case PREVIOUS:
SingleSourceHelper.swtControlTraverse(text, SWT.TRAVERSE_TAB_NEXT);
break;
case KEEP:
default:
break;
}
}
});
}
// Recover text if editing aborted.
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent) {
if (keyEvent.character == SWT.ESC) {
text.setText(getWidgetModel().getText());
}
}
});
text.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (getPV() != null)
text.setText(getWidgetModel().getText());
else if (figure.isEnabled())
outputText(text.getText());
}
});
}
getPVWidgetEditpartDelegate().setUpdateSuppressTime(-1);
updatePropSheet();
return figure;
}
use of org.eclipse.swt.events.KeyAdapter in project yamcs-studio by yamcs.
the class NativeTextEditpartDelegate method doCreateFigure.
@Override
public IFigure doCreateFigure() {
int style = SWT.NONE;
if (model.isShowNativeBorder())
style |= SWT.BORDER;
if (model.isMultilineInput()) {
style |= SWT.MULTI;
if (model.isShowHScroll())
style |= SWT.H_SCROLL;
if (model.isShowVScroll())
style |= SWT.V_SCROLL;
if (model.isWrapWords())
style |= SWT.WRAP;
} else {
style |= SWT.SINGLE;
if (model.isPasswordInput())
style |= SWT.PASSWORD;
}
if (model.isReadOnly())
style |= SWT.READ_ONLY;
switch(model.getHorizontalAlignment()) {
case CENTER:
style |= SWT.CENTER;
break;
case LEFT:
style |= SWT.LEFT;
break;
case RIGHT:
style |= SWT.RIGHT;
default:
break;
}
final NativeTextFigure figure = new NativeTextFigure(editpart, style);
text = figure.getSWTWidget();
if (!model.isReadOnly()) {
if (model.isMultilineInput()) {
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent) {
if (keyEvent.character == '\r') {
// Return key
if (text != null && !text.isDisposed() && (text.getStyle() & SWT.MULTI) != 0) {
if ((keyEvent.stateMask & SWT.CTRL) != 0) {
outputText(text.getText());
keyEvent.doit = false;
text.getShell().setFocus();
}
}
}
}
});
} else {
text.addListener(SWT.DefaultSelection, new Listener() {
@Override
public void handleEvent(Event e) {
outputText(text.getText());
switch(model.getFocusTraverse()) {
case LOSE:
text.getShell().setFocus();
break;
case NEXT:
SingleSourceHelper.swtControlTraverse(text, SWT.TRAVERSE_TAB_PREVIOUS);
break;
case PREVIOUS:
SingleSourceHelper.swtControlTraverse(text, SWT.TRAVERSE_TAB_NEXT);
break;
case KEEP:
default:
break;
}
}
});
text.addTraverseListener(e -> {
// if key code is not tab, ignore
if (e.character != '\t' || skipTraverse)
return;
e.doit = false;
skipTraverse = true;
if (e.stateMask == 0) {
SingleSourceHelper.swtControlTraverse(text, SWT.TRAVERSE_TAB_PREVIOUS);
} else {
SingleSourceHelper.swtControlTraverse(text, SWT.TRAVERSE_TAB_NEXT);
}
skipTraverse = false;
});
}
// Recover text if editing aborted.
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent) {
if (keyEvent.character == SWT.ESC) {
text.setText(model.getText());
}
}
});
text.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (editpart.getPV() != null)
text.setText(model.getText());
else if (figure.isEnabled())
outputText(text.getText());
}
});
}
return figure;
}
Aggregations