private void packTable() { for (int i = 0; i < titles.length; i++) { tree.getColumn(i).pack(); } if (tree.getColumn(titles.length - 1).getWidth() < MAX_WIDTH) { tree.getColumn(titles.length - 1).setWidth(MAX_WIDTH); } }
/** * Gets the column index corresponding to the given column. * * @param tree The tree * @param column The thread column * @return The column index */ private static int getColumnIndex(Tree tree, HeapColumn column) { for (int i = 0; i < tree.getColumnCount(); i++) { if (tree.getColumn(i).getText().equals(column.label)) { return i; } } return -1; }
/** * Gets the column index corresponding to the given column. * * @param column The heap column * @return The column index */ private int getColumnIndex(HeapColumn column) { Tree table = treeViewer.getTree(); for (int i = 0; i < table.getColumnCount(); i++) { if (table.getColumn(i).getText().equals(column.label)) { return i; } } return -1; }
/** * Gets the column index corresponding to the given column. * * @param column The attribute column * @return The column index */ private int getColumnIndex(PropertiesColumn column) { Tree tree = viewer.getTree(); for (int i = 0; i < tree.getColumnCount(); i++) { if (tree.getColumn(i).getText().equals(column.label)) { return i; } } return -1; }
public ContactTab(final Composite composite) { super(composite, 0); this.setLayout(new GridLayout(1, false)); Composite containerHeaderSession = new Composite(this, SWT.NONE); containerHeaderSession.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); containerHeaderSession.setLayout(new GridLayout(2, false)); final Combo comboSession = new Combo(containerHeaderSession, SWT.DROP_DOWN | SWT.READ_ONLY); comboSession.add("分类1"); comboSession.add("分类2"); comboSession.add("分类3"); comboSession.add("分类4"); comboSession.select(0); final Label labelHeader1 = new Label(containerHeaderSession, SWT.NONE); labelHeader1.setText("共1人"); tree = new Tree( this, /*SWT.CHECK | */ SWT.VIRTUAL | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL); tree.setLinesVisible(true); tree.setHeaderVisible(true); // tree.setRedraw(true); tree.setLayoutData(new GridData(GridData.FILL_BOTH)); for (int i = 0; i < titles.length; i++) { TreeColumn column = new TreeColumn(tree, SWT.NONE); column.setText(titles[i]); } tree.addTreeListener( new TreeAdapter() { @Override public void treeCollapsed(TreeEvent e) { super.treeCollapsed(e); expand(); } @Override public void treeExpanded(TreeEvent e) { super.treeExpanded(e); expand(); } private void expand() { // System.out.println("expand..."); isExpanding = true; Display.getDefault() .timerExec( 500, new Runnable() { public void run() { isExpanding = false; // System.out.println("isGroupClick: " + isGroupClick); } }); } }); tree.addMouseListener( new MouseAdapter() { @Override public void mouseDown(MouseEvent event) { super.mouseDown(event); } @Override public void mouseDoubleClick(MouseEvent event) { super.mouseDoubleClick(event); Point point = new Point(event.x, event.y); TreeItem item = tree.getItem(point); // System.out.println("Mouse double click: " + item); if (item != null && item.getData() != null && isExpanding == false) { ContactInfoDialog dialog = new ContactInfoDialog(getParent().getShell(), ContactInfoComposite.TYPE_EDIT); ContactInfo info = (ContactInfo) item.getData(); dialog.setData(info); if (dialog.open() == ContactInfoDialog.OK) { AppData.getInstance().getMainWin().refreshContactData(true); } } } }); tree.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent event) { if (event.detail == SWT.CHECK) { TreeItem item = (TreeItem) event.item; boolean checked = item.getChecked(); UIUtil.checkItems(item, checked); UIUtil.checkPath(item.getParentItem(), checked, false); } else { TreeItem item = (TreeItem) event.item; ContactInfo info = null; if (item != null && item.getData() != null) { info = (ContactInfo) item.getData(); } AppData.getInstance().getMainWin().showDetail(info); } } }); setupPopupMenu(); Composite containerHeaderEdit = new Composite(this, SWT.NONE); containerHeaderEdit.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); containerHeaderEdit.setLayout(new GridLayout(7, false)); final Label labelCatalog = new Label(containerHeaderEdit, SWT.PUSH); labelCatalog.setText("小类:"); final Combo comboCatalog = new Combo(containerHeaderEdit, SWT.DROP_DOWN | SWT.READ_ONLY); comboCatalog.add("小类1"); comboCatalog.add("小类2"); final Label labelSearch = new Label(containerHeaderEdit, SWT.PUSH); labelSearch.setText("联系人搜索:"); final Text textSearch = new Text(containerHeaderEdit, SWT.BORDER); textSearch.setLayoutData(new GridData(150, SWT.DEFAULT)); final Button buttonAdd = new Button(containerHeaderEdit, SWT.PUSH); buttonAdd.setText("新增..."); buttonAdd.setLayoutData(new GridData(100, SWT.DEFAULT)); buttonAdd.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { AppData.getInstance().getMainWin().addContact(); } }); final Button buttonImport = new Button(containerHeaderEdit, SWT.PUSH); buttonImport.setText("导入..."); buttonImport.setLayoutData(new GridData(100, SWT.DEFAULT)); final Button buttonExport = new Button(containerHeaderEdit, SWT.PUSH); buttonExport.setText("导出..."); buttonExport.setLayoutData(new GridData(100, SWT.DEFAULT)); if (!USE_PACK) { for (int i = 0; i < titles.length; i++) { if (i == 0) { tree.getColumn(i).setWidth(200); } else { tree.getColumn(i).setWidth(100); } } } if (USE_VIRTUAL) { tree.addListener( SWT.SetData, new Listener() { @Override public void handleEvent(Event event) { TreeItem item = (TreeItem) event.item; TreeItem parentItem = item.getParentItem(); String text = null; if (parentItem == null) { ContactGroupInfo groupInfo = groupInfoList.get(tree.indexOf(item)); text = groupInfo.getGroupName() + " (共" + groupInfo.getSize() + "人)"; item.setText(text); item.setItemCount(groupInfo.getSize()); item.setExpanded(true); } else { ContactGroupInfo groupInfo = groupInfoList.get(tree.indexOf(parentItem)); ContactInfo info = results.get(groupInfo.getIndex(parentItem.indexOf(item))); fillItem(item, info); } if (USE_PACK) { packTable(); } } }); } reloadData(false); }
public void createPartControl(Composite parent) { statusline = getViewSite().getActionBars().getStatusLineManager(); statusline.setMessage( Activator.getImageDescriptor("icons/info.png").createImage(), Messages.StatusLine_DropFilesHint); makeActions(); ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL); scrolledComposite.setExpandVertical(true); scrolledComposite.setExpandHorizontal(true); Composite content = new Composite(scrolledComposite, SWT.NONE); content.setLayout(new GridLayout(1, false)); scrolledComposite.setContent(content); toolBarManager = new ToolBarManager(); toolBarManager.add(encryptFilesAction); toolBarManager.add(newFolderAction); toolBarManager.add(new Separator("static")); toolBarManager.add(exportSelectionAction); toolBarManager.add(renameAction); toolBarManager.add(deleteSelectionAction); toolBarManager.add(new Separator("static")); toolBarManager.add(expandAllAction); toolBarManager.add(collapseAllAction); ToolBar toolbar = toolBarManager.createControl(content); toolbar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false)); // Create the tree viewer as a child of the composite parent treeViewer = new TreeViewer( content, SWT.FULL_SELECTION | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); // Anpassungen für TreeTable tree = treeViewer.getTree(); GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); treeViewer.getControl().setLayoutData(gridData); treeViewer.setUseHashlookup(true); /** * Tree table specific code starts ** */ tree.setHeaderVisible(true); tree.setLinesVisible(true); for (SteadyTableIdentifier identifier : SteadyTableIdentifier.values()) { new TreeColumn(tree, SWT.NONE).setText(Messages.getSteadyTableColumnTitle(identifier)); tree.getColumn(identifier.ordinal()).setWidth(identifier.columnWidth); } /** * Tree table specific code ends ** */ // layout the tree viewer below the text field gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; treeViewer.getControl().setLayoutData(gridData); treeViewer.setContentProvider(new SteadyTreeTableContentProvider()); treeViewer.setLabelProvider(new SteadyTreeTableLabelProvider()); root = getInitialInput(); treeViewer.setInput(root); treeViewer.expandToLevel(1); // Drag-Part ////////////////////////////////////////////////////////// DragSource source = new DragSource(tree, DND.DROP_COPY | DND.DROP_MOVE); source.setTransfer(new Transfer[] {TextTransfer.getInstance()}); source.addDragListener(new TreeDragSourceListener(tree)); // Drop-Part ////////////////////////////////////////////////////////// DropTarget dropTarget = new DropTarget(tree, DND.DROP_COPY | DND.DROP_DEFAULT); dropTarget.setTransfer(new Transfer[] {TextTransfer.getInstance(), FileTransfer.getInstance()}); dropTarget.addDropListener(new TreeDropTargetAdapter(treeViewer, root)); addListeners(); createContextMenu(); createFiltersAndSorters(); }
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Custom gradient selection for Tree"); shell.setLayout(new FillLayout()); final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION); tree.setHeaderVisible(false); tree.setLinesVisible(false); int columnCount = 4; for (int i = 0; i < columnCount; i++) { TreeColumn column = new TreeColumn(tree, SWT.NONE); column.setText("Column " + i); } int itemCount = 3; for (int i = 0; i < itemCount; i++) { TreeItem item1 = new TreeItem(tree, SWT.NONE); item1.setText("item " + i); for (int c = 1; c < columnCount; c++) { item1.setText(c, "item [" + i + "-" + c + "]"); } for (int j = 0; j < itemCount; j++) { TreeItem item2 = new TreeItem(item1, SWT.NONE); item2.setText("item [" + i + " " + j + "]"); for (int c = 1; c < columnCount; c++) { item2.setText(c, "item [" + i + " " + j + "-" + c + "]"); } for (int k = 0; k < itemCount; k++) { TreeItem item3 = new TreeItem(item2, SWT.NONE); item3.setText("item [" + i + " " + j + " " + k + "]"); for (int c = 1; c < columnCount; c++) { item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]"); } } } } /* * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly. * Therefore, it is critical for performance that these methods be * as efficient as possible. */ tree.addListener( SWT.EraseItem, new Listener() { public void handleEvent(Event event) { event.detail &= ~SWT.HOT; if ((event.detail & SWT.SELECTED) != 0) { GC gc = event.gc; Rectangle area = tree.getClientArea(); /* * If you wish to paint the selection beyond the end of * last column, you must change the clipping region. */ int columnCount = tree.getColumnCount(); if (event.index == columnCount - 1 || columnCount == 0) { int width = area.x + area.width - event.x; if (width > 0) { Region region = new Region(); gc.getClipping(region); region.add(event.x, event.y, width, event.height); gc.setClipping(region); region.dispose(); } } gc.setAdvanced(true); if (gc.getAdvanced()) gc.setAlpha(127); Rectangle rect = event.getBounds(); Color foreground = gc.getForeground(); Color background = gc.getBackground(); gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND)); gc.fillGradientRectangle(0, rect.y, 500, rect.height, false); // restore colors for subsequent drawing gc.setForeground(foreground); gc.setBackground(background); event.detail &= ~SWT.SELECTED; } } }); for (int i = 0; i < columnCount; i++) { tree.getColumn(i).pack(); } tree.setSelection(tree.getItem(0)); shell.setSize(500, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }