public static TreeItem getTreeItem(Tree tree, Object data) { for (TreeItem item : tree.getItems()) { if (item.getData() == data) { return item; } } return null; }
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); } }
@Override protected Control createControl(Composite parent) { Composite propertyComposite = new Composite(parent, SWT.BORDER); GridLayout layout = new GridLayout(1, false); layout.marginWidth = layout.marginHeight = 0; propertyComposite.setLayout(layout); if (mPage instanceof Page) { ((Page) mPage) .init( new IPageSite() { public void registerContextMenu( String menuId, MenuManager menuManager, ISelectionProvider selectionProvider) {} public IActionBars getActionBars() { return null; } public IWorkbenchPage getPage() { return getWorkbenchWindow().getActivePage(); } public ISelectionProvider getSelectionProvider() { return null; } public Shell getShell() { return getWorkbenchWindow().getShell(); } public IWorkbenchWindow getWorkbenchWindow() { return PlatformUI.getWorkbench().getActiveWorkbenchWindow(); } public void setSelectionProvider(ISelectionProvider provider) {} @SuppressWarnings("unchecked") public Object getAdapter(Class adapter) { return null; } @SuppressWarnings("unchecked") public Object getService(Class api) { return null; } @SuppressWarnings("unchecked") public boolean hasService(Class api) { return false; } }); } if (mPage instanceof PropertySheetPage) { ((PropertySheetPage) mPage).setPropertySourceProvider(this); } mPage.createControl(propertyComposite); mPage.setActionBars(new DummyActionBars()); final Control control = mPage.getControl(); GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); if (control instanceof Tree) { final Tree tree = (Tree) control; data.heightHint = tree.getItemHeight() * 13 + (tree.getLinesVisible() ? 12 * tree.getGridLineWidth() : 0) + (tree.getHeaderVisible() ? tree.getHeaderHeight() : 0) + 2 * tree.getBorderWidth() + (tree.getHorizontalBar() != null ? tree.getHorizontalBar().getSize().x : 0); tree.addControlListener( new ControlAdapter() { @Override public void controlResized(ControlEvent e) { Rectangle area = tree.getClientArea(); TreeColumn[] columns = tree.getColumns(); if (area.width > 0) { columns[0].setWidth(area.width * 40 / 100); columns[1].setWidth(area.width - columns[0].getWidth() - 4); } } }); } else if (control instanceof Composite) { control.addControlListener( new ControlAdapter() { @Override public void controlResized(ControlEvent e) { ((Composite) control).layout(true, true); } }); } control.setLayoutData(data); ISelection selection; if (mCurrentWidget == null) { Collection<InstallOptionsModelTypeDef> typeDefs = InstallOptionsModel.INSTANCE.getControlTypeDefs(); if (typeDefs.size() > 0) { InstallOptionsModelTypeDef typeDef = typeDefs.iterator().next(); InstallOptionsElementFactory factory = InstallOptionsElementFactory.getFactory(typeDef.getType()); mCurrentWidget = (InstallOptionsWidget) factory.getNewObject(); mDialog.addChild(mCurrentWidget); } } if (mCurrentWidget != null) { mCurrentWidget.addModelCommandListener(InstallOptionsWidgetEditorDialog.this); mCurrentWidget.addPropertyChangeListener(InstallOptionsWidgetEditorDialog.this); if (mCurrentWidget.getParent() != null) { mCurrentWidget.getParent().addPropertyChangeListener(InstallOptionsWidgetEditorDialog.this); } selection = new StructuredSelection(mCurrentWidget); } else { selection = StructuredSelection.EMPTY; } mPage.selectionChanged(null, selection); PlatformUI.getWorkbench().getHelpSystem().setHelp(mPage.getControl(), HELP_CONTEXT); PlatformUI.getWorkbench().getHelpSystem().setHelp(propertyComposite, HELP_CONTEXT); return propertyComposite; }