private void updateItems(final FileInfo parent) { if (fDisplay == null) return; assert Thread.currentThread() == fDisplay.getThread(); TreeItem[] items = null; boolean expanded = true; if (parent.children == null || parent.children_error != null) { if (parent == fRootInfo) { fileTree.setItemCount(1); items = fileTree.getItems(); } else { TreeItem item = findItem(parent); if (item == null) return; expanded = item.getExpanded(); item.setItemCount(1); items = item.getItems(); } assert items.length == 1; items[0].removeAll(); if (parent.children_pending) { items[0].setForeground(fDisplay.getSystemColor(SWT.COLOR_LIST_FOREGROUND)); items[0].setText("Pending..."); } else if (parent.children_error != null) { String msg = parent.children_error.getMessage(); if (msg == null) msg = parent.children_error.getClass().getName(); else msg = msg.replace('\n', ' '); items[0].setForeground(fDisplay.getSystemColor(SWT.COLOR_RED)); items[0].setText(msg); items[0].setImage((Image) null); } else if (expanded) { loadChildren(parent); items[0].setForeground(fDisplay.getSystemColor(SWT.COLOR_LIST_FOREGROUND)); items[0].setText("Pending..."); } else { items[0].setText(""); } } else { FileInfo[] arr = parent.children; if (parent == fRootInfo) { fileTree.setItemCount(arr.length); items = fileTree.getItems(); } else { TreeItem item = findItem(parent); if (item == null) return; expanded = item.getExpanded(); item.setItemCount(expanded ? arr.length : 1); items = item.getItems(); } if (expanded) { assert items.length == arr.length; for (int i = 0; i < items.length; i++) fillItem(items[i], arr[i]); expandSelect(); } else { items[0].setText(""); } } }
/** * When one element from the tree is selected, all elements with the same VisualID are put into * the same state (checked or unchecked). * * @param items * @param visualID * @param check */ protected void setAllVisualIDsChecked(TreeItem[] items, int visualID, boolean check) { ViewInfo viewInfo = null; for (TreeItem item : items) { if (item != null) { viewInfo = (ViewInfo) Platform.getAdapterManager().getAdapter(item.getData(), ViewInfo.class); if (viewInfo != null && visualID == viewInfo.getVisualID()) { item.setChecked(check); } if (item.getItems() != null) { setAllVisualIDsChecked(item.getItems(), visualID, check); } } } }
private TreeItem findElement(TreeItem[] items, TreeItem[] toBeSkipped, boolean allowToGoUp) { if (fStringMatcher == null) return items.length > 0 ? items[0] : null; ILabelProvider labelProvider = (ILabelProvider) getfTreeViewer().getLabelProvider(); // First search at same level /*for (int i= 0; i < items.length; i++) { final TreeItem item= items[i]; IJavaElement element= (IJavaElement)item.getData(); if (element != null) { String label= labelProvider.getText(element); if (fStringMatcher.match(label)) return item; } }*/ // Go one level down for each item for (int i = 0; i < items.length; i++) { final TreeItem item = items[i]; TreeItem foundItem = findElement(selectItems(item.getItems(), toBeSkipped), null, false); if (foundItem != null) return foundItem; } if (!allowToGoUp || items.length == 0) return null; // Go one level up (parent is the same for all items) TreeItem parentItem = items[0].getParentItem(); if (parentItem != null) return findElement(new TreeItem[] {parentItem}, items, true); // Check root elements return findElement(selectItems(items[0].getParent().getItems(), items), null, false); }
private static TreeItem retrieveTreeItem(final TreeItem[] items, IContextParameter param) { if (items == null || param == null) { return null; } for (TreeItem item : items) { final Object data = item.getData(); if (data != null) { if (data instanceof ContextVariableTabParentModel) { ContextVariableTabParentModel parent = (ContextVariableTabParentModel) data; if (parent.getContextParameter() != null && param.getName().equals(parent.getContextParameter().getName())) { return item; } } else if (data instanceof ContextVariableTabChildModel) { ContextVariableTabChildModel son = (ContextVariableTabChildModel) data; if (son.getContextParameter() != null && param.getName().equals(son.getContextParameter().getName())) { return item; } } } // search child item TreeItem childItem = retrieveTreeItem(item.getItems(), param); if (childItem != null) { return childItem; } } return null; }
/** * Update all the children TreeItems of the given parent to be the same check as the parent. * * @param parent the parent */ protected void updateChildCheckboxes(TreeItem parent) { for (TreeItem child : parent.getItems()) { child.setChecked(parent.getChecked()); child.setGrayed(false); updateChildCheckboxes(child); } }
private static final TreeItem findTreeItem( TreeItem grandParent, TreeItem parent, String parentName, String name) { if (Const.isEmpty(parentName)) { if (parent.getText().equalsIgnoreCase(name)) { return parent; } } else { if (grandParent != null && grandParent.getText().equalsIgnoreCase("OTHER")) { System.out.println("Other"); } if (grandParent != null && grandParent.getText().equalsIgnoreCase(parentName) && parent.getText().equalsIgnoreCase(name)) { return parent; } } TreeItem ti[] = parent.getItems(); for (int i = 0; i < ti.length; i++) { TreeItem child = findTreeItem(parent, ti[i], parentName, name); if (child != null) { return child; } } return null; }
private void addRow( TreeItem item, List<String[]> rowData, IXViewerLabelProvider labelProv, List<XViewerColumn> showCols, int level) throws XViewerException { List<String> cellData = new ArrayList<String>(showCols.size()); boolean firstCell = true; for (XViewerColumn xCol : showCols) { StringBuffer str = new StringBuffer(); if (firstCell) { for (int y = 1; y < level; y++) { str.append("__INSERT_TAB_HERE__"); // $NON-NLS-1$ } firstCell = false; } str.append(labelProv.getColumnText(item.getData(), xColToColumnIndex.get(xCol))); String html = HtmlUtil.textToHtml(str.toString()); html = html.replaceAll( "__INSERT_TAB_HERE__", " "); // $NON-NLS-1$ //$NON-NLS-2$ cellData.add(html); } rowData.add(cellData.toArray(new String[cellData.size()])); if (item.getExpanded()) { for (TreeItem i : item.getItems()) { addRow(i, rowData, labelProv, showCols, level + 1); } } }
protected void setSelectionInViewer(CloudSpace selectedSpace) { // Now set the cloud space in the tree Tree tree = orgsSpacesViewer.getTree(); TreeItem[] orgItems = tree.getItems(); if (orgItems != null) { TreeItem orgItem = null; // Find the tree item corresponding to the cloud space's // org for (TreeItem item : orgItems) { Object treeObj = item.getData(); if (treeObj instanceof CloudOrganization && ((CloudOrganization) treeObj) .getName() .equals(selectedSpace.getOrganization().getName())) { orgItem = item; break; } } if (orgItem != null) { TreeItem[] children = orgItem.getItems(); if (children != null) { for (TreeItem childItem : children) { Object treeObj = childItem.getData(); if (treeObj instanceof CloudSpace && ((CloudSpace) treeObj).getName().equals(selectedSpace.getName())) { tree.select(childItem); break; } } } } } }
/** * Clear all sub items. * * @param item The item root. */ public void clear(TreeItem item) { item.setData(null); for (final TreeItem current : item.getItems()) { clear(current); } item.dispose(); }
public Rectangle buildHighlightRectangle( TreeItem item, boolean includeChildren, GC gc, boolean includeHeaderHeight, boolean adjustHeight) { TreeItem parentItem = item.getParentItem(); Rectangle bounds = item.getBounds(); if (parentItem != null) { bounds = parentItem.getBounds(0); } Rectangle itemBounds = item.getBounds(); int x = bounds.x; int width = itemBounds.width + itemBounds.x - bounds.x; if (parentItem == null) { x = 0; width = bounds.width + bounds.x; } Rectangle highlight = new Rectangle(x, itemBounds.y, width, getTree().getItemHeight()); // expand for column text String columnText = ((ITableLabelProvider) getLabelProvider()).getColumnText(item.getData(), 1); if (columnText != null) { Point textExtent = gc.textExtent(columnText); int textWidth = textExtent.x; Rectangle columnBounds = item.getBounds(1); highlight.width = (columnBounds.x + textWidth) - highlight.x; // increase width to account for space where icon would be // later we will need to account for the icons directly (currently // there are no icons for the second column) highlight.width = highlight.width + 16; } // expand for children if necessary if (includeChildren) { TreeItem[] children = item.getItems(); if (children.length != 0) { expandHighlightRectangleForChildren(highlight, children, gc); } } // shrink the rectangle by one pixel so that back to back // highlights do not overlap if (adjustHeight) { int itemSpace = item.getBounds().height - getTree().getItemHeight(); itemSpace = Math.abs(itemSpace); highlight.height = highlight.height - itemSpace; } if (SWT.getPlatform().equals("gtk") && item.getParentItem() != null) { // $NON-NLS-1$ // GTK puts expansion handles directly // at the location of the parent bounds // we use that location to start the box // to allow better looking highlights // increase the size a bit for linux highlight.x = highlight.x - 5; highlight.width = highlight.width + 5; } if (includeHeaderHeight) { highlight.y = highlight.y + getTree().getHeaderHeight(); } return highlight; }
public static void collectTreeItemsInView(TreeItem[] items, List<TreeItem> collectedItems) { if (items.length > 0) { for (TreeItem childItem : Arrays.asList(items)) { collectedItems.add(childItem); collectTreeItemsInView(childItem.getItems(), collectedItems); } } }
public void collapseNodes(TreeItem item) { item.setExpanded(false); TreeItem[] items = item.getItems(); for (int i = 0; i < items.length; i++) { TreeItem child = items[i]; collapseNodes(child); } }
private void expand(TreeItem item, boolean expand) { item.setExpanded(expand); TreeItem[] children = item.getItems(); if (children == null) return; for (TreeItem child : children) { expand(child, expand); } }
private void collectElementsInBranch( TreeItem item, Collection treeItems, Collection testElements) { treeItems.add(item); testElements.add(item.getData()); TreeItem[] children = item.getItems(); for (int i = 0; i < children.length; i++) { collectElementsInBranch(children[i], treeItems, testElements); } }
/** * Adds the view infos. * * @param root the root * @param infos the infos */ protected void addViewInfos(TreeItem root, Collection<Integer> infos) { for (TreeItem item : root.getItems()) { if (item.getChecked() == false) { Object data = item.getData(); if (data instanceof ViewInfo) { infos.add(((ViewInfo) data).getVisualID()); } } addViewInfos(item, infos); } }
/** * Expands subtrees of given items. Items of type <code>PluginExtensionNode</code> that have * multiple children to expand will only be expanded to the that level. Further expanding is * required to reveal the whole subtree. This is for reasons of convenience. * * @param items tree items to be expand with their children */ private void traverseChildrenAndSetExpanded(TreeItem[] items) { for (int i = 0; i < items.length; i++) { TreeItem treeItem = items[i]; TreeItem[] children = treeItem.getItems(); int extensionsChildCount = getExtensionsChildCount((IPluginParent) treeItem.getData()); boolean furtherExpanding = !(extensionsChildCount > 1 && (!treeItem.getExpanded())); treeItem.setExpanded(furtherExpanding); if (furtherExpanding) { traverseChildrenAndSetExpanded(children); } } }
/** * Make check marks consistent with children. * * @param item the item * @return null if checkbox should be gray, true if checked, false if unchecked */ private Boolean makeChecksConsistentWithChildren(TreeItem item) { Boolean ret = item.getChecked(); for (TreeItem child : item.getItems()) { Boolean current = makeChecksConsistentWithChildren(child); if (current != ret) { ret = null; } } item.setChecked(!Boolean.FALSE.equals(ret)); item.setGrayed(ret == null); return ret; }
/** * Sets the checked. * * @param root the root * @param filters the filters */ protected void setChecked(TreeItem root, Collection<Integer> filters) { for (TreeItem item : root.getItems()) { Object data = item.getData(); if (data instanceof ViewInfo) { ViewInfo info = (ViewInfo) data; if (filters.contains(info.getVisualID())) { item.setChecked(false); } else { item.setChecked(true); } } setChecked(item, filters); } }
public void checkItems(TreeItem[] items, List<TreeItem> toSelect, List<?> sobj) { if (items == null) return; for (TreeItem ti : items) { for (Object obj : sobj) { if (obj != null && ti.getData() != null) { if (obj == ti.getData()) toSelect.add(ti); else if (obj instanceof EditPart && ti.getData() instanceof EditPart) { if (((EditPart) obj).getModel() == ((EditPart) ti.getData()).getModel()) toSelect.add(ti); } } } checkItems(ti.getItems(), toSelect, sobj); } }
private static EmptyElement locateEmptyElement(TreeItem[] items, Object data) { for (TreeItem item : items) { if (item.getData() instanceof EmptyElement) { EmptyElement empty = (EmptyElement) item.getData(); if (empty.getRepresentedMissingElement().equals(data)) { return empty; } } else { EmptyElement empty = locateEmptyElement(item.getItems(), data); if (empty != null) { return empty; } } } return null; }
/** Get the TreeItem that should be selected after removing the current selection. */ public static TreeItem getNewlySelectedItem(Tree tree) { TreeItem[] selection = tree.getSelection(); if ((selection == null) || (selection.length == 0)) { return null; } TreeSet<TreeItem> selectionSet = new TreeSet<TreeItem>( new Comparator<TreeItem>() { @Override public final int compare(TreeItem o1, TreeItem o2) { return CommonUtils.compare(o1.getBounds().y, o2.getBounds().y); } }); List<TreeItem> selectionList = Arrays.asList(selection); selectionSet.addAll(selectionList); TreeItem previousItem = null; TreeItem parentItem = null; while (!selectionSet.isEmpty()) { TreeItem focusItem = selectionSet.iterator().next(); parentItem = focusItem.getParentItem(); TreeItem[] items = (parentItem != null ? parentItem.getItems() : focusItem.getParent().getItems()); boolean found = false; for (int i = 0; i < items.length; i++) { TreeItem item = items[i]; if (focusItem == item) { found = true; if ((i > 0) && (previousItem == null)) { previousItem = items[i - 1]; } } if (found) { if (!selectionSet.remove(item)) { return item; } } } } if (previousItem != null && !selectionList.contains(previousItem)) { return previousItem; } if (parentItem != null && !selectionList.contains(parentItem)) { return parentItem; } return null; }
public static boolean createToTreeItem( TreeItem item, DataObject note, String label, String agroup, String[] group, int groupIndex, ActionContext actionContext) { if ("node".equals(item.getData("type")) && group[groupIndex].equals(item.getText())) { if (groupIndex == group.length - 1) { TreeItem newItem = new TreeItem(item, SWT.None); newItem.setData("type", "note"); newItem.setData(note); newItem.setText(note.getString("name")); newItem.setData("label", label); newItem.setData("group", agroup); setImage(newItem, "noteImage", actionContext); addTreeItemToNode(newItem, note); return true; } else if (groupIndex < group.length - 1) { boolean created = false; for (TreeItem childItem : item.getItems()) { if (createToTreeItem( childItem, note, label, agroup, group, groupIndex + 1, actionContext)) { created = true; return true; } } if (!created) { TreeItem nodeItem = new TreeItem(item, SWT.None); nodeItem.setData("type", "node"); nodeItem.setText(group[groupIndex + 1]); setImage(nodeItem, "nodeImage", actionContext); nodeItem.setData("group", agroup); nodeItem.setData("label", label); return createToTreeItem( nodeItem, note, label, agroup, group, groupIndex + 1, actionContext); } } return false; } else { return false; } }
private static IPath proposeNewRepositoryPath(TreeItem[] treeItems) { IPath p = null; for (TreeItem ti : treeItems) { String gitDirParentCandidate = ti.getText(1); if (gitDirParentCandidate.equals("")) // $NON-NLS-1$ continue; if (ti.getItemCount() > 0) if (hasRepositoryInOwnDirectory(ti.getItems())) return null; if (hasRepositoryInOwnDirectory(ti)) return null; IPath thisPath = Path.fromOSString(gitDirParentCandidate); if (p == null) p = thisPath; else { int n = p.matchingFirstSegments(thisPath); p = p.removeLastSegments(p.segmentCount() - n); } } return p; }
/** * @param items items to be traversed * @return <code>true</code> if at least one of the tree items could be expanded but is not. * Otherwise <code>false</code> is returned. */ protected boolean traverseStateChangeRequired(TreeItem[] items) { if (items != null) { for (int i = 0; i < items.length; i++) { TreeItem treeItem = items[i]; TreeItem[] children = treeItem.getItems(); if (children.length > 0) { if (treeItem.getExpanded()) { if (traverseStateChangeRequired(children)) { return true; } } else { return true; } } } } return false; }
protected void installEditors(TreeItem treeItem, final Object parentPackage) { Object currentDataItem = treeItem.getData(); if (currentDataItem instanceof ProfileApplication) { ProfileApplication profileApplication = (ProfileApplication) currentDataItem; Tree tree = treeItem.getParent(); final Button checkbox = new Button(tree, SWT.CHECK); checkbox.setSelection(true); IProfileApplicationDelegate delegate = getDelegate(profileApplication); final Package applyingPackage = delegate.getApplyingPackage(profileApplication); final Profile appliedProfile = delegate.getAppliedProfile(profileApplication); getProfilesToReapply(applyingPackage).add(appliedProfile); checkbox.addSelectionListener( new SelectionListener() { public void widgetSelected(SelectionEvent e) { if (checkbox.getSelection()) { getProfilesToReapply(applyingPackage).add(appliedProfile); } else { getProfilesToReapply(applyingPackage).remove(appliedProfile); } } public void widgetDefaultSelected(SelectionEvent e) { // Nothing } }); TreeEditor editor = new TreeEditor(tree); editor.horizontalAlignment = SWT.CENTER; editor.grabHorizontal = true; editor.setEditor(checkbox, treeItem, 4); } for (TreeItem subitem : treeItem.getItems()) { installEditors(subitem, currentDataItem); } }
public static TreeItem getPreviousItem(TreeItem parent, TreeDifference difference) { int location = difference.getLocation(); TreeItem[] items = parent.getItems(); TreeItem prevItem = null; if (items.length == 0) { prevItem = parent; } else if (items.length <= location) { prevItem = items[items.length - 1]; } else if (location < 0) { prevItem = parent; } else { if (location == 0) { prevItem = items[0]; } else { prevItem = items[location - 1]; } } return prevItem; }
private static IContextParameter searchEndItemParam(final TreeItem item) { if (item == null) { return null; } TreeItem foundItem = item; // self final TreeItem[] items = item.getItems(); if (items != null && items.length > 0) { foundItem = items[items.length - 1]; } final Object data = foundItem.getData(); if (data == null) { return null; } else if (data instanceof ContextVariableTabChildModel) { return ((ContextVariableTabChildModel) data).getContextParameter(); } else if (data instanceof ContextVariableTabParentModel) { return ((ContextVariableTabParentModel) data).getContextParameter(); } return null; }
String[] getSources(String category) { TreeItem[] treeItems = tree.getItems(); TreeItem treeItem = null; for (TreeItem ele : treeItems) { if (ele.getText().equals(category)) { treeItem = ele; break; } } if (treeItem == null) return null; TreeItem[] items = treeItem.getItems(); if (items.length < 1) return null; List<String> list = new ArrayList<String>(); for (TreeItem item : items) { String value = item.getText(); if (!list.contains(value)) list.add(value); } return list.toArray(new String[list.size()]); }
public void setSelectedSources(Worker[] plugins, String... sources) { TreeItem[] items = tree.getSelection(); if (items.length < 1) return; TreeItem selectedItem = items[0]; items = selectedItem.getItems(); if (items == null || items.length < 1) return; for (int i = 0; i < items.length; i++) { for (String source : sources) { if (!items[i].getText().equals(source)) continue; tree.setSelection(new TreeItem[] {selectedItem, items[i]}); creator.setSource(plugins, getSelectedCategory(), source, false); int topIndex = i; int visibleCount = tree.getSize().y / 48; if (topIndex - visibleCount < 0) return; topIndex -= visibleCount; tree.setTopItem(items[topIndex]); break; } } }
/** return the item of real context parameter. */ private static IContextParameter searchNearItemParam( final TreeItem[] items, final TreeItem searchItem) { if (items == null || searchItem == null) { return null; } for (int i = items.length - 1; i >= 0; i--) { TreeItem item = items[i]; IContextParameter param = searchNearItemParam(item.getItems(), searchItem); if (param != null) { return param; } // if (item == searchItem) { if (i > 0) { // before item return searchEndItemParam(items[i - 1]); } else { // first in current if (i + 1 < items.length) { // next item return searchEndItemParam(items[i + 1]); } else { // only one left TreeItem parentItem = item.getParentItem(); if (parentItem != null) { final Tree parentTree = item.getParent(); final int index = parentTree.indexOf(parentItem); if (index > -1) { // in tree root if (index > 0) { return searchEndItemParam(parentTree.getItem(index - 1)); } else { // is first return null; } } else { // not found return null; } } } } } } return null; }