-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainController.java
205 lines (178 loc) · 6.29 KB
/
MainController.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
package com.sirolf2009.caesar;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.sirolf2009.caesar.component.*;
import com.sirolf2009.caesar.model.*;
import com.sirolf2009.caesar.model.serializer.CaesarKryo;
import com.sirolf2009.caesar.model.table.JMXAttribute;
import com.sirolf2009.caesar.util.FXUtil;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javax.management.MBeanInfo;
import java.io.*;
import java.util.Arrays;
import java.util.stream.Stream;
public class MainController {
private CaesarModel model;
private Connection connection;
@FXML private ToolBar toolbar;
@FXML private AnchorPane variablesAnchor;
@FXML private AnchorPane tablesAnchor;
@FXML private AnchorPane chartsAnchor;
@FXML private TabPane tabs;
@FXML private Label lblUrl;
@FXML private CheckBox connected;
@FXML private ProgressIndicator connecting;
private VariablesTreeView variables;
private TablesTreeView tablesTreeView;
private ChartsTreeView chartsTreeView;
public MainController() {
this(new CaesarModel());
}
public MainController(CaesarModel model) {
this.model = model;
}
@FXML public void initialize() {
variables = new VariablesTreeView();
variablesAnchor.getChildren().add(variables);
FXUtil.maximize(variables);
tablesTreeView = new TablesTreeView(model.getTables());
tablesAnchor.getChildren().add(tablesTreeView);
FXUtil.maximize(tablesTreeView);
chartsTreeView = new ChartsTreeView(model.getCharts());
chartsAnchor.getChildren().add(chartsTreeView);
FXUtil.maximize(chartsTreeView);
}
public void save() {
FileChooser chooser = new FileChooser();
File file = chooser.showSaveDialog(toolbar.getScene().getWindow());
if(file != null) {
try {
getTabs().forEach(tab -> tab.preSave());
CaesarKryo kryo = new CaesarKryo();
Output out = new Output(new FileOutputStream(file));
kryo.writeObject(out, model);
out.close();
getTabs().forEach(tab -> tab.postSave());
} catch(Exception e) {
e.printStackTrace();
}
}
}
public void load() {
FileChooser chooser = new FileChooser();
File file = chooser.showOpenDialog(toolbar.getScene().getWindow());
if(file != null) {
try {
load(file);
} catch(Exception e) {
e.printStackTrace();
}
}
}
public void load(File file) throws FileNotFoundException {
CaesarKryo kryo = new CaesarKryo();
try(Input input = new Input(new FileInputStream(file))) {
switchTo(kryo.readObject(input, CaesarModel.class));
}
}
public void switchTo(CaesarModel newModel) {
tabs.getTabs().stream().filter(tab -> tab.getContent() instanceof TableTab).forEach(tab -> ((TableTab) tab.getContent()).getPuller().runningProperty().set(false));
tabs.getTabs().clear();
this.model = newModel;
tablesTreeView.setItems(model.getTables());
chartsTreeView.setItems(model.getCharts());
model.getTables().forEach(table -> addTable(table));
model.getCharts().forEach(chart -> addChart(chart));
model.getDashboards().forEach(dashboard -> addDashboard(dashboard));
tabs.getTabs().stream().filter(tab -> tab.getContent() instanceof TableTab).forEach(tab -> ((TableTab) tab.getContent()).getPuller().runningProperty().set(true));
}
public void newTable() {
Table table = new Table(getNewTabName());
model.getTables().add(table);
addTable(table);
}
public void addTable(Table table) {
Tab newTableTab = new RenameableTab(table.nameProperty());
tabs.getTabs().add(newTableTab);
newTableTab.setOnCloseRequest(e -> {
model.getTables().remove(table);
});
tabs.getSelectionModel().select(newTableTab);
newTableTab.setContent(new TableTab(table, connection));
}
public void newChart() {
Chart chart = new Chart(getNewTabName());
model.getCharts().add(chart);
addChart(chart);
}
public void addChart(Chart chart) {
Tab newChartTab = new RenameableTab(chart.nameProperty());
newChartTab.setContent(new ChartTab(chart, model.getTables()));
newChartTab.setOnCloseRequest(e -> {
model.getCharts().remove(chart);
});
tabs.getTabs().add(newChartTab);
tabs.getSelectionModel().select(newChartTab);
}
public void newDashboard() {
Dashboard dashboard = new Dashboard(getNewTabName());
model.getDashboards().add(dashboard);
addDashboard(dashboard);
}
public void addDashboard(Dashboard dashboard) {
Tab newDashboardTab = new RenameableTab(dashboard.nameProperty());
newDashboardTab.setContent(new DashboardTab(dashboard));
newDashboardTab.setOnCloseRequest(e -> {
model.getDashboards().remove(dashboard);
});
tabs.getTabs().add(newDashboardTab);
tabs.getSelectionModel().select(newDashboardTab);
}
public void queryAttributes() {
ObservableList<JMXObject> objects = FXCollections.observableArrayList();
connection.getConnection().ifPresent(connection -> {
try {
connection.queryNames(null, null).forEach(objectName -> {
ObservableList<JMXAttribute> attributes = FXCollections.observableArrayList();
try {
MBeanInfo bean = connection.getMBeanInfo(objectName);
Arrays.stream(bean.getAttributes()).forEach(attributeInfo -> {
attributes.add(new JMXAttribute(objectName, attributeInfo));
});
} catch(Exception e) {
e.printStackTrace();
}
objects.add(new JMXObject(objectName, attributes));
});
} catch(IOException e) {
e.printStackTrace();
}
});
variables.getVariables().setItems(objects.sorted());
}
private String getNewTabName() {
return "Untitled " + (tabs.getTabs().size() + 1);
}
private Stream<ITab> getTabs() {
return tabs.getTabs().stream().filter(tab -> tab.getContent() instanceof ITab).map(tab -> (ITab) tab.getContent());
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
lblUrl.setText(connection.getServiceURL().toString());
connected.visibleProperty().bind(connection.connectedProperty());
connecting.visibleProperty().bind(connection.connectedProperty().not());
connecting.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS);
}
}