-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpWindow.java
More file actions
187 lines (161 loc) · 6.32 KB
/
HelpWindow.java
File metadata and controls
187 lines (161 loc) · 6.32 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
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
public class HelpWindow extends JFrame implements ActionListener {
JScrollPane scrollPaneEditor;
JScrollPane scrollPaneMenu;
BasicUtils basicUtils;
MediaTracker tracker;
String stringDocsDir;
private JEditorPane editorpane;
private JEditorPane menuPane;
private URL helpURL;
public HelpWindow(String title, String helpURL) {
super(title);
basicUtils = new BasicUtils();
stringDocsDir = basicUtils.getWorkingDir() + "/docs/";
this.setEnabled(true);
tracker = new MediaTracker(this);
Image iconHelpImg = Toolkit.getDefaultToolkit().getImage(basicUtils.getUfile("docs/images/iconHelp.gif"));
tracker.addImage(iconHelpImg, 1);
try {
tracker.waitForAll();
} catch (Exception interruptedexception) {
System.out.println(interruptedexception.toString());
}
setIconImage(iconHelpImg);
editorpane = new JEditorPane();
scrollPaneEditor = new JScrollPane(editorpane);
editorpane.setEditable(false);
HTMLEditorKit kit = (HTMLEditorKit) editorpane.getEditorKitForContentType("text/html");
editorpane.setEditorKit(kit);
editorpane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent ev) {
try {
if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
doHEventUrlAction(ev.getDescription());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
menuPane = new JEditorPane();
scrollPaneMenu = new JScrollPane(menuPane);
menuPane.setEditable(false);
HTMLEditorKit kitA = (HTMLEditorKit) menuPane.getEditorKitForContentType("text/html");
menuPane.setEditorKit(kitA);
menuPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent ev) {
try {
if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
doHEventUrlAction(ev.getDescription());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
JSplitPane splitPaneHelp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPaneMenu, scrollPaneEditor);
Border emptyBdr = BorderFactory.createEmptyBorder(2, 2, 2, 2);
splitPaneHelp.setOneTouchExpandable(true);
splitPaneHelp.setContinuousLayout(false);
splitPaneHelp.setBorder(emptyBdr);
splitPaneHelp.setDividerLocation(178);
JPanel panelHWmain = new JPanel(new BorderLayout());
panelHWmain.add("Center", splitPaneHelp);
getContentPane().add(panelHWmain, BorderLayout.CENTER);
addButtons();
// no need for close listener just dispose
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
calculateLocation();
setVisible(true);
requestFocus();
try {
doHEventUrlAction(helpURL);
} catch (Exception ex) {
editorpane.setText(getTemplate("docs/index_body.html"));
}
try {
menuPane.setText(getTemplate("docs/index_menu.html"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
try {
if ((args[0] != null) && (args[1] != null)) {
new HelpWindow(args[0], args[1]);
} else {
new HelpWindow("help", "docs/index_body.html");
}
} catch (Exception e) {
new HelpWindow("help", "docs/index_body.html");
}
}
/**
* An Actionlistener so must implement this method
*/
public void actionPerformed(ActionEvent e) {
String strAction = e.getActionCommand();
try {
if (Objects.equals(strAction, "Contents")) {
editorpane.setPage(helpURL);
}
if (Objects.equals(strAction, "Close")) {
processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void addButtons() {
JButton btncontents = new JButton("Contents");
btncontents.addActionListener(this);
JButton btnclose = new JButton("Close");
btnclose.addActionListener(this);
JPanel panebuttons = new JPanel();
getContentPane().add(panebuttons, BorderLayout.SOUTH);
}
private void calculateLocation() {
Dimension screendim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(new Dimension(screendim.width - 200, screendim.height - 200));
int locationx = (screendim.width - (screendim.width - 200)) / 2;
int locationy = (screendim.height - (screendim.height - 200)) / 2;
setLocation(locationx, locationy);
}
public String getTemplate(String s) {
String cleanTplateString = "noQvalue";
try {
String tplateString = basicUtils.readFileAsString(s);
String repTplateUri = "src=\"file:///" + stringDocsDir;
String cleanFileString = basicUtils.replaceString(repTplateUri, "\\", "/");
cleanTplateString = basicUtils.replaceString(tplateString, "src=\"", cleanFileString);
} catch (Exception ex) {
ex.printStackTrace();
}
return cleanTplateString;
}
public void doHEventUrlAction(String urlString) {
if (urlString.startsWith("http")) {
try {
BrowserControl.displayURL(urlString);
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
editorpane.setText(getTemplate(basicUtils.getUfile("docs/" + urlString)));
editorpane.repaint();
editorpane.setCaretPosition(0);
}
}
}