1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
import java.awt.*;
|
8
|
|
import java.awt.event.*;
|
9
|
|
import java.io.*;
|
10
|
|
import java.util.*;
|
11
|
|
|
12
|
|
import javax.swing.*;
|
13
|
|
import javax.swing.text.*;
|
14
|
|
import javax.swing.event.*;
|
15
|
|
import javax.swing.border.*;
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
public class WordCount extends JDialog
|
23
|
|
{
|
24
|
|
|
25
|
|
protected int m_option = JOptionPane.OK_OPTION;
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
protected int wCount;
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
protected int chCount;
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
protected int chwCount;
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
protected double lCount;
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
0
|
public void Count(String txt)
|
56
|
|
{
|
57
|
0
|
wCount = 0;
|
58
|
0
|
chwCount = 0;
|
59
|
0
|
chCount = 0;
|
60
|
0
|
lCount = 0;
|
61
|
0
|
int num = 0;
|
62
|
0
|
String myText = "";
|
63
|
0
|
int count = 0;
|
64
|
|
|
65
|
0
|
chwCount = txt.length();
|
66
|
|
|
67
|
0
|
StringTokenizer st = new StringTokenizer(txt);
|
68
|
0
|
while (st.hasMoreTokens()) {
|
69
|
0
|
wCount++;
|
70
|
0
|
myText = myText.concat(st.nextToken());
|
71
|
|
}
|
72
|
|
|
73
|
0
|
chCount = myText.length();
|
74
|
|
|
75
|
|
|
76
|
0
|
while (txt.indexOf('\n', count) != -1) {
|
77
|
0
|
count = txt.indexOf('\n', count);
|
78
|
0
|
count++;
|
79
|
0
|
num++;
|
80
|
|
}
|
81
|
|
|
82
|
0
|
lCount = java.lang.Math.max(Math.ceil(chwCount / 80.0), num);
|
83
|
|
}
|
84
|
|
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
0
|
public WordCount(SHTMLEditorPane editor)
|
92
|
|
{
|
93
|
0
|
setTitle("Word Count");
|
94
|
0
|
Document doc = editor.getDocument();
|
95
|
|
|
96
|
0
|
try
|
97
|
|
{
|
98
|
0
|
Count(doc.getText(0, doc.getLength()));
|
99
|
|
}
|
100
|
|
catch(Exception ee)
|
101
|
|
{
|
102
|
|
}
|
103
|
|
|
104
|
0
|
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
|
105
|
|
|
106
|
|
|
107
|
0
|
centerOnScreen();
|
108
|
0
|
JPanel p = new JPanel(new GridLayout(1, 2, 2, 2));
|
109
|
0
|
p.setBorder(new TitledBorder(new EtchedBorder(), "Stats:"));
|
110
|
|
|
111
|
0
|
JPanel leftPanel = new JPanel();
|
112
|
0
|
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
|
113
|
0
|
JPanel labelPanel = new JPanel();
|
114
|
0
|
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
|
115
|
0
|
JLabel wCntLabel = new JLabel("Words:");
|
116
|
0
|
labelPanel.add(wCntLabel);
|
117
|
0
|
JLabel chWLabel = new JLabel("Characters(without spaces):");
|
118
|
0
|
labelPanel.add(chWLabel);
|
119
|
0
|
JLabel chLabel = new JLabel("Characters(with spaces):");
|
120
|
0
|
labelPanel.add(chLabel);
|
121
|
0
|
JLabel lCntLabel = new JLabel("Lines:");
|
122
|
0
|
labelPanel.add(lCntLabel);
|
123
|
0
|
leftPanel.add(labelPanel);
|
124
|
|
|
125
|
0
|
JPanel cntPanel = new JPanel(new GridLayout(4, 1, 5, 5));
|
126
|
0
|
cntPanel.setBorder(new EmptyBorder(5, 20, 5, 5));
|
127
|
0
|
cntPanel.setLayout(new BoxLayout(cntPanel, BoxLayout.Y_AXIS));
|
128
|
0
|
JLabel wCnt = new JLabel("" + wCount);
|
129
|
0
|
cntPanel.add(wCnt);
|
130
|
0
|
JLabel chCnt = new JLabel("" + chCount);
|
131
|
0
|
cntPanel.add(chCnt);
|
132
|
0
|
JLabel chwCnt = new JLabel("" + chwCount);
|
133
|
0
|
cntPanel.add(chwCnt);
|
134
|
0
|
JLabel lCnt = new JLabel("" + lCount);
|
135
|
0
|
cntPanel.add(lCnt);
|
136
|
0
|
leftPanel.add(cntPanel);
|
137
|
0
|
p.add(leftPanel);
|
138
|
0
|
getContentPane().add(p);
|
139
|
|
|
140
|
0
|
p = new JPanel(new FlowLayout());
|
141
|
0
|
JPanel p1 = new JPanel(new GridLayout(1, 2, 10, 2));
|
142
|
0
|
JButton btOK = new JButton("OK");
|
143
|
|
|
144
|
0
|
ActionListener lst = new ActionListener()
|
145
|
|
{
|
146
|
0
|
public void actionPerformed(ActionEvent e)
|
147
|
|
{
|
148
|
0
|
m_option = JOptionPane.OK_OPTION;
|
149
|
0
|
setVisible(false);
|
150
|
|
}
|
151
|
|
};
|
152
|
|
|
153
|
0
|
btOK.addActionListener(lst);
|
154
|
0
|
p1.add(btOK);
|
155
|
0
|
p.add(p1);
|
156
|
0
|
getContentPane().add(p);
|
157
|
|
|
158
|
0
|
pack();
|
159
|
|
|
160
|
0
|
setResizable(false);
|
161
|
0
|
setVisible(true);
|
162
|
|
}
|
163
|
|
|
164
|
0
|
public void centerOnScreen()
|
165
|
|
{
|
166
|
0
|
Dimension screen = getToolkit().getScreenSize();
|
167
|
0
|
Rectangle bounds = getBounds();
|
168
|
0
|
setLocation( (screen.width - bounds.width) / 2, (screen.height - bounds.height) / 2 );
|
169
|
0
|
requestFocus();
|
170
|
|
}
|
171
|
|
|
172
|
|
}
|
173
|
|
|
174
|
|
|