1   package debugger.gui.actions.editor;
2   
3   import debugger.gui.GuiFactory;
4   import debugger.gui.MainFrame;
5   import debugger.gui.actions.resources.LrResourceSelectedAction;
6   import debugger.resources.ResourcesFactory;
7   import gate.Document;
8   import gate.LanguageResource;
9   import gate.corpora.DocumentImpl;
10  import gate.util.InvalidOffsetException;
11  
12  import javax.swing.*;
13  
14  /**
15   * Copyright (c) Ontos AG (http://www.ontosearch.com).
16   * This class is part of JAPE Debugger component for
17   * GATE (Copyright (c) "The University of Sheffield" see http://gate.ac.uk/) <br>
18   * @author Andrey Shafirin, Oleg Mishenko
19   */
20  
21  public class ShowResultAction {
22      private static ShowResultAction ourInstance;
23  
24      public synchronized static ShowResultAction getInstance() {
25          if (ourInstance == null) {
26              ourInstance = new ShowResultAction();
27          }
28          return ourInstance;
29      }
30  
31      private ShowResultAction() {
32      }
33  
34      public void actionPerformed(int startOffset, int endOffset) {
35          LanguageResource lr = ResourcesFactory.getCurrentLrModel().getLr();
36          if (lr instanceof Document) {
37              //check if document content changed
38              if(!ResourcesFactory.getCurrentLrModel().getStoredContent().equals(((Document)lr).getContent().toString())) {
39                  JOptionPane.showMessageDialog(
40                          JOptionPane.getRootFrame(),
41                          "Document content changed.\n" +
42                          "Synchronization of rule matching history with\n" +
43                          "changing document content not supported.\n" +
44                          "You should rerun pipeline again.",
45                          "Warning!",
46                          JOptionPane.PLAIN_MESSAGE);
47                  ResourcesFactory.getCurrentLrModel().synchronize();
48                  LrResourceSelectedAction.getInstance().actionPerformed(ResourcesFactory.getCurrentLrModel());
49                  return;
50              }
51  
52              int startSelection = startOffset + getSlashNAmount(startOffset, (DocumentImpl) lr);
53              int endSelection = endOffset + getSlashNAmount(endOffset, (DocumentImpl) lr);
54              GuiFactory.getDebugPanel().getTraceHistoryPanel().setText(startSelection, endSelection, (Document) lr);
55              GuiFactory.getResourceView().getResourceTreeCellRenderer().setIndexes(startSelection, endSelection);
56              GuiFactory.getResourceView().repaint();
57  //            try {
58  //                System.out.println(((DocumentImpl) lr).getContent().getContent(new Long(startSelection), new Long(endSelection)).toString());
59  //            } catch (InvalidOffsetException e) {
60  //                e.printStackTrace();
61  //            }
62              GuiFactory.getResourceView().repaint(); //to highlight rules
63          }
64      }
65  
66      private int getSlashNAmount(int offset, DocumentImpl document) {
67          int result = 0;
68          try {
69              String temp = document.getContent().getContent(new Long(0), new Long(offset)).toString();
70  
71              char[] charArray = temp.toCharArray();
72              for (int i = 0; i < charArray.length; i++) {
73                  char c = charArray[i];
74                  if (c == '\n') result++;
75              }
76  //            if(temp.equals("")) return 0;
77  //            StringTokenizer st = new StringTokenizer(temp, "\n");
78  //            if(temp.endsWith("\n"))
79  //            {
80  //                return st.countTokens();
81  //            }
82  //            else
83  //            {
84  //                return st.countTokens() - 1;
85  //            }
86          } catch (Exception e) {
87              e.printStackTrace();
88          }
89          return result;
90      }
91  }
92  
93