1   package debugger.gui.editor;
2   
3   import debugger.JapeDebugger;
4   import debugger.gui.actions.editor.ShowResultAction;
5   
6   import javax.swing.*;
7   import javax.swing.text.*;
8   import java.awt.*;
9   import java.awt.event.*;
10  
11  /**
12   * Copyright (c) Ontos AG (http://www.ontosearch.com).
13   * This class is part of JAPE Debugger component for
14   * GATE (Copyright (c) "The University of Sheffield" see http://gate.ac.uk/) <br>
15   * @author Oleg Mishenko, Andrey Shafirin
16   */
17  
18  public class DocumentEditor extends JComponent {
19      private JTextPane textPane;
20      private JButton showResultButton;
21  
22      public DocumentEditor() {
23          initGui();
24          initListeners();
25      }
26  
27      public void setTextSelection(Color color, int startOffset, int endOffset) {
28          MutableAttributeSet attributeSet = new SimpleAttributeSet();
29          StyleConstants.setBackground(attributeSet, new Color(210, 210, 210));
30          ((DefaultStyledDocument) textPane.getDocument()).setCharacterAttributes(0, startOffset, attributeSet, false);
31  
32          attributeSet = new SimpleAttributeSet();
33          StyleConstants.setBackground(attributeSet, Color.white);
34          ((DefaultStyledDocument) textPane.getDocument()).setCharacterAttributes(endOffset, textPane.getDocument().getLength() - endOffset, attributeSet, false);
35  
36          attributeSet = new SimpleAttributeSet();
37          StyleConstants.setBackground(attributeSet, color);
38          ((DefaultStyledDocument) textPane.getDocument()).setCharacterAttributes(startOffset, endOffset - startOffset, attributeSet, false);
39          try {
40              textPane.scrollRectToVisible(textPane.modelToView(startOffset));
41          } catch (BadLocationException e) {
42              e.printStackTrace();
43          }
44      }
45  
46      public void setTextFont(String fontName, int size, int startOffset, int endOffset) {
47          MutableAttributeSet attributeSet = new SimpleAttributeSet();
48          StyleConstants.setFontFamily(attributeSet, fontName);
49          StyleConstants.setFontSize(attributeSet, size);
50          ((DefaultStyledDocument) textPane.getDocument()).setCharacterAttributes(startOffset, endOffset - startOffset, attributeSet, false);
51      }
52  
53      public JTextPane getTextPane() {
54          return textPane;
55      }
56  
57      private void initGui() {
58          textPane = new JTextPane();
59          textPane.setEditable(false);
60          //this prevents autoscrolling of the scrollbar
61          textPane.setCaret(new DefaultCaret() {
62              protected void adjustVisibility(Rectangle nloc) {
63              }
64          });
65  
66          this.setLayout(new BorderLayout());
67          JPanel buttonPanel = new JPanel();
68          ImageIcon icon = new ImageIcon(JapeDebugger.class.getResource("gui/icons/go.png"));
69          icon = new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
70          showResultButton = new JButton();
71          showResultButton.setIcon(icon);
72          showResultButton.setPreferredSize(new Dimension(20, 20));
73          showResultButton.setEnabled(false);
74          buttonPanel.add(showResultButton);
75          this.add(buttonPanel, BorderLayout.WEST);
76          this.add(new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
77      }
78  
79      private void initListeners() {
80          showResultButton.addActionListener(new ActionListener() {
81              public void actionPerformed(ActionEvent e) {
82                  ShowResultAction.getInstance().actionPerformed(textPane.getSelectionStart(), textPane.getSelectionEnd());
83              }
84          });
85          textPane.addMouseMotionListener(new MouseMotionAdapter() {
86              public void mouseDragged(MouseEvent e) {
87                  if (textPane.getSelectedText() == null) {
88                      showResultButton.setEnabled(false);
89                  } else {
90                      showResultButton.setEnabled(true);
91                  }
92              }
93          });
94          textPane.addMouseListener(new MouseAdapter() {
95              public void mousePressed(MouseEvent e) {
96                  if (textPane.getSelectedText() == null) {
97                      showResultButton.setEnabled(false);
98                  } else {
99                      showResultButton.setEnabled(true);
100                 }
101             }
102 
103             public void mouseReleased(MouseEvent e) {
104                 mousePressed(e);
105             }
106         });
107     }
108 }
109