public static TreeItem getTreeItem(Tree tree, Object data) { for (TreeItem item : tree.getItems()) { if (item.getData() == data) { return item; } } return null; }
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); } }
static TreeItem PreviousItem(Tree tree, TreeItem item) { if (item == null) return null; TreeItem childItem = item; TreeItem parentItem = childItem.getParentItem(); int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); if (index == 0) { return parentItem; } else { TreeItem nextItem = parentItem == null ? tree.getItem(index - 1) : parentItem.getItem(index - 1); int count = nextItem.getItemCount(); while (count > 0 && nextItem.getExpanded()) { nextItem = nextItem.getItem(count - 1); count = nextItem.getItemCount(); } return nextItem; } }
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Tree tree = new Tree(shell, SWT.FULL_SELECTION | SWT.BORDER); tree.setHeaderVisible(true); TreeColumn column0 = new TreeColumn(tree, SWT.LEFT); column0.setText("Column 0"); TreeColumn column1 = new TreeColumn(tree, SWT.LEFT); column1.setText("Column 1"); TreeColumn column2 = new TreeColumn(tree, SWT.LEFT); column2.setText("Column 2"); for (int i = 0; i < 9; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText("item " + i); item.setText(1, "column 1 - " + i); item.setText(2, "column 2 - " + i); for (int j = 0; j < 9; j++) { TreeItem subItem = new TreeItem(item, SWT.NONE); subItem.setText("item " + i + " " + j); subItem.setText(1, "column 1 - " + i + " " + j); subItem.setText(2, "column 2 - " + i + " " + j); for (int k = 0; k < 9; k++) { TreeItem subsubItem = new TreeItem(subItem, SWT.NONE); subsubItem.setText("item " + i + " " + j + " " + k); subsubItem.setText(1, "column 1 - " + i + " " + j + " " + k); subsubItem.setText(2, "column 2 - " + i + " " + j + " " + k); } } } column0.pack(); column1.pack(); column2.pack(); Heartbeat = new Runnable() { @Override public void run() { if (!Tracking || tree.isDisposed()) return; Point cursor = display.getCursorLocation(); cursor = display.map(null, tree, cursor); Scroll(tree, cursor.x, cursor.y); display.timerExec(ScrollSpeed, Heartbeat); } }; Listener listener = new Listener() { @Override public void handleEvent(Event event) { switch (event.type) { case SWT.MouseEnter: Tracking = true; display.timerExec(0, Heartbeat); break; case SWT.MouseExit: Tracking = false; break; } } }; tree.addListener(SWT.MouseEnter, listener); tree.addListener(SWT.MouseExit, listener); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
static TreeItem NextItem(Tree tree, TreeItem item) { if (item == null) return null; if (item.getExpanded()) { return item.getItem(0); } else { TreeItem childItem = item; TreeItem parentItem = childItem.getParentItem(); int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); int count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount(); while (true) { if (index + 1 < count) { return parentItem == null ? tree.getItem(index + 1) : parentItem.getItem(index + 1); } else { if (parentItem == null) { return null; } else { childItem = parentItem; parentItem = childItem.getParentItem(); index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount(); } } } } }