View Javadoc

1   /*
2    * @(#)$Id: BatchController.java 750 2009-08-18 18:30:55Z kurmannu $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *                  
6    *
7    * Purpose		: 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date				Who			Reason
14   *
15   * 17.04.2008			ukurmann	Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.tool.view.batch;
28  
29  import java.beans.PropertyChangeEvent;
30  import java.io.File;
31  import java.io.IOException;
32  import java.util.logging.Level;
33  import java.util.logging.Logger;
34  
35  import javax.swing.SwingWorker;
36  
37  import org.apache.commons.io.FileUtils;
38  import org.ximtec.igesture.batch.BatchProcess;
39  import org.ximtec.igesture.batch.BatchProcessContainer;
40  import org.ximtec.igesture.batch.BatchResultSet;
41  import org.ximtec.igesture.batch.BatchTools;
42  import org.ximtec.igesture.tool.core.Controller;
43  import org.ximtec.igesture.tool.core.DefaultController;
44  import org.ximtec.igesture.tool.core.EdtProxy;
45  import org.ximtec.igesture.tool.core.ExecCmd;
46  import org.ximtec.igesture.tool.core.TabbedView;
47  import org.ximtec.igesture.util.XMLTool;
48  
49  
50  /**
51   * Comment
52   * @version 1.0 17.04.2008
53   * @author Ueli Kurmann
54   */
55  public class BatchController extends DefaultController {
56  
57     public static final String CMD_RUN_BATCH = "runBatch";
58     public static final String CMD_CANCEL_BATCH = "cancelBatch";
59     private static final Logger LOGGER = Logger.getLogger(BatchController.class
60           .getName());
61  
62     private BatchSwingWorker batchSwingWorker;
63  
64     private IBatchView view;
65  
66  
67     public BatchController(Controller parentController) {
68  	  super(parentController);
69        initialize();
70     }
71  
72  
73     private void initialize() {
74        this.view = EdtProxy.newInstance(new BatchView(this), IBatchView.class);
75     }
76  
77  
78     @Override
79     public TabbedView getView() {
80        return view;
81     }
82  
83     @ExecCmd(name=CMD_RUN_BATCH)
84     protected void executeBatchRun() {
85        view.setRunActionState(false);
86        view.setCancelActionState(true);
87        view.showProgressBar();
88        batchSwingWorker = new BatchSwingWorker();
89        batchSwingWorker.execute();
90     }
91     
92     @ExecCmd(name=CMD_CANCEL_BATCH)
93     protected void executeBatchCancel() {
94        if(batchSwingWorker != null && !batchSwingWorker.isDone()){
95           System.out.println(batchSwingWorker.cancel(true));
96           System.out.println(batchSwingWorker.getState());
97        }
98        
99        view.hideProgressBar();  
100       view.setRunActionState(true);
101       view.setCancelActionState(false);
102    }
103 
104 
105    @Override
106    public void propertyChange(PropertyChangeEvent event) {
107       super.propertyChange(event);
108       view.refresh();
109    }
110 
111    private class BatchSwingWorker extends SwingWorker<BatchResultSet, Void> {
112 
113       File configFile = new File(view.getConfigFile());
114       File outputDir = new File(view.getOutputDir());
115 
116       
117       @Override
118       protected BatchResultSet doInBackground() throws Exception {
119          
120          BatchResultSet resultSet = null;
121 
122          if (configFile.exists() && view.getTestSet() != null
123                && view.getGestureSet() != null) {
124 
125             BatchProcessContainer container = XMLTool
126                   .importBatchProcessContainer(configFile);
127 
128             BatchProcess batchProcess = new BatchProcess(container);
129             batchProcess.setTestSet(view.getTestSet());
130             batchProcess.addGestureSet(view.getGestureSet());
131 
132             resultSet = batchProcess.run();
133 
134             BatchTools.writeResultsOnDisk(outputDir, resultSet);
135 
136          }
137 
138          return resultSet;
139       }
140 
141 
142       @Override
143       protected void done() {
144          super.done();
145          view.hideProgressBar();
146          view.setRunActionState(true);
147          view.setCancelActionState(false);
148          
149          // display html code
150          try {
151             String htmlCode = FileUtils.readFileToString(new File(outputDir, "result.html"));
152             view.setResult(htmlCode);
153          }
154          catch (IOException e) {
155             LOGGER.log(Level.SEVERE, "Could not set result as html page.");
156          }
157          
158       }
159    }
160 
161 }