1   package debugger.gui.actions.resources;
2   
3   import gate.AnnotationSet;
4   
5   import java.util.Iterator;
6   import java.util.StringTokenizer;
7   import java.awt.*;
8   
9   import debugger.resources.pr.RuleModel;
10  import debugger.resources.ResourcesFactory;
11  import debugger.gui.GuiFactory;
12  
13  import javax.swing.*;
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, Oleg Mishenko
20   * */
21  
22  public class RuleSelectedAction  implements Runnable{
23      private static RuleSelectedAction ourInstance;
24      private RuleModel ruleModel;
25  
26      public synchronized static RuleSelectedAction getInstance() {
27          if (ourInstance == null) {
28              ourInstance = new RuleSelectedAction();
29          }
30          return ourInstance;
31      }
32  
33      private RuleSelectedAction() {
34      }
35  
36      public void actionPerformed(RuleModel ruleModel) {
37          this.ruleModel = ruleModel;
38          SwingUtilities.invokeLater(this);
39      }
40  
41      private int getSlashNAmount(int offset, AnnotationSet as) {
42          try {
43              String temp = as.getDocument().getContent().getContent(new Long(0), new Long(offset)).toString();
44              if (temp.equals("")) return 0;
45              StringTokenizer st = new StringTokenizer(temp, "\n");
46              if (temp.endsWith("\n")) {
47                  return st.countTokens();
48              } else {
49                  return st.countTokens() - 1;
50              }
51          } catch (Exception e) {
52              e.printStackTrace();
53          }
54          return 0;
55      }
56  
57      public void run() {
58          ResourcesFactory.setCurrentRuleModel(ruleModel);
59  //        DebugController.getInstance().getPrController().setCurrentPR((PRModelImpl) this.currentRuleModel.getParent().getParent());
60          int startSelection = 0;
61          int endSelection = 0;
62          Iterator it = ruleModel.getBindings().keySet().iterator();
63          while (it.hasNext()) {
64              String s = (String) it.next();
65              if (s.startsWith("(")) {
66                  AnnotationSet as = (AnnotationSet) ruleModel.getBindings().get(s);
67                  if (as != null) {
68                      try {
69                          startSelection = ((AnnotationSet) ruleModel.getBindings().get(s)).firstNode().getOffset().intValue();
70                          endSelection = ((AnnotationSet) ruleModel.getBindings().get(s)).lastNode().getOffset().intValue();
71  //                        startSelection = startSelection - getSlashNAmount(startSelection, as);
72  //                        endSelection = endSelection - getSlashNAmount(endSelection, as);
73                          GuiFactory.getDocumentEditor().setTextSelection(Color.cyan, startSelection, endSelection);
74                      } catch (Exception e) {
75                          e.printStackTrace();
76                      }
77                  } else {
78                      GuiFactory.getDocumentEditor().setTextSelection(Color.cyan, 0, 0);
79                  }
80              }
81          }
82          GuiFactory.getDebugPanel().getRulePanel().ruleSelected();
83  //        this.ruleView.ruleSelected();
84          GuiFactory.getDebugPanel().getJapeSourcePanel().upgradeTextPane();
85          GuiFactory.getDebugPanel().getTraceHistoryPanel().updateRulePanel(ruleModel, null);
86  
87  //        getJapeSourceView().upgradeTextPane();
88      }
89  }
90  
91