Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.32 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Controller;
  7.  
  8.  
  9. import View.mouseHooverAnimationPieChart.MouseHoverAnimation;
  10. import java.net.URL;
  11. import java.sql.Connection;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.ArrayList;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.ResourceBundle;
  18. import javafx.beans.property.SimpleStringProperty;
  19. import javafx.beans.value.ChangeListener;
  20. import javafx.beans.value.ObservableValue;
  21. import javafx.collections.FXCollections;
  22. import javafx.collections.ObservableList;
  23. import javafx.event.ActionEvent;
  24. import javafx.event.EventHandler;
  25. import javafx.fxml.FXML;
  26. import javafx.fxml.Initializable;
  27. import javafx.scene.Group;
  28. import javafx.scene.chart.AreaChart;
  29. import javafx.scene.chart.BarChart;
  30. import javafx.scene.chart.LineChart;
  31. import javafx.scene.chart.PieChart;
  32. import javafx.scene.chart.XYChart;
  33. import javafx.scene.control.ChoiceBox;
  34. import javafx.scene.control.ComboBox;
  35. import javafx.scene.control.ContextMenu;
  36. import javafx.scene.control.Label;
  37. import javafx.scene.control.MenuItem;
  38. import javafx.scene.control.TableColumn;
  39. import javafx.scene.control.TableView;
  40. import javafx.scene.control.TextField;
  41. import javafx.scene.control.cell.PropertyValueFactory;
  42. import javafx.scene.effect.BlendMode;
  43. import javafx.scene.input.MouseEvent;
  44. import javafx.util.Callback;
  45. import org.controlsfx.dialog.Dialogs;
  46. import org.fxmisc.easybind.EasyBind;
  47.  
  48. /**
  49.  *
  50.  * @author Eskil
  51.  */
  52. public class FXMLDocumentController implements Initializable {
  53.  
  54.     SQL_manager sql_manager = new SQL_manager();
  55.  
  56.     ObservableList<List<String>> dataen = FXCollections.observableArrayList();
  57.  
  58.     List<TableColumn> lstClm = new ArrayList<TableColumn>();
  59.  
  60.     static XYChart.Series series1 = new XYChart.Series();
  61.     static ObservableList<String> rows = FXCollections.observableArrayList();
  62.  
  63.     int userChosenDataNameColumnIndex = 0;
  64.     int userChosenDataValueColumnIndex = 0;
  65.  
  66.     @FXML
  67.     protected Label caption;
  68.     @FXML
  69.     protected TextField filterField;
  70.     @FXML
  71.     TableView tableView;
  72.     @FXML
  73.     LineChart lineChart;
  74.     @FXML
  75.     AreaChart bubbleChart;
  76.     @FXML
  77.     TextField t;
  78.     @FXML
  79.     TableView tableView2;
  80.     @FXML
  81.     ChoiceBox choiceBoxTable;
  82.     @FXML
  83.     ChoiceBox choiceBoxName;
  84.     @FXML
  85.     ChoiceBox choiceBoxValue;
  86.     @FXML
  87.     protected BarChart barChart;
  88.     @FXML
  89.     protected TableColumn Month;
  90.     @FXML
  91.     protected TableColumn Value;
  92.     @FXML
  93.     protected PieChart pieChart;
  94.  
  95.     @FXML
  96.     protected TextField txtIPadress;
  97.     @FXML
  98.     protected TextField txtPortNumber;
  99.     @FXML
  100.     protected TextField txtSQLinstance;
  101.     @FXML
  102.     protected Group visualizationGroup;
  103.     @FXML
  104.     protected Group dataSelectGroup;
  105.     @FXML
  106.     protected Group sqlConnectGroup;
  107.  
  108.     @FXML
  109.     protected void handleButtonAction(ActionEvent event) throws ClassNotFoundException, SQLException {
  110.         Connection conn = sql_manager.getConnection("localhost", 8889, "mysql");
  111.         sql_manager.getAllTables(choiceBoxTable);
  112.  
  113.         System.out.println("her kommern ut 1");
  114.  
  115.     }
  116.     public void hideShowGroupsAndElements(Boolean barChartTrueFalse, Boolean pieChartTrueFalse, Boolean lineChartTrueFalse, Boolean visualizationGroupTrueFalse, Boolean dataSelectGroupTrueFalse, Boolean sqlConnectGroupTrueFalse) {
  117.  
  118.         visualizationGroup.visibleProperty().set(visualizationGroupTrueFalse);
  119.         dataSelectGroup.visibleProperty().set(dataSelectGroupTrueFalse);
  120.         sqlConnectGroup.visibleProperty().set(sqlConnectGroupTrueFalse);
  121.  
  122.         barChart.visibleProperty().set(barChartTrueFalse);
  123.         pieChart.visibleProperty().set(pieChartTrueFalse);
  124.         lineChart.visibleProperty().set(lineChartTrueFalse);
  125.     }
  126.  
  127.     @FXML
  128.     protected void btnSQlConnect(ActionEvent event) throws ClassNotFoundException, SQLException {
  129.        hideShowGroupsAndElements(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE);
  130.  
  131.     }
  132.  
  133.     @FXML
  134.     protected void btnDataSelect(ActionEvent event) throws ClassNotFoundException, SQLException {
  135.         hideShowGroupsAndElements(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE);
  136.  
  137.     }
  138.  
  139.     @FXML
  140.     protected void handleButtonAction5(ActionEvent event) throws SQLException, ClassNotFoundException {
  141.         Connection conn = sql_manager.getConnection(txtIPadress.getText(), Integer.parseInt(txtPortNumber.getText()), txtSQLinstance.getText());
  142.         sql_manager.getAllTables(choiceBoxTable);
  143.  
  144.     }
  145.  
  146.     @FXML
  147.     protected void handleButtonAction1(ActionEvent event) throws SQLException, ClassNotFoundException {
  148.         //filterData();
  149.     }
  150.  
  151.     @FXML
  152.     protected void handleButtonPieChart(ActionEvent event) throws SQLException, ClassNotFoundException {
  153.         hideShowGroupsAndElements(Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE);
  154.  
  155.         if (checkWhichIndexOnUserSelectedColumns() == true) {
  156.             getPieChartData(userChosenDataValueColumnIndex, userChosenDataNameColumnIndex);
  157.         }
  158.  
  159.     }
  160.  
  161.     @FXML
  162.     protected void handleButtonBubbleChart(ActionEvent event) throws SQLException, ClassNotFoundException {
  163.       hideShowGroupsAndElements(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE);
  164.  
  165.         if (checkWhichIndexOnUserSelectedColumns() == true) {
  166.             getBubbleChart(userChosenDataValueColumnIndex, userChosenDataNameColumnIndex);
  167.         }
  168.  
  169.     }
  170.  
  171.     @FXML
  172.     protected void handeButtonBarChart(ActionEvent event) throws SQLException, ClassNotFoundException {
  173.       hideShowGroupsAndElements(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE);
  174.         if (checkWhichIndexOnUserSelectedColumns() == true) {
  175.             getBarChartData(userChosenDataValueColumnIndex, userChosenDataNameColumnIndex);
  176.         }
  177.  
  178.     }
  179.  
  180.     @FXML
  181.     protected void handleButtonLineChart(ActionEvent event) throws SQLException, ClassNotFoundException {
  182.        hideShowGroupsAndElements(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE);
  183.         if (checkWhichIndexOnUserSelectedColumns() == true) {
  184.             getLineChartData(userChosenDataValueColumnIndex, userChosenDataNameColumnIndex);
  185.         }
  186.  
  187.     }
  188.  
  189.  
  190.  
  191.     private boolean checkWhichIndexOnUserSelectedColumns() throws NumberFormatException {
  192.  
  193.         Iterator itr = lstClm.iterator();
  194.         int counter = 0;
  195.         Boolean thereIsAValue = false;
  196.         Boolean thereIsAName = false;
  197.         Boolean thereIsOnlyTwo = true;
  198.         while (itr.hasNext()) {
  199.             if (counter > 1) {
  200.                 thereIsOnlyTwo = false;
  201.                 System.out.println("FEIL");
  202.             } else {
  203.                 if (lstClm.get(counter).getUserData() == "Value") {
  204.                     thereIsAValue = true;
  205.                     userChosenDataValueColumnIndex = Integer.parseInt(lstClm.get(counter).getId());
  206.  
  207.                 } else if ((lstClm.get(counter).getUserData() == "Name")) {
  208.                     thereIsAName = true;
  209.                     userChosenDataNameColumnIndex = Integer.parseInt(lstClm.get(counter).getId());
  210.                 }
  211.             }
  212.             counter++;
  213.             itr.next();
  214.         }
  215.         if (thereIsAName == true && thereIsAValue == true && thereIsOnlyTwo == true) {
  216.             return true;
  217.         }
  218.         Dialogs.create()
  219.                 .title("Feil i datavalg")
  220.                 .message("Du har valgt en feil i tabellen. Det må kun velges ett felt med navn og ett med verdier")
  221.                 .showInformation();
  222.         visualizationGroup.visibleProperty().set(false);
  223.         dataSelectGroup.visibleProperty().set(true);
  224.         sqlConnectGroup.visibleProperty().set(false);
  225.  
  226.         return false;
  227.     }
  228.  
  229.     @Override
  230.     public void initialize(URL url, ResourceBundle rb) {
  231.         /* MÅ være med for å binde radene i TableView til standardverdiene */
  232.         Month.setCellValueFactory(new PropertyValueFactory("fieldMonth"));
  233.         Value.setCellValueFactory(new PropertyValueFactory("fieldValue"));
  234.         tableView.setItems(dataen);
  235.         /* Added functionality for filtering */
  236.         filterField.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
  237.         });
  238.         ContextMenu menu = new ContextMenu();
  239.         MenuItem item = new MenuItem("Filter");
  240.         item.setOnAction(new EventHandler<ActionEvent>() {
  241.             @Override
  242.             public void handle(ActionEvent event) {
  243.             }
  244.         });
  245.         menu.getItems().add(item);
  246.         tableView.setContextMenu(menu);
  247.  
  248.         addListenerToChoiceBox(choiceBoxTable);
  249.  
  250.     }
  251.  
  252.     private void addListenerToChoiceBox(ChoiceBox choicebox) {
  253.         //adding a listener to the coicebox to pull out the choice the user selected
  254.         choicebox.valueProperty().addListener(new ChangeListener() {
  255.             @Override
  256.             public void changed(ObservableValue observable, Object oldValue, Object newValue) {
  257.                 String newSelection = (String) newValue;
  258.                 try {
  259.                     getDataForSelectedTable(newSelection);
  260.                 } catch (SQLException ex) {
  261.  
  262.                 }
  263.                 System.out.println("choiceBoxSelected by Listener: " + newSelection);
  264.             }
  265.         });
  266.     }
  267.  
  268.  
  269.  
  270.     private void getDataForSelectedTable(String tableName) throws SQLException {
  271.         // henter ut all data for en spesifikk tabell.
  272.         tableView.getColumns().removeAll(tableView.getColumns());
  273.  
  274.         String SQL = "SELECT * FROM " + tableName;
  275.         ResultSet rs = sql_manager.getDataFromSQL("localhost", 8889, "mysql", SQL);
  276.  
  277.         //først henter vi ut alle kolonnene og legger til de i tableview
  278.         for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
  279.             //We are using non property style for making dynamic table
  280.             System.out.print("her kommern " + i);
  281.             final int j = i;
  282.             TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
  283.             System.out.println(col);
  284.             col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
  285.                 public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
  286.                     return new SimpleStringProperty(param.getValue().get(j).toString());
  287.                 }
  288.             });
  289.  
  290.             col.setId((String.valueOf(i)));
  291.             col.prefWidthProperty().bind(tableView.widthProperty().divide(4)); //for å automatisere bredden på kolonnene
  292.             //col.setGraphic(new CheckBox());
  293.             tableView.getColumns().addAll(col);
  294.             addCheckBoxesToColumns(col);
  295.  
  296.         }
  297.  
  298.         //deretter legges all dataen til i kolonnene ved hjelp av rader
  299.         while (rs.next()) {
  300.             ObservableList<String> row = FXCollections.observableArrayList();
  301.             for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  302.                 if (rs.getString(i) != null) {
  303.                     row.add(rs.getString(i));
  304.                     rows.add(rs.getString(i));
  305.  
  306.                     System.out.println(rs.getString(i));
  307.                 } else {
  308.                     row.add(" ");
  309.                 }
  310.  
  311.             }
  312.             dataen.add(row);
  313.         }
  314.  
  315.         tableView.setItems(dataen);
  316.  
  317.     }
  318.  
  319.     private void addCheckBoxesToColumns(TableColumn col) {
  320.         EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
  321.  
  322.             @Override
  323.             public void handle(ActionEvent event) {
  324.                 ComboBox cb = (ComboBox) event.getSource();
  325.                 TableColumn column = (TableColumn) cb.getUserData();
  326.  
  327.                 if (cb.getValue() == " ") {
  328.                     lstClm.remove(column);
  329.                 } else {
  330.                     lstClm.remove(column);
  331.                     column.setUserData(cb.getValue().toString());
  332.                     System.out.println("kanskje her? " + column.getUserData());
  333.                     System.out.println(column + " " + column.getId());
  334.  
  335.                     lstClm.add(column);
  336.                 }
  337.  
  338.             }
  339.         };
  340.         ComboBox cb = new ComboBox();
  341.         cb.getItems().add("Name");
  342.         cb.getItems().add("Value");
  343.         cb.getItems().add(" ");
  344.         cb.setUserData(col);
  345.         cb.setOnAction(handler);
  346.         col.setGraphic(cb);
  347.  
  348.     }
  349.  
  350.     protected void getPieChartData(Integer userChosenDataValueColumnIndex, Integer userChosenDataNameColumnIndex) {
  351.  
  352.         System.out.println("Kolonne id for data er " + userChosenDataValueColumnIndex + "Kolonne id for name er " + userChosenDataNameColumnIndex);
  353.         ObservableList<PieChart.Data> pieChartData
  354.                 = FXCollections.observableArrayList(EasyBind.map(dataen, rowData -> {
  355.                     String name = (String) rowData.get(userChosenDataNameColumnIndex);
  356.                     int value = Integer.parseInt(rowData.get(userChosenDataValueColumnIndex));
  357.                     return new PieChart.Data(name, value);
  358.                 }));
  359.         System.out.println("aa " + pieChartData.get(0));
  360.         pieChart.setData(pieChartData);
  361.  
  362.         for (PieChart.Data d : pieChartData) {
  363.           //  d.getNode().setOnMouseClicked(new MouseHoverAnimation(d, pieChart, caption));
  364.          //   d.getNode().setUserData(d.getPieValue());
  365.             d.getNode().setBlendMode(BlendMode.SRC_ATOP);
  366.           d.getNode().addEventHandler(MouseEvent.MOUSE_ENTERED,
  367.     new EventHandler<MouseEvent>() {
  368.         @Override public void handle(MouseEvent e) {
  369.            System.out.println("blabla");
  370.          }
  371.     });    
  372.         }
  373.     }
  374.  
  375.    
  376.  
  377.     protected void getBarChartData(Integer userChosenDataValueColumnIndex, Integer userChosenDataNameColumnIndex) {
  378.  
  379.         ObservableList<XYChart.Data<String, Number>> barChartData = EasyBind.map(dataen, rowData -> {
  380.             String name = rowData.get(userChosenDataNameColumnIndex);
  381.             Double value = new Double(rowData.get(userChosenDataValueColumnIndex));
  382.             return new XYChart.Data(name, value);
  383.         });
  384.  
  385.         XYChart.Series series1 = new XYChart.Series();
  386.         series1.getData().addAll(barChartData);
  387.         barChart.getData().addAll(series1);
  388.  
  389.         //series1.getData().clear();
  390.         // series1.getData().add(new XYChart.Data(itr.next(), dataen.get(1)));
  391.         barChart.setData(FXCollections.observableArrayList(series1));
  392.     }
  393.  
  394.     protected void getBubbleChart(Integer userChosenDataValueColumnIndex, Integer userChosenDataNameColumnIndex) {
  395.         //in the making. Bubblechart må ha to int verdier, ikke en name og en value.
  396.         ObservableList<XYChart.Data<String, Number>> bubbleChartData = EasyBind.map(dataen, rowData -> {
  397.             Double name = new Double(rowData.get(1));
  398.             Double value = new Double(rowData.get(4));
  399.             return new XYChart.Data(name, value);
  400.         });
  401.  
  402.         XYChart.Series series1 = new XYChart.Series();
  403.         series1.getData().addAll(bubbleChartData);
  404.         bubbleChart.getData().addAll(series1);
  405.  
  406.         //series1.getData().clear();
  407.         // series1.getData().add(new XYChart.Data(itr.next(), dataen.get(1)));
  408.         bubbleChart.setData(FXCollections.observableArrayList(series1));
  409.  
  410.     }
  411.  
  412.     protected void getLineChartData(Integer userChosenDataValueColumnIndex, Integer userChosenDataNameColumnIndex) {
  413.  
  414.         ObservableList<XYChart.Data<String, Number>> barChartData = EasyBind.map(dataen, rowData -> {
  415.             String name = rowData.get(userChosenDataNameColumnIndex);
  416.             Double value = new Double(rowData.get(userChosenDataValueColumnIndex));
  417.             return new XYChart.Data(name, value);
  418.         });
  419.         XYChart.Series series1 = new XYChart.Series();
  420.         series1.getData().addAll(barChartData);
  421.         lineChart.getData().addAll(series1);
  422.  
  423.         //series1.getData().clear();
  424.         // series1.getData().add(new XYChart.Data(itr.next(), dataen.get(1)));
  425.         lineChart.setData(FXCollections.observableArrayList(series1));
  426.     }
  427.  
  428.    
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement