View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.panels;
17  
18  import james.SimSystem;
19  
20  import java.awt.BorderLayout;
21  import java.io.BufferedReader;
22  import java.io.FileReader;
23  import java.io.IOException;
24  
25  import javax.swing.JEditorPane;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.event.HyperlinkEvent;
29  import javax.swing.event.HyperlinkListener;
30  
31  import p3j.misc.gui.GUI;
32  
33  /**
34   * Panel to welcome the user. Loads file under html/index.html .
35   * 
36   * Created: August 23, 2008
37   * 
38   * @author Christina Bohk
39   * @author Roland Ewald
40   */
41  public class WelcomePanel extends JPanel implements HyperlinkListener {
42  
43  	/** Serialization ID. */
44  	private static final long serialVersionUID = -216475639669435678L;
45  
46  	/** Path to content file. */
47  	protected static final String CONTENT_FILE = "doc/index.html";
48  
49  	/** The html pane. */
50  	private JEditorPane htmlPane;
51  
52  	/**
53  	 * Default constructor.
54  	 */
55  	public WelcomePanel() {
56  		super();
57  		setLayout(new BorderLayout());
58  		htmlPane = new JEditorPane();
59  		htmlPane.setContentType("text/html");
60  		htmlPane.addHyperlinkListener(this);
61  		htmlPane.setEditable(false);
62  		String content = getContent();
63  		htmlPane.setText(content);
64  		this.add(new JScrollPane(htmlPane));
65  	}
66  
67  	/**
68  	 * Reads content from the HTML file specified in
69  	 * {@link WelcomePanel#CONTENT_FILE}.
70  	 * 
71  	 * @return the content of the file
72  	 */
73  	private String getContent() {
74  
75  		StringBuffer content = new StringBuffer();
76  
77  		BufferedReader reader = null;
78  		try {
79  			reader = new BufferedReader(new FileReader(CONTENT_FILE));
80  			String line = reader.readLine();
81  			while (line != null) {
82  				content.append(line);
83  				line = reader.readLine();
84  			}
85  		} catch (Exception ex) {
86  			SimSystem.report(ex);
87  			content.append("An error occurred:\n");
88  			content.append(ex);
89  		} finally {
90  			if (reader != null) {
91  				try {
92  					reader.close();
93  				} catch (IOException ex) {
94  					SimSystem.report(ex);
95  				}
96  			}
97  		}
98  
99  		return content.toString();
100 	}
101 
102 	@Override
103 	public void hyperlinkUpdate(HyperlinkEvent event) {
104 		if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
105 			try {
106 				htmlPane.setPage(event.getURL());
107 			} catch (IOException ioe) {
108 				GUI.printErrorMessage(this, "Could not open link.", ioe.getMessage(),
109 				    ioe);
110 			}
111 		}
112 	}
113 }