MainWindow.java (4551B)
1 package population; 2 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.Arrays; 7 import java.util.List; 8 import javafx.application.Platform; 9 import javafx.collections.FXCollections; 10 import javafx.collections.ObservableList; 11 import javafx.geometry.Insets; 12 import javafx.geometry.Pos; 13 import javafx.scene.Scene; 14 import javafx.scene.control.Alert; 15 import javafx.scene.control.Alert.AlertType; 16 import javafx.scene.control.Button; 17 import javafx.scene.control.ButtonType; 18 import javafx.scene.control.ComboBox; 19 import javafx.scene.control.TableColumn; 20 import javafx.scene.control.TableRow; 21 import javafx.scene.control.TableView; 22 import javafx.scene.control.cell.PropertyValueFactory; 23 import javafx.scene.layout.HBox; 24 import javafx.scene.layout.VBox; 25 import javafx.stage.FileChooser; 26 import javafx.stage.FileChooser.ExtensionFilter; 27 import javafx.stage.Stage; 28 29 public class MainWindow { 30 private final int WIDTH = 1000; 31 private final int HEIGHT = 900; 32 private List<Country> countries; 33 private TableView<Country> tbv; 34 private HBox btns; 35 private Stage stg; 36 private String title; 37 38 MainWindow(Stage stg, String title) { 39 this.stg = stg; 40 this.title = title; 41 } 42 43 public void setup(String path) { 44 ExcelParser ep; 45 46 try { 47 ep = new ExcelParser(path); 48 countries = ep.get_countries(); 49 } catch (FileNotFoundException e) { 50 err(path + ": no such file"); 51 return; 52 53 } catch (IOException e) { 54 err(path + ": io error"); 55 return; 56 } catch (Exception e) { 57 err(e.getMessage()); 58 return; 59 } 60 mktable(); 61 mkbtns(); 62 stg.setTitle(title + " - " + path); 63 stg.setWidth(WIDTH); 64 stg.setHeight(HEIGHT); 65 } 66 67 public void show() { 68 stg.setScene(new Scene(mkvbox(tbv, btns))); 69 stg.show(); 70 } 71 72 public void err(String errstr) { 73 Alert alert; 74 75 alert = new Alert(AlertType.ERROR, errstr, ButtonType.CLOSE); 76 alert.showAndWait(); 77 } 78 79 private void mkbtns() { 80 ComboBox<String> cb; 81 Button btn_file; 82 Button btn_chart; 83 Button btn_exit; 84 85 btn_file = new Button("Load File"); 86 btn_file.setOnAction(ev -> { 87 String path; 88 FileChooser fc = new FileChooser(); 89 90 fc.setTitle("Load File"); 91 fc.getExtensionFilters().add( 92 new ExtensionFilter("XLSX Files", "*.xlsx")); 93 94 path = fc.showOpenDialog(stg).getAbsolutePath(); 95 setup(path); 96 show(); 97 }); 98 99 btn_chart = new Button("Chart"); 100 btn_chart.setOnAction(ev -> { 101 Chart chart = new Chart(countries); 102 chart.show(); 103 }); 104 105 btn_exit = new Button("Exit"); 106 btn_exit.setOnAction(ev -> { 107 Platform.exit(); 108 System.exit(0); 109 }); 110 111 cb = new ComboBox<String>(); 112 for (int i = Country.STARTING_YEAR; i <= Country.LAST_YEAR; i++) 113 cb.getItems().add(String.valueOf(i)); 114 cb.setPromptText("Year"); 115 cb.setOnAction(ev -> { 116 for (Country ctry : countries) { 117 ctry.setCurpop(Integer.valueOf(cb.getValue())); 118 tbv.refresh(); 119 } 120 }); 121 122 btns = new HBox(); 123 btns.getChildren().addAll(btn_file, cb, btn_chart, btn_exit); 124 btns.setSpacing(5); 125 btns.setAlignment(Pos.CENTER); 126 } 127 128 private void mktable() { 129 TableColumn<Country, String> tbc; 130 List<Column> cols; 131 ObservableList<Country> addrs; 132 133 cols = new ArrayList<Column>(Arrays.asList( 134 new Column("Index", "index"), 135 new Column("Variant", "variant"), 136 new Column("Country", "name"), 137 new Column("Notes", "notes"), 138 new Column("Country Code", "ctrycode"), 139 new Column("Type", "type"), 140 new Column("Parent Code", "pntcode"), 141 new Column("Population", "curpop") 142 )); 143 tbv = new TableView<Country>(); 144 addrs = FXCollections.observableArrayList(countries); 145 for (Column col : cols) { 146 tbc = new TableColumn<Country, String>(col.get_name()); 147 /* Why is this line so long? */ 148 tbc.setCellValueFactory( 149 new PropertyValueFactory<Country, String>( 150 col.get_fieldname())); 151 tbv.getColumns().add(tbc); 152 } 153 tbv.setRowFactory(tv -> { 154 TableRow<Country> row = new TableRow<Country>(); 155 row.setOnMouseClicked(ev -> { 156 if (ev.getClickCount() == 2 && !row.isEmpty()) { 157 Country c = row.getItem(); 158 Toast toast = new Toast(c, stg); 159 toast.show(); 160 } 161 }); 162 return row; 163 }); 164 tbv.setItems(addrs); 165 /* Fit to screen. */ 166 tbv.prefWidthProperty().bind(stg.widthProperty()); 167 tbv.prefHeightProperty().bind(stg.heightProperty()); 168 } 169 170 private VBox mkvbox(TableView<Country> tbv, HBox btns) { 171 VBox vb = new VBox(); 172 173 vb.setSpacing(5); 174 vb.setPadding(new Insets(10, 10, 10, 10)); 175 vb.getChildren().addAll(tbv, btns); 176 return vb; 177 } 178 }