1 package debugger.gui.debugging;
2
3 import debugger.resources.ResourcesFactory;
4
5 import javax.swing.*;
6 import javax.swing.text.StyledEditorKit;
7 import java.awt.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10
11
17
18 public class JapeSourcePanel extends JComponent {
19 private JEditorPane editorPane;
20 private JButton saveButton;
21 private JButton reinitButton;
22 private String saveButtonString = "Save";
23 private String reinitButtonString = "Reinitialize";
24
25 public JapeSourcePanel() {
26 initGui();
27 editorPane.setEditorKit(new StyledEditorKit());
28 editorPane.setDocument(new SyntaxDocument());
29 upgradeTextPane();
30 initListeners();
31 }
32
33 private void initGui() {
34 this.setLayout(new GridBagLayout());
35 GridBagConstraints c = new GridBagConstraints();
36
37 editorPane = new JTextPane();
38 c.gridx = 0;
39 c.gridy = 0;
40 c.weightx = 1;
41 c.weighty = 1;
42 c.gridwidth = 3;
43 c.gridheight = 1;
44 c.fill = GridBagConstraints.BOTH;
45 c.insets = new Insets(4, 3, 3, 4);
46 editorPane.setBorder(BorderFactory.createEtchedBorder());
47 this.add(new JScrollPane(editorPane), c);
48
49 c.gridx = 0;
50 c.gridy = 1;
51 c.weightx = 1;
52 c.weighty = 0;
53 c.gridwidth = 1;
54 c.gridheight = 1;
55 c.fill = GridBagConstraints.BOTH;
56 this.add(new JPanel(), c);
57
58 saveButton = new JButton(saveButtonString);
59 c.gridx = 1;
60 c.gridy = 1;
61 c.weightx = 0;
62 c.weighty = 0;
63 c.gridwidth = 1;
64 c.gridheight = 1;
65 c.fill = GridBagConstraints.NONE;
66 this.add(saveButton, c);
67
68 reinitButton = new JButton(reinitButtonString);
69 c.gridx = 2;
70 c.gridy = 1;
71 c.weightx = 0;
72 c.weighty = 0;
73 c.gridwidth = 1;
74 c.gridheight = 1;
75 saveButton.setPreferredSize(getButtonSize());
76 saveButton.setMinimumSize(getButtonSize());
77 saveButton.setMaximumSize(getButtonSize());
78 reinitButton.setPreferredSize(getButtonSize());
79 reinitButton.setMinimumSize(getButtonSize());
80 reinitButton.setMaximumSize(getButtonSize());
81 this.add(reinitButton, c);
82 }
83
84 public void upgradeTextPane() {
85 if (ResourcesFactory.getCurrentJapeFile() == null) return;
86 if (!editorPane.getText().equals(ResourcesFactory.getCurrentJapeText())) {
87 editorPane.setText(ResourcesFactory.getCurrentJapeText()); }
89 try {
90 int caretPosition = editorPane.getText().indexOf(ResourcesFactory.getCurrentRuleModel().getName());
92 editorPane.setCaretPosition(caretPosition);
93 editorPane.grabFocus();
94 Rectangle caretRectangle = editorPane.modelToView(caretPosition);
95 Rectangle visibleRect = editorPane.getVisibleRect();
96 editorPane.scrollRectToVisible(new Rectangle(0, caretRectangle.y + visibleRect.height, 1, 1));
97 } catch (Exception e) {
98 e.printStackTrace();
99 }
100 if (ResourcesFactory.getCurrentJapeFile().canWrite()) {
102 saveButton.setEnabled(true);
103 editorPane.setEditable(true);
104 } else {
105 saveButton.setEnabled(false);
106 editorPane.setEditable(false);
107 }
108 }
109
110 private Dimension getButtonSize() {
111 int saveButtonTextLength = saveButton.getFontMetrics(saveButton.getFont()).stringWidth(saveButtonString);
112 int reinitButtonTextLength = reinitButton.getFontMetrics(reinitButton.getFont()).stringWidth(reinitButtonString) + 25;
113 return new Dimension(Math.max(saveButtonTextLength, reinitButtonTextLength) + 21, 25);
114 }
115
116 private void initListeners() {
117 saveButton.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
119 }
121 });
122 reinitButton.addActionListener(new ActionListener() {
123 public void actionPerformed(ActionEvent e) {
124 }
126 });
127 }
128
133 }
134