-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
343 lines (280 loc) · 13.8 KB
/
Main.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package application;
import model.Unit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class Main extends Application {
//Creates the lists
ObservableList<Unit> factionList = FXCollections.observableArrayList();
ObservableList<Unit> warbandList = FXCollections.observableArrayList();
// Puts some test data in the lists
public Main() {
factionList.add(new Unit("Skelleton",5,4,9,4,1,6,9));
factionList.add(new Unit("Ghoul",6,4,010,4,1,6,7));
factionList.add(new Unit("Zombie",4,5,1,3,1,6,5));
factionList.add(new Unit("Wraith",4,5,19,3,1,6,5));
factionList.add(new Unit("Spectre",4,5,1,3,1,6,5));
warbandList.add(new Unit("Sam the Skelly",5,4,0,4,1,6,9));
}
@Override
public void start(Stage mainStage) {
mainStage.setTitle("Star-Struck City Warband Calculator");
BorderPane borderpane = new BorderPane();
Scene scene = new Scene(borderpane, 1000, 400);
//Create the SplitPane
SplitPane splitPane = new SplitPane();
splitPane.setPrefSize(800, 400);
//Creates the tables
final TableView<Unit> table1 = new TableView<>(
factionList
// FXCollections.observableArrayList(
// new Unit("Skellies",5,4,9,4,1,6,9),
// new Unit("Ghoulies",6,4,010,4,1,6,7),
// new Unit("Zombies",4,5,1,3,1,6,5),
// new Unit("Wraiths",4,5,19,3,1,6,5),
// new Unit("Spectres",4,5,1,3,1,6,5)
// )
);
final TableView<Unit> table2 = new TableView<>(
warbandList
// FXCollections.observableArrayList(
// new Unit("Sam the Skelly",5,4,0,4,1,6,9),
// new Unit("Gustav the Ghoulie",5,4,5,4,1,6,9),
// new Unit("Zanders the Zombie",5,4,010,4,1,6,9),
// new Unit("Wally the Wraith",5,4,5,4,1,6,9),
// new Unit("Scott the Spectre",5,4,5,4,1,6,9)
// )
);
//Defines the table columns
//Columns for table 1
TableColumn<Unit,String> unitNameCol = new TableColumn<>("Unit");
unitNameCol.setCellValueFactory(new PropertyValueFactory("unitName"));
unitNameCol.setMinWidth(100);
//unitNameCol.prefWidthProperty().bind(table1.widthProperty().divide(3));
TableColumn<Unit,Integer> speedCol = new TableColumn<>("Spd");
speedCol.setCellValueFactory(new PropertyValueFactory("speed"));
//speedCol.setPrefWidth(35.0);
//speedCol.setMaxWidth(100.0);
//speedCol.setResizable(false);
//speedCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
TableColumn<Unit,Integer> meleeCol = new TableColumn<>("Me");
meleeCol.setCellValueFactory(new PropertyValueFactory("melee"));
//meleeCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
TableColumn<Unit,Integer> rangedCol = new TableColumn<>("Ra");
rangedCol.setCellValueFactory(new PropertyValueFactory("ranged"));
//rangedCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
TableColumn<Unit,Integer> defenseCol = new TableColumn<>("Def");
defenseCol.setCellValueFactory(new PropertyValueFactory("defense"));
//defenseCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
TableColumn<Unit,Integer> attackCol = new TableColumn<>("Att");
attackCol.setCellValueFactory(new PropertyValueFactory("attack"));
//attackCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
TableColumn<Unit,Integer> toughnessCol = new TableColumn<>("To");
toughnessCol.setCellValueFactory(new PropertyValueFactory("toughness"));
//toughnessCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
TableColumn<Unit,Integer> costCol = new TableColumn<>("Cost");
costCol.setCellValueFactory(new PropertyValueFactory("cost"));
//costCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
TableColumn<Unit, Boolean> actionCol = new TableColumn<>("Action");
actionCol.setSortable(false);
actionCol.setMinWidth(35);
//actionCol.prefWidthProperty().bind(table1.widthProperty().divide(1));
//Columns for table 2
TableColumn<Unit,String> unitNameCol2 = new TableColumn<>("Unit");
unitNameCol2.setCellValueFactory(new PropertyValueFactory("unitName"));
unitNameCol2.setMinWidth(100);
//unitNameCol2.isEditable();
TableColumn<Unit,Integer> speedCol2 = new TableColumn<>("Spd");
speedCol2.setCellValueFactory(new PropertyValueFactory("speed"));
TableColumn<Unit,Integer> meleeCol2 = new TableColumn<>("Me");
meleeCol2.setCellValueFactory(new PropertyValueFactory("melee"));
TableColumn<Unit,Integer> rangedCol2 = new TableColumn<>("Ra");
rangedCol2.setCellValueFactory(new PropertyValueFactory("ranged"));
TableColumn<Unit,Integer> defenseCol2 = new TableColumn<>("Def");
defenseCol2.setCellValueFactory(new PropertyValueFactory("defense"));
TableColumn<Unit,Integer> attackCol2 = new TableColumn<>("Att");
attackCol2.setCellValueFactory(new PropertyValueFactory("attack"));
TableColumn<Unit,Integer> toughnessCol2 = new TableColumn<>("To");
toughnessCol2.setCellValueFactory(new PropertyValueFactory("toughness"));
TableColumn<Unit,Integer> costCol2 = new TableColumn<>("Cost");
costCol2.setCellValueFactory(new PropertyValueFactory("cost"));
TableColumn<Unit, Boolean> actionCol2 = new TableColumn<>("Action");
actionCol2.setSortable(false);
actionCol2.setMinWidth(35);
// define a simple boolean cell value for the action column so that the column will only be shown for non-empty rows for table 1
actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Unit, Boolean>, ObservableValue<Boolean>>() {
@Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Unit, Boolean> features) {
return new SimpleBooleanProperty(features.getValue() != null);
}
});
// create a cell value factory with an add button for each row in the table for table 1
actionCol.setCellFactory(new Callback<TableColumn<Unit, Boolean>, TableCell<Unit, Boolean>>() {
@Override public TableCell<Unit, Boolean> call(TableColumn<Unit, Boolean> unitBooleanTableColumn) {
return new AddUnitCell(mainStage, table1);
}
});
// define a simple boolean cell value for the action column so that the column will only be shown for non-empty rows for table 2
actionCol2.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Unit, Boolean>, ObservableValue<Boolean>>() {
@Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Unit, Boolean> features) {
return new SimpleBooleanProperty(features.getValue() != null);
}
});
// create a cell value factory with an add button for each row in the table for table 2
actionCol2.setCellFactory(new Callback<TableColumn<Unit, Boolean>, TableCell<Unit, Boolean>>() {
@Override public TableCell<Unit, Boolean> call(TableColumn<Unit, Boolean> unitBooleanTableColumn) {
return new ViewUnitCell(mainStage, table1);
}
});
table1.getColumns().setAll(unitNameCol,speedCol,meleeCol,rangedCol,defenseCol,attackCol,toughnessCol,costCol,actionCol);
table1.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
table2.getColumns().setAll(unitNameCol2,speedCol2,meleeCol2,rangedCol2,defenseCol2,attackCol2,toughnessCol2,costCol2,actionCol2);
table2.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
// The menu bar in the top area
MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(mainStage.widthProperty());
Menu fileMenu = new Menu("_File");
fileMenu.setMnemonicParsing(true);
MenuItem newMenuItem = new MenuItem("New");
MenuItem exitMenuItem = new MenuItem("Exit");
exitMenuItem.setOnAction(actionevent -> Platform.exit());
fileMenu.getItems().addAll(newMenuItem, new SeparatorMenuItem(),exitMenuItem);
Menu editMenu = new Menu("_Edit");
editMenu.setMnemonicParsing(true);
MenuItem copyMenuItem = new MenuItem("Copy");
//copyMenuItem.setMnemonicParsing(true);
//copyMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN));
MenuItem selectAllMenuItem = new MenuItem("Select all");
MenuItem selectFontMenuItem = new MenuItem("Select font");
editMenu.getItems().addAll(copyMenuItem, selectAllMenuItem, new SeparatorMenuItem(),selectFontMenuItem);
Menu aboutMenu = new Menu("_About");
aboutMenu.setMnemonicParsing(true);
MenuItem helpMenuItem = new MenuItem("Help");
MenuItem aboutMenuItem = new MenuItem("About");
aboutMenu.getItems().addAll(helpMenuItem, aboutMenuItem);
menuBar.getMenus().addAll(fileMenu,editMenu,aboutMenu);
Label summa = new Label("Summa av Warband");
// Puts the components onto the borderpane
borderpane.setCenter(splitPane);
borderpane.setTop(menuBar);
borderpane.setBottom(summa);
splitPane.getItems().add(table1);
splitPane.getItems().add(table2);
mainStage.setScene(scene);
mainStage.show();
}
private class ViewUnitCell extends TableCell<Unit, Boolean> {
// a button for viewing the stats of a unit.
final Button viewButton = new Button("View");
// pads and centers the view button in the cell.
final StackPane paddedButton = new StackPane();
// records the y pos of the last button press so that the view unit dialog can be shown next to the cell.
final DoubleProperty buttonY = new SimpleDoubleProperty();
/**
* ViewUnitCell constructor
* @param stage the stage in which the table is placed.
* @param table the table to which a unit can be viewed.
*/
ViewUnitCell(final Stage stage, final TableView table) {
paddedButton.setPadding(new Insets(3));
paddedButton.getChildren().add(viewButton);
viewButton.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
buttonY.set(mouseEvent.getScreenY());
}
});
viewButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
}
});
}
/** places a view button in the row only if the row is not empty. */
@Override protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(paddedButton);
} else {
setGraphic(null);
}
}
}
/** A table cell containing a button for adding a unit */
private class AddUnitCell extends TableCell<Unit, Boolean> {
// a button for adding a new Unit.
final Button addButton = new Button("Add");
// pads and centers the add button in the cell.
final StackPane paddedButton = new StackPane();
/**
* AddUnitCell constructor
* @param stage the stage in which the table is placed.
* @param table the table to which a unit can be added.
*/
AddUnitCell(final Stage stage, final TableView<Unit> table) {
paddedButton.setPadding(new Insets(3));
paddedButton.getChildren().add(addButton);
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
//ObservableList<Unit> test = table.getFocusModel().getSelectedItems();
//System.out.println(test.getUnitName());
Unit selectedUnit = table.getSelectionModel().getSelectedItem();
System.out.println(selectedUnit.getUnitName());
warbandList.add(selectedUnit);
}
});
}
/** places an add button in the row only if the row is not empty. */
@Override protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(paddedButton);
} else {
setGraphic(null);
}
}
}
public static void main(String[] args) {
launch(args);
}
}