use of org.eclipse.swt.widgets.Listener in project knime-core by knime.
the class ExportPreferencesDialog method createFileSelection.
protected void createFileSelection(final Composite parent) {
Composite panel = new Composite(parent, SWT.FILL);
GridData fillBoth = new GridData(GridData.FILL_BOTH);
panel.setLayoutData(fillBoth);
panel.setLayout(new GridLayout(1, true));
Group border = new Group(panel, SWT.SHADOW_IN);
border.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
border.setLayout(new GridLayout(2, false));
border.setText("File to store preferences:");
final Text filenameUI = new Text(border, SWT.FILL | SWT.SINGLE | SWT.BORDER);
filenameUI.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
filenameUI.addListener(SWT.Modify, new Listener() {
@Override
public void handleEvent(final Event event) {
m_filename = filenameUI.getText().trim();
validate();
}
});
final Button browse = new Button(border, SWT.PUSH);
browse.setText("Select...");
browse.setToolTipText("Opens a file selection dialog.");
browse.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent se) {
FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
fileDialog.setFilterExtensions(new String[] { "*.epf", "*.*" });
fileDialog.setText("Specify the preferences export file.");
if (m_filename != null) {
fileDialog.setFileName(m_filename);
}
String filePath = fileDialog.open();
if (filePath != null && filePath.trim().length() > 0) {
if (filePath.length() < 5 || filePath.lastIndexOf('.') < filePath.length() - 4) {
// they have no extension - add .epf
filePath += ".epf";
}
m_filename = filePath;
filenameUI.setText(filePath);
validate();
}
}
@Override
public void widgetDefaultSelected(final SelectionEvent se) {
widgetSelected(se);
}
});
}
use of org.eclipse.swt.widgets.Listener in project eclipse.platform.swt by eclipse.
the class AnimatedGraphicsTab method createToolBar.
/**
* Creates the toolbar controls: play, pause and animation timer.
*
* @param parent A composite
*/
void createToolBar(final Composite parent) {
final Display display = parent.getDisplay();
toolBar = new ToolBar(parent, SWT.FLAT);
Listener toolBarListener = event -> {
switch(event.type) {
case SWT.Selection:
{
if (event.widget == playItem) {
animate = true;
playItem.setEnabled(!animate);
pauseItem.setEnabled(animate);
} else if (event.widget == pauseItem) {
animate = false;
playItem.setEnabled(!animate);
pauseItem.setEnabled(animate);
}
}
break;
}
};
// play tool item
playItem = new ToolItem(toolBar, SWT.PUSH);
// $NON-NLS-1$
playItem.setText(GraphicsExample.getResourceString("Play"));
// $NON-NLS-1$
playItem.setImage(example.loadImage(display, "play.gif"));
playItem.addListener(SWT.Selection, toolBarListener);
// pause tool item
pauseItem = new ToolItem(toolBar, SWT.PUSH);
// $NON-NLS-1$
pauseItem.setText(GraphicsExample.getResourceString("Pause"));
// $NON-NLS-1$
pauseItem.setImage(example.loadImage(display, "pause.gif"));
pauseItem.addListener(SWT.Selection, toolBarListener);
// timer spinner
Composite comp = new Composite(parent, SWT.NONE);
GridLayout gridLayout = new GridLayout(2, false);
comp.setLayout(gridLayout);
Label label = new Label(comp, SWT.CENTER);
// $NON-NLS-1$
label.setText(GraphicsExample.getResourceString("Animation"));
timerSpinner = new Spinner(comp, SWT.BORDER | SWT.WRAP);
timerSpinner.setMaximum(1000);
playItem.setEnabled(false);
animate = true;
timerSpinner.setSelection(getInitialAnimationTime());
}
use of org.eclipse.swt.widgets.Listener in project eclipse.platform.swt by eclipse.
the class MJ_Table method tableEditor_dynamically_created_Snippet124.
@Test
public void tableEditor_dynamically_created_Snippet124() {
Shell shell = mkShell("Verify you can edit cells");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setLinesVisible(true);
for (int i = 0; i < 3; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(100);
}
for (int i = 0; i < 3; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "" + i, "" + i, "" + i });
}
final TableEditor editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.addListener(SWT.MouseDown, event -> {
Rectangle clientArea = table.getClientArea();
Point pt = new Point(event.x, event.y);
int index = table.getTopIndex();
while (index < table.getItemCount()) {
boolean visible = false;
final TableItem item = table.getItem(index);
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
final int column = i;
final Text text = new Text(table, SWT.NONE);
Listener textListener = e -> {
switch(e.type) {
case SWT.FocusOut:
item.setText(column, text.getText());
text.dispose();
break;
case SWT.Traverse:
switch(e.detail) {
case SWT.TRAVERSE_RETURN:
item.setText(column, text.getText());
// FALL THROUGH
case SWT.TRAVERSE_ESCAPE:
text.dispose();
e.doit = false;
}
break;
}
};
text.addListener(SWT.FocusOut, textListener);
text.addListener(SWT.Traverse, textListener);
editor.setEditor(text, item, i);
text.setText(item.getText(i));
text.selectAll();
text.setFocus();
return;
}
if (!visible && rect.intersects(clientArea)) {
visible = true;
}
}
if (!visible)
return;
index++;
}
});
shell.setSize(SWIDTH, SHEIGHT);
shell.open();
mainLoop(shell);
}
use of org.eclipse.swt.widgets.Listener in project eclipse.platform.swt by eclipse.
the class MJ_Table method icons_ofDiffSize_inItems_Snippet349.
@Test
public void icons_ofDiffSize_inItems_Snippet349() {
Shell shell = mkShell("Verify that items have icons of different sizes and icons are not cut off");
BiFunction<Integer, Integer, Image> createImage = (width, height) -> {
Image result = new Image(display, width, height);
GC gc = new GC(result);
for (int x = -height; x < width; x += 4) {
gc.drawLine(x, 0, x + height, height);
}
gc.dispose();
return result;
};
final int COLUMN_COUNT = 3;
final int TEXT_MARGIN = 3;
final String KEY_WIDTHS = "widths";
final String KEY_IMAGES = "images";
Image[] images = new Image[4];
images[0] = createImage.apply(16, 16);
images[1] = createImage.apply(32, 16);
images[2] = createImage.apply(48, 16);
shell.setLayout(new FillLayout());
Table table = new Table(shell, SWT.NONE);
for (int i = 0; i < COLUMN_COUNT; i++) {
new TableColumn(table, SWT.NONE);
}
for (int i = 0; i < 8; i++) {
TableItem item = new TableItem(table, SWT.NONE);
Image[] itemImages = new Image[COLUMN_COUNT];
item.setData(KEY_IMAGES, itemImages);
for (int j = 0; j < COLUMN_COUNT; j++) {
item.setText(j, "item " + i + " col " + j);
itemImages[j] = images[(i * COLUMN_COUNT + j) % images.length];
}
}
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
final int itemHeight = table.getItemHeight();
GC gc = new GC(table);
FontMetrics metrics = gc.getFontMetrics();
final int fontHeight = metrics.getHeight();
gc.dispose();
Listener paintListener = event -> {
switch(event.type) {
case SWT.MeasureItem:
{
int column1 = event.index;
TableItem item1 = (TableItem) event.item;
Image[] images1 = (Image[]) item1.getData(KEY_IMAGES);
Image image1 = images1[column1];
if (image1 == null) {
/* don't change the native-calculated event.width */
break;
}
int[] cachedWidths = (int[]) item1.getData(KEY_WIDTHS);
if (cachedWidths == null) {
cachedWidths = new int[COLUMN_COUNT];
item1.setData(KEY_WIDTHS, cachedWidths);
}
if (cachedWidths[column1] == 0) {
int width = image1.getBounds().width + 2 * TEXT_MARGIN;
GC gc1 = new GC(item1.getParent());
width += gc1.stringExtent(item1.getText()).x;
gc1.dispose();
cachedWidths[column1] = width;
}
event.width = cachedWidths[column1];
break;
}
case SWT.EraseItem:
{
int column2 = event.index;
TableItem item2 = (TableItem) event.item;
Image[] images2 = (Image[]) item2.getData(KEY_IMAGES);
Image image2 = images2[column2];
if (image2 == null) {
break;
}
/* disable the native drawing of this item */
event.detail &= ~SWT.FOREGROUND;
break;
}
case SWT.PaintItem:
{
int column3 = event.index;
TableItem item3 = (TableItem) event.item;
Image[] images3 = (Image[]) item3.getData(KEY_IMAGES);
Image image3 = images3[column3];
if (image3 == null) {
/* this item is drawn natively, don't touch it*/
break;
}
int x = event.x;
event.gc.drawImage(image3, x, event.y + (itemHeight - image3.getBounds().height) / 2);
x += image3.getBounds().width + TEXT_MARGIN;
event.gc.drawString(item3.getText(column3), x, event.y + (itemHeight - fontHeight) / 2);
break;
}
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.EraseItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
for (int i = 0; i < COLUMN_COUNT; i++) {
table.getColumn(i).pack();
}
shell.addDisposeListener(e -> {
for (int i = 0; i < images.length; i++) {
if (images[i] != null) {
images[i].dispose();
}
}
});
shell.setSize(SWIDTH, SHEIGHT);
shell.open();
mainLoop(shell);
}
use of org.eclipse.swt.widgets.Listener in project eclipse.platform.swt by eclipse.
the class MJ_Table method ownerDrawn_multiColumn_gc_snippet239.
@Test
public void ownerDrawn_multiColumn_gc_snippet239() {
Shell shell = mkShell("Verify that text is correctly drawn across 2 columns. 4 columns in total.");
shell.setText("Text spans two columns in a TableItem");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
int columnCount = 4;
for (int i = 0; i < columnCount; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
System.out.println("Column " + i);
column.setText("Column " + i);
}
int itemCount = 8;
for (int i = 0; i < itemCount; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "item " + i + " a");
item.setText(3, "item " + i + " d");
}
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
final String string = "text that spans two columns";
GC gc = new GC(table);
final Point extent = gc.stringExtent(string);
gc.dispose();
final Color red = display.getSystemColor(SWT.COLOR_RED);
Listener paintListener = event -> {
switch(event.type) {
case SWT.MeasureItem:
{
if (event.index == 1 || event.index == 2) {
event.width = extent.x / 2;
event.height = Math.max(event.height, extent.y + 2);
}
break;
}
case SWT.PaintItem:
{
if (event.index == 1 || event.index == 2) {
int offset = 0;
if (event.index == 2) {
TableColumn column1 = table.getColumn(1);
offset = column1.getWidth();
}
event.gc.setForeground(red);
int y = event.y + (event.height - extent.y) / 2;
event.gc.drawString(string, event.x - offset, y, true);
}
break;
}
}
};
table.addListener(SWT.MeasureItem, paintListener);
table.addListener(SWT.PaintItem, paintListener);
for (int i = 0; i < columnCount; i++) {
table.getColumn(i).pack();
}
shell.setSize(SWIDTH, SHEIGHT);
shell.open();
mainLoop(shell);
}
Aggregations