下面列出了javafx.scene.control.TreeItem#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void selectedRowChanged() {
TreeItem<ModularFeatureListRow> selectedItem = featureTable.getSelectionModel()
.getSelectedItem();
// featureTable.getColumns().forEach(c -> logger.info(c.getText()));
logger.info(
"selected: " + featureTable.getSelectionModel().getSelectedCells().get(0).getTableColumn()
.getText());
if (selectedItem == null) {
return;
}
ModularFeatureListRow selectedRow = selectedItem.getValue();
if (selectedRow == null) {
return;
}
updateXICPlot(selectedRow);
updateSpectrumPlot(selectedRow);
}
/**
* 删除数据源
*/
private void deleteDataSource(ActionEvent actionEvent) {
// 选中的数据源
TreeItem<DataItem> dataItemTreeItem = mainController.getTreeViewDataSource().getSelectionModel().getSelectedItem();
// 从根节点删除数据源
mainController.getTreeItemDataSourceRoot().getChildren().remove(dataItemTreeItem);
DataSource dataSource = (DataSource) dataItemTreeItem.getValue();
// 关闭右边的 Border 展示
this.rightBorderShowClose(dataSource);
// 删除对应的文件
dataSourceService.deleteDataSource(dataSource);
// 删除全局的记录
BaseConstants.allDataSources.remove(dataItemTreeItem);
}
/**
* Load children of the tree item in the background.
*
* @param treeItem the tree item.
*/
@BackgroundThread
private void lazyLoadChildren(
@NotNull TreeItem<ResourceElement> treeItem,
@Nullable Consumer<TreeItem<ResourceElement>> callback
) {
var element = treeItem.getValue();
var children = element.getChildren(extensionFilter, isOnlyFolders());
if (children == null) {
return;
}
children.sort(NAME_COMPARATOR);
EXECUTOR_MANAGER.addFxTask(() -> lazyLoadChildren(treeItem, children, callback));
}
public void setSelection(TreeItem<ConditionNode> item) {
try {
if (item == null || !(item instanceof CheckBoxTreeItem)) {
return;
}
CheckBoxTreeItem<ConditionNode> citem = (CheckBoxTreeItem<ConditionNode>) item;
ConditionNode node = item.getValue();
if (selectedTitles.contains(node.getTitle())) {
citem.setSelected(true);
return;
} else if (item.isLeaf()) {
return;
}
for (TreeItem<ConditionNode> child : item.getChildren()) {
setSelection(child);
}
} catch (Exception e) {
logger.debug(e.toString());
}
}
/**
* Fill the tree item.
*
* @param item the tree item.
*/
@FxThread
private void fill(@NotNull final TreeItem<VirtualResourceElement<?>> item) {
final VirtualResourceElement<?> element = item.getValue();
if(!element.hasChildren()) {
return;
}
final ObservableList<TreeItem<VirtualResourceElement<?>>> items = item.getChildren();
final Array<VirtualResourceElement<?>> children = element.getChildren();
children.sort(NAME_COMPARATOR);
children.forEach(child -> items.add(new TreeItem<>(child)));
items.forEach(this::fill);
}
@FXML
private void onSaveAllMaterial() {
try {
// current material could have been edited
setAttributes(selectedMaterialItem);
// save all modified materials
for (TreeItem<MaterialNode> editedMaterialItem : editedMaterialItems) {
MaterialNode node = editedMaterialItem.getValue();
Material saved = (Material) PersistenceService.instance().save(node.getMaterial());
node.setMaterial(saved);
resetGraphic(editedMaterialItem);
}
editedMaterialItems.clear();
// update category list
populateCategories();
tvMaterials.refresh();
} catch (Exception e) {
AppUtils.showErrorDialog(e);
}
}
@FXML
private void onSaveAllEntities() {
try {
// current entity could have been edited
setAttributes(selectedEntityItem);
// save all modified entities
for (TreeItem<EntityNode> editedEntityItem : editedEntityItems) {
EntityNode node = editedEntityItem.getValue();
PlantEntity entity = node.getPlantEntity();
PlantEntity saved = (PlantEntity) PersistenceService.instance().save(entity);
node.setPlantEntity(saved);
setEntityGraphic(editedEntityItem);
}
editedEntityItems.clear();
tvEntities.refresh();
} catch (Exception e) {
AppUtils.showErrorDialog(e);
}
}
@SuppressWarnings("unchecked")
public void deleteItem(TreeItem<NamedTag> item) {
if (item.getValue().parent != null) {
// named tag is in compound tag, indexed tag is in list tag
if (item.getValue().parent.getID() == 10) {
CompoundTag comp = (CompoundTag) item.getValue().parent;
comp.remove(item.getValue().name);
item.getParent().getChildren().remove(item);
} else if (item.getValue().parent.getID() == 9) {
ListTag<Tag<?>> list = (ListTag<Tag<?>>) item.getValue().parent;
int index;
for (index = 0; index < list.size(); index++) {
if (list.get(index) == item.getValue().tag) {
list.remove(index);
break;
}
}
item.getParent().getChildren().remove(item);
}
}
}
/** @param items Model's root items or body of command
* @param command Command to remove
* @return <code>true</code> if command was found and tree item was removed
*/
private boolean remove(final List<TreeItem<ScanCommand>> items,
final ScanCommand command)
{
for (TreeItem<ScanCommand> item : items)
if (item.getValue() == command)
{
items.remove(item);
return true;
}
else if (remove(item.getChildren(), command))
return true;
return false;
}
@Override
public void changed(ObservableValue<? extends TreeItem<Object>> observable, TreeItem<Object> oldValue,
TreeItem<Object> newValue) {
if (newValue == null) {
okButton.setDisable(true);
documentArea.setText("");
argumentPane.getChildren().clear();
return;
}
TreeItem<Object> item = tree.getSelectionModel().getSelectedItem();
String doc = "";
boolean disable = true;
functionItem = item;
Object value = item.getValue();
if (value instanceof Module) {
doc = ((Module) value).getDocumentation();
} else {
doc = ((Function) value).getDocumentation();
disable = false;
}
okButton.setDisable(disable);
documentArea.setText(doc);
argumentPane.getChildren().clear();
if (item.isLeaf()) {
addArguments(item);
}
}
private void assertTreeContains(final TreeItem<FileInfo> item, final Path file)
{
final Path dir = item.getValue().file.toPath();
if (! file.startsWith(dir))
{
logger.log(Level.WARNING, "Cannot check for " + file + " within " + dir);
return;
}
// Are we looking for a directory, and this one is it? Done!
if (dir.equals(file))
return;
final int dir_len = dir.getNameCount();
final File sub = new File(item.getValue().file, file.getName(dir_len).toString());
logger.log(Level.FINE, () -> "Looking for " + sub + " in " + dir);
for (TreeItem<FileInfo> child : item.getChildren())
if (sub.equals(child.getValue().file))
{
logger.log(Level.FINE,"Found it!");
if (sub.isDirectory())
assertTreeContains(child, file);
return;
}
logger.log(Level.FINE, () -> "Forcing refresh of " + dir + " to show " + sub);
Platform.runLater(() -> ((FileTreeItem)item).forceRefresh());
}
private void onModuleTrace(ActionEvent e) {
TreeItem<ModFunc> selectedItem = selectedTreeItemProperty().get();
if(selectedItem == null)
return;
if(selectedItem.getValue() == null)
return;
if(!selectedItem.getValue().isModule())
selectedItem = selectedItem.getParent();
HashSet<ModFunc> funcs = new HashSet<ModFunc>();
recurseModFuncItems(selectedItem, funcs);
toggleTraceMod(funcs);
}
public void removeFile(final RawDataFile rawDataFile) {
super.removeFile(rawDataFile);
for (TreeItem<?> df1 : rawDataRootItem.getChildren()) {
if (df1.getValue() == rawDataFile) {
rawDataRootItem.getChildren().remove(df1);
break;
}
}
}
/** Paste commands from clipboard, append after currently selected item */
void pasteFromClipboard()
{
final Clipboard clip = Clipboard.getSystemClipboard();
if (ScanCommandDragDrop.hasCommands(clip))
{
final List<ScanCommand> commands = ScanCommandDragDrop.getCommands(clip);
final TreeItem<ScanCommand> item = getSelectionModel().getSelectedItem();
final ScanCommand location = item != null ? item.getValue() : null;
undo.execute(new AddCommands(model, location, commands, true));
}
}
public void checkSelection(TreeItem<ConditionNode> item) {
try {
if (item == null || !(item instanceof CheckBoxTreeItem)) {
return;
}
CheckBoxTreeItem<ConditionNode> citem = (CheckBoxTreeItem<ConditionNode>) item;
ConditionNode node = item.getValue();
if (citem.isSelected()) {
if (finalConditions == null || finalConditions.isBlank()) {
if (node.getCondition() == null || node.getCondition().isBlank()) {
finalConditions = "";
} else {
finalConditions = " ( " + node.getCondition() + ") ";
}
} else {
if (node.getCondition() != null && !node.getCondition().isBlank()) {
finalConditions += " OR ( " + node.getCondition() + " ) ";
}
}
if (finalTitle == null || finalTitle.isBlank()) {
finalTitle = "\"" + node.getTitle() + "\"";
} else {
finalTitle += " + \"" + node.getTitle() + "\"";
}
if (!selectedTitles.contains(node.getTitle())) {
selectedTitles.add(node.getTitle());
}
return;
} else if (item.isLeaf() || !citem.isIndeterminate()) {
return;
}
for (TreeItem<ConditionNode> child : item.getChildren()) {
checkSelection(child);
}
} catch (Exception e) {
logger.debug(e.toString());
}
}
/** @param file File to move or copy
* @param target_item Destination directory's tree item
*/
private void move_or_copy(final File file, final TreeItem<FileInfo> target_item, final TransferMode transferMode)
{
final File dir = target_item.getValue().file;
// Ignore NOP move
if (file.getParentFile().equals(dir))
return;
JobManager.schedule(Messages.MoveOrCopyJobName, monitor ->
{
// System.out.println("Move " + file + " into " + dir);
final File new_name = new File(dir, file.getName());
final DirectoryMonitor mon = ((FileTreeItem)target_item).getMonitor();
try
{
if (transferMode.equals(TransferMode.MOVE))
FileHelper.move(file, dir);
else
FileHelper.copy(file, dir);
Platform.runLater(() ->
{
// System.out.println("Add tree item for " + new_name + " to " + target_item.getValue());
final ObservableList<TreeItem<FileInfo>> siblings = target_item.getChildren();
siblings.add(new FileTreeItem(mon, new_name));
FileTreeItem.sortSiblings(siblings);
});
}
catch (Exception ex)
{
final TreeTableView<FileInfo> tree = getTreeTableView();
ExceptionDetailsErrorDialog.openError(tree, Messages.MoveOrCopyAlertTitle,
MessageFormat.format(Messages.MoveOrCopyAlert, file, target_item.getValue()), ex);
// Force full refresh
Platform.runLater(() ->
tree.setRoot(new FileTreeItem(mon, tree.getRoot().getValue().file)) );
}
});
}
private void trigger(Event t) {
BibleSearchTreeView tv = (BibleSearchTreeView) t.getSource();
TreeItem<BibleInterface> ti = tv.getSelectionModel().getSelectedItem();
if (ti != null) {
if (ti.getValue() instanceof BibleVerse) {
textPane.getChildren().clear();
BibleChapter chapter = (BibleChapter) ti.getValue().getParent();
BibleVerse[] verses = chapter.getVerses();
BibleVerse selected = (BibleVerse) ti.getValue();
int x = selected.getNum() - 1;
for (int i = 0; i < verses.length; i++) {
Text text = new Text(verses[i].toString() + " ");
text.getStyleClass().add("text");
if (i == x) {
text.setFont(Font.font("Sans", FontWeight.BOLD, 14));
} else {
text.setFont(Font.font("Sans", 14));
}
textPane.getChildren().add(text);
text.wrappingWidthProperty().bind(sp.widthProperty().subtract(20)); //-20 to account for scroll bar width
}
} else {
ti.setExpanded(!ti.isExpanded());
}
} else {
tv.selectionModelProperty().get().selectFirst();
}
}
private void copy(ObservableList<TreeItem<Resource>> selectedItems) {
Clipboard clipboard = Clipboard.getSystemClipboard();
Map<DataFormat, Object> content = new HashMap<>();
for (TreeItem<Resource> treeItem : selectedItems) {
Resource resource = treeItem.getValue();
if (resource != null) {
if (!resource.copy(content)) {
FXUIUtils.showMessageDialog(null, "Clipboard operation failed", "Unhandled resource selection",
AlertType.ERROR);
}
}
}
clipboard.setContent(content);
clipboardOperation = Operation.COPY;
}
public static @Nonnull List<FeatureTable> getSelectedFeatureTables() {
final ArrayList<FeatureTable> list = new ArrayList<>();
final TreeView<Object> featureTableTree = mainWindowController.getFeatureTree();
for (TreeItem<Object> item : featureTableTree.getSelectionModel().getSelectedItems()) {
if (!(item.getValue() instanceof FeatureTable))
continue;
FeatureTable ft = (FeatureTable) item.getValue();
list.add(ft);
}
return list;
}
private void onNewFolder() {
MarathonInputStage testNameStage = new MarathonInputStage("Folder name", "Create a new folder",
FXUIUtils.getIcon("fldr_closed")) {
@Override
protected String validateInput(String name) {
String errorMessage = null;
if (name.length() == 0 || name.trim().isEmpty()) {
errorMessage = "Enter a valid name";
}
return errorMessage;
}
@Override
protected String getInputFiledLabelText() {
return "Enter a folder name: ";
}
@Override
protected void setDefaultButton() {
okButton.setDefaultButton(true);
}
};
FolderNameHandler testNameHandler = new FolderNameHandler();
testNameStage.setInputHandler(testNameHandler);
testNameStage.getStage().showAndWait();
TreeItem<File> selectedItem = parentTreeView.getSelectionModel().getSelectedItem();
String testName = testNameHandler.getTestName();
if (testName == null || "".equals(testName)) {
return;
}
File file = new File(selectedItem.getValue(), testName);
if (file.exists()) {
FXUIUtils.showMessageDialog(getStage(), "Folder with name '" + testName + "' already exists.", "File exists",
AlertType.INFORMATION);
} else {
selectedItem.getChildren().add(new TreeItem<File>(file));
if (!file.mkdir()) {
FXUIUtils.showMessageDialog(getStage(), "Couldn't create folder with name '" + testName + "'", "Creation Fail",
AlertType.ERROR);
}
}
}