View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.dialog;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Dimension;
24  import java.awt.FlowLayout;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.IOException;
30  
31  import javax.swing.JButton;
32  import javax.swing.JDialog;
33  import javax.swing.JPanel;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTextArea;
36  
37  import org.apache.log4j.Logger;
38  
39  import ui.Messages;
40  import base.ConfigurationManager;
41  
42  @SuppressWarnings("serial") //$NON-NLS-1$
43  public class LicenseDialog extends JDialog {
44  
45  	private static final transient Logger logger = Logger
46  			.getLogger(LicenseDialog.class.getName());
47  
48  	private JTextArea licenseTextArea;
49  
50  	private JScrollPane licenseScrollPane;
51  
52  	private JPanel buttonPanel;
53  
54  	private JButton closeButton;
55  
56  	public LicenseDialog() {
57  		initialize();
58  	}
59  
60  	protected void initialize() {
61  		this.setSize(new Dimension(650, 600));
62  		this.setLayout(new BorderLayout());
63  		this.add(getLicenseScrollPane(), BorderLayout.CENTER);
64  		this.add(getButtonPanel(), BorderLayout.SOUTH);
65  		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
66  	}
67  
68  	/***
69  	 * @return Returns the buttonPanel.
70  	 */
71  	protected JPanel getButtonPanel() {
72  		if (buttonPanel == null) {
73  			buttonPanel = new JPanel();
74  			buttonPanel.setLayout(new FlowLayout());
75  			buttonPanel.add(getCloseButton());
76  		}
77  		return buttonPanel;
78  	}
79  
80  	/***
81  	 * @return Returns the closeButton.
82  	 */
83  	protected JButton getCloseButton() {
84  		if (closeButton == null) {
85  			closeButton = new JButton(Messages.getString("button.close")); //$NON-NLS-1$
86  			closeButton.addActionListener(new ActionListener() {
87  				public void actionPerformed(ActionEvent arg0) {
88  					logger.debug("actionPerformed closeButton"); //$NON-NLS-1$
89  					setVisible(false);
90  				}
91  			});
92  		}
93  		return closeButton;
94  	}
95  
96  	/***
97  	 * @return Returns the licenseScrollPane.
98  	 */
99  	protected JScrollPane getLicenseScrollPane() {
100 		if (licenseScrollPane == null) {
101 			licenseScrollPane = new JScrollPane();
102 			licenseScrollPane.setViewportView(getLicenseTextArea());
103 			licenseScrollPane
104 					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
105 			licenseScrollPane
106 					.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
107 		}
108 		return licenseScrollPane;
109 	}
110 
111 	/***
112 	 * @return Returns the licenseTextArea.
113 	 */
114 	protected JTextArea getLicenseTextArea() {
115 		if (licenseTextArea == null) {
116 			licenseTextArea = new JTextArea();
117 			licenseTextArea.setEditable(false);
118 			licenseTextArea
119 					.setText(openFile(ConfigurationManager.LICENSE_FILE));
120 			licenseTextArea.setCaretPosition(0);
121 			licenseTextArea.setWrapStyleWord(true);
122 		}
123 		return licenseTextArea;
124 	}
125 
126 	public static String openFile(String fileName) {
127 		String result = ""; //$NON-NLS-1$
128 		try {
129 			File file = new File(fileName);
130 			if (!file.exists())
131 				return ""; //$NON-NLS-1$
132 			// Read the contents of the file into a byte[] object.
133 			FileInputStream input_file = new FileInputStream(fileName);
134 			byte[] file_data = new byte[(int) file.length()];
135 			input_file.read(file_data);
136 			result = new String(file_data);
137 		} catch (IOException ex) {
138 			ex.printStackTrace();
139 		}
140 		return result;
141 	}
142 
143 }