Skip to main content
summaryrefslogtreecommitdiffstats
blob: fc1409a837455b7778694cde69eec4c9e346ec16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.eclipse.fx.emf.edit.ui;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.control.Cell;
import javafx.scene.control.Control;
import javafx.scene.control.ListCell;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableRow;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

public class CellUtil2 {

	public static Cell<?> getRowNode(final Cell<?> cell) {
		if (cell instanceof TableCell)
			return ((TableCell<?, ?>) cell).getTableRow();
		else
			return cell;
	}

	public static MultipleSelectionModel<?> getSelectionModel(Cell<?> cell) {
		if (cell instanceof ListCell)
			return ((ListCell<?>) cell).getListView().getSelectionModel();
		else if (cell instanceof TreeCell)
			return ((TreeCell<?>) cell).getTreeView().getSelectionModel();
		else if (cell instanceof TableCell)
			return ((TableCell<?, ?>) cell).getTableView().getSelectionModel();
		else if (cell instanceof TableRow)
			return ((TableRow<?>) cell).getTableView().getSelectionModel();
		else
			throw new IllegalArgumentException("Unsupported Cell type");
	}

	public static MultipleSelectionModel<?> getSelectionModel(Control view) {
		if (view instanceof TreeView<?>)
			return ((TreeView<?>) view).getSelectionModel();
		else
			throw new IllegalArgumentException("Unsupported View type");
	}

	public static List<?> getSelectedItems(Cell<?> cell) {
		MultipleSelectionModel<?> selectionModel = getSelectionModel(cell);
		List<?> items = selectionModel.getSelectedItems();

		if (cell instanceof TreeCell) {
			List<Object> unwrappedItems = new ArrayList<>(items.size());
			for (Object item : items)
				unwrappedItems.add(((TreeItem<?>) item).getValue());
			return unwrappedItems;
		}

		return items;
	}

}

Back to the top