C:\Java WorkShop\Lexx\src\lexx\plugins\javadoc\JavadocViewer.java

1    package lexx.plugins.javadoc; 
2     
3    import java.io.File; 
4    import java.net.MalformedURLException; 
5    import java.net.URL; 
6    import java.util.Vector; 
7     
8    import java.awt.BorderLayout; 
9    import java.awt.event.ActionEvent; 
10   import javax.swing.JButton; 
11   import javax.swing.JMenu; 
12   import javax.swing.JMenuBar; 
13   import javax.swing.JMenuItem; 
14   import javax.swing.JPanel; 
15   import javax.swing.JScrollPane; 
16   import javax.swing.JToolBar; 
17    
18   import calpa.html.CalHTMLPane; 
19   import lexx.images.Icons; 
20    
21   /** 
22    * <p>This loads a process and controls the output from the screen</p> 
23    * <p>This displays the javadoc from the source code or from the suns javadoc 
24    * website. This is achieved by using Capla Web Browser</p> 
25    * <p>It also includes the option to view the current java file that is being 
26    * displayed</p> 
27    * <p>It also includes a back/forward buttons 
28    * <p>Copyright (c) 2002-2003</p> 
29    * @since 20/03/2003 
30    * @author Mohammed Imran 
31    * @version 1.0 
32    */ 
33    
34   public final class JavadocViewer extends JPanel 
35   { 
36     private final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(this. 
37         getClass()); 
38     private final BorderLayout borderLayout1 = new BorderLayout(); 
39     private final JMenuBar menuBar = new JMenuBar(); 
40     private final Vector urlList = new Vector(); 
41     private int currentPos; 
42     private CalHTMLPane pane; 
43     private final JPanel buttonPanel = new JPanel(); 
44     private final JButton backButton = new JButton(Icons.getImageIcon( 
45         "Back24.gif")); 
46     private final JButton forwardButton = new JButton(Icons.getImageIcon( 
47         "Forward24.gif")); 
48     private final JButton homeButton = new JButton(Icons.getImageIcon( 
49         "homeFolder.gif")); 
50     private final JButton currentButton = new JButton(Icons.getImageIcon( 
51         "History24.gif")); 
52     private final JButton jdkButton = new JButton(Icons.getImageIcon( 
53         "duke_wave_shadow.gif")); 
54     private final JMenu favouritesMenu = new JMenu(); 
55     private final JMenuItem developeMenu = new JMenuItem(); 
56     private final JMenuItem jguruMenu = new JMenuItem(); 
57    
58     public JavadocViewer() 
59     { 
60       try 
61       { 
62         setup(); 
63         pane = new CalHTMLPane(); 
64         this.add(pane); 
65    
66         backButton.setEnabled(false); 
67         forwardButton.setEnabled(false); 
68         String address = JavaDoc.getHomeAddress(); 
69         pane.showHTMLDocument(new URL(address)); 
70         urlList.add(address); 
71         currentPos = urlList.indexOf(address); 
72       } 
73       catch(Exception ex) 
74       { 
75         log.fatal(ex, ex); 
76       } 
77     } 
78    
79     private void setup() throws Exception 
80     { 
81       this.setLayout(borderLayout1); 
82       backButton.setText("Back"); 
83       backButton.addActionListener(new java.awt.event.ActionListener() 
84       { 
85         public void actionPerformed(ActionEvent e) 
86         { 
87           backButton_actionPerformed(); 
88         } 
89       }); 
90       forwardButton.setText("Forward"); 
91       forwardButton.addActionListener(new java.awt.event.ActionListener() 
92       { 
93         public void actionPerformed(ActionEvent e) 
94         { 
95           forwardButton_actionPerformed(); 
96         } 
97       }); 
98       homeButton.setText("Home"); 
99       homeButton.addActionListener(new java.awt.event.ActionListener() 
100      { 
101        public void actionPerformed(ActionEvent e) 
102        { 
103          homeButton_actionPerformed(); 
104        } 
105      }); 
106      currentButton.setText("Current Class"); 
107      currentButton.addActionListener(new java.awt.event.ActionListener() 
108      { 
109        public void actionPerformed(ActionEvent e) 
110        { 
111          currentButton_actionPerformed(); 
112        } 
113      }); 
114      jdkButton.setText("JDK API"); 
115      jdkButton.addActionListener(new java.awt.event.ActionListener() 
116      { 
117        public void actionPerformed(ActionEvent e) 
118        { 
119          jdkButton_actionPerformed(); 
120        } 
121      }); 
122      favouritesMenu.setText("Favourites"); 
123      developeMenu.setText("Java Developer Forum"); 
124      developeMenu.addActionListener(new java.awt.event.ActionListener() 
125      { 
126        public void actionPerformed(ActionEvent e) 
127        { 
128          developeMenu_actionPerformed(); 
129        } 
130      }); 
131      jguruMenu.setText("jGuru"); 
132      jguruMenu.addActionListener(new java.awt.event.ActionListener() 
133      { 
134        public void actionPerformed(ActionEvent e) 
135        { 
136          jguruMenu_actionPerformed(); 
137        } 
138      }); 
139   
140      JToolBar bar = new JToolBar("Navigation"); 
141      buttonPanel.add(bar); 
142   
143      bar.add(backButton, null); 
144      bar.add(forwardButton, null); 
145      bar.add(homeButton, null); 
146      bar.add(jdkButton, null); 
147      bar.add(currentButton, null); 
148      bar.add(menuBar, null); 
149   
150  //    buttonPanel.add(backButton, null); 
151  //    buttonPanel.add(forwardButton, null); 
152  //    buttonPanel.add(homeButton, null); 
153  //    buttonPanel.add(jdkButton, null); 
154  //    buttonPanel.add(currentButton, null); 
155  //    buttonPanel.add(menuBar, null); 
156      this.add(new JScrollPane(pane), BorderLayout.CENTER); 
157      this.add(buttonPanel, BorderLayout.NORTH); 
158      menuBar.add(favouritesMenu); 
159      favouritesMenu.add(developeMenu); 
160      favouritesMenu.add(jguruMenu); 
161    } 
162   
163    private void backButton_actionPerformed() 
164    { 
165      if(currentPos < 1) 
166      { 
167        return; 
168      } 
169   
170      try 
171      { 
172        currentPos--; 
173        String address = urlList.get(currentPos).toString(); 
174        pane.showHTMLDocument(new URL(address)); 
175      } 
176      catch(MalformedURLException ex) 
177      { 
178        log.fatal(ex, ex); 
179      } 
180      this.check(); 
181    } 
182   
183    /** 
184     * Loads a webpage up 
185     * @param address web adress 
186     */ 
187    public final void loadPage(String address) 
188    { 
189      if(JavaDoc.checkAddress(address)) 
190      { 
191        try 
192        { 
193          pane.showHTMLDocument(new URL(address)); 
194          for(int i = currentPos + 1; i < urlList.size(); i++) 
195          { 
196            urlList.remove(i); 
197          } 
198          urlList.add(address); 
199          currentPos++; 
200        } 
201        catch(MalformedURLException ex) 
202        { 
203          log.fatal(ex, ex); 
204        } 
205      } 
206      this.check(); 
207    } 
208   
209    private void forwardButton_actionPerformed() 
210    { 
211      if(currentPos + 1 >= urlList.size()) 
212      { 
213        return; 
214      } 
215   
216      try 
217      { 
218        currentPos++; 
219        String url = urlList.get(currentPos).toString(); 
220        pane.showHTMLDocument(new URL(url)); 
221        this.check(); 
222      } 
223      catch(MalformedURLException ex) 
224      { 
225        log.fatal(ex, ex); 
226      } 
227    } 
228   
229    private void homeButton_actionPerformed() 
230    { 
231      this.loadPage(JavaDoc.getHomeAddress()); 
232    } 
233   
234    private void currentButton_actionPerformed() 
235    { 
236      lexx.gui.editor.EditorPanel ep = ( (lexx.gui.EditorFrame) lexx.utils.Config. 
237                                        getFrameForDialog()).getEditorPanel(); 
238      if(ep != null) 
239      { 
240        File f = ep.getFileName(); 
241        if(f != null) 
242        { 
243          this.loadPage(JavaDoc.getClassURL(f.toString())); 
244        } 
245      } 
246    } 
247   
248    private void check() 
249    { 
250      /*    boolean currentOn = false; 
251          lexx.gui.editor.EditorPanel ep = ((lexx.gui.EditorFrame)lexx.utils.Config.getFrameForDialog()).getEditorPanel(); 
252          if( ep != null) 
253          { 
254            File f = ep.getFileName(); 
255            if( f != null) 
256            { 
257              currentOn = true; 
258            } 
259          } 
260          currentButton.setEnabled(currentOn);*/ 
261   
262      if(currentPos < 1) 
263      { 
264        backButton.setEnabled(false); 
265      } 
266      else 
267      { 
268        backButton.setEnabled(true); 
269   
270      } 
271      if(currentPos + 1 >= urlList.size()) 
272      { 
273        forwardButton.setEnabled(false); 
274      } 
275      else 
276      { 
277        forwardButton.setEnabled(true); 
278      } 
279    } 
280   
281    private void jdkButton_actionPerformed() 
282    { 
283      this.loadPage(JavaDoc.getJDKaddress()); 
284    } 
285   
286    private void jguruMenu_actionPerformed() 
287    { 
288      this.loadPage("http://www.jguru.com"); 
289    } 
290   
291    private void developeMenu_actionPerformed() 
292    { 
293      this.loadPage("http://forum.java.sun.com"); 
294    } 
295  }