public void positionWindow(boolean resetToDefault) { String mapWindow; mapShellBounds = mapShell.getBounds(); mapWindow = prefs.get(RadarConsts.PREF_KEY_MAP_WINDOW, ""); if (mapWindow.equals("") || resetToDefault) { Rectangle dispBounds, tableBounds; dispBounds = ((Display.getCurrent()).getPrimaryMonitor()).getClientArea(); tableBounds = display.getShells()[1].getBounds(); mapShellBounds.width = dispBounds.width - tableBounds.width; mapShellBounds.height = dispBounds.height; mapShellBounds.x = dispBounds.x + tableBounds.width; mapShellBounds.y = dispBounds.y; } else { String[] tokens = mapWindow.split(","); mapShellBounds = new Rectangle( new Integer(tokens[0]), // X value new Integer(tokens[1]), // Y value new Integer(tokens[2]), // Width new Integer(tokens[3]) // Height ); } mapShell.setBounds(mapShellBounds); }
public static int getColumnAtPos(TreeItem item, int x, int y) { int columnCount = item.getParent().getColumnCount(); for (int i = 0; i < columnCount; i++) { Rectangle rect = item.getBounds(i); if (rect.contains(x, y)) { return i; } } return -1; }
public static void packColumns(@NotNull Tree tree, boolean fit, @Nullable float[] ratios) { tree.setRedraw(false); try { // Check for disposed items // TODO: it looks like SWT error. Sometimes tree items are disposed and NPE is thrown from // column.pack for (TreeItem item : tree.getItems()) { if (item.isDisposed()) { return; } } int totalWidth = 0; final TreeColumn[] columns = tree.getColumns(); for (TreeColumn column : columns) { column.pack(); totalWidth += column.getWidth(); } Rectangle clientArea = tree.getClientArea(); if (clientArea.isEmpty()) { return; } if (fit) { int areaWidth = clientArea.width; if (tree.getVerticalBar() != null) { areaWidth -= tree.getVerticalBar().getSize().x; } if (totalWidth > areaWidth) { int extraSpace = totalWidth - areaWidth; for (TreeColumn tc : columns) { double ratio = (double) tc.getWidth() / totalWidth; tc.setWidth((int) (tc.getWidth() - extraSpace * ratio)); } } else if (totalWidth < areaWidth) { float extraSpace = areaWidth - totalWidth; if (columns.length > 0) { if (ratios == null || ratios.length < columns.length) { extraSpace /= columns.length; extraSpace--; for (TreeColumn tc : columns) { tc.setWidth((int) (tc.getWidth() + extraSpace)); } } else { for (int i = 0; i < columns.length; i++) { TreeColumn tc = columns[i]; tc.setWidth((int) (tc.getWidth() + extraSpace * ratios[i])); } } } } } } finally { tree.setRedraw(true); } }
void tableMouseDown(Event event) { if (isDisposed() || !isVisible()) return; Point pt = new Point(event.x, event.y); int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0; TableItem item = table.getItem(pt); if ((table.getStyle() & SWT.FULL_SELECTION) != 0) { if (item == null) return; } else { int start = item != null ? table.indexOf(item) : table.getTopIndex(); int end = table.getItemCount(); Rectangle clientRect = table.getClientArea(); for (int i = start; i < end; i++) { TableItem nextItem = table.getItem(i); Rectangle rect = nextItem.getBounds(0); if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) { item = nextItem; break; } if (rect.y > clientRect.y + clientRect.height) return; } if (item == null) return; } TableColumn newColumn = null; int columnCount = table.getColumnCount(); if (columnCount == 0) { if ((table.getStyle() & SWT.FULL_SELECTION) == 0) { Rectangle rect = item.getBounds(0); rect.width += lineWidth; rect.height += lineWidth; if (!rect.contains(pt)) return; } } else { for (int i = 0; i < columnCount; i++) { Rectangle rect = item.getBounds(i); rect.width += lineWidth; rect.height += lineWidth; if (rect.contains(pt)) { newColumn = table.getColumn(i); break; } } if (newColumn == null) { if ((table.getStyle() & SWT.FULL_SELECTION) == 0) return; newColumn = table.getColumn(0); } } setRowColumn(item, newColumn, true); setFocus(); return; }
public static void main(String[] args) { final Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); final Shell shell = new Shell(display, SWT.NO_TRIM); Region region = new Region(); final ImageData imageData = image.getImageData(); if (imageData.alphaData != null) { Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < imageData.height; y++) { for (int x = 0; x < imageData.width; x++) { if (imageData.getAlpha(x, y) == 255) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } else { ImageData mask = imageData.getTransparencyMask(); Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < mask.height; y++) { for (int x = 0; x < mask.width; x++) { if (mask.getPixel(x, y) != 0) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } shell.setRegion(region); Listener l = new Listener() { /** The x/y of the MouseDown, relative to top-left of the shell. */ int startX, startY; @Override public void handleEvent(Event e) { if (e.type == SWT.KeyDown && e.character == SWT.ESC) { shell.dispose(); } if (e.type == SWT.MouseDown && e.button == 1) { Point p = shell.toDisplay(e.x, e.y); Point loc = shell.getLocation(); startX = p.x - loc.x; startY = p.y - loc.y; } if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) { Point p = shell.toDisplay(e.x, e.y); p.x -= startX; p.y -= startY; shell.setLocation(p); } if (e.type == SWT.Paint) { e.gc.drawImage(image, imageData.x, imageData.y); } } }; shell.addListener(SWT.KeyDown, l); shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseMove, l); shell.addListener(SWT.Paint, l); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); image.dispose(); display.dispose(); }
void keyDown(Event event) { if (row == null) return; switch (event.character) { case SWT.CR: notifyListeners(SWT.DefaultSelection, new Event()); return; } int rowIndex = table.indexOf(row); int columnIndex = column == null ? 0 : table.indexOf(column); switch (event.keyCode) { case SWT.ARROW_UP: setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true); break; case SWT.ARROW_DOWN: setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true); break; case SWT.ARROW_LEFT: case SWT.ARROW_RIGHT: { int columnCount = table.getColumnCount(); if (columnCount == 0) break; int[] order = table.getColumnOrder(); int index = 0; while (index < order.length) { if (order[index] == columnIndex) break; index++; } if (index == order.length) index = 0; int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT; if (event.keyCode == leadKey) { setRowColumn(rowIndex, order[Math.max(0, index - 1)], true); } else { setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true); } break; } case SWT.HOME: setRowColumn(0, columnIndex, true); break; case SWT.END: { int i = table.getItemCount() - 1; setRowColumn(i, columnIndex, true); break; } case SWT.PAGE_UP: { int index = table.getTopIndex(); if (index == rowIndex) { Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); index = Math.max(0, index - page + 1); } setRowColumn(index, columnIndex, true); break; } case SWT.PAGE_DOWN: { int index = table.getTopIndex(); Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); int end = table.getItemCount() - 1; index = Math.min(end, index + page - 1); if (index == rowIndex) { index = Math.min(end, index + page - 1); } setRowColumn(index, columnIndex, true); break; } } }