/Users/lyon/j4p/src/gui/browser/BrowserLogic.java

1    package gui.browser; 
2     
3    import javax.swing.*; 
4    import javax.swing.event.HyperlinkEvent; 
5    import java.awt.*; 
6    import java.io.File; 
7    import java.net.MalformedURLException; 
8    import java.net.URL; 
9     
10   /** 
11    */ 
12    
13    
14   public class BrowserLogic { 
15       private Main b; 
16       private String fileName; 
17       LinkQueue lq = new LinkQueue(); 
18    
19       public BrowserLogic(Main _b) { 
20           b = _b; 
21       } 
22    
23    
24       /** 
25        *  When user click on "File:open", 
26        *  the openHtmlFile is invoked to open the selected gui.html file. 
27        */ 
28       public void openHtmlFile() { 
29           File file = getInputFile("Select File"); 
30           fileName = file.getAbsolutePath(); 
31           String urlS = "file://localhost/" + fileName; 
32           processTextField(urlS); 
33       } 
34    
35       /** 
36        *  When user click on "Forward", 
37        *  the forward method is invoked and it then calls 
38        *  the LinkQueue class. 
39        */ 
40    
41       public void forward() { 
42           setUrlNoException(lq.next()); 
43       } 
44    
45       /** 
46        *  When user click on "Backward", 
47        *  the Backward method is invoked and it then calls 
48        *  the LinkQueue class. 
49        */ 
50    
51       public void backward() { 
52           setUrlNoException(lq.previous()); 
53       } 
54    
55       public File getInputFile(String title) { 
56           FileDialog fd = new FileDialog(new Frame(), title); 
57           fd.setVisible(true); 
58           return new File(fd.getDirectory() + 
59                   fd.getFile()); 
60       } 
61    
62       public void setUrlNoException(URL u) { 
63           updateTextField(u.toString()); 
64           new ThreadedLoad(b.getHtmlPane(), u); 
65       } 
66    
67       public void updateTextField(String urlS) { 
68           b.setTextField(urlS); 
69       } 
70    
71       public void processTextField(String s) { 
72    
73           try { 
74               URL u = new URL(s); 
75               setUrl(u); 
76           } catch (MalformedURLException e) { 
77               e.printStackTrace(); 
78           } 
79    
80       } 
81    
82       /** 
83        *  hyperlinkPage method get invoked from BrowserListener 
84        *  to update the URL textField, 
85        *  to add the new url to the LinkQueue and 
86        *  to start an external thread that gui.run jep.setPage(url), 
87        */ 
88    
89       public void hyperlinkPage(JEditorPane jep, HyperlinkEvent e, JTextField tf) { 
90           URL url = e.getURL(); 
91           lq.enQueue(url); 
92           updateTextField(url.toString()); 
93           try { 
94               ThreadedLoad tl = new ThreadedLoad(jep, url); 
95           } catch (Throwable t) { 
96               System.out.println("could not open:" + url); 
97           } 
98       } 
99    
100      /** 
101       *  setUrl method is invoked to start an external thread that 
102       *  gui.run jep.setPage(url), 
103       *  update the URL textField and 
104       *  add the new url to the LinkQueue and 
105       */ 
106   
107   
108      public void setUrl(URL u) { 
109          new ThreadedLoad(b.getHtmlPane(), u); 
110          updateTextField(u.toString()); 
111          lq.enQueue(u); 
112      } 
113  }