1
|
|
import java.awt.*;
|
2
|
|
import java.awt.event.*;
|
3
|
|
import javax.swing.*;
|
4
|
|
import javax.swing.border.*;
|
5
|
|
import javax.swing.text.BadLocationException;
|
6
|
|
import javax.swing.text.*;
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
public class GoToDialog extends JDialog
|
19
|
|
{
|
20
|
|
private JTextField lineText;
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
1
|
public GoToDialog(SHTMLEditorPane editor)
|
32
|
|
{
|
33
|
1
|
setTitle("Go To Line");
|
34
|
1
|
centerOnScreen();
|
35
|
1
|
final SHTMLEditorPane nEditor = editor;
|
36
|
1
|
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
|
37
|
1
|
JPanel linePanel = new JPanel();
|
38
|
1
|
linePanel.setLayout(new GridLayout(2, 1, 5, 5));
|
39
|
1
|
linePanel.setBorder(new EmptyBorder(5, 5, 5, 2));
|
40
|
1
|
JLabel lineLabel = new JLabel("Line Number:");
|
41
|
1
|
lineText = new JTextField();
|
42
|
1
|
lineLabel.setLabelFor(lineText);
|
43
|
1
|
linePanel.add(lineLabel);
|
44
|
1
|
linePanel.add(lineText);
|
45
|
1
|
getContentPane().add(linePanel);
|
46
|
1
|
JPanel buttonPanel = new JPanel();
|
47
|
1
|
buttonPanel.setLayout(new GridLayout(2, 1, 5, 5));
|
48
|
1
|
buttonPanel.setBorder(new EmptyBorder(5, 3, 5, 5));
|
49
|
1
|
JButton okButton = new JButton("OK");
|
50
|
1
|
okButton.addActionListener(new ActionListener()
|
51
|
|
{
|
52
|
0
|
public void actionPerformed(ActionEvent event)
|
53
|
|
{
|
54
|
0
|
try
|
55
|
|
{
|
56
|
0
|
goToLine(Integer.parseInt(lineText.getText()), nEditor);
|
57
|
|
}
|
58
|
|
catch(Exception e)
|
59
|
|
{
|
60
|
0
|
JOptionPane.showMessageDialog(null, "Invalid line number", "Error", JOptionPane.ERROR_MESSAGE);
|
61
|
|
}
|
62
|
0
|
dispose();
|
63
|
|
}
|
64
|
|
});
|
65
|
|
|
66
|
1
|
buttonPanel.add(okButton);
|
67
|
1
|
JButton cancelButton = new JButton("Cancel");
|
68
|
1
|
cancelButton.addActionListener(new ActionListener()
|
69
|
|
{
|
70
|
0
|
public void actionPerformed(ActionEvent event)
|
71
|
|
{
|
72
|
0
|
dispose();
|
73
|
|
}
|
74
|
|
});
|
75
|
|
|
76
|
1
|
buttonPanel.add(cancelButton);
|
77
|
1
|
getContentPane().add(buttonPanel);
|
78
|
1
|
pack();
|
79
|
|
|
80
|
1
|
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
81
|
1
|
setResizable(false);
|
82
|
1
|
setVisible(true);
|
83
|
|
}
|
84
|
0
|
public void goToLine(int lineNumber, SHTMLEditorPane editor) throws BadLocationException
|
85
|
|
{
|
86
|
0
|
Document doc = editor.getDocument();
|
87
|
0
|
if (lineNumber == 1) {
|
88
|
0
|
editor.setCaretPosition(0);
|
89
|
0
|
return;
|
90
|
|
}
|
91
|
|
|
92
|
0
|
int currentLine = 1;
|
93
|
0
|
for (int counter = 0; counter < doc.getLength() + 2; counter++)
|
94
|
|
{
|
95
|
0
|
if (doc.getText(counter, 1).equals("\n"))
|
96
|
|
{
|
97
|
0
|
currentLine++;
|
98
|
0
|
if (currentLine == lineNumber)
|
99
|
|
{
|
100
|
0
|
editor.setCaretPosition(counter + 1);
|
101
|
0
|
break;
|
102
|
|
}
|
103
|
|
}
|
104
|
|
}
|
105
|
|
}
|
106
|
|
|
107
|
1
|
public void centerOnScreen()
|
108
|
|
{
|
109
|
1
|
Dimension screen = getToolkit().getScreenSize();
|
110
|
1
|
Rectangle bounds = getBounds();
|
111
|
1
|
setLocation( (screen.width - bounds.width) / 2, (screen.height - bounds.height) / 2 );
|
112
|
1
|
requestFocus();
|
113
|
|
}
|
114
|
|
}
|