1 package com.explosion.expfmodules.texteditor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Dimension;
25 import java.awt.Insets;
26 import java.awt.Point;
27 import java.awt.event.KeyEvent;
28
29 import javax.swing.JPanel;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTextArea;
32 import javax.swing.JViewport;
33
34 import com.explosion.utilities.GeneralConstants;
35 import com.explosion.utilities.GeneralUtils;
36
37 public class LineNumberTextEditor extends JPanel
38 {
39 private JScrollPane editorPane = new JScrollPane();
40 private ExpfTextArea editorTextComponent;
41 private JScrollPane marginPane = new JScrollPane();
42 private JTextArea marginTextArea = new JTextArea("");
43 private Object parentReference;
44 private String line1Text = "1";
45 private boolean showLineNumbers = false;
46
47
48 /***
49 * Constructor
50 */
51 public LineNumberTextEditor(Component parentReference, String line1Text, boolean showLineNumbers) throws Exception
52 {
53 this.parentReference = parentReference;
54 this.line1Text = line1Text;
55 this.editorTextComponent = new ExpfTextArea(parentReference);
56 this.showLineNumbers = showLineNumbers;
57 init();
58 }
59
60 /***
61 * Sets up the GUI
62 */
63 private void init() throws Exception
64 {
65 editorTextComponent.setMargin(new Insets(0, 5, 0, 0));
66
67 if (showLineNumbers)
68 {
69 editorPane.setViewport(
70 new JViewport() {
71 public void setViewPosition(Point p) {
72 super.setViewPosition(p);
73 marginPane.getViewport().setViewPosition(new Point((int)marginPane.getViewport().getViewPosition().getX(),(int)this.getViewPosition().getY()));
74 marginPane.getViewport().setViewSize(new Dimension((int)marginPane.getViewport().getViewSize().getWidth(),(int)this.getViewSize().getHeight()));
75 }});
76 }
77
78 editorPane.getViewport().add(editorTextComponent);
79
80 this.setLayout(new BorderLayout());
81 this.add(editorPane, BorderLayout.CENTER);
82
83 if (showLineNumbers)
84 {
85 marginTextArea.setEditable(false);
86 marginTextArea.setColumns(5);
87 marginTextArea.setText(getMarginText(1));
88
89 marginPane.getViewport().add(marginTextArea);
90 marginPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
91 marginPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
92
93
94 this.add(marginPane, BorderLayout.WEST);
95
96 editorTextComponent.addKeyListener(new java.awt.event.KeyAdapter()
97 {
98 public void keyTyped(KeyEvent e)
99 {
100 commandTextArea_keyTyped(e);
101 }
102
103 public void keyPressed(KeyEvent e)
104 {
105 commandTextArea_keyPressed(e);
106 }
107 });
108 }
109 }
110
111 /***
112 * Look for linebreaks
113 */
114 void commandTextArea_keyTyped(KeyEvent e)
115 {
116 if (showLineNumbers)
117 marginTextArea.setText(getMarginText(GeneralUtils.lineCount(editorTextComponent.getText())));
118 }
119
120 /***
121 * Something is typed onto the command line
122 */
123 void commandTextArea_keyPressed(KeyEvent e)
124 {
125 if (showLineNumbers)
126 marginTextArea.setText(getMarginText(GeneralUtils.lineCount(editorTextComponent.getText())));
127 }
128
129 public void clear()
130 {
131 editorTextComponent.setText("");
132 marginTextArea.setText(getMarginText(1));
133 }
134
135 /***
136 * This method returns the text for the margin
137 */
138 private String getMarginText(int numberOfLines)
139 {
140 String returnText = " " + line1Text;
141 for (int i = 2; i <= numberOfLines; i++)
142 returnText += GeneralConstants.LS + " " + i + "";
143 return returnText;
144 }
145
146 public void disableComponent()
147 {
148 editorTextComponent.setEnabled(false);
149 marginTextArea.setEnabled(false);
150 }
151
152 public void enableComponent()
153 {
154 editorTextComponent.setEnabled(true);
155 marginTextArea.setEnabled(true);
156 }
157
158
159
160 /***
161 * @return
162 */
163 public ExpfTextArea getEditorTextComponent() {
164 return editorTextComponent;
165 }
166
167 /***
168 * @return
169 */
170 public String getLine1Text() {
171 return line1Text;
172 }
173
174 /***
175 * @return
176 */
177 public JTextArea getMarginTextArea() {
178 return marginTextArea;
179 }
180
181 }
182