From e13040112654b1fedaaa270e135b4effb27f6c03 Mon Sep 17 00:00:00 2001 From: Date: Mon, 1 Oct 2018 11:31:05 +0200 Subject: [PATCH] added categories --- src/application/DBController.java | 167 ++++++++++++++++++---- src/application/Main.java | 4 +- src/application/MainWindow.fxml | 75 ++++++++-- src/application/MainWindowController.java | 166 ++++++++++++++++++--- src/application/tableDataPositionen.java | 17 ++- 5 files changed, 369 insertions(+), 60 deletions(-) diff --git a/src/application/DBController.java b/src/application/DBController.java index 88955c9..a527374 100644 --- a/src/application/DBController.java +++ b/src/application/DBController.java @@ -75,8 +75,7 @@ class DBController return true; } } - - + // table Position section public void createTablePositionen() { // create table position @@ -85,7 +84,7 @@ class DBController Statement stmt = connection.createStatement(); stmt.executeUpdate("DROP TABLE IF EXISTS positionen;"); stmt.executeUpdate( - "CREATE TABLE positionen (id, name, value, color);"); + "CREATE TABLE positionen (posid, name, value, cat, color);"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); e.printStackTrace(); @@ -93,21 +92,91 @@ class DBController // create 25 demo/default data entries for (int i = 0; i < 25; i++) { - fillPositionen(i + 1, "Noch frei", (float) 0.00, "#ad0000"); + fillPositionen_Positionen(i + 1, "Noch frei", (float) 0.00, + ((int) (i / 5)) + 1, "#ad0000"); } } - public void fillPositionen(int pID, String pName, float pValue, - String pColor) + public void createTableCategory() + { // create table position + System.out.println("Erstelle Tabelle Kategorie"); + try { + Statement stmt = connection.createStatement(); + stmt.executeUpdate("DROP TABLE IF EXISTS category;"); + stmt.executeUpdate("CREATE TABLE category (catid, catname);"); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + } + + for (int i = 1; i < 6; i++) { + fillCategory_Category(i, "Cat: " + (i)); + } + + } + + public String getCategoryNameFromPositionen(int pID) + { + //System.out.println("getCategoryName: " + pID); + + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery( + "SELECT posid, cat, catid, catname FROM positionen, category " + + "WHERE posid = " + pID + " AND cat = catid;"); + return rs.getString("catname"); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + return "Error 404"; + } + + } + + public void setName_Category(int pID, String pName) + { // Setzte den Namen + try { + Statement stmt = connection.createStatement(); + stmt.executeUpdate("UPDATE category SET catname = '" + pName + + "'WHERE catid =" + pID + ";"); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + } + } + + public void fillCategory_Category(int pID, String pName) + { + + System.out.println("Erstelle neuen Kategorie Eintrag"); + try { + PreparedStatement ps = connection + .prepareStatement("INSERT INTO category VALUES (?, ?);"); + ps.setInt(1, pID); // primary + ps.setString(2, pName); + ps.addBatch(); + connection.setAutoCommit(false); + ps.executeBatch(); // SQL execution + connection.setAutoCommit(true); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + } + + } + + public void fillPositionen_Positionen(int pID, String pName, float pValue, + int pCat, String pColor) { // create new data in table System.out.println("Erstelle neuen positionen eintrag"); try { - PreparedStatement ps = connection - .prepareStatement("INSERT INTO positionen VALUES (?, ?, ?, ?);"); + PreparedStatement ps = connection.prepareStatement( + "INSERT INTO positionen VALUES (?, ?, ?, ?, ?);"); ps.setInt(1, pID); // primary ps.setString(2, pName); ps.setFloat(3, pValue); - ps.setString(4, pColor); + ps.setInt(4, pCat); + ps.setString(5, pColor); ps.addBatch(); connection.setAutoCommit(false); @@ -119,12 +188,12 @@ class DBController } } - public String getName(int pID) + public String getName_Positionen(int pID) { // Gibt das Datum zurück try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( - "SELECT id, name FROM positionen WHERE id = " + pID + ";"); + "SELECT posid, name FROM positionen WHERE posid = " + pID + ";"); return rs.getString("name"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); @@ -133,12 +202,28 @@ class DBController } } - public String getValue(int pID) + public String getName_Category(int pID) + { // Gibt das Datum zurück + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery( + "SELECT catid, catname FROM category WHERE catid = " + pID + + ";"); + return rs.getString("catname"); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + return "Error 404"; + } + } + + public String getValue_Positionen(int pID) { // Gibt das Konto zurück try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( - "SELECT id, value FROM positionen WHERE id = " + pID + ";"); + "SELECT posid, value FROM positionen WHERE posid = " + pID + + ";"); return rs.getString("value"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); @@ -147,12 +232,28 @@ class DBController } } - public String getColor(int pID) + public int getCat_Positionen(int pID) { // Gibt den Nutzernamen zurück try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( - "SELECT id, color FROM positionen WHERE id = " + pID + ";"); + "SELECT catid, cat FROM positionen WHERE catid = " + pID + ";"); + System.out.println("getCarTet: " + rs.getInt("cat")); + return rs.getInt("cat"); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + return 0; + } + } + + public String getColor_Positionen(int pID) + { // Gibt den Nutzernamen zurück + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery( + "SELECT posid, color FROM positionen WHERE posid = " + pID + + ";"); return rs.getString("color"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); @@ -161,36 +262,48 @@ class DBController } } - public void setName(int pID, String pName) + public void setName_Positionen(int pID, String pName) { // Setzt das Datum try { Statement stmt = connection.createStatement(); stmt.executeUpdate("UPDATE positionen SET name = '" + pName - + "'WHERE id =" + pID + ";"); + + "'WHERE posid =" + pID + ";"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); e.printStackTrace(); } } - public void setValue(int pID, String pValue) + public void setValue_Positionen(int pID, String pValue) { // Setzt das Konto try { Statement stmt = connection.createStatement(); stmt.executeUpdate("UPDATE positionen SET value = '" + pValue - + "'WHERE id =" + pID + ";"); + + "'WHERE posid =" + pID + ";"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); e.printStackTrace(); } } - public void setColor(int pID, String pColor) + public void setCat_Positionen(int pID, int pCat) + { // Setzt den Nutzername + try { + Statement stmt = connection.createStatement(); + stmt.executeUpdate("UPDATE positionen SET cat = '" + pCat + + "'WHERE catid =" + pID + ";"); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + } + } + + public void setColor_Positionen(int pID, String pColor) { // Setzt den Nutzername try { Statement stmt = connection.createStatement(); stmt.executeUpdate("UPDATE positionen SET color = '" + pColor - + "'WHERE id =" + pID + ";"); + + "'WHERE posid =" + pID + ";"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); e.printStackTrace(); @@ -206,9 +319,9 @@ class DBController while (rs.next()) { try { // Entschlüsselte Daten werden als Datenobjekt gespeichert - daten.add(new tableDataPositionen(rs.getInt("id"), + daten.add(new tableDataPositionen(rs.getInt("posid"), rs.getString("name"), rs.getString("value"), - rs.getString("color"))); + rs.getString("cat"), rs.getString("color"))); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -220,7 +333,7 @@ class DBController } return daten; } - + public void ausgebenSysoPositionen() { // Debugging Ausgabe der kompletten Tabelle System.out.println("Print positionen"); @@ -228,7 +341,7 @@ class DBController Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM positionen;"); while (rs.next()) { - System.out.println("id = " + rs.getString("id")); + System.out.println("posid = " + rs.getString("posid")); System.out.println("name = " + rs.getString("name")); System.out.println("vale = " + rs.getString("value")); System.out.println("color = " + rs.getString("color")); @@ -241,7 +354,6 @@ class DBController } } - // table Jobs section public void erstelleTabelleJobs() { // Erstelle Tabelle mit Reihen @@ -257,7 +369,4 @@ class DBController } } - - - } diff --git a/src/application/Main.java b/src/application/Main.java index 76260e7..704df5d 100644 --- a/src/application/Main.java +++ b/src/application/Main.java @@ -33,7 +33,7 @@ public class Main extends Application FXMLLoader loader = new FXMLLoader( getClass().getResource("MainWindow.fxml")); AnchorPane pane = loader.load(); - primaryStage.setTitle("jFxKasse - devVersion"); // Title of window + primaryStage.setTitle("jFxKasse"); // Title of window mwc = loader.getController(); mwc.setMain(this, dbc); @@ -76,7 +76,9 @@ public class Main extends Application dbc.dbname = mwc.getDatabaseName(); // handover database name dbc.connectDatabase(); // estabishing DB conection mwc.fillTablePositionen(); // fill TreeTable 'Positionen' + mwc.fillCategory(); mwc.loadGridButtons(); + mwc.getSelectedCat(); //Load DB entries in Chois Box } else { // config.xml NOT found, first start of app System.out.println("keine XML gefunden!"); diff --git a/src/application/MainWindow.fxml b/src/application/MainWindow.fxml index ab38714..eee2c8a 100644 --- a/src/application/MainWindow.fxml +++ b/src/application/MainWindow.fxml @@ -25,10 +25,10 @@ - - + + + + + + @@ -68,10 +114,11 @@ - - - - + + + + + @@ -114,7 +161,13 @@ - + + + diff --git a/src/application/MainWindowController.java b/src/application/MainWindowController.java index 81dfb64..c1fdc83 100644 --- a/src/application/MainWindowController.java +++ b/src/application/MainWindowController.java @@ -23,6 +23,9 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; + +import com.jfoenix.controls.JFXTextField; + import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; @@ -87,6 +90,9 @@ public class MainWindowController @FXML private TreeTableColumn columnColor; + @FXML + private TreeTableColumn columnCat; + @FXML private TreeTableColumn columnPrize; @@ -99,6 +105,9 @@ public class MainWindowController @FXML private ChoiceBox colorChoise; + @FXML + private ChoiceBox catChoise; + @FXML private Button ueberbtn; @@ -207,6 +216,39 @@ public class MainWindowController @FXML private Button btnOpenFolder; + @FXML + private Label labelCat01; + + @FXML + private Label labelCat02; + + @FXML + private Label labelCat05; + + @FXML + private Label labelCat04; + + @FXML + private Label labelCat03; + + @FXML + private JFXTextField tftKat01; + + @FXML + private JFXTextField tftKat02; + + @FXML + private JFXTextField tftKat03; + + @FXML + private JFXTextField tftKat04; + + @FXML + private JFXTextField tftKat05; + + @FXML + private Button btnSaveCat; + @FXML private Label labelAllPrize; @@ -225,6 +267,9 @@ public class MainWindowController @FXML private Label lableAllValue; + @FXML + private Label lableSelectCat; + @FXML private Label lableNewPosition; @@ -243,6 +288,9 @@ public class MainWindowController @FXML private TitledPane titlePaneStats; + @FXML + private TitledPane titlePaneCat; + @FXML private TextField tftNewPosition; @@ -264,6 +312,8 @@ public class MainWindowController private String selectedColorName; + private String selectedCatName; + private String databaseName; private boolean lockState = false; @@ -277,7 +327,7 @@ public class MainWindowController @FXML TreeItem rootPositionen = new TreeItem<>( - new tableDataPositionen(0, "0", "0", "0")); + new tableDataPositionen(0, "0", "0", "0", "0")); Properties props = new Properties(); @@ -330,6 +380,7 @@ public class MainWindowController dbc.connectDatabase(); // establish DB connection dbc.createTablePositionen(); // Create new table dbc.erstelleTabelleJobs(); // Create new table + dbc.createTableCategory(); // Create new table try { saveSettings(getDatabaseName()); } catch (Exception e) { @@ -339,6 +390,7 @@ public class MainWindowController setDBLabel(); // Set new databese labels blockUI(false); // unlock UI elements that need DB fillTablePositionen(); // fill TreeTable 'Positionen' + fillCategory(); initUI(); // Starting the UI elements } @@ -348,10 +400,11 @@ public class MainWindowController { System.out.println("Speichere Eintrag!"); - dbc.setName(idPositionen, tftNewPosition.getText()); - dbc.setValue(idPositionen, tftNewValue.getText()); - dbc.setColor(idPositionen, getColorCodes(selectedColorName)); + dbc.setName_Positionen(idPositionen, tftNewPosition.getText()); + dbc.setValue_Positionen(idPositionen, tftNewValue.getText()); + dbc.setColor_Positionen(idPositionen, getColorCodes(selectedColorName)); + System.out.println("refill pos"); fillTablePositionen(); // fill TreeTable 'Positionen' loadGridButtons(); @@ -361,9 +414,9 @@ public class MainWindowController public void btnClearEntryAction(ActionEvent event) { // set default values - dbc.setName(idPositionen, "Noch frei"); - dbc.setValue(idPositionen, "0.00"); - dbc.setColor(idPositionen, "#FAF0E6"); + dbc.setName_Positionen(idPositionen, "Noch frei"); + dbc.setValue_Positionen(idPositionen, "0.00"); + dbc.setColor_Positionen(idPositionen, "#FAF0E6"); fillTablePositionen(); // fill TreeTable 'Positionen' } @@ -401,6 +454,23 @@ public class MainWindowController } + @FXML + public void btnSaveCatAction(ActionEvent event) + { + System.out.println("Cat´s speichern"); + + dbc.setName_Category(1, tftKat01.getText()); + dbc.setName_Category(2, tftKat02.getText()); + dbc.setName_Category(3, tftKat03.getText()); + dbc.setName_Category(4, tftKat04.getText()); + dbc.setName_Category(5, tftKat05.getText()); + + fillCategory(); + fillTablePositionen(); + getSelectedCat(); + + } + @FXML public void btnDeleteSelectedPositionAction(ActionEvent event) { @@ -567,6 +637,7 @@ public class MainWindowController @FXML public void fillTablePositionen() { // loads the table in the TreeTableView + rootPositionen.getChildren().remove(0, rootPositionen.getChildren().size()); @@ -575,11 +646,17 @@ public class MainWindowController dbc.ladeTabellePositionen().get(i).getID(), dbc.ladeTabellePositionen().get(i).getName(), dbc.ladeTabellePositionen().get(i).getValue() + " €", + + // dbc.ladeTabellePositionen().get(i).getCat(), + // dbc.getCategoryName(dbc.ladeTabellePositionen().get(i).getCat())) + dbc.getCategoryNameFromPositionen(i + 1), + getColorNames(dbc.ladeTabellePositionen().get(i).getColor())); rootPositionen.getChildren() .add(new TreeItem(helpTableData)); } + } public void initUI() @@ -587,6 +664,50 @@ public class MainWindowController System.out.println("initUI"); tftNewDBName.setText(getDatabaseName()); initPositionen(); + + } + + public int getSelectedCat() + { + + ObservableList cats = FXCollections.observableArrayList(); + + cats.add(dbc.getName_Category(1)); + cats.add(dbc.getName_Category(2)); + cats.add(dbc.getName_Category(3)); + cats.add(dbc.getName_Category(4)); + cats.add(dbc.getName_Category(5)); + + catChoise.setItems(cats); + catChoise.getSelectionModel().selectedIndexProperty() + .addListener(new ChangeListener() { + @Override + public void changed(ObservableValue ov, + Number value, Number new_value) + { + selectedCatName = catChoise.getItems().get((int) new_value) + .toString(); + + System.out.println("Ausgewählte Cat: " + selectedCatName); + } + }); + + for (int i = 1; i < 5; i++) { + if (selectedCatName == dbc.getName_Category(i)) { + return i; + } + } + + return -1; + } + + public void fillCategory() + { + tftKat01.setText(dbc.getName_Category(1)); + tftKat02.setText(dbc.getName_Category(2)); + tftKat03.setText(dbc.getName_Category(3)); + tftKat04.setText(dbc.getName_Category(4)); + tftKat05.setText(dbc.getName_Category(5)); } private void initPositionen() @@ -619,6 +740,9 @@ public class MainWindowController columnPrize.setCellValueFactory( cellData -> cellData.getValue().getValue().valueProperty()); + columnCat.setCellValueFactory( + cellData -> cellData.getValue().getValue().catProperty()); + columnColor.setCellValueFactory( cellData -> cellData.getValue().getValue().colorProperty()); @@ -631,18 +755,20 @@ public class MainWindowController // last = selected; //for auto-play int selected = entryTreeTable.getSelectionModel() .getSelectedIndex(); // get selected item + idPositionen = columnPosnumber.getCellData(selected); // Ausgewählte - // Spalte + // Spalte System.out.println( "Positionen - Ausgewaehlte Spalte: " + idPositionen); try { // Setzt den Inhalt in die Textfelder - tftNewPosition.setText(dbc.getName(idPositionen)); - tftNewValue.setText(dbc.getValue(idPositionen)); - colorChoise.getSelectionModel() - .select(getColorID(dbc.getColor(idPositionen))); + tftNewPosition + .setText(dbc.getName_Positionen(idPositionen)); + tftNewValue.setText(dbc.getValue_Positionen(idPositionen)); + colorChoise.getSelectionModel().select( + getColorID(dbc.getColor_Positionen(idPositionen))); } catch (Exception e) { // TODO Auto-generated catch block @@ -654,6 +780,7 @@ public class MainWindowController columnPosnumber.setStyle("-fx-alignment: CENTER;"); columnPositionsEdit.setStyle("-fx-alignment: CENTER;"); columnPrize.setStyle("-fx-alignment: CENTER;"); + columnCat.setStyle("-fx-alignment: CENTER;"); columnColor.setStyle("-fx-alignment: CENTER;"); tftNewValue.textProperty().addListener(new ChangeListener() { @@ -855,6 +982,9 @@ public class MainWindowController labelJobCounter.setVisible(!pState); titlePaneStats.setVisible(!pState); + + titlePaneCat.setDisable(pState); + } public void loadGridButtons() @@ -862,22 +992,22 @@ public class MainWindowController for (int i = 0; i < 25; i++) { - getButtonByID(i).setText(dbc.getName(i + 1)); + getButtonByID(i).setText(dbc.getName_Positionen(i + 1)); - if ((getColorID(dbc.getColor(i + 1)) == 0) - || (getColorID(dbc.getColor(i + 1)) == 7)) { + if ((getColorID(dbc.getColor_Positionen(i + 1)) == 0) + || (getColorID(dbc.getColor_Positionen(i + 1)) == 7)) { getButtonByID(i).setStyle("-fx-background-color: " - + dbc.getColor(i + 1) + "; -fx-text-fill: white;"); + + dbc.getColor_Positionen(i + 1) + "; -fx-text-fill: white;"); } else { getButtonByID(i).setStyle("-fx-background-color: " - + dbc.getColor(i + 1) + "; -fx-text-fill: black;"); + + dbc.getColor_Positionen(i + 1) + "; -fx-text-fill: black;"); } } for (int i = 0; i < 25; i++) { - if (dbc.getName(i + 1).equals("Noch frei")) { + if (dbc.getName_Positionen(i + 1).equals("Noch frei")) { getButtonByID(i).setVisible(false); } else { getButtonByID(i).setVisible(true); diff --git a/src/application/tableDataPositionen.java b/src/application/tableDataPositionen.java index 9566922..b22bafe 100644 --- a/src/application/tableDataPositionen.java +++ b/src/application/tableDataPositionen.java @@ -14,13 +14,16 @@ public class tableDataPositionen private final StringProperty value = new SimpleStringProperty(); + private final StringProperty cat = new SimpleStringProperty(); + private final StringProperty color = new SimpleStringProperty(); - public tableDataPositionen(final int id, final String name, final String value, final String color) + public tableDataPositionen(final int id, final String name, final String value, final String cat, final String color) { this.id.set(id); this.name.set(name); this.value.set(value); + this.cat.set(cat); this.color.set(color); } @@ -39,6 +42,10 @@ public class tableDataPositionen return value; } + public StringProperty catProperty() { + return cat; + } + public StringProperty colorProperty() { return color; @@ -59,6 +66,10 @@ public class tableDataPositionen return valueProperty().get(); } + public String getCat() { + return catProperty().get(); + } + public String getColor() { return colorProperty().get(); @@ -79,6 +90,10 @@ public class tableDataPositionen valueProperty().set(value); } + public final void setCat(String cat) { + catProperty().set(cat); + } + public final void setColor(String color) { colorProperty().set(color);