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  
21  package base.jdbs.ui.dialog;
22  
23  import java.awt.BorderLayout;
24  import java.awt.Color;
25  import java.awt.FlowLayout;
26  import java.awt.GridLayout;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.io.File;
30  
31  import javax.swing.JButton;
32  import javax.swing.JDialog;
33  import javax.swing.JLabel;
34  import javax.swing.JPanel;
35  import javax.swing.JTextField;
36  import javax.swing.border.TitledBorder;
37  
38  import org.apache.log4j.Logger;
39  
40  import base.jdbs.ConfigurationManager;
41  
42  /***
43   * @author skunk
44   * 
45   */
46  @SuppressWarnings("serial")
47  public class RepositorySetupDialog extends JDialog {
48  
49  	private static final transient Logger logger = Logger
50  			.getLogger(RepositorySetupDialog.class.getName());
51  
52  	private JPanel locationPanel;
53  
54  	private JTextField locationTextField;
55  
56  	private JButton locationButton;
57  
58  	private JPanel declaredAvailableSpacePanel;
59  
60  	private JTextField sizeTextField;
61  
62  	private JPanel buttonPanel;
63  
64  	private JButton saveButton;
65  
66  	public RepositorySetupDialog() {
67  		initialize();
68  	}
69  
70  	protected void initialize() {
71  		this.setTitle("Repository Setup");
72  		this.setSize(640, 250);
73  		JPanel topPanel = new JPanel();
74  		topPanel.setLayout(new GridLayout(2, 1));
75  		topPanel.add(this.getLocationPanel());
76  		topPanel.add(this.getDeclaredAvailableSpacePanel());
77  		this.setLayout(new BorderLayout());
78  		this.add(topPanel, BorderLayout.CENTER);
79  		this.add(this.getButtonPanel(), BorderLayout.SOUTH);
80  
81  	}
82  
83  	/***
84  	 * @return Returns the locationTextField.
85  	 */
86  	protected JTextField getLocationTextField() {
87  		if (this.locationTextField == null) {
88  			this.locationTextField = new JTextField();
89  			this.locationTextField.setEditable(false);
90  			this.locationTextField
91  					.setText(ConfigurationManager.getInstance().getRepository()
92  							.getLocation() != null ? ConfigurationManager
93  							.getInstance().getRepository().getLocation()
94  							.getAbsolutePath()
95  							: "The Repository's is not yet configured. Select a repository by pressing the Choose button!");
96  		}
97  		return locationTextField;
98  	}
99  
100 	/***
101 	 * @return Returns the locationPanel.
102 	 */
103 	protected JPanel getLocationPanel() {
104 		if (this.locationPanel == null) {
105 			this.locationPanel = new JPanel();
106 			this.locationPanel
107 					.setBorder(new TitledBorder("Repository Location"));
108 			this.locationPanel.setLayout(new BorderLayout());
109 
110 			this.locationPanel.add(this.getLocationTextField(),
111 					BorderLayout.CENTER);
112 			JPanel panel = new JPanel();
113 			panel.setLayout(new BorderLayout());
114 			panel.add(this.getLocationButton(), BorderLayout.EAST);
115 			this.locationPanel.add(panel, BorderLayout.SOUTH);
116 		}
117 		return locationPanel;
118 	}
119 
120 	/***
121 	 * @return Returns the declaredAvailableSpacePanel.
122 	 */
123 	protected JPanel getDeclaredAvailableSpacePanel() {
124 		if (this.declaredAvailableSpacePanel == null) {
125 			this.declaredAvailableSpacePanel = new JPanel();
126 			this.declaredAvailableSpacePanel.setBorder(new TitledBorder(
127 					"Declared Repository Size"));
128 			this.declaredAvailableSpacePanel.setLayout(new FlowLayout());
129 			this.declaredAvailableSpacePanel.add(this.getSizeTextField());
130 			this.declaredAvailableSpacePanel.add(new JLabel("Mb"));
131 		}
132 		return declaredAvailableSpacePanel;
133 	}
134 
135 	/***
136 	 * @return Returns the locationButton.
137 	 */
138 	protected JButton getLocationButton() {
139 		if (this.locationButton == null) {
140 			this.locationButton = new JButton("Choose");
141 			this.locationButton.addActionListener(new ActionListener() {
142 				public void actionPerformed(ActionEvent arg0) {
143 					javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
144 					chooser
145 							.setDialogTitle("Select the JDBS repository folder...");
146 					chooser
147 							.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
148 					chooser.setApproveButtonText("Select");
149 					chooser.setMultiSelectionEnabled(true);
150 
151 					if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) {
152 						File repositoryLocation = chooser.getSelectedFile();
153 						getLocationTextField().setText(
154 								repositoryLocation.getAbsolutePath());
155 						getLocationTextField().setBackground(Color.WHITE);
156 					}
157 				}
158 
159 			});
160 		}
161 		return locationButton;
162 	}
163 
164 	/***
165 	 * @return Returns the sizeTextField.
166 	 */
167 	protected JTextField getSizeTextField() {
168 		if (this.sizeTextField == null) {
169 			this.sizeTextField = new JTextField();
170 			this.sizeTextField.setSize(150, 25);
171 			this.sizeTextField.setPreferredSize(this.sizeTextField.getSize());
172 			this.sizeTextField
173 					.setToolTipText("Note that a declared repository size is not a real quantity of available free space on your hard drive! Try to select an appropriate value in Mega Bytes anyway!");
174 			this.sizeTextField.setText(""
175 					+ ConfigurationManager.getInstance().getRepository()
176 							.getDeclaredAvailableSpace());
177 		}
178 		return sizeTextField;
179 	}
180 
181 	/***
182 	 * @return Returns the buttonPanel.
183 	 */
184 	protected JPanel getButtonPanel() {
185 		if (this.buttonPanel == null) {
186 			this.buttonPanel = new JPanel();
187 			this.buttonPanel.setLayout(new GridLayout(1, 3));
188 			this.buttonPanel.add(new JLabel());
189 			this.buttonPanel.add(this.getSaveButton());
190 			this.buttonPanel.add(new JLabel());
191 
192 		}
193 		return buttonPanel;
194 	}
195 
196 	/***
197 	 * @return Returns the saveButton.
198 	 */
199 	protected JButton getSaveButton() {
200 		if (this.saveButton == null) {
201 			this.saveButton = new JButton("Save");
202 			this.saveButton.addActionListener(new ActionListener() {
203 
204 				public void actionPerformed(ActionEvent arg0) {
205 					String repositoryLocation = getLocationTextField()
206 							.getText();
207 					if (!new File(repositoryLocation).exists()) {
208 						getLocationTextField().setBackground(Color.RED);
209 						return;
210 					} else
211 						getLocationTextField().setBackground(Color.WHITE);
212 
213 					int repositoryDeclaredAvailableSpace = 0;
214 					try {
215 						repositoryDeclaredAvailableSpace = Integer
216 								.parseInt(getSizeTextField().getText());
217 						if (repositoryDeclaredAvailableSpace < 0) {
218 							getSizeTextField().setBackground(Color.RED);
219 						} else
220 							getSizeTextField().setBackground(Color.WHITE);
221 					} catch (Exception ex) {
222 						getSizeTextField().setBackground(Color.RED);
223 						return;
224 					}
225 					ConfigurationManager.getInstance().getRepository()
226 							.setLocation(new File(repositoryLocation));
227 					ConfigurationManager.getInstance().getRepository()
228 							.setDeclaredAvailableSpace(
229 									repositoryDeclaredAvailableSpace);
230 					setVisible(false);
231 				}
232 
233 			});
234 		}
235 		return saveButton;
236 	}
237 
238 }