View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.output;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Color;
26  import java.net.URISyntaxException;
27  
28  import javax.swing.JEditorPane;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.JScrollPane;
32  import javax.swing.event.HyperlinkEvent;
33  import javax.swing.event.HyperlinkListener;
34  
35  import org.apache.log4j.Logger;
36  import org.xmlhammer.gui.util.ExternalApplicationLauncher;
37  
38  /***
39   * Log Panel.
40   * 
41   * @version $Revision$, $Date$
42   * @author Edwin Dankert <edankert@gmail.com>
43   */
44  
45  public class JavaPanel extends SearchablePanel {
46  	private static final long serialVersionUID = -5024093857377606353L;
47  
48  	private JEditorPane srcPane = null;
49      private JPanel dummy = null;
50  
51  	/***
52  	 * Constructs a new Input Panel.
53  	 */
54  	public JavaPanel() {
55  		super(new BorderLayout());
56  
57          dummy = new JPanel(new BorderLayout());
58          dummy.setBackground(Color.white);
59          dummy.add(new JLabel("Loading Java Source ..."), BorderLayout.NORTH);
60          add(dummy, BorderLayout.CENTER);
61  	}
62      
63      public void setSource(SourceBuilder source) {
64          if (isShowing()) {
65              getSourcePane().setText(source.toString());
66              getSourcePane().setCaretPosition(0);
67              dummy.setVisible(false);
68          } else {
69              Logger.getLogger(getClass()).debug("setSource() [invisible]");
70          }
71      }
72      
73      public boolean search(String search, boolean matchCase, boolean forward) {
74          return search(getSourcePane(), search, matchCase, forward);
75      }
76  
77      public JEditorPane getSourcePane() {
78          Logger.getLogger(getClass().getName()).debug("getSourcePane()");
79          if (srcPane == null) {
80              srcPane = new JEditorPane("text/html", "Loading Java Source ...");
81              srcPane.addHyperlinkListener(new DefaultHyperlinkListener());
82              srcPane.setEditable(false);
83              JScrollPane scrollPane = new JScrollPane( srcPane,
84                                        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
85                                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
86              add( scrollPane, BorderLayout.CENTER);
87          }
88          
89          return srcPane;
90      }
91      
92      class DefaultHyperlinkListener implements HyperlinkListener {
93          public void hyperlinkUpdate(HyperlinkEvent event) {
94              if (event.getEventType() == HyperlinkEvent.EventType.ENTERED) {
95                  srcPane.setToolTipText(event.getURL().toString());
96              } else if (event.getEventType() == HyperlinkEvent.EventType.EXITED) {
97                  srcPane.setToolTipText("");
98              } else if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
99                  try {
100                     ExternalApplicationLauncher.getInstance().browse(event.getURL().toURI());
101                 } catch (URISyntaxException e1) {
102                     Logger.getLogger(getClass()).debug(event);
103                 }
104             }
105         }
106     }    
107 }