|
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 |
| package edu.rice.cs.drjava.ui; |
|
38 |
| |
|
39 |
| import javax.swing.*; |
|
40 |
| import javax.swing.event.*; |
|
41 |
| import javax.swing.border.*; |
|
42 |
| import javax.swing.text.html.*; |
|
43 |
| import java.awt.event.*; |
|
44 |
| import java.awt.*; |
|
45 |
| import java.net.*; |
|
46 |
| import java.io.*; |
|
47 |
| import java.util.ArrayList; |
|
48 |
| |
|
49 |
| import edu.rice.cs.util.FileOps; |
|
50 |
| import edu.rice.cs.util.UnexpectedException; |
|
51 |
| import edu.rice.cs.util.swing.BorderlessScrollPane; |
|
52 |
| import edu.rice.cs.util.swing.SwingFrame; |
|
53 |
| import edu.rice.cs.util.swing.Utilities; |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| public class HTMLFrame extends SwingFrame { |
|
59 |
| |
|
60 |
| private static final int FRAME_WIDTH = 750; |
|
61 |
| private static final int FRAME_HEIGHT = 600; |
|
62 |
| private static final int LEFT_PANEL_WIDTH = 250; |
|
63 |
| private JEditorPane _mainDocPane; |
|
64 |
| private JScrollPane _mainScroll; |
|
65 |
| private JSplitPane _splitPane; |
|
66 |
| private JPanel _splitPaneHolder; |
|
67 |
| private JEditorPane _contentsDocPane; |
|
68 |
| private JPanel _closePanel; |
|
69 |
| private JButton _closeButton; |
|
70 |
| private JButton _backButton; |
|
71 |
| private JButton _forwardButton; |
|
72 |
| protected URL _baseURL; |
|
73 |
| private ArrayList<HyperlinkListener> _hyperlinkListeners; |
|
74 |
| private boolean _linkError; |
|
75 |
| private URL _lastURL; |
|
76 |
| |
|
77 |
| private JPanel _navPane; |
|
78 |
| |
|
79 |
| protected HistoryList _history; |
|
80 |
| |
|
81 |
| private HyperlinkListener _resetListener = new HyperlinkListener() { |
|
82 |
0
| public void hyperlinkUpdate(HyperlinkEvent e) {
|
|
83 |
0
| if (_linkError && e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
|
|
84 |
0
| _resetMainPane();
|
|
85 |
| } |
|
86 |
| } |
|
87 |
| }; |
|
88 |
| |
|
89 |
| protected static class HistoryList { |
|
90 |
| private HistoryList next = null; |
|
91 |
| private final HistoryList prev; |
|
92 |
| protected final URL contents; |
|
93 |
76
| private HistoryList(URL contents) {
|
|
94 |
76
| this.contents = contents;
|
|
95 |
76
| this.prev = null;
|
|
96 |
| } |
|
97 |
0
| private HistoryList(URL contents, HistoryList prev) {
|
|
98 |
0
| this.contents = contents;
|
|
99 |
0
| this.prev = prev;
|
|
100 |
0
| prev.next = this;
|
|
101 |
| } |
|
102 |
| } |
|
103 |
| |
|
104 |
| public static abstract class ResourceAction extends AbstractAction { |
|
105 |
152
| public ResourceAction(String name, String iconName) {
|
|
106 |
152
| super(name,MainFrame.getIcon(iconName));
|
|
107 |
| } |
|
108 |
| } |
|
109 |
| |
|
110 |
| private static abstract class ConsolidatedAction extends ResourceAction { |
|
111 |
152
| private ConsolidatedAction(String name) {
|
|
112 |
152
| super(name,name + "16.gif");
|
|
113 |
| } |
|
114 |
| } |
|
115 |
| |
|
116 |
| private Action _forwardAction = new ConsolidatedAction("Forward") { |
|
117 |
0
| public void actionPerformed(ActionEvent e) {
|
|
118 |
0
| _history = _history.next;
|
|
119 |
| |
|
120 |
| |
|
121 |
0
| _backAction.setEnabled(true);
|
|
122 |
| |
|
123 |
0
| if (_history.next == null) {
|
|
124 |
| |
|
125 |
0
| _forwardAction.setEnabled(false);
|
|
126 |
| } |
|
127 |
0
| _displayPage(_history.contents);
|
|
128 |
| } |
|
129 |
| }; |
|
130 |
| |
|
131 |
| private Action _backAction = new ConsolidatedAction("Back") { |
|
132 |
0
| public void actionPerformed(ActionEvent e) {
|
|
133 |
0
| _history = _history.prev;
|
|
134 |
| |
|
135 |
| |
|
136 |
0
| _forwardAction.setEnabled(true);
|
|
137 |
| |
|
138 |
0
| if (_history.prev == null) {
|
|
139 |
| |
|
140 |
0
| _backAction.setEnabled(false);
|
|
141 |
| } |
|
142 |
0
| _displayPage(_history.contents);
|
|
143 |
| } |
|
144 |
| }; |
|
145 |
| |
|
146 |
| private Action _closeAction = new AbstractAction("Close") { |
|
147 |
0
| public void actionPerformed(ActionEvent e) {
|
|
148 |
0
| HTMLFrame.this.setVisible(false);
|
|
149 |
| } |
|
150 |
| }; |
|
151 |
| |
|
152 |
152
| private static JButton makeButton(Action a, int horTextPos, int left, int right) {
|
|
153 |
152
| JButton j = new JButton(a);
|
|
154 |
152
| j.setHorizontalTextPosition(horTextPos);
|
|
155 |
152
| j.setVerticalTextPosition(JButton.CENTER);
|
|
156 |
| |
|
157 |
| |
|
158 |
152
| j.setMargin(new Insets(3,left+3,3,right+3));
|
|
159 |
152
| return j;
|
|
160 |
| } |
|
161 |
| |
|
162 |
76
| public void addHyperlinkListener(HyperlinkListener linkListener) {
|
|
163 |
76
| _hyperlinkListeners.add(linkListener);
|
|
164 |
76
| _contentsDocPane.addHyperlinkListener(linkListener);
|
|
165 |
76
| _mainDocPane.addHyperlinkListener(linkListener);
|
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
76
| public HTMLFrame(String frameName, URL introUrl, URL indexUrl, String iconString) {
|
|
170 |
76
| this(frameName, introUrl, indexUrl, iconString, null);
|
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
76
| public HTMLFrame(String frameName, URL introUrl, URL indexUrl, String iconString, File baseDir) {
|
|
175 |
76
| super(frameName);
|
|
176 |
| |
|
177 |
76
| _contentsDocPane = new JEditorPane();
|
|
178 |
76
| _contentsDocPane.setEditable(false);
|
|
179 |
76
| JScrollPane contentsScroll = new BorderlessScrollPane(_contentsDocPane);
|
|
180 |
| |
|
181 |
76
| _mainDocPane = new JEditorPane();
|
|
182 |
76
| _mainDocPane.setEditable(false);
|
|
183 |
76
| _mainScroll = new BorderlessScrollPane(_mainDocPane);
|
|
184 |
| |
|
185 |
76
| _splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, contentsScroll, _mainScroll);
|
|
186 |
76
| _splitPane.setDividerLocation(LEFT_PANEL_WIDTH);
|
|
187 |
76
| _splitPaneHolder = new JPanel(new GridLayout(1,1));
|
|
188 |
76
| _splitPaneHolder.setBorder(new EmptyBorder(0,5,0,5));
|
|
189 |
76
| _splitPaneHolder.add(_splitPane);
|
|
190 |
| |
|
191 |
76
| _closeButton = new JButton(_closeAction);
|
|
192 |
76
| _backButton = makeButton(_backAction,JButton.RIGHT,0,3);
|
|
193 |
76
| _forwardButton = makeButton(_forwardAction,JButton.LEFT,3,0);
|
|
194 |
76
| _backAction.setEnabled(false);
|
|
195 |
76
| _forwardAction.setEnabled(false);
|
|
196 |
76
| _closePanel = new JPanel(new BorderLayout());
|
|
197 |
76
| _closePanel.add(_closeButton, BorderLayout.EAST);
|
|
198 |
76
| _closePanel.setBorder(new EmptyBorder(5,5,5,5));
|
|
199 |
76
| _navPane = new JPanel();
|
|
200 |
76
| _navPane.setBackground(new Color(0xCCCCFF));
|
|
201 |
76
| _navPane.setLayout(new BoxLayout(_navPane,BoxLayout.X_AXIS));
|
|
202 |
76
| JLabel icon = new JLabel(MainFrame.getIcon(iconString));
|
|
203 |
76
| _navPane.add(icon);
|
|
204 |
76
| _navPane.add(Box.createHorizontalStrut(8));
|
|
205 |
76
| _navPane.add(Box.createHorizontalGlue());
|
|
206 |
76
| _navPane.add(_backButton);
|
|
207 |
76
| _navPane.add(Box.createHorizontalStrut(8));
|
|
208 |
76
| _navPane.add(_forwardButton);
|
|
209 |
76
| _navPane.add(Box.createHorizontalStrut(3));
|
|
210 |
76
| _navPane.setBorder(new EmptyBorder(0,0,0,5));
|
|
211 |
76
| JPanel navContainer = new JPanel(new GridLayout(1,1));
|
|
212 |
76
| navContainer.setBorder(new CompoundBorder(new EmptyBorder(5,5,5,5), new EtchedBorder()));
|
|
213 |
| |
|
214 |
76
| navContainer.add(_navPane);
|
|
215 |
76
| Container cp = getContentPane();
|
|
216 |
76
| cp.setLayout(new BorderLayout());
|
|
217 |
76
| cp.add(navContainer, BorderLayout.NORTH);
|
|
218 |
76
| cp.add(_splitPaneHolder, BorderLayout.CENTER);
|
|
219 |
76
| cp.add(_closePanel, BorderLayout.SOUTH);
|
|
220 |
| |
|
221 |
76
| _linkError = false;
|
|
222 |
76
| _hyperlinkListeners = new ArrayList<HyperlinkListener>();
|
|
223 |
76
| _hyperlinkListeners.add(_resetListener);
|
|
224 |
76
| _mainDocPane.addHyperlinkListener(_resetListener);
|
|
225 |
| |
|
226 |
76
| if (baseDir == null) _baseURL = null;
|
|
227 |
| else |
|
228 |
0
| try { _baseURL = FileOps.toURL(baseDir); }
|
|
229 |
| catch(MalformedURLException ex) { |
|
230 |
0
| throw new UnexpectedException(ex);
|
|
231 |
| } |
|
232 |
| |
|
233 |
| |
|
234 |
0
| if (indexUrl == null) _displayContentsError(null);
|
|
235 |
| else |
|
236 |
76
| try {
|
|
237 |
76
| _contentsDocPane.setPage(indexUrl);
|
|
238 |
0
| if (_baseURL != null) ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
|
|
239 |
| } |
|
240 |
| catch (IOException ioe) { |
|
241 |
| |
|
242 |
0
| _displayContentsError(indexUrl, ioe);
|
|
243 |
| } |
|
244 |
| |
|
245 |
0
| if (introUrl == null) _displayMainError(null);
|
|
246 |
| else { |
|
247 |
76
| _history = new HistoryList(introUrl);
|
|
248 |
76
| _displayPage(introUrl);
|
|
249 |
76
| _displayPage(introUrl);
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
76
| setSize(FRAME_WIDTH, FRAME_HEIGHT);
|
|
254 |
76
| Utilities.setPopupLoc(this, null);
|
|
255 |
| |
|
256 |
76
| initDone();
|
|
257 |
| } |
|
258 |
| |
|
259 |
| |
|
260 |
0
| protected void _hideNavigationPane() {
|
|
261 |
0
| _splitPaneHolder.remove(_splitPane);
|
|
262 |
0
| _splitPaneHolder.add(_mainScroll);
|
|
263 |
| } |
|
264 |
| |
|
265 |
0
| private void _resetMainPane() {
|
|
266 |
0
| _linkError = false;
|
|
267 |
| |
|
268 |
0
| _mainDocPane = new JEditorPane();
|
|
269 |
0
| _mainDocPane.setEditable(false);
|
|
270 |
0
| for (int i = 0; i < _hyperlinkListeners.size(); i++) {
|
|
271 |
0
| _mainDocPane.addHyperlinkListener(_hyperlinkListeners.get(i));
|
|
272 |
| } |
|
273 |
0
| _displayPage(_lastURL);
|
|
274 |
| |
|
275 |
0
| _splitPane.setRightComponent(new BorderlessScrollPane(_mainDocPane));
|
|
276 |
0
| _splitPane.setDividerLocation(LEFT_PANEL_WIDTH);
|
|
277 |
| } |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
152
| private void _displayPage(URL url) {
|
|
283 |
0
| if (url == null) return;
|
|
284 |
152
| try {
|
|
285 |
152
| _mainDocPane.setPage(url);
|
|
286 |
152
| if (_baseURL != null) {
|
|
287 |
0
| ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
|
|
288 |
| } |
|
289 |
152
| _lastURL = url;
|
|
290 |
| } |
|
291 |
| catch (IOException ioe) { |
|
292 |
0
| String path = url.getPath();
|
|
293 |
0
| try {
|
|
294 |
0
| URL newURL = new URL(_baseURL + path);
|
|
295 |
0
| _mainDocPane.setPage(newURL);
|
|
296 |
0
| if (_baseURL != null) {
|
|
297 |
0
| ((HTMLDocument)_contentsDocPane.getDocument()).setBase(_baseURL);
|
|
298 |
| } |
|
299 |
0
| _lastURL = newURL;
|
|
300 |
| } |
|
301 |
| catch (IOException ioe2) { |
|
302 |
| |
|
303 |
0
| _displayMainError(url, ioe2);
|
|
304 |
| |
|
305 |
| } |
|
306 |
| } |
|
307 |
| } |
|
308 |
| |
|
309 |
| |
|
310 |
0
| private void _displayMainError(URL url) {
|
|
311 |
0
| if (!_linkError) {
|
|
312 |
0
| _linkError = true;
|
|
313 |
0
| _mainDocPane.setText(getErrorText(url));
|
|
314 |
| } |
|
315 |
0
| else _resetMainPane();
|
|
316 |
| } |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
0
| private void _displayMainError(URL url, Exception ex) {
|
|
321 |
0
| if (!_linkError) {
|
|
322 |
0
| _linkError = true;
|
|
323 |
0
| _mainDocPane.setText(getErrorText(url) + "\n" + ex);
|
|
324 |
| } |
|
325 |
0
| else _resetMainPane();
|
|
326 |
| } |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
0
| private void _displayContentsError(URL url) {
|
|
332 |
0
| _contentsDocPane.setText(getErrorText(url));
|
|
333 |
| } |
|
334 |
| |
|
335 |
| |
|
336 |
0
| private void _displayContentsError(URL url, Exception ex) {
|
|
337 |
0
| _contentsDocPane.setText(getErrorText(url) + "\n" + ex);
|
|
338 |
| } |
|
339 |
| |
|
340 |
| |
|
341 |
0
| protected String getErrorText(URL url) {
|
|
342 |
0
| return "Could not load the specified URL: " + url;
|
|
343 |
| } |
|
344 |
| |
|
345 |
0
| public void jumpTo(URL url) {
|
|
346 |
0
| _history = new HistoryList(url,_history);
|
|
347 |
| |
|
348 |
0
| _backAction.setEnabled(true);
|
|
349 |
0
| _forwardAction.setEnabled(false);
|
|
350 |
| |
|
351 |
0
| _displayPage(url);
|
|
352 |
| } |
|
353 |
| } |