|
1 | | -package aima.gui.fx.framework; |
2 | | - |
3 | | -import javafx.application.Platform; |
4 | | -import javafx.beans.binding.Bindings; |
5 | | -import javafx.beans.property.DoubleProperty; |
6 | | -import javafx.collections.ObservableList; |
7 | | -import javafx.scene.control.Menu; |
8 | | -import javafx.scene.control.MenuBar; |
9 | | -import javafx.scene.control.MenuItem; |
10 | | -import javafx.scene.input.KeyCode; |
11 | | -import javafx.scene.input.KeyCodeCombination; |
12 | | -import javafx.scene.input.KeyCombination; |
13 | | -import javafx.scene.layout.BorderPane; |
14 | | -import javafx.scene.layout.Pane; |
15 | | -import javafx.scene.text.Font; |
16 | | -import javafx.stage.Stage; |
17 | | - |
18 | | -/** |
19 | | - * Builder for integrated applications. To create an integrated application, |
20 | | - * just create a builder, define a title, register apps (integrable |
21 | | - * applications) and progs (console applications) and place the result into a |
22 | | - * scene of a stage. |
23 | | - * |
24 | | - * @author Ruediger Lunde |
25 | | - */ |
26 | | -public class IntegratedAppPaneBuilder { |
27 | | - |
28 | | - private MenuBar menuBar = new MenuBar(); |
29 | | - private Menu appsMenu = new Menu("Apps"); |
30 | | - private Menu progsMenu = new Menu("Progs"); |
31 | | - private String title = ""; |
32 | | - |
33 | | - private IntegratedAppPaneCtrl paneCtrl; |
34 | | - |
35 | | - public IntegratedAppPaneBuilder() { |
36 | | - paneCtrl = new IntegratedAppPaneCtrl(); |
37 | | - final DoubleProperty scale = paneCtrl.scaleProperty(); |
38 | | - MenuItem incScaleItem = new MenuItem("Inc Scale"); |
39 | | - incScaleItem.setOnAction(ev -> scale.set(trunc(scale.get() * 1.3))); |
40 | | - incScaleItem.setAccelerator(new KeyCodeCombination(KeyCode.PLUS, KeyCombination.CONTROL_DOWN)); |
41 | | - MenuItem decScaleItem = new MenuItem("Dec Scale"); |
42 | | - decScaleItem.setOnAction(ev -> scale.set(trunc(scale.get() / 1.3))); |
43 | | - decScaleItem.setAccelerator(new KeyCodeCombination(KeyCode.MINUS, KeyCombination.CONTROL_DOWN)); |
44 | | - MenuItem exitItem = new MenuItem("Exit"); |
45 | | - exitItem.setOnAction(ev -> Platform.exit()); |
46 | | - |
47 | | - Menu fileMenu = new Menu("File"); |
48 | | - fileMenu.getItems().addAll(incScaleItem, decScaleItem, exitItem); |
49 | | - menuBar.getMenus().addAll(fileMenu, appsMenu, progsMenu); |
50 | | - menuBar.styleProperty().bind(Bindings.concat("-fx-font-size: ", |
51 | | - paneCtrl.scaleProperty().multiply(Font.getDefault().getSize()).asString())); |
52 | | - } |
53 | | - |
54 | | - public void defineTitle(String title) { |
55 | | - this.title = title; |
56 | | - } |
57 | | - |
58 | | - public void registerApp(Class<? extends IntegrableApplication> appClass) { |
59 | | - final IntegratedAppPaneCtrl ctrl = paneCtrl; |
60 | | - MenuItem item = new MenuItem(appClass.getSimpleName()); |
61 | | - item.setOnAction(ev -> ctrl.startApp(appClass)); |
62 | | - addToMenu(appsMenu, appClass.getPackage().getName(), item); |
63 | | - } |
64 | | - |
65 | | - public void registerProg(Class<?> progClass) { |
66 | | - final IntegratedAppPaneCtrl ctrl = paneCtrl; |
67 | | - MenuItem item = new MenuItem(progClass.getSimpleName()); |
68 | | - item.setOnAction(ev -> ctrl.startProg(progClass)); |
69 | | - addToMenu(progsMenu, progClass.getPackage().getName(), item); |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * Adds a menu bar and a scalable container pane to the provided root pane and |
74 | | - * returns a controller instance containing user interface logic. |
75 | | - */ |
76 | | - public IntegratedAppPaneCtrl getResultFor(BorderPane root, Stage stage) { |
77 | | - |
78 | | - // create a pane, content is affected by scale |
79 | | - final DoubleProperty scale = paneCtrl.scaleProperty(); |
80 | | - BorderPane appPane = new BorderPane(); |
81 | | - appPane.scaleXProperty().bind(scale); |
82 | | - appPane.scaleYProperty().bind(scale); |
83 | | - |
84 | | - Pane appPaneContainer = new Pane(); |
85 | | - appPaneContainer.getChildren().add(appPane); |
86 | | - appPane.prefWidthProperty().bind(appPaneContainer.widthProperty().divide(scale)); |
87 | | - appPane.prefHeightProperty().bind(appPaneContainer.heightProperty().divide(scale)); |
88 | | - appPane.translateXProperty() |
89 | | - .bind(appPaneContainer.widthProperty().subtract(appPane.prefWidthProperty()).divide(2)); |
90 | | - appPane.translateYProperty() |
91 | | - .bind(appPaneContainer.heightProperty().subtract(appPane.prefHeightProperty()).divide(2)); |
92 | | - |
93 | | - paneCtrl.setContext(appPane, stage, title); |
94 | | - |
95 | | - root.setTop(menuBar); |
96 | | - root.setCenter(appPaneContainer); |
97 | | - // just in case, the builder is called twice... |
98 | | - IntegratedAppPaneCtrl result = paneCtrl; |
99 | | - paneCtrl = new IntegratedAppPaneCtrl(); |
100 | | - return result; |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Adds a new starter item to the specified menu. |
105 | | - */ |
106 | | - private MenuItem addToMenu(Menu menu, String packageName, MenuItem item) { |
107 | | - Menu subMenu = null; |
108 | | - ObservableList<MenuItem> menuComps = menu.getItems(); |
109 | | - int i; |
110 | | - for (i = 0; i < menuComps.size(); i++) { |
111 | | - Menu comp = (Menu) menuComps.get(i); |
112 | | - if (comp.getText().equals(packageName)) |
113 | | - subMenu = comp; |
114 | | - else if (comp.getText().compareTo(packageName) > 0) |
115 | | - break; |
116 | | - } |
117 | | - if (subMenu == null) { |
118 | | - subMenu = new Menu(packageName); |
119 | | - menu.getItems().add(i, subMenu); |
120 | | - } |
121 | | - subMenu.getItems().add(item); |
122 | | - return item; |
123 | | - } |
124 | | - |
125 | | - private double trunc(double num) { |
126 | | - return Math.round(num * 2) / 2.0; |
127 | | - } |
128 | | -} |
| 1 | +package aima.gui.fx.framework; |
| 2 | + |
| 3 | +import javafx.application.Platform; |
| 4 | +import javafx.beans.binding.Bindings; |
| 5 | +import javafx.beans.property.DoubleProperty; |
| 6 | +import javafx.collections.ObservableList; |
| 7 | +import javafx.scene.control.Menu; |
| 8 | +import javafx.scene.control.MenuBar; |
| 9 | +import javafx.scene.control.MenuItem; |
| 10 | +import javafx.scene.input.KeyCode; |
| 11 | +import javafx.scene.input.KeyCodeCombination; |
| 12 | +import javafx.scene.input.KeyCombination; |
| 13 | +import javafx.scene.layout.BorderPane; |
| 14 | +import javafx.scene.layout.Pane; |
| 15 | +import javafx.scene.text.Font; |
| 16 | +import javafx.stage.Stage; |
| 17 | + |
| 18 | +/** |
| 19 | + * Builder for integrated applications. To create an integrated application, |
| 20 | + * just create a builder, define a title, register apps (integrable JavaFX |
| 21 | + * applications) and demos (command line applications) and place the result into a |
| 22 | + * scene of a stage. |
| 23 | + * |
| 24 | + * @author Ruediger Lunde |
| 25 | + */ |
| 26 | +public class IntegratedAppPaneBuilder { |
| 27 | + |
| 28 | + private MenuBar menuBar = new MenuBar(); |
| 29 | + private Menu appsMenu = new Menu("Apps"); |
| 30 | + private Menu demosMenu = new Menu("Demos"); |
| 31 | + private String title = ""; |
| 32 | + |
| 33 | + private IntegratedAppPaneCtrl paneCtrl; |
| 34 | + |
| 35 | + public IntegratedAppPaneBuilder() { |
| 36 | + paneCtrl = new IntegratedAppPaneCtrl(); |
| 37 | + final DoubleProperty scale = paneCtrl.scaleProperty(); |
| 38 | + MenuItem incScaleItem = new MenuItem("Inc Scale"); |
| 39 | + incScaleItem.setOnAction(ev -> scale.set(trunc(scale.get() * 1.3))); |
| 40 | + incScaleItem.setAccelerator(new KeyCodeCombination(KeyCode.PLUS, KeyCombination.CONTROL_DOWN)); |
| 41 | + MenuItem decScaleItem = new MenuItem("Dec Scale"); |
| 42 | + decScaleItem.setOnAction(ev -> scale.set(trunc(scale.get() / 1.3))); |
| 43 | + decScaleItem.setAccelerator(new KeyCodeCombination(KeyCode.MINUS, KeyCombination.CONTROL_DOWN)); |
| 44 | + MenuItem exitItem = new MenuItem("Exit"); |
| 45 | + exitItem.setOnAction(ev -> Platform.exit()); |
| 46 | + |
| 47 | + Menu fileMenu = new Menu("File"); |
| 48 | + fileMenu.getItems().addAll(incScaleItem, decScaleItem, exitItem); |
| 49 | + menuBar.getMenus().addAll(fileMenu, appsMenu, demosMenu); |
| 50 | + menuBar.styleProperty().bind(Bindings.concat("-fx-font-size: ", |
| 51 | + paneCtrl.scaleProperty().multiply(Font.getDefault().getSize()).asString())); |
| 52 | + } |
| 53 | + |
| 54 | + public void defineTitle(String title) { |
| 55 | + this.title = title; |
| 56 | + } |
| 57 | + |
| 58 | + public void registerApp(Class<? extends IntegrableApplication> appClass) { |
| 59 | + final IntegratedAppPaneCtrl ctrl = paneCtrl; |
| 60 | + MenuItem item = new MenuItem(appClass.getSimpleName()); |
| 61 | + item.setOnAction(ev -> ctrl.startApp(appClass)); |
| 62 | + addToMenu(appsMenu, appClass.getPackage().getName(), item); |
| 63 | + } |
| 64 | + |
| 65 | + public void registerProg(Class<?> progClass) { |
| 66 | + final IntegratedAppPaneCtrl ctrl = paneCtrl; |
| 67 | + MenuItem item = new MenuItem(progClass.getSimpleName()); |
| 68 | + item.setOnAction(ev -> ctrl.startProg(progClass)); |
| 69 | + addToMenu(demosMenu, progClass.getPackage().getName(), item); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Adds a menu bar and a scalable container pane to the provided root pane and |
| 74 | + * returns a controller instance containing user interface logic. |
| 75 | + */ |
| 76 | + public IntegratedAppPaneCtrl getResultFor(BorderPane root, Stage stage) { |
| 77 | + |
| 78 | + // create a pane, content is affected by scale |
| 79 | + final DoubleProperty scale = paneCtrl.scaleProperty(); |
| 80 | + BorderPane appPane = new BorderPane(); |
| 81 | + appPane.scaleXProperty().bind(scale); |
| 82 | + appPane.scaleYProperty().bind(scale); |
| 83 | + |
| 84 | + Pane appPaneContainer = new Pane(); |
| 85 | + appPaneContainer.getChildren().add(appPane); |
| 86 | + appPane.prefWidthProperty().bind(appPaneContainer.widthProperty().divide(scale)); |
| 87 | + appPane.prefHeightProperty().bind(appPaneContainer.heightProperty().divide(scale)); |
| 88 | + appPane.translateXProperty() |
| 89 | + .bind(appPaneContainer.widthProperty().subtract(appPane.prefWidthProperty()).divide(2)); |
| 90 | + appPane.translateYProperty() |
| 91 | + .bind(appPaneContainer.heightProperty().subtract(appPane.prefHeightProperty()).divide(2)); |
| 92 | + |
| 93 | + paneCtrl.setContext(appPane, stage, title); |
| 94 | + |
| 95 | + root.setTop(menuBar); |
| 96 | + root.setCenter(appPaneContainer); |
| 97 | + // just in case, the builder is called twice... |
| 98 | + IntegratedAppPaneCtrl result = paneCtrl; |
| 99 | + paneCtrl = new IntegratedAppPaneCtrl(); |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Adds a new starter item to the specified menu. |
| 105 | + */ |
| 106 | + private MenuItem addToMenu(Menu menu, String packageName, MenuItem item) { |
| 107 | + Menu subMenu = null; |
| 108 | + ObservableList<MenuItem> menuComps = menu.getItems(); |
| 109 | + int i; |
| 110 | + for (i = 0; i < menuComps.size(); i++) { |
| 111 | + Menu comp = (Menu) menuComps.get(i); |
| 112 | + if (comp.getText().equals(packageName)) |
| 113 | + subMenu = comp; |
| 114 | + else if (comp.getText().compareTo(packageName) > 0) |
| 115 | + break; |
| 116 | + } |
| 117 | + if (subMenu == null) { |
| 118 | + subMenu = new Menu(packageName); |
| 119 | + menu.getItems().add(i, subMenu); |
| 120 | + } |
| 121 | + subMenu.getItems().add(item); |
| 122 | + return item; |
| 123 | + } |
| 124 | + |
| 125 | + private double trunc(double num) { |
| 126 | + return Math.round(num * 2) / 2.0; |
| 127 | + } |
| 128 | +} |
0 commit comments