001    /*
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: 2002-8-22
005     * Time: 20:04:38
006     */
007    package EVolve.util.batchutils;
008    
009    import java.awt.*;
010    import java.awt.event.*;
011    import java.io.File;
012    import java.util.*;
013    import javax.swing.*;
014    import javax.swing.filechooser.FileSystemView;
015    import EVolve.Scene;
016    import EVolve.exceptions.EVolveException;
017    import EVolve.util.predefinedutils.VisualizationSerializer;
018    import EVolve.util.xmlutils.datastructures.SerializedVisualization;
019    import EVolve.util.xmlutils.datastructures.SerializedData;
020    
021    public class BatchRunner {
022        private JDialog dialog;
023        private java.awt.List fileList;
024        private java.awt.List procList;
025        private Frame owner;
026        private HashSet setFile;
027        private HashSet setProcessFiles;
028        private HashMap batches;
029        private JTextField txtConfName;
030        private SerializedVisualization vizInfo = null;
031    
032        public BatchRunner() {
033            owner = Scene.getFrame();
034            setFile = new HashSet();
035            setProcessFiles = new HashSet();
036            batches = new HashMap();
037        }
038    
039        public void showWindow() {
040            if (dialog == null) {
041                createDialog();
042                Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight());
043            }
044            else dialog.setVisible(true);
045        }
046    
047        public void runBatch() {
048            int iBatchNum = batches.keySet().size();
049            Iterator it = batches.keySet().iterator();
050            BatchInfo batchInfo;
051    
052            if (iBatchNum <= 0) {
053                Scene.showErrorMessage("No batch available!");
054                return;
055            }
056    
057            while (it.hasNext()) {
058                String outPath,dataFileName,batchName = (String) it.next();
059                Iterator it2;
060                batchInfo = (BatchInfo) batches.get(batchName);
061    
062                // read in configure file
063                try {
064                    it2 = batchInfo.getTraceFileList().iterator();
065                    dataFileName = (String) it2.next();
066                    Scene.setDataFilename(dataFileName);
067                    Scene.autoLoadDataSource();
068    
069                    SerializedData data = VisualizationSerializer.v().getVizInfoFromDisk(batchInfo.getConfigureFile());
070                    vizInfo = (SerializedVisualization)data.SerializedVisualizations.get(0);
071                    outPath = data.PathForResult;
072    
073                    execCmd(outPath,dataFileName);
074                } catch (EVolveException e) {
075                    Scene.showErrorMessage(e.getMessage());
076                    continue;
077                }
078    
079                while (it2.hasNext()) {
080                    // load data file first
081                    dataFileName = (String)it2.next();
082                    Scene.setDataFilename(dataFileName);
083                    Scene.autoLoadDataSource();
084                    execCmd(outPath,dataFileName);
085                }
086            }  // end of while(it)
087            batches.clear();
088            Scene.setDataFilename(null);
089        }
090    
091        private void execCmd(String path,String dataFn) {
092            Scene.getVisualizationManager().newVisualization(vizInfo.FactoryName);
093    
094            Scene.getVisualizationManager().autoPreVisualize(vizInfo);
095            Scene.autoVisualize();
096            Scene.getVisualizationManager().autoSaveResult(path,dataFn);
097    
098        }
099    
100        private void createDialog(){
101            dialog = new JDialog(owner,"Create Batch",true);
102            dialog.setBounds(new Rectangle(500,400));
103    
104            JPanel batchName = new JPanel(new FlowLayout());
105            dialog.getContentPane().add(batchName,BorderLayout.NORTH);
106    
107            Box boxMain = new Box(BoxLayout.Y_AXIS);
108            boxMain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
109                    "Choose file(s) to be processed & Processing configuration"));
110            dialog.getContentPane().add(boxMain,BorderLayout.CENTER);
111    
112    
113            Box boxLabels = new Box(BoxLayout.X_AXIS);
114            JButton buttonGetDir = new JButton("Select Directory ..");
115            buttonGetDir.addActionListener(new ActionListener(){
116                public void actionPerformed(ActionEvent e){
117                        fillFileList();
118                }
119            });
120            boxLabels.add(buttonGetDir);
121            boxLabels.add(Box.createHorizontalGlue());
122            boxLabels.add(new JLabel("Processing List                          "));
123    
124            Box boxLists = new Box(BoxLayout.X_AXIS);
125            fileList = new java.awt.List(8,true);
126    
127            Box boxAddRemove = new Box(BoxLayout.Y_AXIS);
128            JButton buttonAdd = new JButton("  >  ");
129            buttonAdd.addActionListener(new ActionListener(){
130                public void actionPerformed(ActionEvent e){
131                    addFiles();
132                }
133            });
134            JButton buttonRemove = new JButton("  <  ");
135            buttonRemove.addActionListener(new ActionListener(){
136                public void actionPerformed(ActionEvent e){
137                    removeFiles();
138                }
139            });
140            boxAddRemove.add(Box.createVerticalStrut(40));
141            boxAddRemove.add(buttonAdd);
142            boxAddRemove.add(Box.createVerticalStrut(20));
143            boxAddRemove.add(buttonRemove);
144    
145            procList = new java.awt.List(8,true);
146            boxLists.add(fileList);
147            boxLists.add(Box.createHorizontalStrut(40));
148            boxLists.add(boxAddRemove);
149            boxLists.add(procList);
150    
151            boxMain.add(boxLabels);
152            boxMain.add(Box.createVerticalStrut(5));
153            boxMain.add(boxLists);
154    
155            Box boxConfig = new Box(BoxLayout.X_AXIS);
156            boxConfig.add(new JLabel("Choose configure file:"));
157            txtConfName = new JTextField(12);
158            boxConfig.add(Box.createHorizontalStrut(20));
159            boxConfig.add(txtConfName,BorderLayout.CENTER);
160            JButton buttonConfig = new JButton("...");
161            buttonConfig.addActionListener(new ActionListener(){
162                public void actionPerformed(ActionEvent e){
163                    txtConfName.setText(chooseConfig());
164                }
165            });
166            boxConfig.add(buttonConfig,BorderLayout.EAST);
167    
168            Box boxOkCancel = new Box(BoxLayout.X_AXIS);
169            JButton buttonOK = new JButton("OK");
170            JButton buttonCancel = new JButton("Cancel");
171    
172            buttonOK.addActionListener(new ActionListener(){
173                public void actionPerformed(ActionEvent e){
174                    onOK();
175                }
176            });
177            buttonCancel.addActionListener(new ActionListener(){
178                public void actionPerformed(ActionEvent e){
179                    onCancel();
180                }
181            });
182            boxOkCancel.add(Box.createHorizontalStrut(30));
183            boxOkCancel.add(buttonOK);
184            boxOkCancel.add(Box.createHorizontalStrut(20));
185            boxOkCancel.add(buttonCancel);
186    
187            Box boxBottom = Box.createVerticalBox();
188            boxBottom.add(Box.createVerticalStrut(12));
189            boxBottom.add(boxConfig);
190            boxBottom.add(Box.createVerticalStrut(40));
191            boxBottom.add(boxOkCancel);
192    
193    
194            dialog.getContentPane().add(boxBottom,BorderLayout.SOUTH);
195            //dialog.setResizable(false);
196        }
197    
198        private void clearList() {
199            fileList.removeAll();
200            procList.removeAll();
201            setFile.clear();
202            setProcessFiles.clear();
203        }
204    
205        private void onOK() {
206            BatchInfo batchInfo = new BatchInfo();
207    
208            if (txtConfName.getText().trim().length() == 0) {
209                Scene.showErrorMessage("No configuration file selected!");
210                return;
211            }
212    
213            if (setProcessFiles.size() == 0) {
214                Scene.showErrorMessage("Select file(s) to be processed please!");
215                return;
216            }
217    
218            batchInfo.setConfigureFile(txtConfName.getText().trim());
219            batchInfo.setTraceFileList(setProcessFiles);
220            batches.put("Batch "+batches.entrySet().size(),batchInfo);
221            dialog.setVisible(false);
222            clearList();
223        }
224    
225        private void onCancel() {
226            dialog.setVisible(false);
227            clearList();
228        }
229    
230        private String chooseConfig() {
231            JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastConfigDir());
232    
233            if(fc.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
234                File f = fc.getSelectedFile();
235                Scene.getUIManager().setLastConfigDir(f.getPath());
236                return (f.getPath());
237            }
238            else return "";
239        }
240    
241        private void addFiles() {
242            String[] selItems = fileList.getSelectedItems();
243    
244            for (int i=0;i<selItems.length;i++) {
245                if (setProcessFiles.contains(selItems[i])) continue;
246    
247                setProcessFiles.add(selItems[i]);
248                procList.add(selItems[i]);
249            }
250        }
251    
252        private void removeFiles() {
253            String[] selItems = procList.getSelectedItems();
254    
255            for (int i=0;i<selItems.length;i++) {
256                setProcessFiles.remove(selItems[i]);
257                procList.remove(selItems[i]);
258            }
259        }
260    
261        private void fillFileList() {
262            JFileChooser fc = new JFileChooser();
263            String path;
264    
265            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
266    
267            if(fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
268                path = fc.getSelectedFile().getAbsolutePath();
269                File dir = new File(path);
270                FileSystemView fv = fc.getFileSystemView();
271                File[] fl = fv.getFiles(dir,false);
272                for (int i=0;i<fl.length;i++) {
273                    String fn = fl[i].getName();
274                    if (!fl[i].isFile() || !fn.endsWith(".dat") || setFile.contains(path+File.separator+fn))
275                        continue;
276                    setFile.add(path+File.separator+fn);
277                    fileList.add(path+File.separator+fn);
278                }
279            }
280    
281        }
282    
283    }