Java Code Examples for org.eclipse.swt.widgets.Tree#getSelection()

The following examples show how to use org.eclipse.swt.widgets.Tree#getSelection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExternalLibraryPreferencePage.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/** @return either a URI of a node_modules folder or a external N4JSProject instance */
private Object getSelectedItem() {
	final Tree tree = viewer.getTree();
	final TreeItem[] selection = tree.getSelection();
	if (!Arrays2.isEmpty(selection) && 1 == selection.length) {
		Object data = selection[0].getData();

		if (data instanceof SafeURI<?>) {
			SafeURI<?> uri = (SafeURI<?>) data;
			if (ExternalLibraryPreferenceModel.isNodeModulesLocation(uri)) {
				return data;
			}
		}

		if (data instanceof IN4JSProject) {
			return data;
		}
	}
	return null;
}
 
Example 2
Source File: TreeItemAccelerator.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static final void addDoubleClick( final TreeItem treeItem, final DoubleClickInterface doubleClick ) {
  final String[] path1 = ConstUI.getTreeStrings( treeItem );
  final Tree tree = treeItem.getParent();

  if ( doubleClick != null ) {
    final SelectionAdapter selectionAdapter = new SelectionAdapter() {
      public void widgetDefaultSelected( SelectionEvent selectionEvent ) {
        TreeItem[] items = tree.getSelection();
        for ( int i = 0; i < items.length; i++ ) {
          String[] path2 = ConstUI.getTreeStrings( items[i] );
          if ( equalPaths( path1, path2 ) ) {
            doubleClick.action( treeItem );
          }
        }
      }
    };
    tree.addSelectionListener( selectionAdapter );

    // Clean up when we do a refresh too.
    treeItem.addDisposeListener( new DisposeListener() {
      public void widgetDisposed( DisposeEvent disposeEvent ) {
        tree.removeSelectionListener( selectionAdapter );
      }
    } );
  }
}
 
Example 3
Source File: TreeClipboard.java    From olca-app with Mozilla Public License 2.0 6 votes vote down vote up
private static void copyItems(Tree tree, int levels, StringBuilder text) {
	int cols = tree.getColumnCount();
	if (cols < 1)
		return;
	for (TreeItem item : tree.getSelection()) {
		int itemLevel = getLevel(item);
		for (int level = 0; level < levels; level++) {
			if (itemLevel == level)
				text.append(item.getText(0));
			text.append('\t');
		}
		for (int col = 1; col < cols; col++) {
			String s = item.getText(col);
			text.append(s == null ? "" : s);
			if (col != (cols - 1))
				text.append('\t');
		}
		text.append('\n');
	}
}
 
Example 4
Source File: TreeToClipboardAdapter.java    From logbook with MIT License 6 votes vote down vote up
/**
 * ツリーの選択されている部分をヘッダー付きでクリップボードにコピーします
 * 
 * @param header ヘッダー
 * @param tree ツリー
 */
public static void copyTree(String[] header, Tree tree) {
    TreeItem[] treeItems = tree.getSelection();
    StringBuilder sb = new StringBuilder();
    sb.append(StringUtils.join(header, "\t"));
    sb.append("\r\n");
    for (TreeItem column : treeItems) {
        String[] columns = new String[header.length];
        for (int i = 0; i < header.length; i++) {
            columns[i] = column.getText(i);
        }
        sb.append(StringUtils.join(columns, "\t"));
        sb.append("\r\n");
    }
    Clipboard clipboard = new Clipboard(Display.getDefault());
    clipboard.setContents(new Object[] { sb.toString() }, new Transfer[] { TextTransfer.getInstance() });
}
 
Example 5
Source File: TreeItemAccelerator.java    From hop with Apache License 2.0 6 votes vote down vote up
public static final void addDoubleClick( final TreeItem treeItem, final IDoubleClick doubleClick ) {
  final String[] path1 = ConstUi.getTreeStrings( treeItem );
  final Tree tree = treeItem.getParent();

  if ( doubleClick != null ) {
    final SelectionAdapter selectionAdapter = new SelectionAdapter() {
      public void widgetDefaultSelected( SelectionEvent selectionEvent ) {
        TreeItem[] items = tree.getSelection();
        for ( int i = 0; i < items.length; i++ ) {
          String[] path2 = ConstUi.getTreeStrings( items[ i ] );
          if ( equalPaths( path1, path2 ) ) {
            doubleClick.action( treeItem );
          }
        }
      }
    };
    tree.addSelectionListener( selectionAdapter );

    // Clean up when we do a refresh too.
    treeItem.addDisposeListener( new DisposeListener() {
      public void widgetDisposed( DisposeEvent disposeEvent ) {
        tree.removeSelectionListener( selectionAdapter );
      }
    } );
  }
}
 
Example 6
Source File: AbstractInformationControl.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Handles mouse up action for the tree viewer
 * 
 * @param tree
 *            current tree
 * @param e
 *            mouse event
 */
private void handleTreeViewerMouseUp(final Tree tree, MouseEvent e) {
	// Ensure a selection was made, the first mouse button was
	// used and the event happened in the tree
	if ((tree.getSelectionCount() < 1) || (e.button != 1) || !tree.equals(e.getSource())) {
		return;
	}
	// Selection is made in the selection changed listener
	Object object = tree.getItem(new Point(e.x, e.y));
	TreeItem selection = tree.getSelection()[0];
	if (selection.equals(object)) {
		gotoSelectedElement();
	}
}
 
Example 7
Source File: RenameResourceAndCloseEditorAction.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
Composite createParent() {
	Tree tree = getTree();
	Composite result = new Composite(tree, SWT.NONE);
	TreeItem[] selectedItems = tree.getSelection();
	treeEditor.horizontalAlignment = SWT.LEFT;
	treeEditor.grabHorizontal = true;
	treeEditor.setEditor(result, selectedItems[0]);
	return result;
}
 
Example 8
Source File: ColumnChooserDialog.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private boolean isLastLeafSelected(Tree tree) {
	TreeItem[] selectedLeaves = tree.getSelection();
	for (int i = 0; i < selectedLeaves.length; i++) {
		if (tree.indexOf(selectedLeaves[i])+1 == tree.getItemCount()) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: ColumnChooserDialog.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private boolean isFirstLeafSelected(Tree tree) {
	TreeItem[] selectedLeaves = tree.getSelection();
	for (int i = 0; i < selectedLeaves.length; i++) {
		if (selectedTree.indexOf(selectedLeaves[i]) == 0) {
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: RenameResourceAndCloseEditorAction.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
Composite createParent() {
	Tree tree = getTree();
	Composite result = new Composite(tree, SWT.NONE);
	TreeItem[] selectedItems = tree.getSelection();
	treeEditor.horizontalAlignment = SWT.LEFT;
	treeEditor.grabHorizontal = true;
	treeEditor.setEditor(result, selectedItems[0]);
	return result;
}
 
Example 11
Source File: ColumnChooserDialog.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private boolean isLastLeafSelected(Tree tree) {
	TreeItem[] selectedLeaves = tree.getSelection();
	for (int i = 0; i < selectedLeaves.length; i++) {
		if (tree.indexOf(selectedLeaves[i])+1 == tree.getItemCount()) {
			return true;
		}
	}
	return false;
}
 
Example 12
Source File: ColumnChooserDialog.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private boolean isFirstLeafSelected(Tree tree) {
	TreeItem[] selectedLeaves = tree.getSelection();
	for (int i = 0; i < selectedLeaves.length; i++) {
		if (selectedTree.indexOf(selectedLeaves[i]) == 0) {
			return true;
		}
	}
	return false;
}
 
Example 13
Source File: TreeSelectionDialog.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
protected void updateSelectionIfNothingSelected(Tree tree) {
    TreeItem[] sel = tree.getSelection();
    if (sel == null || sel.length == 0) {
        TreeItem[] items = tree.getItems();
        if (items != null && items.length > 0) {
            tree.setSelection(items[0]);
        }
    }
}
 
Example 14
Source File: TreeClipboard.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static void copyItems(Tree tree, ClipboardLabelProvider label, StringBuilder text) {
	int cols = label.columns();
	for (TreeItem item : tree.getSelection()) {
		for (int col = 0; col < cols; col++) {
			String s = label.getLabel(item, col);
			text.append(s == null ? "" : s);
			if (col != (cols - 1))
				text.append('\t');
		}
		text.append('\n');
	}
}
 
Example 15
Source File: TreeViewerNavigator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private TreeItem getCurrentItem(boolean forward) {
	Tree tree = viewer.getTree();
	TreeItem[] selection = tree.getSelection();
	if (selection.length == 0) {
		selection = tree.getItems();
	}

	TreeItem nextItem = null;
	if (selection.length > 0) {
		nextItem = forward ? selection[0] : selection[selection.length - 1];
	}
	return nextItem;
}
 
Example 16
Source File: TreeClipboard.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static int getLevelCount(Tree tree) {
	if (tree == null)
		return -1;
	int count = 0;
	for (TreeItem item : tree.getSelection()) {
		int c = getLevel(item) + 1;
		count = Math.max(c, count);
	}
	return count;
}
 
Example 17
Source File: ColumnChooserDialog.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
private boolean isAnyLeafSelected(Tree tree) {
	TreeItem[] selectedLeaves = tree.getSelection();
	return selectedLeaves != null && selectedLeaves.length > 0;
}
 
Example 18
Source File: ColumnChooserDialog.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
private boolean isAnyLeafSelected(Tree tree) {
	TreeItem[] selectedLeaves = tree.getSelection();
	return selectedLeaves != null && selectedLeaves.length > 0;
}