/* * Copyright (c) 2012, 2013 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.chart.PieChart; import javafx.scene.chart.PieChart.Data; import javafx.scene.control.ContextMenuBuilder; import javafx.scene.control.MenuItemBuilder; import javafx.scene.control.TableView; import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn.CellDataFeatures; import javafx.scene.control.cell.TextFieldTableCell; import javafx.scene.input.ContextMenuEvent; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCodeCombinationBuilder; import javafx.scene.input.KeyCombination; import javafx.util.Callback; import javafx.util.StringConverter; /** * * @author akouznet */ public class PieChartDataVisualizer extends TableView { PieChart chart; public PieChartDataVisualizer(PieChart chart) { this.chart = chart; setItems(chart.getData()); setEditable(true); setMinHeight(100); setMinWidth(100); chart.dataProperty().addListener(new ChangeListener>() { @Override public void changed(ObservableValue> ov, ObservableList t, ObservableList t1) { setItems(t1); } }); TableColumn nameColumn = new TableColumn<>("Name"); nameColumn.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(CellDataFeatures p) { return p.getValue().nameProperty(); } }); nameColumn.setCellFactory(TextFieldTableCell.forTableColumn()); nameColumn.setEditable(true); nameColumn.setSortable(false); nameColumn.setMinWidth(80); TableColumn pieValueColumn = new TableColumn<>("PieValue"); pieValueColumn.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(CellDataFeatures p) { return p.getValue().pieValueProperty(); } }); pieValueColumn.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter() { @Override public String toString(Number t) { return t == null ? null : t.toString(); } @Override public Number fromString(String string) { if (string == null) { return null; } return (Double) new Double(string); } })); pieValueColumn.setEditable(true); pieValueColumn.setSortable(false); pieValueColumn.setMinWidth(80); // new KeyCodeCombination(KeyCode.DELETE, KeyCombination.CONTROL_DOWN); setOnContextMenuRequested(new EventHandler() { @Override public void handle(ContextMenuEvent t) { } }); setContextMenu(ContextMenuBuilder.create() .items( MenuItemBuilder.create() .text("Insert item") .accelerator(new KeyCodeCombination(KeyCode.INSERT, KeyCombination.CONTROL_DOWN)) // .accelerator(KeyCodeCombinationBuilder.create().control(KeyCombination.ModifierValue.DOWN).code(KeyCode.DELETE).build()) .onAction(new EventHandler() { @Override public void handle(ActionEvent t) { System.out.println("Add Item t = " + t); } }) .build(), MenuItemBuilder.create() .text("Delete item") .accelerator(new KeyCodeCombination(KeyCode.DELETE, KeyCombination.CONTROL_DOWN)) // .accelerator(KeyCodeCombinationBuilder.create().control(KeyCombination.ModifierValue.DOWN).code(KeyCode.DELETE).build()) .onAction(new EventHandler() { @Override public void handle(ActionEvent t) { System.out.println("Delete Item t = " + t); } }) .build()) .build()); getColumns().setAll(nameColumn, pieValueColumn); } }