From aa756cefd73750f2e15328f358b08025bf10cdc9 Mon Sep 17 00:00:00 2001 From: tainfante Date: Wed, 24 Jan 2018 22:38:46 +0100 Subject: [PATCH 1/5] Null pointers handled --- src/mainwindow/Channel.java | 146 ++++++++++++++++++++++----- src/mainwindow/Chart.java | 196 +++++++++++++++++++++++++++--------- src/mainwindow/channel.fxml | 7 +- src/plot/Plot.java | 32 +++--- 4 files changed, 289 insertions(+), 92 deletions(-) diff --git a/src/mainwindow/Channel.java b/src/mainwindow/Channel.java index 4f07bc8..437d6bf 100644 --- a/src/mainwindow/Channel.java +++ b/src/mainwindow/Channel.java @@ -8,6 +8,8 @@ import javafx.scene.control.ChoiceBox; import javafx.scene.control.ColorPicker; import javafx.scene.control.TextField; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.stage.Stage; @@ -21,9 +23,13 @@ public class Channel implements Initializable{ @FXML private TextField unitText; @FXML - private ChoiceBox channelBox; + private ChoiceBox channelBox; @FXML private ColorPicker colorPicker; + private String channel; + private String name; + private String units; + private Color color=Color.RED; @Override public void initialize(URL location, ResourceBundle resources) { @@ -37,36 +43,122 @@ public void initialize(URL location, ResourceBundle resources) { channelBox.getItems().add("Channel 8"); } - public void onSetAction(ActionEvent actionEvent) { - String channel=new String(); - String name=nameText.getText(); - String units=unitText.getText(); - Color color=colorPicker.getValue(); - try{ - channel=(String)channelBox.getSelectionModel().getSelectedItem(); - if(channel.equals("Channel 1")){ - Chart.getInstance().channelUpdate(1,name+" ["+units+"]",color); - } - else if(channel.equals("Channel 2")){ - Chart.getInstance().channelUpdate(2,name+" ["+units+"]",color); - } - else if(channel.equals("Channel 3")){ - Chart.getInstance().channelUpdate(3,name+" ["+units+"]", color); + public void onNameKeyPressed(KeyEvent keyEvent) { + if(keyEvent.getCode() == KeyCode.ENTER){ + try{ + name=nameText.getText(); + channel=channelBox.getSelectionModel().getSelectedItem(); + channel=channelBox.getSelectionModel().getSelectedItem(); + switch (channel) { + case "Channel 1": + Chart.getInstance().channelUpdateWithoutColor(1, name, units); + break; + case "Channel 2": + Chart.getInstance().channelUpdateWithoutColor(2, name, units); + break; + case "Channel 3": + Chart.getInstance().channelUpdateWithoutColor(3, name,units); + break; + case "Channel 4": + Chart.getInstance().channelUpdateWithoutColor(4, name,units); + break; + case "Channel 5": + Chart.getInstance().channelUpdateWithoutColor(5, name,units); + break; + case "Channel 6": + Chart.getInstance().channelUpdateWithoutColor(6, name ,units); + break; + case "Channel 7": + Chart.getInstance().channelUpdateWithoutColor(7, name,units); + break; + case "Channel 8": + Chart.getInstance().channelUpdateWithoutColor(8, name,units); + break; + } } - else if(channel.equals("Channel 4")){ - Chart.getInstance().channelUpdate(4,name+" ["+units+"]", color); + catch(NullPointerException exe){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Channel has not been chosen"); + alert.setHeaderText("To submit channel settings you have to choose a channel"); + alert.setContentText("Please, choose a channel"); + alert.showAndWait(); } - else if(channel.equals("Channel 5")){ - Chart.getInstance().channelUpdate(5,name+" ["+units+"]", color); - } - else if(channel.equals("Channel 6")){ - Chart.getInstance().channelUpdate(6,name+" ["+units+"]", color); + } + } + + public void onUnitKeyPressed(KeyEvent keyEvent) { + if(keyEvent.getCode() == KeyCode.ENTER){ + try{ + if(!unitText.getText().isEmpty()){ + units="["+unitText.getText()+"]"; + } + channel=channelBox.getSelectionModel().getSelectedItem(); + switch (channel) { + case "Channel 1": + Chart.getInstance().channelUpdateWithoutColor(1, name, units); + break; + case "Channel 2": + Chart.getInstance().channelUpdateWithoutColor(2, name, units); + break; + case "Channel 3": + Chart.getInstance().channelUpdateWithoutColor(3, name,units); + break; + case "Channel 4": + Chart.getInstance().channelUpdateWithoutColor(4, name,units); + break; + case "Channel 5": + Chart.getInstance().channelUpdateWithoutColor(5, name,units); + break; + case "Channel 6": + Chart.getInstance().channelUpdateWithoutColor(6, name ,units); + break; + case "Channel 7": + Chart.getInstance().channelUpdateWithoutColor(7, name,units); + break; + case "Channel 8": + Chart.getInstance().channelUpdateWithoutColor(8, name,units); + break; + } } - else if(channel.equals("Channel 7")){ - Chart.getInstance().channelUpdate(7,name+" ["+units+"]", color); + catch(NullPointerException exe){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Channel has not been chosen"); + alert.setHeaderText("To submit channel settings you have to choose a channel"); + alert.setContentText("Please, choose a channel"); + alert.showAndWait(); } - else if(channel.equals("Channel 8")){ - Chart.getInstance().channelUpdate(8,name+" ["+units+"]", color); + } + } + + public void onPickColor(ActionEvent actionEvent) { + try{ + color=colorPicker.getValue(); + channel=channelBox.getSelectionModel().getSelectedItem(); + switch (channel) { + case "Channel 1": + Chart.getInstance().channelUpdateColor(1, color); + break; + case "Channel 2": + Chart.getInstance().channelUpdateColor(2, color); + break; + case "Channel 3": + Chart.getInstance().channelUpdateColor(3, color); + break; + case "Channel 4": + Chart.getInstance().channelUpdateColor(4, color); + break; + case "Channel 5": + Chart.getInstance().channelUpdateColor(5, color); + break; + case "Channel 6": + Chart.getInstance().channelUpdateColor(6, color); + break; + case "Channel 7": + Chart.getInstance().channelUpdateColor(7, color); + break; + case "Channel 8": + Chart.getInstance().channelUpdateColor(8, color); + break; } } catch(NullPointerException exe){ diff --git a/src/mainwindow/Chart.java b/src/mainwindow/Chart.java index f2767c1..ba9ce24 100644 --- a/src/mainwindow/Chart.java +++ b/src/mainwindow/Chart.java @@ -60,7 +60,7 @@ public class Chart implements Initializable { @FXML private TextField maxText; - private int samples=200; + private int samples = 200; @Override public void initialize(URL location, ResourceBundle resources) { @@ -83,7 +83,7 @@ public void initialize(URL location, ResourceBundle resources) { xAxis.setAutoRanging(true); xAxis.setTickLabelsVisible(false); - //Styling chart with colors// + //Styling chart with colors and styles// Node line1 = series1.getNode().lookup(".chart-series-line"); Node line2 = series2.getNode().lookup(".chart-series-line"); @@ -145,6 +145,15 @@ public void initialize(URL location, ResourceBundle resources) { line7.setStyle("-fx-stroke: rgba(" + rgb7 + ", 1.0);"); line8.setStyle("-fx-stroke: rgba(" + rgb8 + ", 1.0);"); + line1.setStyle("-fx-stroke-width: 1px;"); + line2.setStyle("-fx-stroke-width: 1px;"); + line3.setStyle("-fx-stroke-width: 1px;"); + line4.setStyle("-fx-stroke-width: 1px;"); + line5.setStyle("-fx-stroke-width: 1px;"); + line6.setStyle("-fx-stroke-width: 1px;"); + line7.setStyle("-fx-stroke-width: 1px;"); + line8.setStyle("-fx-stroke-width: 1px;"); + checkOne.setStyle("-fx-text-fill:rgb(" + rgb1 + ");"); checkTwo.setStyle("-fx-text-fill:rgb(" + rgb2 + ");"); checkThree.setStyle("-fx-text-fill:rgb(" + rgb3 + ");"); @@ -214,7 +223,7 @@ public void onChooseVariable(ActionEvent actionEvent) { } } - void channelUpdate(int channelNumber, String name, Color color) { + void channelUpdate(int channelNumber, String name, String units, Color color) { String rgb = String.format("%d, %d, %d", (int) (color.getRed() * 255), @@ -224,52 +233,147 @@ void channelUpdate(int channelNumber, String name, Color color) { if (null != ChartINSTANCE) { switch (channelNumber) { case 1: - checkOne.setText(name); + checkOne.setText(name + units); checkOne.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line1 = series1.getNode().lookup(".chart-series-line"); line1.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line1.setStyle("-fx-stroke-width: 1px;"); break; case 2: - checkTwo.setText(name); + checkTwo.setText(name + units); checkTwo.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line2 = series2.getNode().lookup(".chart-series-line"); line2.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line2.setStyle("-fx-stroke-width: 1px;"); break; case 3: - checkThree.setText(name); + checkThree.setText(name + units); checkThree.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line3 = series3.getNode().lookup(".chart-series-line"); line3.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line3.setStyle("-fx-stroke-width: 1px;"); break; case 4: - checkFour.setText(name); + checkFour.setText(name + units); checkFour.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line4 = series4.getNode().lookup(".chart-series-line"); line4.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line4.setStyle("-fx-stroke-width: 1px;"); break; case 5: - checkFive.setText(name); + checkFive.setText(name + units); checkFive.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line5 = series5.getNode().lookup(".chart-series-line"); line5.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line5.setStyle("-fx-stroke-width: 1px;"); break; case 6: - checkSix.setText(name); + checkSix.setText(name + units); checkSix.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line6 = series6.getNode().lookup(".chart-series-line"); line6.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line6.setStyle("-fx-stroke-width: 1px;"); break; case 7: - checkSeven.setText(name); + checkSeven.setText(name + units); checkSeven.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line7 = series7.getNode().lookup(".chart-series-line"); line7.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line7.setStyle("-fx-stroke-width: 1px;"); break; case 8: - checkEight.setText(name); + checkEight.setText(name + units); checkEight.setStyle("-fx-text-fill:rgb(" + rgb + ");"); Node line8 = series8.getNode().lookup(".chart-series-line"); line8.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + line8.setStyle("-fx-stroke-width: 1px;"); + break; + } + } + + } + + void channelUpdateColor(int channelNumber, Color color) { + + String rgb = String.format("%d, %d, %d", + (int) (color.getRed() * 255), + (int) (color.getGreen() * 255), + (int) (color.getBlue() * 255)); + + if (null != ChartINSTANCE) { + switch (channelNumber) { + case 1: + checkOne.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line1 = series1.getNode().lookup(".chart-series-line"); + line1.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + case 2: + checkTwo.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line2 = series2.getNode().lookup(".chart-series-line"); + line2.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + case 3: + checkThree.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line3 = series3.getNode().lookup(".chart-series-line"); + line3.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + case 4: + checkFour.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line4 = series4.getNode().lookup(".chart-series-line"); + line4.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + case 5: + checkFive.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line5 = series5.getNode().lookup(".chart-series-line"); + line5.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + case 6: + checkSix.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line6 = series6.getNode().lookup(".chart-series-line"); + line6.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + case 7: + checkSeven.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line7 = series7.getNode().lookup(".chart-series-line"); + line7.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + case 8: + checkEight.setStyle("-fx-text-fill:rgb(" + rgb + ");"); + Node line8 = series8.getNode().lookup(".chart-series-line"); + line8.setStyle("-fx-stroke: rgba(" + rgb + ", 1.0);"); + break; + } + } + + } + + void channelUpdateWithoutColor(int channelNumber, String name, String units) { + + if (null != ChartINSTANCE) { + switch (channelNumber) { + case 1: + checkOne.setText(name + units); + break; + case 2: + checkTwo.setText(name + units); + break; + case 3: + checkThree.setText(name + units); + break; + case 4: + checkFour.setText(name + units); + break; + case 5: + checkFive.setText(name + units); + break; + case 6: + checkSix.setText(name + units); + break; + case 7: + checkSeven.setText(name + units); + break; + case 8: + checkEight.setText(name + units); break; } } @@ -282,7 +386,7 @@ public void onCheck1(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series1)) { lchart.getData().add(series1); - channelUpdate(1, checkOne.getText(), Color.RED); + channelUpdate(1, checkOne.getText(), "", Color.RED); } } } @@ -293,7 +397,7 @@ public void onCheck2(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series2)) { lchart.getData().add(series2); - channelUpdate(2, checkTwo.getText(), Color.BLUE); + channelUpdate(2, checkTwo.getText(), "", Color.BLUE); } } } @@ -304,7 +408,7 @@ public void onCheck3(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series3)) { lchart.getData().add(series3); - channelUpdate(3, checkThree.getText(), Color.BLACK); + channelUpdate(3, checkThree.getText(), "", Color.BLACK); } } } @@ -315,7 +419,7 @@ public void onCheck4(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series4)) { lchart.getData().add(series4); - channelUpdate(4, checkFour.getText(), Color.GRAY); + channelUpdate(4, checkFour.getText(), "", Color.GRAY); } } } @@ -326,7 +430,7 @@ public void onCheck5(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series5)) { lchart.getData().add(series5); - channelUpdate(5, checkFive.getText(), Color.GREEN); + channelUpdate(5, checkFive.getText(), "", Color.GREEN); } } } @@ -337,7 +441,7 @@ public void onCheck6(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series6)) { lchart.getData().add(series6); - channelUpdate(6, checkSix.getText(), Color.DARKBLUE); + channelUpdate(6, checkSix.getText(), "", Color.DARKBLUE); } } } @@ -348,7 +452,7 @@ public void onCheck7(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series7)) { lchart.getData().add(series7); - channelUpdate(7, checkSeven.getText(), Color.VIOLET); + channelUpdate(7, checkSeven.getText(), "", Color.VIOLET); } } } @@ -359,56 +463,58 @@ public void onCheck8(ActionEvent actionEvent) { } else { if (!lchart.getData().contains(series8)) { lchart.getData().add(series8); - channelUpdate(8, checkEight.getText(), Color.ORANGE); + channelUpdate(8, checkEight.getText(), "", Color.ORANGE); } } } - public int numberOfSamples(){ + public int numberOfSamples() { return samples; } - public boolean update(){ + public boolean update() { - if (series1.getData().sorted().size() > samples) { - return true; - } - else{ - return false; + if (null != series1) { + return series1.getData().sorted().size() > samples; } + else return false; } public void onKeyPressed(KeyEvent keyEvent) { - if(keyEvent.getCode() == KeyCode.ENTER){ + if (keyEvent.getCode() == KeyCode.ENTER) { String selectedTime = sampleText.getText(); - try{ + try { samples = Integer.parseInt(selectedTime); - } - catch(NumberFormatException num){ + } catch (NumberFormatException num) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Wrong data type"); alert.setHeaderText("You have submitted wrong data"); alert.setContentText("Please, set some numbers"); alert.showAndWait(); - samples=200; + samples = 200; } - if (series1.getData().sorted().size() > samples) { - try { - series1.getData().remove(0, (series1.getData().size() - samples)); - series2.getData().remove(0, (series2.getData().size() - samples)); - series3.getData().remove(0, (series3.getData().size() - samples)); - series4.getData().remove(0, (series4.getData().size() - samples)); - series5.getData().remove(0, (series5.getData().size() - samples)); - series6.getData().remove(0, (series6.getData().size() - samples)); - series7.getData().remove(0, (series7.getData().size() - samples)); - series8.getData().remove(0, (series8.getData().size() - samples)); - } - catch (NullPointerException nul) { - System.out.println("No such data in the Series"); + if (null != series1) { + if (series1.getData().sorted().size() > samples) { + try { + if (null != series1) series1.getData().remove(0, (series1.getData().size() - samples)); + if (null != series2) series2.getData().remove(0, (series2.getData().size() - samples)); + if (null != series3) series3.getData().remove(0, (series3.getData().size() - samples)); + if (null != series4) series4.getData().remove(0, (series4.getData().size() - samples)); + if (null != series5) series5.getData().remove(0, (series5.getData().size() - samples)); + if (null != series6) series6.getData().remove(0, (series6.getData().size() - samples)); + if (null != series7) series7.getData().remove(0, (series7.getData().size() - samples)); + if (null != series8) series8.getData().remove(0, (series8.getData().size() - samples)); + } catch (NullPointerException nul) { + System.out.println("No such data in the Series"); + } } } } } -} \ No newline at end of file +} + + + + diff --git a/src/mainwindow/channel.fxml b/src/mainwindow/channel.fxml index 4afa25b..c9e5b14 100644 --- a/src/mainwindow/channel.fxml +++ b/src/mainwindow/channel.fxml @@ -8,10 +8,9 @@ - - - -