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.Toolkit;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33 import javax.swing.JTextArea;
34
35 import org.apache.log4j.Logger;
36
37 import ui.Messages;
38
39 @SuppressWarnings("serial")
40 public class TextFileDialog extends JDialog {
41
42 private static final transient Logger logger = Logger
43 .getLogger(TextFileDialog.class.getName());
44
45 private JTextArea textArea;
46
47 private JScrollPane textAreaScrollPane;
48
49 private JPanel buttonPanel;
50
51 private JButton closeButton;
52
53 private String text;
54
55 public TextFileDialog(String text) {
56 this.text = text;
57 initialize();
58 }
59
60 protected void initialize() {
61 this.setSize(550, 600);
62 this.setResizable(false);
63
64 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
65 Dimension frameSize = this.getSize();
66 if (frameSize.height > screenSize.height) {
67 frameSize.height = screenSize.height;
68 }
69 if (frameSize.width > screenSize.width) {
70 frameSize.width = screenSize.width;
71 }
72 this.setLocation((screenSize.width - frameSize.width) / 2,
73 (screenSize.height - frameSize.height) / 2);
74 this.setLayout(new BorderLayout());
75 this.add(getTextAreaScrollPane(), BorderLayout.CENTER);
76 this.add(getButtonPanel(), BorderLayout.SOUTH);
77 this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
78 }
79
80 /***
81 * @return Returns the buttonPanel.
82 */
83 protected JPanel getButtonPanel() {
84 if (buttonPanel == null) {
85 buttonPanel = new JPanel();
86 buttonPanel.setLayout(new FlowLayout());
87 buttonPanel.add(getCloseButton());
88 }
89 return buttonPanel;
90 }
91
92 /***
93 * @return Returns the closeButton.
94 */
95 protected JButton getCloseButton() {
96 if (closeButton == null) {
97 closeButton = new JButton(Messages.getString("button.close"));
98 closeButton.addActionListener(new ActionListener() {
99 public void actionPerformed(ActionEvent arg0) {
100 logger.debug("actionPerformed closeButton");
101 setVisible(false);
102 }
103 });
104 }
105 return closeButton;
106 }
107
108 /***
109 * @return Returns the textAreaScrollPane.
110 */
111 protected JScrollPane getTextAreaScrollPane() {
112 if (textAreaScrollPane == null) {
113 textAreaScrollPane = new JScrollPane();
114 textAreaScrollPane.setViewportView(getTextArea());
115 textAreaScrollPane
116 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
117 textAreaScrollPane
118 .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
119 }
120 return textAreaScrollPane;
121 }
122
123 /***
124 * @return Returns the textArea.
125 */
126 protected JTextArea getTextArea() {
127 if (textArea == null) {
128 textArea = new JTextArea();
129 textArea.setEditable(false);
130 textArea.setText(text);
131 textArea.setCaretPosition(0);
132 textArea.setWrapStyleWord(true);
133 }
134 return textArea;
135 }
136 }