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.command.information;
21  
22  import java.awt.Dimension;
23  import java.awt.Toolkit;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  
28  import org.apache.log4j.Logger;
29  
30  import ui.Messages;
31  import ui.command.Command;
32  import ui.dialog.TextFileDialog;
33  import base.InternetCafe;
34  
35  public class ShowTextFileDialogCommand extends Command {
36  
37  	private static final transient Logger logger = Logger
38  			.getLogger(ShowTextFileDialogCommand.class.getName());
39  
40  	private String textFileDialogTitle;
41  
42  	private String textFileContent;
43  
44  	private String textFilePath;
45  
46  	public ShowTextFileDialogCommand(String textFileDialogTitle,
47  			String textFilePath) {
48  		this.textFileDialogTitle = textFileDialogTitle;
49  		this.textFilePath = textFilePath;
50  	}
51  
52  	/*
53  	 * (non-Javadoc)
54  	 * 
55  	 * @see ui.command.Command#prologo()
56  	 */
57  	@Override
58  	protected void prologo() {
59  		InternetCafe.setStatusBarMessage(Messages.getString("command.showtextfiledialogcommand.displaying")+" " + textFileDialogTitle+"..."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
60  		try {
61  			textFileContent = openFile(textFilePath);
62  			setStatus(EXECUTE_STATUS);
63  		} catch (Exception ex) {
64  			logger.error(ex.getMessage());
65  			ex.printStackTrace();
66  			setStatus(ERROR_STATUS);
67  		}
68  	}
69  
70  	/*
71  	 * (non-Javadoc)
72  	 * 
73  	 * @see ui.command.Command#execution()
74  	 */
75  	@Override
76  	protected void execution() throws Exception {
77  		switch (getStatus()) {
78  		case ABORT_STATUS:
79  			break;
80  		case VETOED_STATUS:
81  			break;
82  		case EXECUTE_STATUS:
83  			TextFileDialog dialog = new TextFileDialog(textFileContent);
84  			dialog.setModal(true);
85  			dialog.setTitle(textFileDialogTitle);
86  			// Center the dialog
87  			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
88  			Dimension frameSize = dialog.getSize();
89  			if (frameSize.height > screenSize.height) {
90  				frameSize.height = screenSize.height;
91  			}
92  			if (frameSize.width > screenSize.width) {
93  				frameSize.width = screenSize.width;
94  			}
95  			dialog.setLocation((screenSize.width - frameSize.width) / 2,
96  					(screenSize.height - frameSize.height) / 2);
97  			dialog.setVisible(true);
98  			setStatus(SUCCESS_STATUS);
99  			break;
100 		}
101 	}
102 
103 	public static String openFile(String fileName) {
104 		String result = ""; //$NON-NLS-1$
105 		try {
106 			File file = new File(fileName);
107 			if (!file.exists())
108 				return ""; //$NON-NLS-1$
109 			// Read the contents of the file into a byte[] object.
110 			FileInputStream input_file = new FileInputStream(fileName);
111 			byte[] file_data = new byte[(int) file.length()];
112 			input_file.read(file_data);
113 			result = new String(file_data);
114 		} catch (IOException ex) {
115 			ex.printStackTrace();
116 		}
117 		return result;
118 	}
119 
120 }