List of usage examples for org.eclipse.swt.widgets ScrollBar setSelection
public void setSelection(int selection)
From source file:org.eclipse.swt.examples.clipboard.ClipboardExample.java
void createImageTransfer(Composite copyParent, Composite pasteParent) { final Image[] copyImage = new Image[] { null }; Label l = new Label(copyParent, SWT.NONE); l.setText("ImageTransfer:"); //$NON-NLS-1$ GridData data = new GridData(); data.verticalSpan = 2;/*from w ww . j av a 2s. co m*/ l.setLayoutData(data); final Canvas copyImageCanvas = new Canvas(copyParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_BOTH); data.verticalSpan = 2; data.widthHint = HSIZE; data.heightHint = VSIZE; copyImageCanvas.setLayoutData(data); final Point copyOrigin = new Point(0, 0); final ScrollBar copyHBar = copyImageCanvas.getHorizontalBar(); copyHBar.setEnabled(false); copyHBar.addListener(SWT.Selection, e -> { if (copyImage[0] != null) { int hSelection = copyHBar.getSelection(); int destX = -hSelection - copyOrigin.x; Rectangle rect = copyImage[0].getBounds(); copyImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); copyOrigin.x = -hSelection; } }); final ScrollBar copyVBar = copyImageCanvas.getVerticalBar(); copyVBar.setEnabled(false); copyVBar.addListener(SWT.Selection, e -> { if (copyImage[0] != null) { int vSelection = copyVBar.getSelection(); int destY = -vSelection - copyOrigin.y; Rectangle rect = copyImage[0].getBounds(); copyImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); copyOrigin.y = -vSelection; } }); copyImageCanvas.addListener(SWT.Paint, e -> { if (copyImage[0] != null) { GC gc = e.gc; gc.drawImage(copyImage[0], copyOrigin.x, copyOrigin.y); Rectangle rect = copyImage[0].getBounds(); Rectangle client = copyImageCanvas.getClientArea(); int marginWidth = client.width - rect.width; if (marginWidth > 0) { gc.fillRectangle(rect.width, 0, marginWidth, client.height); } int marginHeight = client.height - rect.height; if (marginHeight > 0) { gc.fillRectangle(0, rect.height, client.width, marginHeight); } gc.dispose(); } }); Button openButton = new Button(copyParent, SWT.PUSH); openButton.setText("Open Image"); openButton.addSelectionListener(widgetSelectedAdapter(e -> { FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { if (copyImage[0] != null) { System.out.println("CopyImage"); copyImage[0].dispose(); } copyImage[0] = new Image(e.display, string); copyVBar.setEnabled(true); copyHBar.setEnabled(true); copyOrigin.x = 0; copyOrigin.y = 0; Rectangle rect = copyImage[0].getBounds(); Rectangle client = copyImageCanvas.getClientArea(); copyHBar.setMaximum(rect.width); copyVBar.setMaximum(rect.height); copyHBar.setThumb(Math.min(rect.width, client.width)); copyVBar.setThumb(Math.min(rect.height, client.height)); copyImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true); copyVBar.setSelection(0); copyHBar.setSelection(0); copyImageCanvas.redraw(); } })); Button b = new Button(copyParent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener(widgetSelectedAdapter(e -> { if (copyImage[0] != null) { status.setText(""); // Fetch ImageData at current zoom and save in the clip-board. clipboard.setContents(new Object[] { copyImage[0].getImageDataAtCurrentZoom() }, new Transfer[] { ImageTransfer.getInstance() }); } else { status.setText("No image to copy"); } })); final Image[] pasteImage = new Image[] { null }; l = new Label(pasteParent, SWT.NONE); l.setText("ImageTransfer:"); final Canvas pasteImageCanvas = new Canvas(pasteParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_BOTH); data.widthHint = HSIZE; data.heightHint = VSIZE; pasteImageCanvas.setLayoutData(data); final Point pasteOrigin = new Point(0, 0); final ScrollBar pasteHBar = pasteImageCanvas.getHorizontalBar(); pasteHBar.setEnabled(false); pasteHBar.addListener(SWT.Selection, e -> { if (pasteImage[0] != null) { int hSelection = pasteHBar.getSelection(); int destX = -hSelection - pasteOrigin.x; Rectangle rect = pasteImage[0].getBounds(); pasteImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false); pasteOrigin.x = -hSelection; } }); final ScrollBar pasteVBar = pasteImageCanvas.getVerticalBar(); pasteVBar.setEnabled(false); pasteVBar.addListener(SWT.Selection, e -> { if (pasteImage[0] != null) { int vSelection = pasteVBar.getSelection(); int destY = -vSelection - pasteOrigin.y; Rectangle rect = pasteImage[0].getBounds(); pasteImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false); pasteOrigin.y = -vSelection; } }); pasteImageCanvas.addListener(SWT.Paint, e -> { if (pasteImage[0] != null) { GC gc = e.gc; gc.drawImage(pasteImage[0], pasteOrigin.x, pasteOrigin.y); Rectangle rect = pasteImage[0].getBounds(); Rectangle client = pasteImageCanvas.getClientArea(); int marginWidth = client.width - rect.width; if (marginWidth > 0) { gc.fillRectangle(rect.width, 0, marginWidth, client.height); } int marginHeight = client.height - rect.height; if (marginHeight > 0) { gc.fillRectangle(0, rect.height, client.width, marginHeight); } } }); b = new Button(pasteParent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener(widgetSelectedAdapter(e -> { ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance()); if (imageData != null) { if (pasteImage[0] != null) { System.out.println("PasteImage"); pasteImage[0].dispose(); } status.setText(""); // Consume the ImageData at current zoom as-is. pasteImage[0] = new Image(e.display, new AutoScaleImageDataProvider(imageData)); pasteVBar.setEnabled(true); pasteHBar.setEnabled(true); pasteOrigin.x = 0; pasteOrigin.y = 0; Rectangle rect = pasteImage[0].getBounds(); Rectangle client = pasteImageCanvas.getClientArea(); pasteHBar.setMaximum(rect.width); pasteVBar.setMaximum(rect.height); pasteHBar.setThumb(Math.min(rect.width, client.width)); pasteVBar.setThumb(Math.min(rect.height, client.height)); pasteImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true); pasteVBar.setSelection(0); pasteHBar.setSelection(0); pasteImageCanvas.redraw(); } else { status.setText("No image to paste"); } })); }
From source file:PaintExample.java
/** * Handles resize events//from www . j a v a 2 s . c om */ private void handleResize() { paintCanvas.update(); Rectangle visibleRect = paintCanvas.getClientArea(); visibleWidth = visibleRect.width; visibleHeight = visibleRect.height; ScrollBar horizontal = paintCanvas.getHorizontalBar(); if (horizontal != null) { displayFDC.xOffset = Math.min(horizontal.getSelection(), imageWidth - visibleWidth); if (imageWidth <= visibleWidth) { horizontal.setEnabled(false); horizontal.setSelection(0); } else { horizontal.setEnabled(true); horizontal.setValues(displayFDC.xOffset, 0, imageWidth, visibleWidth, 8, visibleWidth); } } ScrollBar vertical = paintCanvas.getVerticalBar(); if (vertical != null) { displayFDC.yOffset = Math.min(vertical.getSelection(), imageHeight - visibleHeight); if (imageHeight <= visibleHeight) { vertical.setEnabled(false); vertical.setSelection(0); } else { vertical.setEnabled(true); vertical.setValues(displayFDC.yOffset, 0, imageHeight, visibleHeight, 8, visibleHeight); } } }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
void onArrowLeft(int stateMask) { if (horizontalOffset == 0) return;/*from w w w. java 2 s .co m*/ int newSelection = Math.max(0, horizontalOffset - SIZE_HORIZONTALSCROLL); update(); GC gc = new GC(this); gc.copyArea(0, 0, clientArea.width, clientArea.height, horizontalOffset - newSelection, 0); gc.dispose(); if (header.getVisible()) { header.update(); Rectangle headerClientArea = header.getClientArea(); gc = new GC(header); gc.copyArea(0, 0, headerClientArea.width, headerClientArea.height, horizontalOffset - newSelection, 0); gc.dispose(); } horizontalOffset = newSelection; ScrollBar hBar = getHorizontalBar(); if (hBar != null) hBar.setSelection(horizontalOffset); }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
/** * Sets the zero-relative index of the item which is currently * at the top of the receiver. This index can change when items * are scrolled or new items are added and removed. * * @param index the index of the top item * * @exception SWTException <ul>/*from w w w . j a v a 2 s .co m*/ * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */ public void setTopIndex(int index) { checkWidget(); if (!(0 <= index && index < itemsCount)) return; int visibleItemCount = (clientArea.height - getHeaderHeight()) / itemHeight; if (itemsCount <= visibleItemCount) return; index = Math.min(index, itemsCount - visibleItemCount); if (index == topIndex) return; update(); int change = topIndex - index; topIndex = index; ScrollBar vBar = getVerticalBar(); if (vBar != null) vBar.setSelection(topIndex); if (drawCount <= 0) { GC gc = new GC(this); gc.copyArea(0, 0, clientArea.width, clientArea.height, 0, change * itemHeight); gc.dispose(); } }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
/** * Shows the column. If the column is already showing in the receiver, * this method simply returns. Otherwise, the columns are scrolled until * the column is visible./*from w w w .j a v a 2s. c o m*/ * * @param column the column to be shown * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the column is null</li> * <li>ERROR_INVALID_ARGUMENT - if the column has been disposed</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @since 3.0 */ public void showColumn(CTableColumn column) { checkWidget(); if (column == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (column.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); if (column.parent != this) return; int x = column.getX(); int rightX = x + column.width; if (0 <= x && rightX <= clientArea.width) return; /* column is fully visible */ headerHideToolTip(); int absX = 0; /* the X of the column irrespective of the horizontal scroll */ CTableColumn[] orderedColumns = getOrderedColumns(); for (int i = 0; i < column.getOrderIndex(); i++) { absX += orderedColumns[i].width; } if (x < clientArea.x) { /* column is to left of viewport */ horizontalOffset = absX; } else { horizontalOffset = absX + column.width - clientArea.width; } ScrollBar hBar = getHorizontalBar(); if (hBar != null) hBar.setSelection(horizontalOffset); redraw(); if (drawCount <= 0 && header.isVisible()) header.redraw(); }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
void createItem(CTableItem item) { int index = item.index; if (itemsCount == items.length) { int grow = drawCount <= 0 ? 4 : Math.max(4, items.length * 3 / 2); CTableItem[] newItems = new CTableItem[items.length + grow]; System.arraycopy(items, 0, newItems, 0, items.length); items = newItems;// www . j a va2 s . co m } if (index != itemsCount) { /* new item is not at end of list, so shift other items right to create space for it */ System.arraycopy(items, index, items, index + 1, itemsCount - index); } items[index] = item; itemsCount++; /* update the index for items bumped down by this new item */ for (int i = index + 1; i < itemsCount; i++) { items[i].index = i; } /* Rows were added, so notify the accessible. */ int[] eventData = new int[5]; eventData[0] = ACC.INSERT; eventData[1] = index; eventData[2] = 1; eventData[3] = 0; eventData[4] = 0; getAccessible().sendEvent(ACC.EVENT_TABLE_CHANGED, eventData); /* update scrollbars */ updateVerticalBar(); Rectangle bounds = item.getBounds(false); int rightX = bounds.x + bounds.width; updateHorizontalBar(rightX, rightX); /* * If new item is above viewport then adjust topIndex and the vertical * scrollbar so that the current viewport items will not change. */ if (item.index < topIndex) { topIndex++; ScrollBar vBar = getVerticalBar(); if (vBar != null) vBar.setSelection(topIndex); return; } /* * If this is the first item and the receiver has focus then its boundary * focus ring must be removed. */ if (itemsCount == 1 && isFocusControl()) { focusItem = item; redraw(); return; } if (item.isInViewport()) { redrawFromItemDownwards(index); } }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
void destroyItem(CTableItem item) { if (item == focusItem) reassignFocus();//from ww w. ja v a 2 s . c om int index = item.index; Rectangle bounds = item.getBounds(false); int rightX = bounds.x + bounds.width; if (index != itemsCount - 1) { /* item is not at end of items list, so must shift items left to reclaim its slot */ System.arraycopy(items, index + 1, items, index, itemsCount - index - 1); items[itemsCount - 1] = null; } else { items[index] = null; /* last item, so no array copy needed */ } itemsCount--; if (drawCount <= 0 && items.length - itemsCount == 4) { /* shrink the items array */ CTableItem[] newItems = new CTableItem[itemsCount]; System.arraycopy(items, 0, newItems, 0, newItems.length); items = newItems; } /* update the index on affected items */ for (int i = index; i < itemsCount; i++) { items[i].index = i; } item.index = -1; int oldTopIndex = topIndex; updateVerticalBar(); updateHorizontalBar(0, -rightX); /* * If destroyed item is above viewport then adjust topIndex and the vertical * scrollbar so that the current viewport items will not change. */ if (index < topIndex) { topIndex = oldTopIndex - 1; ScrollBar vBar = getVerticalBar(); if (vBar != null) vBar.setSelection(topIndex); } /* selectedItems array */ if (item.isSelected()) { int selectionIndex = getSelectionIndex(item); CTableItem[] newSelectedItems = new CTableItem[selectedItems.length - 1]; System.arraycopy(selectedItems, 0, newSelectedItems, 0, selectionIndex); System.arraycopy(selectedItems, selectionIndex + 1, newSelectedItems, selectionIndex, newSelectedItems.length - selectionIndex); selectedItems = newSelectedItems; } if (item == anchorItem) anchorItem = null; if (item == lastClickedItem) lastClickedItem = null; /* * If this was the last item and the receiver has focus then its boundary * focus ring must be redrawn. */ if (itemsCount == 0 && isFocusControl()) { redraw(); } int[] eventData = new int[5]; eventData[0] = ACC.DELETE; eventData[1] = index; eventData[2] = 1; eventData[3] = 0; eventData[4] = 0; getAccessible().sendEvent(ACC.EVENT_TABLE_CHANGED, eventData); }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
void onArrowDown(int stateMask) { if ((stateMask & (SWT.SHIFT | SWT.CTRL)) == 0) { /* Down Arrow with no modifiers */ int newFocusIndex = focusItem.index + 1; if (newFocusIndex == itemsCount) return; /* at bottom */ selectItem(items[newFocusIndex], false); setFocusItem(items[newFocusIndex], true); redrawItem(newFocusIndex, true); showItem(items[newFocusIndex]);//from w w w . java2s . c om Event newEvent = new Event(); newEvent.item = items[newFocusIndex]; notifyListeners(SWT.Selection, newEvent); return; } if ((getStyle() & SWT.SINGLE) != 0) { if ((stateMask & SWT.CTRL) != 0) { /* CTRL+Down Arrow, CTRL+Shift+Down Arrow */ int visibleItemCount = (clientArea.height - getHeaderHeight()) / itemHeight; if (itemsCount <= topIndex + visibleItemCount) return; /* at bottom */ update(); topIndex++; ScrollBar vBar = getVerticalBar(); if (vBar != null) vBar.setSelection(topIndex); GC gc = new GC(this); gc.copyArea(0, 0, clientArea.width, clientArea.height, 0, -itemHeight); gc.dispose(); return; } /* Shift+Down Arrow */ int newFocusIndex = focusItem.index + 1; if (newFocusIndex == itemsCount) return; /* at bottom */ selectItem(items[newFocusIndex], false); setFocusItem(items[newFocusIndex], true); redrawItem(newFocusIndex, true); showItem(items[newFocusIndex]); Event newEvent = new Event(); newEvent.item = items[newFocusIndex]; notifyListeners(SWT.Selection, newEvent); return; } /* SWT.MULTI */ if ((stateMask & SWT.CTRL) != 0) { if ((stateMask & SWT.SHIFT) != 0) { /* CTRL+Shift+Down Arrow */ int visibleItemCount = (clientArea.height - getHeaderHeight()) / itemHeight; if (itemsCount <= topIndex + visibleItemCount) return; /* at bottom */ update(); topIndex++; ScrollBar vBar = getVerticalBar(); if (vBar != null) vBar.setSelection(topIndex); GC gc = new GC(this); gc.copyArea(0, 0, clientArea.width, clientArea.height, 0, -itemHeight); gc.dispose(); return; } /* CTRL+Down Arrow */ int focusIndex = focusItem.index; if (focusIndex == itemsCount - 1) return; /* at bottom */ CTableItem newFocusItem = items[focusIndex + 1]; setFocusItem(newFocusItem, true); redrawItem(newFocusItem.index, true); showItem(newFocusItem); return; } /* Shift+Down Arrow */ int newFocusIndex = focusItem.index + 1; if (newFocusIndex == itemsCount) return; /* at bottom */ if (anchorItem == null) anchorItem = focusItem; if (focusItem.index < anchorItem.index) { deselectItem(focusItem); redrawItem(focusItem.index, true); } selectItem(items[newFocusIndex], true); setFocusItem(items[newFocusIndex], true); redrawItem(newFocusIndex, true); showItem(items[newFocusIndex]); Event newEvent = new Event(); newEvent.item = items[newFocusIndex]; notifyListeners(SWT.Selection, newEvent); }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
void onArrowUp(int stateMask) { if ((stateMask & (SWT.SHIFT | SWT.CTRL)) == 0) { /* Up Arrow with no modifiers */ int newFocusIndex = focusItem.index - 1; if (newFocusIndex < 0) return; /* at top */ CTableItem item = items[newFocusIndex]; selectItem(item, false);/* w w w . j a v a 2s .c om*/ setFocusItem(item, true); redrawItem(newFocusIndex, true); showItem(item); Event newEvent = new Event(); newEvent.item = item; notifyListeners(SWT.Selection, newEvent); return; } if ((getStyle() & SWT.SINGLE) != 0) { if ((stateMask & SWT.CTRL) != 0) { /* CTRL+Up Arrow, CTRL+Shift+Up Arrow */ if (topIndex == 0) return; /* at top */ update(); topIndex--; ScrollBar vBar = getVerticalBar(); if (vBar != null) vBar.setSelection(topIndex); GC gc = new GC(this); gc.copyArea(0, 0, clientArea.width, clientArea.height, 0, itemHeight); gc.dispose(); return; } /* Shift+Up Arrow */ int newFocusIndex = focusItem.index - 1; if (newFocusIndex < 0) return; /* at top */ CTableItem item = items[newFocusIndex]; selectItem(item, false); setFocusItem(item, true); redrawItem(newFocusIndex, true); showItem(item); Event newEvent = new Event(); newEvent.item = item; notifyListeners(SWT.Selection, newEvent); return; } /* SWT.MULTI */ if ((stateMask & SWT.CTRL) != 0) { if ((stateMask & SWT.SHIFT) != 0) { /* CTRL+Shift+Up Arrow */ if (topIndex == 0) return; /* at top */ update(); topIndex--; ScrollBar vBar = getVerticalBar(); if (vBar != null) vBar.setSelection(topIndex); GC gc = new GC(this); gc.copyArea(0, 0, clientArea.width, clientArea.height, 0, itemHeight); gc.dispose(); return; } /* CTRL+Up Arrow */ int focusIndex = focusItem.index; if (focusIndex == 0) return; /* at top */ CTableItem newFocusItem = items[focusIndex - 1]; setFocusItem(newFocusItem, true); showItem(newFocusItem); redrawItem(newFocusItem.index, true); return; } /* Shift+Up Arrow */ int newFocusIndex = focusItem.index - 1; if (newFocusIndex < 0) return; /* at top */ if (anchorItem == null) anchorItem = focusItem; if (anchorItem.index < focusItem.index) { deselectItem(focusItem); redrawItem(focusItem.index, true); } CTableItem item = items[newFocusIndex]; selectItem(item, true); setFocusItem(item, true); redrawItem(newFocusIndex, true); showItem(item); Event newEvent = new Event(); newEvent.item = item; notifyListeners(SWT.Selection, newEvent); }
From source file:org.eclipse.swt.examples.accessibility.CTable.java
void updateVerticalBar() { if (drawCount > 0) return;/*w ww . ja v a 2 s .co m*/ ScrollBar vBar = getVerticalBar(); if (vBar == null) return; int pageSize = (clientArea.height - getHeaderHeight()) / itemHeight; int maximum = Math.max(1, itemsCount); /* setting a value of 0 here is ignored */ if (maximum != vBar.getMaximum()) { vBar.setMaximum(maximum); } int thumb = Math.min(pageSize, maximum); if (thumb != vBar.getThumb()) { vBar.setThumb(thumb); vBar.setPageIncrement(thumb); } vBar.setVisible(pageSize < maximum); /* reclaim any space now left on the bottom */ if (maximum < topIndex + thumb) { topIndex = maximum - thumb; vBar.setSelection(topIndex); redraw(); } else { int selection = vBar.getSelection(); if (selection != topIndex) { topIndex = selection; redraw(); } } }