不得多次调用应用程序启动

时间:2018-06-14 18:28:23

标签: java javafx javafx-8

我正在使用此方法使用JavaFx应用程序创建一个Barchart startApplication(String chartPath,......)来自 Barchart.java ,我将在其中启动javafx应用程序

在那个方法中我也在做其他一些事情但是如果我想再次使用这个方法就像程序 TestBarchart.java 我不能使用因为应用程序已经已经启动的例外即将到来。

我想使用Platform.runLater,但我不确定如何使用它。我知道这可能是重复的。但是Platform.runLater会在事件触发时运行但在我的情况下没有触发器我只想运行该方法。

请让我知道如何才能让它发挥作用。

提前致谢

Barchart.java

convenience init

TestBarchart.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;

public class Barchart extends Application {
static Log cat = LogFactory.getLog(Barchart.class);

public void startApplication(String chartPath, String chartName, String total, String passed, String failed,
        String excelFile) {
    // launch(chartPath, chartName, total, passed, failed, excelFile);

    try {
        //ExecutorService executorService = Executors.newFixedThreadPool(10);
        launch(chartPath, chartName, total, passed, failed, excelFile);
        //executorService.shutdown();
        //executorService.awaitTermination(10, TimeUnit.SECONDS); // wait for 10s in this case
        //executorService.shutdownNow();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@SuppressWarnings("unchecked")
public void start(Stage stage) throws Exception {
    // TODO Auto-generated method stub
    try {
        String title = "";
        int total = 0;
        int passed = 0;
        int failed = 0;

        String chartPath = getParameters().getRaw().get(0);
        String chartName = getParameters().getRaw().get(1);
        int overallTotal = Integer.parseInt(getParameters().getRaw().get(2));
        int overallPassed = Integer.parseInt(getParameters().getRaw().get(3));
        int overallFailed = Integer.parseInt(getParameters().getRaw().get(4));

        String excelFilePath = getParameters().getRaw().get(5);
        if (excelFilePath != null && !excelFilePath.isEmpty()) {
            FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
            XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(inputStream);
            DataFormatter formatter = new DataFormatter();
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                XSSFSheet sheet = workbook.getSheetAt(i);
                title = sheet.getSheetName();
                for (Row row : sheet) {
                    for (Cell cell : row) {
                        if (formatter.formatCellValue(cell).contains("Failed")
                                || formatter.formatCellValue(cell).contains("FAILED")) {
                            // passed = passed + 1;
                            failed = failed + 1;
                            break;
                        } else if (formatter.formatCellValue(cell).contains("SUCCESS")) {
                            if (row.getLastCellNum() == cell.getColumnIndex() + 1) {
                                passed = passed + 1;
                            }
                        } else if (formatter.formatCellValue(cell).contains("Passed")) {
                            passed = passed + 1;

                        }

                    }
                }
                total = passed + failed;
                stage.setTitle("Bar Chart");
                final CategoryAxis xAxis = new CategoryAxis();
                final NumberAxis yAxis = new NumberAxis();
                final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
                bc.setAnimated(false);
                bc.setTitle(title.toUpperCase());
                xAxis.setLabel("Summary");
                yAxis.setLabel("No of Testcases");

                XYChart.Series<String, Number> series1 = new XYChart.Series<String, Number>();
                XYChart.Data<String, Number> data1 = new XYChart.Data<String, Number>("Total", total);
                XYChart.Data<String, Number> data2 = new XYChart.Data<String, Number>("Passed", passed);
                XYChart.Data<String, Number> data3 = new XYChart.Data<String, Number>("Failed", failed);
                series1.getData().add(data1);
                series1.getData().add(data2);
                series1.getData().add(data3);
                series1.setName("Test Cases");
                data1.nodeProperty().addListener(new ChangeListener<Node>() {
                    @Override
                    public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
                        if (newNode != null) {
                            newNode.setStyle("-fx-bar-fill: navy;");
                        }
                    }
                });
                data2.nodeProperty().addListener(new ChangeListener<Node>() {
                    @Override
                    public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
                        if (newNode != null) {
                            newNode.setStyle("-fx-bar-fill: green;");
                        }
                    }
                });
                data3.nodeProperty().addListener(new ChangeListener<Node>() {
                    @Override
                    public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
                        if (newNode != null) {
                            newNode.setStyle("-fx-bar-fill: red;");
                        }
                    }
                });

                Scene scene = new Scene(bc, 800, 600);
                bc.getData().addAll(series1);

                cat.debug("Writing Chart file to the output");
                String path = chartPath + "/" + chartName + "- " + title + ".png";
                stage.setScene(scene);
                WritableImage image = scene.snapshot(null);
                File file = new File(path);
                ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
                cat.debug("End of stage method");
                total = 0;
                passed = 0;
                failed = 0;
                /* Read the input image into InputStream */
                InputStream my_banner_image = new FileInputStream(path);
                /* Convert Image to byte array */
                byte[] bytes = IOUtils.toByteArray(my_banner_image);
                /*
                 * Add Picture to workbook and get a index for the picture
                 */
                int my_picture_id = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
                /* Close Input Stream */
                my_banner_image.close();
                /* Create the drawing container */
                XSSFDrawing drawing = sheet.createDrawingPatriarch();
                /* Create an anchor point */
                ClientAnchor my_anchor = new XSSFClientAnchor();
                /*
                 * Define top left corner, and we can resize picture
                 * suitable from there
                 */
                my_anchor.setCol1(2);
                my_anchor.setRow1(sheet.getLastRowNum() + 1);
                /* Invoke createPicture and pass the anchor point and ID */
                XSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
                /* Call resize method, which resizes the image */
                my_picture.resize();
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File(excelFilePath));
                workbook.write(out);
                out.close();
                // stage.show();

            }
        }
        stage.setTitle("Bar Chart");
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
        bc.setAnimated(false);
        bc.setTitle(chartName.toUpperCase());
        xAxis.setLabel("Summary");
        yAxis.setLabel("No of Testcases");

        XYChart.Series<String, Number> series1 = new XYChart.Series<String, Number>();
        XYChart.Data<String, Number> data1 = new XYChart.Data<String, Number>("Total", overallTotal);
        XYChart.Data<String, Number> data2 = new XYChart.Data<String, Number>("Passed", overallPassed);
        XYChart.Data<String, Number> data3 = new XYChart.Data<String, Number>("Failed", overallFailed);
        series1.getData().add(data1);
        series1.getData().add(data2);
        series1.getData().add(data3);
        series1.setName("Test Cases");
        data1.nodeProperty().addListener(new ChangeListener<Node>() {
            @Override
            public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
                if (newNode != null) {
                    newNode.setStyle("-fx-bar-fill: navy;");
                }
            }
        });
        data2.nodeProperty().addListener(new ChangeListener<Node>() {
            @Override
            public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
                if (newNode != null) {
                    newNode.setStyle("-fx-bar-fill: green;");
                }
            }
        });
        data3.nodeProperty().addListener(new ChangeListener<Node>() {
            @Override
            public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
                if (newNode != null) {
                    newNode.setStyle("-fx-bar-fill: red;");
                }
            }
        });

        Scene scene = new Scene(bc, 800, 600);
        bc.getData().addAll(series1);

        cat.debug("Writing Chart file to the output");
        String path = chartPath + "/" + chartName + ".png";
        stage.setScene(scene);
        WritableImage image = scene.snapshot(null);
        File file = new File(path);
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
        cat.debug("End of stage method");

    } catch (IOException e) {
        e.printStackTrace();
        cat.error("Error at Stage in Barchart" + e.getMessage());
    }

    Platform.exit();

}

0 个答案:

没有答案
相关问题