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.GridLayout;
25  import java.awt.Toolkit;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  
29  import javax.swing.ImageIcon;
30  import javax.swing.JButton;
31  import javax.swing.JDialog;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.border.TitledBorder;
35  
36  import org.apache.log4j.Logger;
37  
38  import ui.Messages;
39  import ui.command.CommandExecutor;
40  import ui.command.IO.SaveUserCommand;
41  import ui.command.IO.SaveUserDocumentCommand;
42  import ui.panel.DocumentPanel;
43  import base.user.Document;
44  import base.user.User;
45  
46  @SuppressWarnings("serial") //$NON-NLS-1$
47  public class DocumentDialog extends JDialog {
48  
49  	private static final transient Logger logger = Logger
50  			.getLogger(DocumentDialog.class.getName());
51  
52  	private JPanel contentPanel;
53  
54  	private DocumentPanel documentPanel;
55  
56  	private JPanel buttonPanel;
57  
58  	private JButton saveDocumentButton;
59  
60  	private JButton clearDocumentDataButton;
61  
62  	private final Document document;
63  
64  	private final User user;
65  
66  	private boolean createDocument = false;
67  
68  	public DocumentDialog(User user, Document document) {
69  		this.user = user;
70  		this.document = document;
71  		if (document == null)
72  			createDocument = true;
73  		initialize();
74  	}
75  
76  	public DocumentDialog(User user) {
77  		this.user = user;
78  		this.document = this.user.getDocument();
79  		createDocument = true;
80  		initialize();
81  	}
82  
83  	protected void initialize() {
84  		this.setResizable(false);
85  		this.setSize(550, 600);
86  		// Center the dialog
87  		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
88  		Dimension frameSize = this.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  		this.setLocation((screenSize.width - frameSize.width) / 2,
96  				(screenSize.height - frameSize.height) / 2);
97  
98  		this.setContentPane(getContentPanel());
99  		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
100 	}
101 
102 	/***
103 	 * @return Returns the documentPanel.
104 	 */
105 	protected DocumentPanel getDocumentPanel() {
106 		if (documentPanel == null) {
107 			documentPanel = new DocumentPanel(document);
108 		}
109 		return documentPanel;
110 	}
111 
112 	/***
113 	 * @return Returns the document.
114 	 */
115 	public Document getDocument() {
116 		return document;
117 	}
118 
119 	/***
120 	 * @return Returns the contentPanel.
121 	 */
122 	protected JPanel getContentPanel() {
123 		if (contentPanel == null) {
124 			contentPanel = new JPanel();
125 			contentPanel.setLayout(new BorderLayout());
126 			contentPanel.add(getDocumentPanel(), BorderLayout.CENTER);
127 			contentPanel.add(getButtonPanel(), BorderLayout.SOUTH);
128 		}
129 		return contentPanel;
130 	}
131 
132 	/***
133 	 * @return Returns the buttonPanel.
134 	 */
135 	protected JPanel getButtonPanel() {
136 		if (buttonPanel == null) {
137 			buttonPanel = new JPanel();
138 			TitledBorder titledBorder = new TitledBorder(Messages.getString("dialog.availableactions")); //$NON-NLS-1$
139 			buttonPanel.setBorder(titledBorder);
140 			buttonPanel.setLayout(new GridLayout(1, 4));
141 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
142 			buttonPanel.add(getSaveDocumentButton());
143 			buttonPanel.add(getClearDocumentDataButton());
144 			buttonPanel.add(new JLabel("")); //$NON-NLS-1$
145 		}
146 		return buttonPanel;
147 	}
148 
149 	/***
150 	 * @return Returns the clearDocumentDataButton.
151 	 */
152 	protected JButton getClearDocumentDataButton() {
153 		if (clearDocumentDataButton == null) {
154 			clearDocumentDataButton = new JButton(Messages.getString("button.clear")); //$NON-NLS-1$
155 			clearDocumentDataButton.setIcon(new ImageIcon(this.getClass()
156 					.getResource("/icon/16x16/actions/view-refresh.png"))); //$NON-NLS-1$
157 
158 			clearDocumentDataButton.addActionListener(new ActionListener() {
159 				public void actionPerformed(ActionEvent arg0) {
160 					logger.debug("actionPerformed clearDocumentDataButton"); //$NON-NLS-1$
161 					getDocumentPanel().clearDocumentData();
162 				}
163 			});
164 		}
165 		return clearDocumentDataButton;
166 	}
167 
168 	/***
169 	 * @return Returns the saveDocumentButton.
170 	 */
171 	protected JButton getSaveDocumentButton() {
172 		if (saveDocumentButton == null) {
173 			saveDocumentButton = new JButton(Messages.getString("button.save")); //$NON-NLS-1$
174 			saveDocumentButton.setIcon(new ImageIcon(this.getClass()
175 					.getResource("/icon/16x16/actions/document-save.png"))); //$NON-NLS-1$
176 
177 			saveDocumentButton.addActionListener(new ActionListener() {
178 				public void actionPerformed(ActionEvent arg0) {
179 					logger.debug("actionPerformed saveDocumentButton"); //$NON-NLS-1$
180 					SaveUserDocumentCommand saveUserDocumentCommand = new SaveUserDocumentCommand(
181 							getDocumentPanel(), document, createDocument);
182 					CommandExecutor.getInstance().executeCommand(
183 							saveUserDocumentCommand, true);
184 					if (saveUserDocumentCommand.getStatus() == SaveUserCommand.SUCCESS_STATUS)
185 						setVisible(false);
186 				}
187 			});
188 
189 		}
190 		return saveDocumentButton;
191 	}
192 }