View Javadoc
1   /*
2    * Copyright (c) Patrick Magauran 2018.
3    *   Licensed under the AGPLv3. All conditions of said license apply.
4    *       This file is part of ABOS.
5    *
6    *       ABOS is free software: you can redistribute it and/or modify
7    *       it under the terms of the GNU Affero General Public License as published by
8    *       the Free Software Foundation, either version 3 of the License, or
9    *       (at your option) any later version.
10   *
11   *       ABOS is distributed in the hope that it will be useful,
12   *       but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *       GNU Affero General Public License for more details.
15   *
16   *       You should have received a copy of the GNU Affero General Public License
17   *       along with ABOS.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package Controllers;/*
21   * Copyright (c) Patrick Magauran 2018.
22   *   Licensed under the AGPLv3. All conditions of said license apply.
23   *       This file is part of ABOS.
24   *
25   *       ABOS is free software: you can redistribute it and/or modify
26   *       it under the terms of the GNU Affero General Public License as published by
27   *       the Free Software Foundation, either version 3 of the License, or
28   *       (at your option) any later version.
29   *
30   *       ABOS is distributed in the hope that it will be useful,
31   *       but WITHOUT ANY WARRANTY; without even the implied warranty of
32   *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33   *       GNU Affero General Public License for more details.
34   *
35   *       You should have received a copy of the GNU Affero General Public License
36   *       along with ABOS.  If not, see <http://www.gnu.org/licenses/>.
37   */
38  
39  import Launchers.*;
40  import Utilities.*;
41  import Workers.LoadMainWorker;
42  import javafx.application.Application;
43  import javafx.application.Platform;
44  import javafx.event.EventType;
45  import javafx.fxml.FXML;
46  import javafx.fxml.FXMLLoader;
47  import javafx.geometry.Insets;
48  import javafx.scene.Scene;
49  import javafx.scene.control.*;
50  import javafx.scene.input.MouseEvent;
51  import javafx.scene.layout.*;
52  import javafx.stage.Stage;
53  import javafx.util.Pair;
54  
55  import java.io.IOException;
56  import java.sql.SQLException;
57  import java.util.ArrayList;
58  import java.util.Map;
59  import java.util.Objects;
60  import java.util.Optional;
61  
62  @SuppressWarnings("WeakerAccess")
63  
64  public class MainController {
65      public final EventType closeEvent = new EventType("Close");
66      private boolean isRightClick;
67      // the FXML annotation tells the loader to inject this variable before invoking initialize.
68      @FXML
69      private TreeView<TreeItemPair<String, Pair<String, Object>>> selectNav;
70      // @FXML
71      //public Pane tabPane;
72      //@FXML
73      // public Tab tab1;
74      @FXML
75      private TabPane tabPane2;
76      private Boolean isAdmin = false;
77      @FXML
78      private FlowPane namePane;
79      @FXML
80      private BorderPane sidePane;
81  
82      private void login(Boolean failed) {
83          Dialog<Pair<String, String>> dialog = new Dialog<>();
84          dialog.setTitle("Login");
85  
86  // Set the button types.
87          ButtonType login = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
88          dialog.getDialogPane().getButtonTypes().addAll(login, ButtonType.CANCEL);
89          if (failed) {
90              dialog.setHeaderText("Invalid Username/Password");
91          }
92  // Create the username and password labels and fields.
93          GridPane grid = new GridPane();
94          grid.setHgap(10);
95          grid.setVgap(10);
96          grid.setPadding(new Insets(20, 150, 10, 10));
97  
98          TextField userNameTextField = new TextField();
99          userNameTextField.setPromptText("Username");
100         PasswordField passwordField = new PasswordField();
101         passwordField.setPromptText("Password");
102 
103         grid.add(new Label("Username:"), 0, 0);
104         grid.add(userNameTextField, 1, 0);
105         grid.add(new Label("Password:"), 0, 1);
106         grid.add(passwordField, 1, 1);
107 
108 
109 // Enable/Disable login button depending on whether a username was entered.
110         javafx.scene.Node loginButton = dialog.getDialogPane().lookupButton(login);
111         loginButton.setDisable(true);
112 
113 // Do some validation (using the Java 8 lambda syntax).
114         userNameTextField.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(newValue.trim().isEmpty()));
115 
116         dialog.getDialogPane().setContent(grid);
117 
118 // Request focus on the username field by default.
119         Platform.runLater(() -> userNameTextField.requestFocus());
120 
121 // Convert the result to a username-password-pair when the login button is clicked.
122         dialog.setResultConverter(dialogButton -> {
123             if (dialogButton == login) {
124                 return new Pair<String, String>(userNameTextField.getText(), passwordField.getText());
125             }
126             System.exit(0);
127             return null;
128         });
129 
130         Optional<Pair<String, String>> result = dialog.showAndWait();
131 
132         result.ifPresent(userPass -> {
133             if (!DbInt.verifyLoginAndUser(userPass)) {
134                 login(true);
135             }
136             if (!DbInt.testConnection()) {
137                 Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
138                 alert.setTitle("Verify Databse?");
139                 alert.setHeaderText("Failed to connect to the databasse");
140                 alert.setContentText("Would you like to open the settings window to verify the connection?");
141 
142                 ButtonType buttonTypeOne = new ButtonType("Open");
143                 ButtonType buttonTypeTwo = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
144 
145                 alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo);
146 
147                 Optional<ButtonType> res = alert.showAndWait();
148                 if (res.get() == buttonTypeOne) {
149                     new Settings();
150 
151                 }
152             }
153 
154         });
155 
156     }
157 
158     // the initialize method is automatically invoked by the FXMLLoader - it's magic
159     public void initialize(Stage stage, Application.Parameters parameters) {
160         Map<String, String> params = parameters.getNamed();
161         if (params.containsKey("config")) {
162             Config.setConfigLocation(params.get("config"));
163         }
164         if (params.containsKey("username") && params.containsKey("password")) {
165             if (!DbInt.verifyLoginAndUser(new Pair<>(params.get("username"), params.get("password")))) {
166                 login(true);
167             }
168         } else {
169             login(false);
170         }
171         DbInt.setPrefix(Config.getPrefix());
172         // Utilities.DbInt.username = "tw";
173         ArrayList<String> years = DbInt.getUserYears();
174         if (!years.isEmpty()) {
175             User latestUser;
176             try {
177                 latestUser = DbInt.getCurrentUser();
178                 stage.setTitle("ABOS - " + latestUser.getFullName());
179 
180             } catch (SQLException e) {
181                 LogToFile.log(e, Severity.SEVERE, CommonErrors.returnSqlMessage(e));
182 
183             }
184         }
185         //loadTreeItems("initial 1", "initial 2", "initial 3");
186         fillTreeView();
187 
188         selectNav.setShowRoot(false);
189         selectNav.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
190             if (event.isSecondaryButtonDown()) {
191                 isRightClick = true;
192 
193             }
194             if (event.getClickCount() == 2) {
195 
196                 Platform.runLater(() -> {
197                     TreeItemPair<String, Pair<String, Object>> newValue = selectNav.getSelectionModel().getSelectedItem().getValue();
198                     if (isRightClick) {
199                         //reset the flag
200                         isRightClick = false;
201                     } else if (newValue != null && !Objects.equals(newValue.getValue().getValue(), "RootNode")) {
202                         Pane newPane = null;
203                         FXMLLoader loader;
204                         String tabTitle;
205 
206                         // load new pane
207                         switch (newValue.getValue().getKey()) {
208                             case "Window": {
209                                 switch (newValue.getKey()) {
210                                     case "Add Customer": {
211                                         loader = new FXMLLoader(getClass().getResource("/UI/AddCustomer.fxml"));
212                                         try {
213                                             newPane = loader.load();
214                                         } catch (IOException e) {
215                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
216                                         }
217                                         AddCustomerController addCustCont = loader.getController();
218                                         Pair<String, String> data = (Pair<String, String>) newValue.getValue().getValue();
219                                         Tab tab = addTab(newPane, "Add Customer - " + data.getKey());
220 
221                                         addCustCont.initAddCust(data.getKey(), this, tab, data.getValue(), selectNav.getSelectionModel().getSelectedItem().getParent());
222 
223                                         break;
224                                     }
225                                     case "Users Groups & Years": {
226                                         new UsersGroupsAndYears(getWindow());
227 
228                                         break;
229                                     }
230                                     case "Reports":
231                                         new Reports(tabPane2.getScene().getWindow());
232                                         break;
233                                     case "View Map":
234                                         loader = new FXMLLoader(getClass().getResource("/UI/Map.fxml"));
235                                         try {
236                                             newPane = loader.load();
237                                         } catch (IOException e) {
238                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
239                                         }
240                                         MapController mapCont = loader.getController();
241                                         mapCont.initMap(this);
242                                         addTab(newPane, "Map");
243 
244                                         break;
245                                     case "Add Year":
246                                         new AddYear(getWindow());
247                                         break;
248                                     case "Add User":
249                                         new AddUser(getWindow());
250                                         break;
251                                     case "Add Group":
252                                         AddGroup.addGroup(newValue.getValue().getValue().toString(), null);
253                                         break;
254                                     case "Settings":
255                                         new Settings(tabPane2.getScene().getWindow());
256                                         break;
257                                 }
258                                 break;
259                             }
260 
261                             case "Year": {
262                                 loader = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
263                                 try {
264                                     newPane = loader.load();
265                                 } catch (IOException e) {
266                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
267                                 }
268                                 YearController yearCont = loader.getController();
269                                 yearCont.initYear(newValue.getKey());
270                                 tabTitle = ("Year View - " + newValue.getKey());
271                                 addTab(newPane, tabTitle);
272                                 break;
273                             }
274                             case "Customer": {
275                                 loader = new FXMLLoader(getClass().getResource("/UI/Customer.fxml"));
276                                 try {
277                                     newPane = loader.load();
278                                 } catch (IOException e) {
279                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
280                                 }
281                                 CustomerController customerCont = loader.getController();
282                                 customerCont.initCustomer((Customer) newValue.getValue().getValue(), this);
283                                 tabTitle = ("Customer View - " + newValue.getKey() + " - " + ((Customer) newValue.getValue().getValue()).getYear());
284                                 addTab(newPane, tabTitle);
285                                 break;
286                             }
287                             case "Group": {
288                                 AddGroup.addGroup(newValue.getValue().getValue().toString(), newValue.getKey(), null);
289 
290                                 break;
291                             }
292                             case "UserCustomerView": {
293                                 loader = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
294                                 try {
295                                     newPane = loader.load();
296                                 } catch (IOException e) {
297                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
298                                 }
299                                 YearController yearCont = loader.getController();
300                                 yearCont.initYear(newValue.getValue().getValue().toString(), newValue.getKey());
301                                 tabTitle = ("Year View - " + newValue.getValue().getValue() + " - " + newValue.getKey());
302                                 addTab(newPane, tabTitle);
303                                 break;
304                             }
305                             case "User": {
306                                 new AddUser(getWindow(), newValue.getValue().getValue().toString());
307                                 break;
308                             }
309 
310 
311                         }
312                     }
313                 });
314             }
315         });
316 
317         selectNav.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
318 
319             
320         });
321         selectNav.setCellFactory(p -> new TreeCellImpl());
322 
323 
324     }
325 
326     private ContextMenu createContextMenu(TreeItem<TreeItemPair<String, Pair<String, Object>>> cell) {
327         ContextMenu cm = new ContextMenu();
328         ContextMenu cmContent = new ContextMenu();
329         Pane newPane = null;
330         FXMLLoader loader;
331         Pane finalNewPane;
332         // String tabTitle = "";
333         if (cell != null && cell.getValue() != null && !Objects.equals(cell.getValue().getValue().getKey(), "RootNode")) {
334             switch (cell.getValue().getValue().getKey()) {
335                 case "Window": {
336                     switch (cell.getValue().getKey()) {
337                         case "Add Customer": {
338                             cmContent = createContextMenuContent(
339                                     //Open
340                                     () -> {
341                                         FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/AddCustomer.fxml"));
342 
343                                         Pane NewPane2 = null;
344                                         try {
345                                             NewPane2 = loader2.load();
346                                         } catch (IOException e) {
347                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
348                                         }
349                                         AddCustomerController addCustCont = loader2.getController();
350                                         Pair<String, String> data = (Pair<String, String>) cell.getValue().getValue().getValue();
351 
352                                         String tabTitle = ("Add Customer - " + data.getKey());
353                                         addCustCont.initAddCust(data.getKey(), this, openTabInCurrentWindow(NewPane2, tabTitle), data.getValue(), cell.getParent());
354 
355                                     }, () -> { //Open In New Tab
356                                         FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/AddCustomer.fxml"));
357 
358                                         Pane NewPane2 = null;
359                                         try {
360                                             NewPane2 = loader2.load();
361                                         } catch (IOException e) {
362                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
363                                         }
364                                         Pair<String, String> data = (Pair<String, String>) cell.getValue().getValue().getValue();
365 
366                                         AddCustomerController addCustCont = loader2.getController();
367                                         String tabTitle = ("Add Customer - " + data.getKey());
368                                         addCustCont.initAddCust(data.getKey(), this, addTab(NewPane2, tabTitle), data.getValue(), cell.getParent());
369                                     }, () -> { //Open In New Window
370                                         FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/AddCustomer.fxml"));
371 
372                                         Pane NewPane2 = null;
373                                         try {
374                                             NewPane2 = loader2.load();
375                                         } catch (IOException e) {
376                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
377                                         }
378                                         Pair<String, String> data = (Pair<String, String>) cell.getValue().getValue().getValue();
379 
380                                         AddCustomerController addCustCont = loader2.getController();
381                                         String tabTitle = ("Add Customer - " + data.getKey());
382                                         addCustCont.initAddCust(data.getKey(), this, openInNewWindow(NewPane2, tabTitle), data.getValue(), cell.getParent());
383                                     }, null);
384                             break;
385                         }
386                         case "Reports":
387                             cmContent = createContextMenuContent(
388                                     //Open
389                                     () -> new Reports(tabPane2.getScene().getWindow()), null, null, null);
390                             //  new Launchers.Reports(tabPane2.getScene().getWindow());
391                             break;
392                         case "View Map":
393                             cmContent = createContextMenuContent(
394                                     //Open
395                                     () -> {
396                                         FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Map.fxml"));
397 
398                                         Pane NewPane2 = null;
399                                         try {
400                                             NewPane2 = loader2.load();
401                                         } catch (IOException e) {
402                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
403                                         }
404                                         MapController mapCont = loader2.getController();
405                                         mapCont.initMap(this);
406                                         String tabTitle = ("Map");
407                                         openTabInCurrentWindow(NewPane2, tabTitle);
408 
409                                     }, () -> { //Open In New Tab
410                                         FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Map.fxml"));
411 
412                                         Pane NewPane2 = null;
413                                         try {
414                                             NewPane2 = loader2.load();
415                                         } catch (IOException e) {
416                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
417                                         }
418                                         MapController mapCont = loader2.getController();
419                                         mapCont.initMap(this);
420                                         String tabTitle = ("Map");
421                                         addTab(NewPane2, tabTitle);
422                                     }, () -> { //Open In New Window
423                                         FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Map.fxml"));
424 
425                                         Pane NewPane2 = null;
426                                         try {
427                                             NewPane2 = loader2.load();
428                                         } catch (IOException e) {
429                                             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
430                                         }
431                                         MapController mapCont = loader2.getController();
432                                         mapCont.initMap(this);
433                                         String tabTitle = ("Map");
434                                         openInNewWindow(NewPane2, tabTitle);
435                                     }, null);
436                             break;
437                         case "Add Year":
438                             cmContent = createContextMenuContent(
439                                     //Open
440                                     () -> new AddYear(getWindow()), null, null, null);
441                             // new Launchers.AddYear(getWindow());
442                             break;
443                         case "Users Groups & Years":
444                             cmContent = createContextMenuContent(
445                                     //Open
446                                     () -> new UsersGroupsAndYears(getWindow()), null, null, null);
447                             // new Launchers.AddYear(getWindow());
448                             break;
449                         case "Add User":
450                             cmContent = createContextMenuContent(
451                                     //Open
452                                     () -> new AddUser(getWindow()), null, null, null);
453                             // new Launchers.AddYear(getWindow());
454                             break;
455                         case "Add Group":
456                             cmContent = createContextMenuContent(
457                                     //Open
458                                     () -> AddGroup.addGroup(cell.getValue().getValue().getValue().toString(), null), null, null, null);
459                             // new Launchers.AddYear(getWindow());
460                             break;
461                         case "Settings":
462                             cmContent = createContextMenuContent(
463                                     //Open
464                                     () -> new Settings(tabPane2.getScene().getWindow()), null, null, null);
465                             // new Launchers.Settings(tabPane2.getScene().getWindow());
466                             break;
467                     }
468                     break;
469                 }
470                 case "Year": {
471                     cmContent = createContextMenuContent(
472                             //Open
473                             () -> {
474                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
475 
476                                 Pane NewPane2 = null;
477                                 try {
478                                     NewPane2 = loader2.load();
479                                 } catch (IOException e) {
480                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
481                                 }
482                                 YearController yearCont = loader2.getController();
483                                 yearCont.initYear(cell.getValue().getKey());
484                                 String tabTitle = ("Year View - " + cell.getValue().getKey());
485                                 openTabInCurrentWindow(NewPane2, tabTitle);
486                             }, () -> { //Open In New Tab
487                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
488 
489                                 Pane NewPane2 = null;
490                                 try {
491                                     NewPane2 = loader2.load();
492                                 } catch (IOException e) {
493                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
494                                 }
495                                 YearController yearCont = loader2.getController();
496                                 yearCont.initYear(cell.getValue().getKey());
497                                 String tabTitle = ("Year View - " + cell.getValue().getKey());
498                                 addTab(NewPane2, tabTitle);
499                             }, () -> { //Open In New Window
500                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
501 
502                                 Pane NewPane2 = null;
503                                 try {
504                                     NewPane2 = loader2.load();
505                                 } catch (IOException e) {
506                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
507                                 }
508                                 YearController yearCont = loader2.getController();
509                                 yearCont.initYear(cell.getValue().getKey());
510                                 String tabTitle = ("Year View - " + cell.getValue().getKey());
511                                 openInNewWindow(NewPane2, tabTitle);
512                             }, () -> new AddYear(cell.getValue().getKey(), this.getWindow()));
513 
514                     break;
515                 }
516                 case "Customer": {
517                     cmContent = createContextMenuContent(
518                             //Open
519                             () -> {
520                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Customer.fxml"));
521 
522                                 Pane NewPane2 = null;
523                                 try {
524                                     NewPane2 = loader2.load();
525                                 } catch (IOException e) {
526                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
527                                 }
528                                 CustomerController customerCont = loader2.getController();
529                                 customerCont.initCustomer((Customer) cell.getValue().getValue().getValue(), this);
530                                 String tabTitle = ("Customer View - " + cell.getValue().getKey() + " - " + ((Customer) cell.getValue().getValue().getValue()).getYear());
531                                 openTabInCurrentWindow(NewPane2, tabTitle);
532                             }, () -> { //Open In New Tab
533                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Customer.fxml"));
534 
535                                 Pane NewPane2 = null;
536                                 try {
537                                     NewPane2 = loader2.load();
538                                 } catch (IOException e) {
539                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
540                                 }
541                                 CustomerController customerCont = loader2.getController();
542                                 customerCont.initCustomer((Customer) cell.getValue().getValue().getValue(), this);
543                                 String tabTitle = ("Customer View - " + cell.getValue().getKey() + " - " + ((Customer) cell.getValue().getValue().getValue()).getYear());
544                                 addTab(NewPane2, tabTitle);
545                             }, () -> { //Open In New Window
546                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Customer.fxml"));
547 
548                                 Pane NewPane2 = null;
549                                 try {
550                                     NewPane2 = loader2.load();
551                                 } catch (IOException e) {
552                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
553                                 }
554                                 CustomerController customerCont = loader2.getController();
555                                 customerCont.initCustomer((Customer) cell.getValue().getValue().getValue(), this);
556                                 String tabTitle = ("Customer View - " + cell.getValue().getKey() + " - " + ((Customer) cell.getValue().getValue().getValue()).getYear());
557                                 openInNewWindow(NewPane2, tabTitle);
558                             }, () -> { //Edit
559                                 openEditCustomer((Customer) cell.getValue().getValue().getValue());
560                             });
561                     break;
562                 }
563                 case "Group": {
564                     cmContent = createContextMenuContent(
565                             //Open
566                             () -> AddGroup.addGroup(cell.getValue().getValue().getValue().toString(), cell.getValue().getKey(), null), null, null, null);
567                     break;
568                 }
569                 case "UserCustomerView": {
570                     cmContent = createContextMenuContent(
571                             //Open
572                             () -> {
573                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
574 
575                                 Pane NewPane2 = null;
576                                 try {
577                                     NewPane2 = loader2.load();
578                                 } catch (IOException e) {
579                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
580                                 }
581                                 YearController yearCont = loader2.getController();
582                                 yearCont.initYear(cell.getValue().getValue().getValue().toString(), cell.getValue().getKey());
583                                 String tabTitle = ("Year View - " + cell.getValue().getValue().getValue() + " - " + cell.getValue().getKey());
584 
585                                 openTabInCurrentWindow(NewPane2, tabTitle);
586                             },
587                             () -> {
588                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
589 
590                                 Pane NewPane2 = null;
591                                 try {
592                                     NewPane2 = loader2.load();
593                                 } catch (IOException e) {
594                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
595                                 }
596                                 YearController yearCont = loader2.getController();
597                                 yearCont.initYear(cell.getValue().getValue().getValue().toString(), cell.getValue().getKey());
598                                 String tabTitle = ("Year View - " + cell.getValue().getValue().getValue() + " - " + cell.getValue().getKey());
599 
600                                 addTab(NewPane2, tabTitle);
601                             },
602                             () -> {
603                                 FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/UI/Year.fxml"));
604 
605                                 Pane NewPane2 = null;
606                                 try {
607                                     NewPane2 = loader2.load();
608                                 } catch (IOException e) {
609                                     LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
610                                 }
611                                 YearController yearCont = loader2.getController();
612                                 yearCont.initYear(cell.getValue().getValue().getValue().toString(), cell.getValue().getKey());
613                                 String tabTitle = ("Year View - " + cell.getValue().getValue().getValue() + " - " + cell.getValue().getKey());
614 
615                                 openInNewWindow(NewPane2, tabTitle);
616                             },
617                             null);
618                     break;
619                 }
620 
621                 case "User": {
622                     cmContent = createContextMenuContent(
623                             //Open
624                             () -> new AddUser(getWindow(), cell.getValue().getValue().getValue().toString()), null, null, null);
625                     break;
626                 }
627 
628             }
629 
630 
631         }
632         cm.getItems().addAll(cmContent.getItems());
633         MenuItem refresh = new MenuItem("Refresh");
634         refresh.setOnAction(event -> fillTreeView());
635         cm.getItems().add(refresh);
636         // other menu items...
637         return cm;
638     }
639 
640     private ContextMenu createContextMenuContent(contextActionCallback open, contextActionCallback openInNewTab, contextActionCallback openInNewWindow, contextActionCallback edit) {
641         ContextMenu cm = new ContextMenu();
642         if (open != null) {
643             MenuItem openItem = new MenuItem("Open");
644             openItem.setOnAction(event -> open.doAction());
645             cm.getItems().add(openItem);
646         }
647         if (openInNewTab != null) {
648             MenuItem openItem = new MenuItem("Open in New Tab");
649             openItem.setOnAction(event -> openInNewTab.doAction());
650             cm.getItems().add(openItem);
651         }
652         if (openInNewWindow != null) {
653             MenuItem openItem = new MenuItem("Open in New Window");
654             openItem.setOnAction(event -> openInNewWindow.doAction());
655             cm.getItems().add(openItem);
656         }
657         if (edit != null) {
658             MenuItem openItem = new MenuItem("Edit");
659             openItem.setOnAction(event -> edit.doAction());
660             cm.getItems().add(openItem);
661         }
662 
663         return cm;
664     }
665 
666     public javafx.stage.Window getWindow() {
667         return tabPane2.getScene().getWindow();
668     }
669 
670     public Tab addTab(Pane fillPane, String tabTitle) {
671         Tab tab = new Tab(tabTitle);
672         AnchorPane tabContentPane = new AnchorPane(fillPane);
673         AnchorPane.setBottomAnchor(tabContentPane, 0.0);
674         AnchorPane.setTopAnchor(tabContentPane, 0.0);
675         AnchorPane.setLeftAnchor(tabContentPane, 0.0);
676         AnchorPane.setRightAnchor(tabContentPane, 0.0);
677         tabContentPane.setMinWidth(0);
678         tabContentPane.setMinHeight(0);
679         tab.setContent(tabContentPane);
680         tab.setClosable(true);
681 
682         tabPane2.getTabs().add(tab);
683         tabPane2.getSelectionModel().select(tab);
684         return tab;
685     }
686 
687     public void closeTab(Tab tab) {
688         tabPane2.getTabs().remove(tab);
689     }
690 
691     private Tab openTabInCurrentWindow(Pane fillPane, String tabTitle) {
692         Tab tab = new Tab(tabTitle);
693         AnchorPane tabContentPane = new AnchorPane(fillPane);
694         AnchorPane.setBottomAnchor(tabContentPane, 0.0);
695         AnchorPane.setTopAnchor(tabContentPane, 0.0);
696         AnchorPane.setLeftAnchor(tabContentPane, 0.0);
697         AnchorPane.setRightAnchor(tabContentPane, 0.0);
698         tab.setContent(tabContentPane);
699         tab.setClosable(true);
700         if (tabPane2.getTabs().isEmpty()) {
701             tabPane2.getTabs().add(tab);
702         } else {
703             tabPane2.getTabs().set(tabPane2.getSelectionModel().getSelectedIndex(), tab);
704         }
705         tabPane2.getSelectionModel().select(tab);
706         return tab;
707     }
708 
709     private Stage openInNewWindow(Pane fillPane, String tabTitle) {
710         Stage stage = new Stage();
711         stage.setTitle(tabTitle);
712         stage.setScene(new Scene(fillPane));
713         stage.initOwner(getWindow());
714         stage.setMinWidth(850);
715         stage.setMinHeight(850);
716         stage.show();
717 
718         return stage;
719     }
720 // --Commented out by Inspection START (10/28/17 5:02 PM):
721 //    public void openAddCustomer(String year) {
722 //        Pane newPane = null;
723 //        FXMLLoader loader;
724 //        loader = new FXMLLoader(getClass().getResource("/UI/AddCustomer.fxml"));
725 //        try {
726 //            newPane = loader.load();
727 //        } catch (IOException e) {
728 //            e.printStackTrace();
729 //        }
730 //        Controllers.AddCustomerController addCustCont = loader.getController();
731 //        Tab tab = addTab(newPane, "Add Utilities.Customer - " + year);
732 //        // get children of parent of secPane (the VBox)
733 //
734 //        addCustCont.initAddCust(year, this, tab);
735 //    }
736 // --Commented out by Inspection STOP (10/28/17 5:02 PM)
737 
738     public void openEditCustomer(Customer customer) {
739         Pane newPane = null;
740         FXMLLoader loader;
741         loader = new FXMLLoader(getClass().getResource("/UI/AddCustomer.fxml"));
742         try {
743             newPane = loader.load();
744         } catch (IOException e) {
745             LogToFile.log(e, Severity.SEVERE, "Error loading window. Please retry then reinstall application. If error persists, contact the developers.");
746         }
747         AddCustomerController addCustCont = loader.getController();
748         Tab tab = addTab(newPane, "Edit Customer - " + customer.getName() + " - " + customer.getYear());
749         // get children of parent of secPane (the VBox)
750 
751         addCustCont.initAddCust(customer, this, tab, null);
752     }
753 
754     public void addCustomerToTreeView(Customer customer, TreeItem<TreeItemPair<String, Pair<String, Object>>> parent) {
755         parent.getChildren().add(parent.getChildren().size() - 1, new contextTreeItem(customer.getName(), new Pair<String, Customer>("Customer", customer)));
756 
757     }
758     /**
759      * Adds the year buttons to the main panel.
760      */
761     void fillTreeView() {
762         //ProgressDialog progDial = new ProgressDialog();
763         ProgressForm progDial = new ProgressForm();
764 //Do check if new or not, send -1 as ID
765 
766         LoadMainWorker loadWorker = new LoadMainWorker(this);
767 
768         progDial.activateProgressBar(loadWorker);
769         loadWorker.setOnSucceeded(event -> {
770             selectNav.setRoot(loadWorker.getValue());
771             progDial.getDialogStage().close();
772 
773         });
774         loadWorker.setOnFailed(event -> {
775             progDial.getDialogStage().close();
776 
777             Throwable e = loadWorker.getException();
778 
779             if (e instanceof SQLException) {
780                 LogToFile.log((SQLException) e, Severity.SEVERE, CommonErrors.returnSqlMessage(((SQLException) loadWorker.getException())));
781 
782             }
783             if (e instanceof InterruptedException) {
784                 if (loadWorker.isCancelled()) {
785                     LogToFile.log((InterruptedException) e, Severity.FINE, "Load process canceled.");
786 
787                 }
788             }
789 
790 
791         });
792 
793 
794         progDial.getDialogStage().show();
795         new Thread(loadWorker).start();
796 
797 
798 
799     }
800 
801     interface contextActionCallback {
802         void doAction();
803     }
804 
805     abstract class AbstractTreeItem extends TreeItem {
806 
807         protected abstract ContextMenu getMenu();
808     }
809 
810     public class contextTreeItem<K, V> extends AbstractTreeItem {
811         // make class vars here like psswd
812 
813         public contextTreeItem(String key, Pair<String, String> value) {
814             this.setValue(new TreeItemPair<>(key, value));
815         }
816 
817         public contextTreeItem(String key, String value) {
818             this.setValue(new TreeItemPair<>(key, new Pair<String, String>(value, "")));
819         }
820 
821         public contextTreeItem(String key) {
822             this.setValue(new TreeItemPair<>(key, null));
823         }
824 
825         @Override
826         public ContextMenu getMenu() {
827 
828             return createContextMenu(this);
829         }
830     }
831 
832     private final class TreeCellImpl<K, V> extends TreeCell<TreeItemPair<String, Pair<String, String>>> {
833 
834         @Override
835         public void updateItem(TreeItemPair<String, Pair<String, String>> item, boolean empty) {
836             super.updateItem(item, empty);
837 
838             if (empty) {
839                 setText(null);
840                 setGraphic(null);
841             } else {
842                 setText(getItem() == null ? "" : getItem().getKey());
843                 setGraphic(getTreeItem().getGraphic());
844                 setContextMenu(((AbstractTreeItem) getTreeItem()).getMenu());
845             }
846         }
847     }
848 
849 
850 }