1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package Controllers;
21
22 import Utilities.*;
23 import com.lynden.gmapsfx.GoogleMapView;
24 import com.lynden.gmapsfx.javascript.event.UIEventType;
25 import com.lynden.gmapsfx.javascript.object.*;
26 import javafx.fxml.FXML;
27 import javafx.fxml.FXMLLoader;
28 import javafx.fxml.Initializable;
29 import javafx.scene.control.Button;
30 import javafx.scene.control.Label;
31 import javafx.scene.layout.HBox;
32 import javafx.scene.layout.Pane;
33 import javafx.scene.layout.VBox;
34 import netscape.javascript.JSObject;
35
36 import java.io.IOException;
37 import java.net.URL;
38 import java.sql.Connection;
39 import java.sql.PreparedStatement;
40 import java.sql.ResultSet;
41 import java.sql.SQLException;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.ResourceBundle;
46
47 @SuppressWarnings("WeakerAccess")
48
49 public class MapController implements Initializable {
50
51
52
53
54
55 @FXML
56 private VBox infoPanel;
57 @FXML
58 private Button button;
59
60 @FXML
61 private GoogleMapView googleMapView;
62
63 private MainController mainCont;
64 private HashMap<LatLongComparable, ArrayList<Customer>> customersToClick = new HashMap<>();
65
66 @Override
67 public void initialize(URL url, ResourceBundle rb) {
68
69 }
70
71 protected void configureMap() {
72 try {
73 MapOptions mapOptions = new MapOptions();
74
75 mapOptions.center(new LatLong(47.6097, -122.3331))
76 .mapType(MapTypeIdEnum.ROADMAP).zoom(9);
77
78
79
80 final double[] totCoords = {0, 0, 0};
81 double totLat = 0;
82 final double[] totLon = {0};
83 HashMap<Marker, LatLongComparable> markers = new HashMap<>();
84
85
86
87
88
89
90
91
92
93
94 Iterable<String> years = DbInt.getUserYears();
95
96 years.forEach(year -> {
97 List<String> ret = new ArrayList<>();
98 Year yearObj = new Year(year);
99 yearObj.getCustomers().forEach(customer -> {
100 LatLongComparable custLatLong = new LatLongComparable(customer.getLat(), customer.getLon());
101 if (!customersToClick.containsKey(custLatLong)) {
102 double lat = customer.getLat();
103 double lon = customer.getLon();
104 totCoords[0] += lat;
105 totCoords[1] += lon;
106 MarkerOptions opts = new MarkerOptions();
107 String address = customer.getAddr();
108 String cName = customer.getName();
109
110 opts.title(cName + " " + address + " " + customer.getCustAddressFrmName()[0] + ", " + customer.getCustAddressFrmName()[1]);
111 opts.position(new LatLong(lat, lon));
112 Marker m = new Marker(opts);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 markers.put(m, custLatLong);
130 totCoords[2]++;
131 ArrayList<Customer> customs = new ArrayList();
132 customs.add(customer);
133 customersToClick.put(custLatLong, customs);
134
135 } else {
136 customersToClick.get(custLatLong).add(customer);
137 }
138 });
139
140 });
141 GoogleMap map = googleMapView.createMap(mapOptions, false);
142
143
144 markers.forEach((m, latLon) -> {
145 map.addMarker(m);
146 map.addUIEventHandler(m, UIEventType.click, (JSObject obj) -> markerClicked(latLon));
147 });
148
149 map.setCenter(new LatLong(totCoords[0] / totCoords[2], totCoords[1] / totCoords[2]));
150
151
152 } catch (Exception e) {
153 LogToFile.log(e, Severity.WARNING, "Error loading map");
154 }
155
156
157 }
158
159 public void initMap(MainController mainController) {
160 mainCont = mainController;
161 googleMapView.addMapInializedListener(() -> configureMap());
162
163
164
165
166
167 }
168
169 private void markerClicked(LatLongComparable latLong) {
170
171
172
173
174
175
176
177
178 infoPanel.getChildren().clear();
179
180
181
182
183
184
185 String Name;
186
187 List<Customer> customers = customersToClick.get(latLong);
188 HashMap<String, HBox> custNames = new HashMap<>();
189 customers.forEach(customer -> {
190 if (custNames.containsKey(customer.getName())) {
191 String PhoneD = customer.getPhone();
192
193
194
195
196
197 Button b = new Button(customer.getYear());
198 b.setOnAction(e1 -> {
199 Pane newPane = null;
200 FXMLLoader loader;
201 String tabTitle;
202 loader = new FXMLLoader(getClass().getResource("/UI/Customer.fxml"));
203 try {
204 newPane = loader.load();
205 } catch (IOException e) {
206 LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
207 }
208 CustomerController customerCont = loader.getController();
209
210 customerCont.initCustomer(customer, mainCont);
211 tabTitle = ("Customer View - " + customer.getName() + " - " + customer.getYear());
212 mainCont.addTab(newPane, tabTitle);
213 });
214 custNames.get(customer.getName()).getChildren().add(b);
215 } else {
216 HBox custOrders = new HBox();
217
218 Label addrHeader = new Label("Customer address");
219 Label nameHeader = new Label("Customer Name");
220 Label phoneHeader = new Label("Customer Phone");
221 Label orderHeader = new Label("Customer Orders");
222 addrHeader.getStyleClass().add("Info-Header");
223 nameHeader.getStyleClass().add("Info-Header");
224 phoneHeader.getStyleClass().add("Info-Header");
225 orderHeader.getStyleClass().add("Info-Header");
226
227 Label custAddress = new Label(customer.getAddr());
228 Label custPhone = new Label(customer.getPhone());
229 Label custName = new Label(customer.getName());
230 String PhoneD = customer.getPhone();
231 custNames.put(customer.getName(), custOrders);
232
233
234
235
236 Button b = new Button(customer.getYear());
237 b.setOnAction(e1 -> {
238 Pane newPane = null;
239 FXMLLoader loader;
240 String tabTitle;
241 loader = new FXMLLoader(getClass().getResource("/UI/Customer.fxml"));
242 try {
243 newPane = loader.load();
244 } catch (IOException e) {
245 LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
246 }
247 CustomerController customerCont = loader.getController();
248
249 customerCont.initCustomer(customer, mainCont);
250 tabTitle = ("Customer View - " + customer.getName() + " - " + customer.getYear());
251 mainCont.addTab(newPane, tabTitle);
252 });
253 custOrders.getChildren().add(b);
254 VBox info = new VBox(nameHeader, custName, phoneHeader, custPhone, addrHeader, custAddress, orderHeader, custOrders);
255 info.getStyleClass().add("Map-Info-Box");
256 info.getStyleClass().add("informationPane");
257
258 infoPanel.getChildren().add(info);
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 private List<String> getAllCustomersInfo(String info) {
319 List<String> ret = new ArrayList<>();
320
321 try (Connection con = DbInt.getConnection("Set");
322 PreparedStatement prep = con.prepareStatement("SELECT * FROM Customers", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
323 ResultSet rs = prep.executeQuery()) {
324 while (rs.next()) {
325
326 ret.add(rs.getString(info));
327
328 }
329
330
331 } catch (SQLException e) {
332 LogToFile.log(e, Severity.SEVERE, CommonErrors.returnSqlMessage(e));
333 }
334 return ret;
335 }
336
337 private List<String> getCustInfo(String Db, String info, String Address) {
338 List<String> ret = new ArrayList<>();
339
340
341 try (Connection con = DbInt.getConnection(Db);
342 PreparedStatement prep = con.prepareStatement("SELECT * FROM Customers WHERE ADDRESS=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
343 prep.setString(1, Address);
344 try (ResultSet rs = prep.executeQuery()) {
345
346 while (rs.next()) {
347
348 ret.add(rs.getString(info));
349
350 }
351 }
352
353
354 } catch (SQLException e) {
355 LogToFile.log(e, Severity.SEVERE, CommonErrors.returnSqlMessage(e));
356 }
357 return ret;
358 }
359
360 class LatLongComparable extends LatLong {
361
362 public LatLongComparable(double latitude, double longitude) {
363 super(latitude, longitude);
364 }
365
366 @Override
367 public int hashCode() {
368 return Double.hashCode(getLatitude()) + Double.hashCode(getLongitude());
369 }
370
371 @Override
372 public boolean equals(Object o) {
373 if (!(o instanceof LatLongComparable)) { return false; }
374 if (o == this) { return true; }
375 LatLongComparable compare = (LatLongComparable) o;
376 return (this.getLatitude() == compare.getLatitude() && this.getLongitude() == compare.getLongitude());
377 }
378 }
379 }