JavaFX 8计算脚本类的进度条更新

时间:2015-06-10 23:35:00

标签: progress-bar javafx-8 essbase

我正在努力让JavaFX 8中的这个进度条工作。我知道我可能应该使用任务更新进度但我无法让它工作。

为了简单起见,我删除了服务器API特定代码并保留了GUI,Call和1 Calc Class。

目标是:

  1. 识别从选择框中调用的计算
  2. 设置变量以监控计算迭代的进度
  3. 使用“计算脚本类”中的进度更新progressbar
  4. 这是我的代码:

    UI主类

    package javafxessbasecalcscriptapp;
    
    import com.essbase.api.base.EssException;
    import com.essbase.api.datasource.IEssOlapServer;
    import java.io.InputStream;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.beans.property.BooleanProperty;
    import javafx.beans.property.SimpleBooleanProperty;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import static javafx.collections.FXCollections.observableArrayList;
    import javafx.concurrent.Task;
    import javafx.event.ActionEvent;
    import javafx.event.EventType;
    import javafx.geometry.Insets;
    import javafx.scene.Cursor;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Button;
    import javafx.scene.control.ButtonBar.ButtonData;
    import javafx.scene.control.ButtonType;
    import javafx.scene.control.ChoiceBox;
    import javafx.scene.control.Dialog;
    import javafx.scene.control.Label;
    import javafx.scene.control.Menu;
    import javafx.scene.control.MenuBar;
    import javafx.scene.control.MenuItem;
    import javafx.scene.control.PasswordField;
    import javafx.scene.control.ProgressBar;
    import javafx.scene.control.TextField;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;
    import javafx.util.Pair;
    import static javafxessbasecalcscriptapp.Connect.getEss;
    import static javafxessbasecalcscriptapp.Connect.getOLAPSvr;
    import static javafxessbasecalcscriptapp.ForecastCalcs.getFCalcProg;
    import static javafxessbasecalcscriptapp.SupplementalCalcs.getCalcSProg;
    import static javafxessbasecalcscriptapp.WrapperCalcs.getCalcWProg;
    import static javafxessbasecalcscriptapp.ForecastCalcs.getForecastList;
    import static javafxessbasecalcscriptapp.SupplementalCalcs.getSupplementalList;
    import static javafxessbasecalcscriptapp.WrapperCalcs.getWrapperList;
    
    public class JavafxEssbaseCalcScriptApp extends Application {
    Button btnRunCalc = new Button();
    ChoiceBox mainChoiceBox = new ChoiceBox();
    ProgressBar mainProgBar = new ProgressBar(0);
    public Double calcProg = 0.0;
    public Double calcListSize = 0.0;
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        new SecondStage();
    }
    
    
        public class SecondStage extends Stage {
    
                ChoiceBox mainChoiceBox = mainChoiceBox();
                mainChoiceBox.setDisable(false);
                mainChoiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
                    @Override public void changed(ObservableValue<? extends String> selected, String oldCalc, String newCalc) {
                        if (oldCalc != null) {
                            switch(oldCalc) {
                                case "Forecast": System.out.println("Clear Forecast"); setCalcScript(null); calcProg = getFCalcProg(); calcListSize = (double) getForecastList().size() /*select forecast calc scripts*/; break;
                                case "Wrapper": System.out.println("Clear Wrapper"); setCalcScript(null); calcProg = getCalcWProg(); calcListSize = (double) getWrapperList().size()/*select wrapper calc scripts*/; break;
                                case "Supplemental": System.out.println("Clear Supplemental"); setCalcScript(null); calcProg = getCalcSProg(); ; calcListSize = (double) getSupplementalList().size()/*select supplemental calc scripts*/; break;
                            }
                        }
                        if (newCalc != null) {
                            switch(newCalc) {
                                case "Forecast": System.out.println("Load Forecast"); setCalcScript("Forecast") /*select forecast calc scripts*/; break;
                                case "Wrapper": System.out.println("Load Wrapper"); setCalcScript("Wrapper") /*select wrapper calc scripts*/; break;
                                case "Supplemental": System.out.println("Load Supplemental"); setCalcScript("Supplemental") /*select supplemental calc scripts*/; break;
                            }
                        }
                    }
                });
    
                ProgressBar mainProgBar = mainProgBar();
                mainProgBar.setProgress(calcProg);
    
                try {
                    btnRunCalc = btnRunCalc();
                } catch (EssException ex) {
                    Logger.getLogger(JavafxEssbaseCalcScriptApp.class.getName()).log(Level.SEVERE, null, ex);
                }
                btnRunCalc.setDisable(false);
                btnRunCalc.setOnAction((ActionEvent revent) -> {
                    System.out.println("Calc Running");
                    //Run Calc Script                
                    RunCalcs.main(args);
                    Task t = new Task() {
                        @Override
                        protected Void call() throws Exception {
                            double i = calcProg;
                            double maximum = calcListSize;
                            for (i < maximum; i++) {
                                // Here you update the progress each time
                                updateProgress(i, maximum);
                            }
                            return null;
                        }
                    };
                });
    
                final Pane rootMain = new Pane();
                    rootMain.getChildren().add(btnRunCalc);
                    rootMain.getChildren().add(mainChoiceBox);
                    rootMain.getChildren().add(mainProgBar);
                    rootMain.getChildren().add(btnCancelCalc);
    
                Scene sceneMain = new Scene(rootMain, 330, 320, Color.WHITE);
                this.setTitle("Essbase Calc Script Application");
                this.setScene(sceneMain);
                this.initStyle(StageStyle.UNIFIED);
                this.show();
            }
        }
    
        public ChoiceBox mainChoiceBox() {
            mainChoiceBox.setPrefSize(230, 25);
            mainChoiceBox.setItems(observableArrayList("Forecast", "Wrapper", "Supplemental"));
            mainChoiceBox.setLayoutX(50);
            mainChoiceBox.setLayoutY(200);
            mainChoiceBox.setRotate(0);
            mainChoiceBox.setPadding(Insets.EMPTY);
            mainChoiceBox.setCursor(Cursor.HAND);
            return mainChoiceBox;
        }
    
        public ProgressBar mainProgBar() {
            mainProgBar.setPrefSize(230, 25);
            mainProgBar.setLayoutX(50);
            mainProgBar.setLayoutY(290);
            mainProgBar.setRotate(0);
            mainProgBar.setPadding(Insets.EMPTY);
            mainProgBar.setCursor(Cursor.WAIT);
            return mainProgBar;
        }
    
        private Button btnRunCalc() throws EssException {
            btnRunCalc.setText("Run Calculations");
            btnRunCalc.setDefaultButton(true);
            btnRunCalc.setPrefSize(100, 25);
            btnRunCalc.setLayoutX(50);
            btnRunCalc.setLayoutY(237);
            btnRunCalc.setRotate(0);
            btnRunCalc.setPadding(Insets.EMPTY);
            btnRunCalc.setCursor(Cursor.HAND);
            return btnRunCalc;
        }
    
        public Button btnCancelCalc(Stage secondStage) {
            btnCancelCalc.setText("Close");
            btnCancelCalc.setCancelButton(true);
            btnCancelCalc.setPrefSize(60, 25);
            btnCancelCalc.setLayoutX(220);
            btnCancelCalc.setLayoutY(237);
            btnCancelCalc.setRotate(0);
            btnCancelCalc.setPadding(Insets.EMPTY);
            btnCancelCalc.setCursor(Cursor.HAND);
            return btnCancelCalc;
        }
    
        /**
         *
         * @return
         */
        public static String getCalcScript() {
            return calcScript;
        }
    
        /**
         *
         * @param calcScript
         */
        public static void setCalcScript(String calcScript) {
            JavafxEssbaseCalcScriptApp.calcScript = calcScript;
        }
    }
    

    调用计算脚本类

    package javafxessbasecalcscriptapp;
    
    /**
     *
     * @author jdsmith
     */
    public class RunCalcs {
    
            String[] args = {};    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
    
            String calcName = JavafxEssbaseCalcScriptApp.getCalcScript();
            switch (calcName) {
                case "Forecast":
                    ForecastCalcs.main(args);
                    System.out.println("Forecast Calc Scripts Complete");
                    break;
                case "Wrapper":
                    WrapperCalcs.main(args);
                    System.out.println("Wrapper Calc Scripts Complete");
                    break;
                case "Supplemental":
                    SupplementalCalcs.main(args);
                    System.out.println("Supplemental Calc Scripts Complete");
                    break;
                default:
                    break;
            }
        }
    }
    

    3个计算脚本类中的一个

    package javafxessbasecalcscriptapp;
    
    import com.essbase.api.base.EssException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.scene.control.Alert;
    import static javafxessbasecalcscriptapp.Connect.getEss;
    import static javafxessbasecalcscriptapp.Connect.getEssCube;
    import static javafxessbasecalcscriptapp.Connect.getOLAPSvr;
    
    /**
     *
     * @author jdsmith
     */
    public class ForecastCalcs {
        private static double calcFProg;
        public static double iF;
        //create list
        public static List<String> forecastList = new ArrayList<String>();
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            //add Calc Scripts to list
            Collections.addAll(getForecastList(),"Agg_Act","EAMN_Int","EAOP_NOI"
                      ,"EAOP_RCX","EAOP_CEx","EA_RENet","EA_DProp","IA_RECst"
                      ,"IA_NOI","IA_CNOI","IA_IntEx","IAMN_Cex","IAOP_Cex"
                      ,"IA_DepEx","IA_Debt","UA_RECst","UA_ACR","UA_NOI","UA_CNOI"
                      ,"UA_IntEx","UA_CeX","UA_DepEx","Agg_Fcst","ENT_GA","ENT_Inv"
                      ,"ENT_OFF","ENT_OCF","ENT_PED","ENT_Bnd","ENT_TOS","ENT_AMF"
                      ,"ENT_Dil","ENT_PSQ","ENT_CIP","ENT_TOS","ENT_CI","ENT_TOS"
                      ,"ENT_Div","ENT_TOS","ENT_Drp","ENT_TOS","ENT_CF","Agg_Fcst");
    
    
            //Iterate via "while loop"
            System.out.println("\n==> While Loop Iterate Calcs....");
            setiF(0);
    
            for (String item : getForecastList()) {   
                setFCalcProg(getiF()/getForecastList().size());
                System.out.println(getiF()/getForecastList().size());                                
                System.out.println(getForecastList().get((int) getiF()));
                setiF(getiF() + 1);
                System.out.println(getiF()+1);
             }
    
    
        /**
         * @return the iF
         */
        public static double getiF() {
            return iF;
        }
    
        /**
         * @param aiF the iF to set
         */
        public static void setiF(double aiF) {
            iF = aiF;
        }
    
        /**
         * @return the forecastList
         */
        public static List<String> getForecastList() {
            return forecastList;
        }
    
        /**
         * @param aForecastList the forecastList to set
         */
        public static void setForecastList(List<String> aForecastList) {
            forecastList = aForecastList;
        }
    
        /**
         * @return the calcProg
         */
        public static double getFCalcProg() {
            return calcFProg;
        }
    
        /**
         * @param aCalcProg the calcProg to set
         */
        public static void setFCalcProg(double aFCalcProg) {
            calcFProg = aFCalcProg;
        }
    }
    

    我试图在Stackoverflow上找到解决方案,但似乎没有任何东西适合我的场景。任何帮助将不胜感激。谢谢!

0 个答案:

没有答案