1   package debugger.gui.actions.debugging;
2   
3   import debugger.ClassRipper;
4   import debugger.JapeDebugger;
5   import gate.Gate;
6   import gate.creole.ExecutionException;
7   import gate.creole.ExecutionInterruptedException;
8   import gate.creole.SerialAnalyserController;
9   import gate.gui.SerialControllerEditor;
10  
11  import javax.swing.*;
12  import java.util.Iterator;
13  import java.awt.event.ActionEvent;
14  
15  /**
16   * Copyright (c) Ontos AG (http://www.ontosearch.com).
17   * This class is part of JAPE Debugger component for
18   * GATE (Copyright (c) "The University of Sheffield" see http://gate.ac.uk/) <br>
19   * @author Andrey Shafirin
20   */
21  public class RunControllerAction extends AbstractAction {
22      private static RunControllerAction ourInstance;
23  
24      public synchronized static RunControllerAction getInstance() {
25          if (ourInstance == null) {
26              ourInstance = new RunControllerAction();
27          }
28          return ourInstance;
29      }
30  
31      private RunControllerAction() {
32          super();
33          putValue(Action.SHORT_DESCRIPTION, new String("Run controller"));
34          putValue(Action.SMALL_ICON, new ImageIcon(JapeDebugger.class.getResource("gui/icons/controller.gif")));
35          setEnabled(false);
36      }
37  
38      public void actionPerformed(ActionEvent aEvt) {
39          setEnabled(false);
40          for (Iterator itr = Gate.getCreoleRegister().getVrInstances().iterator(); itr.hasNext();) {
41              Object current = itr.next();
42              if (current instanceof SerialControllerEditor) {
43                  SerialAnalyserController controller = null;
44                  try {
45                      controller = (SerialAnalyserController) ClassRipper.getFieldValue(current, "controller");
46                  } catch (IllegalAccessException e) {
47                      e.printStackTrace();
48                  }
49                  if (controller != null) {
50                      final SerialAnalyserController controller1 = controller;
51                      Runnable runnable = new Runnable() {
52                          public void run() {
53                              try {
54                                  Gate.setExecutable(controller1);
55                                  controller1.execute();
56                              } catch (ExecutionInterruptedException eie) {
57                                  eie.printStackTrace();
58                              } catch (ExecutionException e) {
59                                  e.printStackTrace();
60                              } catch (Exception ee) {
61                                  ee.printStackTrace();
62                              } finally {
63                                  Gate.setExecutable(null);
64                              }
65                          }
66                      };
67                      Thread thread = new Thread(Thread.currentThread().getThreadGroup(), runnable, "ApplicationViewer1");
68                      thread.setPriority(Thread.MIN_PRIORITY);
69                      thread.start();
70                  }
71                  break;
72              }
73          }
74      }
75  }
76  
77