Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tablemap;
- import javafx.application.Application;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.event.ActionEvent;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import java.util.AbstractMap;
- import java.util.Map;
- import javafx.beans.property.DoubleProperty;
- import javafx.beans.property.IntegerProperty;
- import javafx.beans.property.SimpleDoubleProperty;
- import javafx.beans.property.SimpleIntegerProperty;
- import javafx.beans.property.StringProperty;
- import javafx.collections.FXCollections;
- import javafx.collections.ListChangeListener;
- import javafx.collections.MapChangeListener;
- import javafx.collections.ObservableList;
- import javafx.collections.ObservableMap;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableColumn.CellEditEvent;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.TextFieldTableCell;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Priority;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- public class TableMap extends Application {
- @Override
- public void start(Stage primaryStage) {
- VBox root = new VBox();
- final ObservableMap<String, LineItem> obsMap = FXCollections.observableHashMap();
- for (int i = 0; i < 3; i++)
- obsMap.put(String.valueOf(i), new LineItem(String.valueOf(i),"i"));
- final MapTableView<String,LineItem> mtv = new MapTableView(obsMap);
- final TableColumn<Map.Entry<String,LineItem>,String> keyCol = new TableColumn("Key");
- keyCol.setCellValueFactory(
- (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) ->
- new SimpleStringProperty(p.getValue().getKey()));
- keyCol.setCellFactory(TextFieldTableCell.forTableColumn());
- keyCol.setOnEditCommit((CellEditEvent<Map.Entry<String,LineItem>, String> t) -> {
- final String oldKey = t.getOldValue();
- final LineItem oldLineItem = obsMap.get(oldKey);
- obsMap.remove(oldKey);//should remove from list but maybe doesn't always
- obsMap.put(t.getNewValue(),oldLineItem);
- });
- final TableColumn<Map.Entry<String,LineItem>,String> lineNoCol = new TableColumn("Line No");
- lineNoCol.setCellValueFactory(
- (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) ->
- new SimpleStringProperty(p.getValue().getValue().getLineNo()));
- final TableColumn<Map.Entry<String,LineItem>,String> descCol = new TableColumn("Desc");
- descCol.setCellValueFactory(
- (TableColumn.CellDataFeatures<Map.Entry<String,LineItem>, String> p) ->
- new SimpleStringProperty(p.getValue().getValue().getDesc()));
- descCol.setCellFactory(TextFieldTableCell.forTableColumn());
- descCol.setOnEditCommit((CellEditEvent<Map.Entry<String,LineItem>, String> t) -> {
- t.getTableView().getItems().get(t.getTablePosition().getRow())
- .getValue().setDesc(t.getNewValue());
- });
- mtv.getColumns().addAll(keyCol,lineNoCol, descCol);
- mtv.setEditable(true);
- mtv.getSelectionModel().setCellSelectionEnabled(true);
- mtv.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
- final Button btnOut = new Button("out");
- btnOut.setOnAction((ActionEvent t) -> {
- System.out.println("MAP");
- for (Map.Entry<String,LineItem> me : obsMap.entrySet()){
- System.out.print("key->"+me.getKey()+" entry->"+me.getValue().toCSVString());
- }
- System.out.println("LIST");
- for (Map.Entry<String,LineItem> me : mtv.getItems()){
- System.out.print("key->"+me.getKey()+" entry->"+me.getValue().toCSVString());
- }
- System.out.println("");
- });
- final Button btnMapAdd = new Button("Map add");
- btnMapAdd.setOnAction((ActionEvent t) -> {
- obsMap.put("from map code", new LineItem("5","hi from map btn"));
- });
- final Button btnListAdd = new Button("List add");
- btnListAdd.setOnAction((ActionEvent t) -> {
- //I could just call add to map instead of list on list.add
- mtv.addUnique("from list code", new LineItem("10","hi from list btn"));
- });
- final Button btnMapDel = new Button("Map del");
- btnMapDel.setOnAction((ActionEvent t) -> {
- LineItem remove = obsMap.remove("from map code");
- });
- final Button btnListDel = new Button("List del");
- btnListDel.setOnAction((ActionEvent t) -> {
- mtv.getItems().remove(0);
- });
- final Button btnTblDel = new Button("Table row del");
- btnTblDel.setOnAction((ActionEvent t) -> {
- mtv.getItems().remove(mtv.getSelectionModel().getSelectedIndex());
- });
- final Button btnMapOps = new Button("Map ops");
- btnMapOps.setOnAction((ActionEvent t) -> {
- long start = System.currentTimeMillis();
- mtv.removeMapListener();
- for (int i = 0; i < 100000; i++)
- obsMap.put(String.valueOf(i), new LineItem(String.valueOf(i),"hi from map ops"));
- for (int i = 0; i < 99999; i++)
- obsMap.remove(String.valueOf(i));
- mtv.resetMapListener();
- System.out.println("time w/out listeners->"+(System.currentTimeMillis()-start));
- start = System.currentTimeMillis();
- for (int i = 100000; i < 200000; i++)
- obsMap.put(String.valueOf(i), new LineItem(String.valueOf(i),"hi from map ops"));
- for (int i = 100000; i < 199999; i++)
- obsMap.remove(String.valueOf(i));
- System.out.println("time with listeners->"+(System.currentTimeMillis()-start));
- });
- final HBox hbox = new HBox();
- hbox.getChildren().addAll(btnOut,btnMapAdd,btnListAdd,btnMapDel,btnListDel,btnTblDel,btnMapOps);
- root.getChildren().addAll(mtv,hbox);
- VBox.setVgrow(mtv, Priority.ALWAYS);
- final Scene scene = new Scene(root, 600, 500);
- primaryStage.setTitle("Map Table Test");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- }
- class MapTableView<K,V> extends TableView<Map.Entry<K,V>>{
- private final ObservableList<Map.Entry<K,V>> obsList;
- private final ObservableMap<K,V> map;
- private final MapChangeListener<K,V> mapChange;
- private final ListChangeListener<Map.Entry<K,V>> listChange;
- public MapTableView(ObservableMap<K,V> map) {
- this.map = map;
- obsList = FXCollections.observableArrayList(map.entrySet());
- setItems(obsList);
- mapChange = new MapChangeListener<K, V>() {
- @Override
- public void onChanged(MapChangeListener.Change<? extends K, ? extends V> change) {
- obsList.removeListener(listChange);
- if (change.wasAdded())
- obsList.add(new AbstractMap.SimpleEntry(change.getKey(),change.getValueAdded()));
- if (change.wasRemoved()){
- //obsList.remove(new AbstractMap.SimpleEntry(change.getKey(),change.getValueRemoved()));
- // ^ doesn't work always, use loop instead
- for (Map.Entry<K,V> me : obsList){
- if (me.getKey().equals(change.getKey())){
- obsList.remove(me);
- break;
- }
- }
- }
- obsList.addListener(listChange);
- }
- };
- listChange = (ListChangeListener.Change<? extends Map.Entry<K, V>> change) -> {
- map.removeListener(mapChange);
- while (change.next()){
- //maybe check for uniqueness here
- if (change.wasAdded()) for (Map.Entry<K, V> me: change.getAddedSubList())
- map.put(me.getKey(),me.getValue());
- if (change.wasRemoved()) for (Map.Entry<K, V> me: change.getRemoved())
- map.remove(me.getKey());
- }
- map.addListener(mapChange);
- };
- map.addListener(mapChange);
- obsList.addListener(listChange);
- }
- //adding to list should be unique
- public void addUnique(K key, V value){
- boolean isFound = false;
- //if a duplicate key just change the value
- for (Map.Entry<K,V> me : getItems()){
- if (me.getKey().equals(key)){
- isFound = true;
- me.setValue(value);
- break;//only first match
- }
- }
- if (!isFound) // add new entry
- getItems().add(new AbstractMap.SimpleEntry<>(key,value));
- }
- //for doing lenghty map operations
- public void removeMapListener(){
- map.removeListener(mapChange);
- }
- //for resyncing list to map after many changes
- public void resetMapListener(){
- obsList.removeListener(listChange);
- obsList.clear();
- obsList.addAll(map.entrySet());
- obsList.addListener(listChange);
- map.addListener(mapChange);
- }
- }
- class LineItem {
- private final StringProperty lineNo = new SimpleStringProperty();//sym
- private final StringProperty desc = new SimpleStringProperty();//type->gives yld cpn
- private final StringProperty formula = new SimpleStringProperty();//mer ??
- private final DoubleProperty amount = new SimpleDoubleProperty();
- private final IntegerProperty sort = new SimpleIntegerProperty();//mult
- private final StringProperty note = new SimpleStringProperty();
- public LineItem() {
- }
- public LineItem(String ln, String dsc, String frm, double amt, int srt) {
- lineNo.set(ln);desc.set(dsc);formula.set(frm);amount.set(amt);sort.set(srt);
- }
- public LineItem(String ln, String dsc) {
- lineNo.set(ln); desc.set(dsc);
- }
- public String getLineNo() {return (lineNo.getValue() != null) ?lineNo.get():"";}
- public void setLineNo(String lineNo) {this.lineNo.set(lineNo);}
- public StringProperty lineNoProperty() {return lineNo;}
- public String getDesc() {return (desc.getValue() != null) ?desc.get():"";}
- public void setDesc(String desc) {this.desc.set(desc);}
- public StringProperty descProperty() {return desc;}
- //mebbe return "0" as it will get parsed to 0
- public String getFormula() {return (formula.getValue() != null) ? formula.get():"0";}
- public void setFormula(String formula) {this.formula.set(formula);}
- public StringProperty formulaProperty() {return formula;}
- //return 0 to prevent nfe
- public double getAmount() {return (amount.getValue() != null) ? amount.doubleValue():0d;}
- public void setAmount(double amount) {this.amount.set(amount);}
- public DoubleProperty amountProperty() {return amount;}
- //return 0 to prevent nfe
- public void setSort(Integer sort) {this.sort.set(sort);}
- public Integer getSort() {return (sort.getValue() != null) ? sort.getValue():0;}
- public IntegerProperty sortProperty() {return sort;}
- //return "" if null
- public String getNote() {return (note.getValue() != null) ? note.get():"";}
- public void setNote(String note) {this.note.set(note);}
- public StringProperty noteProperty() {return note;}
- public String toCSVString(){
- //todo test for , and wrap in quotes
- return lineNo.getValueSafe()+","+
- desc.getValueSafe()+","+
- "\""+formula.getValueSafe()+"\","+ //wrap in quotes
- amount.doubleValue()+","+ //no commas anywhere else
- sort.intValue()+","+
- "\""+note.getValueSafe()+"\""+//wrap in quotes
- "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement