-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpFrame.java
More file actions
215 lines (187 loc) · 4.58 KB
/
HelpFrame.java
File metadata and controls
215 lines (187 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
/** @author Gergely Kota
HelpFrame handles all the help files for SPLAT.
*/
public class HelpFrame extends JFrame implements ActionListener
{
private File[] files;
private String[] endings;
private HashMap contents, filemap;
// private HTMLPanel window;
private JEditorPane window;
private JList topics;
private JScrollPane jsp;
private static final Stack stack = new Stack();
private final int offset = 24;
public HelpFrame()
{
jsp = new JScrollPane();
// window = new HTMLPanel();
window = new JEditorPane();
window.setContentType("text/html");
window.setEditable(false);
JScrollPane winjsp = new JScrollPane();
winjsp.getViewport().add(window);
actionPerformed(null);
Config.addActionListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jsp, BorderLayout.WEST);
getContentPane().add(winjsp, BorderLayout.CENTER);
setSize(600,400);
int x = offset*stack.size();
setLocation(400+x, 200+x);
setTitle("Splat Help");
stack.add(this);
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
JMenu file = new JMenu("File");
JMenuItem newF = new JMenuItem("New Window");
JMenuItem reload = new JMenuItem("Refresh Info");
file.add(newF);
file.add(reload);
jmb.add(file);
newF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
newFrame();
}
});
reload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
reload();
}
});
}
public void actionPerformed(ActionEvent ae)
{
if(ae == null || ae.getSource().equals("helptype") || ae.getSource().equals("helpdir"))
init();
}
private void init()
{
try
{
String s = Config.read("helptype");
StringTokenizer st = new StringTokenizer(s);
String[] temp = new String[st.countTokens()];
int count = 0;
while(st.hasMoreTokens())
temp[count++] = st.nextToken();
endings = temp;
}
catch(Exception e)
{
endings = new String[]{"html", "htm", "txt"};
}
reload();
}
private void reload()
{
topics = collectTopics();
jsp.getViewport().removeAll();
jsp.getViewport().add(topics);
if(files.length > 0)
goTopic(topic(files[0]));
}
private void newFrame()
{
new HelpFrame().show();
}
public static HelpFrame getTop()
{
if(stack.size() == 0)
return new HelpFrame();
return (HelpFrame) stack.peek();
}
private JList collectTopics()
{
JList temp = new JList();
String loc = Config.read("helpdir");
if(loc == null)
loc = ".";
files = collectFiles(new File(loc));
contents = collectContents();
Arrays.sort(files);
String[] s = new String[files.length];
for(int i = 0; i < files.length; i++)
s[i] = topic(files[i]);
temp.setListData(s);
temp.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
setTopic((String) topics.getSelectedValue());
}
});
return temp;
}
private HashMap collectContents()
{
HashMap temp = new HashMap();
filemap = new HashMap();
for(int i = 0; i < files.length; i++)
{
try
{
BufferedReader br = new BufferedReader(new FileReader(files[i]));
StringBuffer sb = new StringBuffer();
while(br.ready())
sb.append(br.readLine()).append("<BR>");
temp.put(topic(files[i]), sb.toString());
filemap.put(topic(files[i]), files[i]);
}
catch(Exception e) {}
}
return temp;
}
private String topic(File f)
{
return f.getName().substring(0, f.getName().lastIndexOf("."));
}
private File[] collectFiles(File dir)
{
if(!dir.isDirectory())
return new File[0];
return dir.listFiles(new FileFilter() {
public boolean accept(File f) {
for(int i = 0; i < endings.length; i++)
if(f.getName().toLowerCase().endsWith(endings[i]))
return true;
return false;
}
});
}
private void setTopic(String topic)
{
String s = (String) contents.get(topic);
// File f = (File) filemap.get(topic);
if(s != null)
window.setText(s);
}
public HelpFrame goTopic(String s)
{
// find help dealing with this class and go there
if(s == null)
return this;
for(int i = 0; i < files.length; i++)
if(topic(files[i]).equalsIgnoreCase(s))
topics.setSelectedIndex(i);
return this;
}
// not yet supported
public HelpFrame goKeyword(String s)
{
if(s == null)
return this;
return this;
}
public static void main(String[] arghs)
{
// new HelpFrame().goTopic("WebSearch").show();
// new HelpFrame().show();
new HelpFrame().show();
}
}