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.panel;
21  
22  import java.awt.BorderLayout;
23  import java.awt.GridLayout;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.util.Date;
27  
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  import javax.swing.JCheckBox;
31  import javax.swing.JOptionPane;
32  import javax.swing.JPanel;
33  import javax.swing.JTextField;
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.information.SearchSessionCommand;
41  import ui.table.model.SessionTableModel;
42  
43  import com.toedter.calendar.JDateChooser;
44  
45  @SuppressWarnings("serial") //$NON-NLS-1$
46  public class SessionSearchPanel extends JPanel {
47  
48  	private static final transient Logger logger = Logger
49  			.getLogger(SessionSearchPanel.class.getName());
50  
51  	private JPanel containedTextPanel;
52  
53  	private JPanel datePanel;
54  
55  	private JPanel buttonPanel;
56  
57  	private JButton searchButton;
58  
59  	private JButton stopButton;
60  
61  	private JDateChooser dateChooser;
62  
63  	private JTextField containedTextTextField;
64  
65  	private JCheckBox sortByDateCheckBox;
66  
67  	private JCheckBox sortByTextCheckBox;
68  
69  	private final SessionTableModel sessionTableModel;
70  
71  	public SessionSearchPanel(SessionTableModel sessionTableModel) {
72  		this.sessionTableModel = sessionTableModel;
73  		initialize();
74  	}
75  
76  	protected void initialize() {
77  		this.setLayout(new GridLayout(1, 3));
78  		TitledBorder titledBorder = new TitledBorder(Messages.getString("panel.sessionsearchpanel.searchsession")); //$NON-NLS-1$
79  		this.setBorder(titledBorder);
80  		this.add(getContainedTextPanel(), null);
81  		this.add(getDatePanel(), null);
82  		this.add(getButtonPanel(), null);
83  	}
84  
85  	/***
86  	 * @return Returns the buttonPanel.
87  	 */
88  	protected JPanel getButtonPanel() {
89  		if (buttonPanel == null) {
90  			buttonPanel = new JPanel();
91  			TitledBorder titledBorder = new TitledBorder(Messages.getString("dialog.availableactions")); //$NON-NLS-1$
92  			buttonPanel.setBorder(titledBorder);
93  			buttonPanel.setLayout(new GridLayout(1, 2));
94  			buttonPanel.add(getSearchButton());
95  			buttonPanel.add(getStopButton());
96  
97  		}
98  		return this.buttonPanel;
99  	}
100 
101 	/***
102 	 * @return Returns the searchButton.
103 	 */
104 	protected JButton getSearchButton() {
105 		if (searchButton == null) {
106 			searchButton = new JButton(Messages.getString("button.search")); //$NON-NLS-1$
107 			searchButton.setIcon(new ImageIcon(this.getClass().getResource(
108 					"/icon/22x22/actions/system-search.png"))); //$NON-NLS-1$
109 			searchButton.addActionListener(new ActionListener() {
110 				public void actionPerformed(ActionEvent arg0) {
111 					logger.debug("actionPerformed searchButton"); //$NON-NLS-1$
112 					if (getSortByTextCheckBox().isSelected()
113 							&& getSortByDateCheckBox().isSelected()) {
114 						CommandExecutor.getInstance().executeCommand(
115 								new SearchSessionCommand(sessionTableModel,
116 										getContainedTextTextField().getText(),
117 										getDateChooser().getDate()), false);
118 					} else if (getSortByTextCheckBox().isSelected()) {
119 						CommandExecutor.getInstance().executeCommand(
120 								new SearchSessionCommand(sessionTableModel,
121 										getContainedTextTextField().getText(),
122 										null), false);
123 					} else if (getSortByDateCheckBox().isSelected()) {
124 						CommandExecutor.getInstance().executeCommand(
125 								new SearchSessionCommand(sessionTableModel,
126 										null, getDateChooser().getDate()),
127 								false);
128 					} else
129 						JOptionPane
130 								.showMessageDialog(
131 										null,
132 										Messages.getString("panel.sessionsearchpanel.message1")); //$NON-NLS-1$
133 				}
134 			});
135 		}
136 		return searchButton;
137 	}
138 
139 	/***
140 	 * @return Returns the stopButton.
141 	 */
142 	protected JButton getStopButton() {
143 		if (stopButton == null) {
144 			stopButton = new JButton(Messages.getString("button.refresh")); //$NON-NLS-1$
145 			stopButton.setIcon(new ImageIcon(this.getClass().getResource(
146 					"/icon/22x22/actions/view-refresh.png"))); //$NON-NLS-1$
147 
148 			stopButton.addActionListener(new ActionListener() {
149 				public void actionPerformed(ActionEvent arg0) {
150 					logger.debug("actionPerformed stopButton"); //$NON-NLS-1$
151 					getContainedTextTextField().setText(""); //$NON-NLS-1$
152 					getDateChooser().setDate(new Date());
153 					getSortByDateCheckBox().setSelected(false);
154 					getSortByTextCheckBox().setSelected(false);
155 					sessionTableModel.setData(null);
156 					sessionTableModel.fireTableDataChanged();
157 				}
158 			});
159 		}
160 		return stopButton;
161 	}
162 
163 	/***
164 	 * @return Returns the datePanel.
165 	 */
166 	protected JPanel getDatePanel() {
167 		if (datePanel == null) {
168 			datePanel = new JPanel();
169 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.searchbydate")); //$NON-NLS-1$
170 			datePanel.setBorder(titledBorder);
171 			datePanel.setLayout(new BorderLayout());
172 			datePanel.add(getDateChooser(), BorderLayout.CENTER);
173 			datePanel.add(getSortByDateCheckBox(), BorderLayout.WEST);
174 		}
175 		return datePanel;
176 	}
177 
178 	/***
179 	 * @return Returns the containedTextPanel.
180 	 */
181 	protected JPanel getContainedTextPanel() {
182 		if (containedTextPanel == null) {
183 			containedTextPanel = new JPanel();
184 			TitledBorder titledBorder = new TitledBorder(Messages.getString("common.searchbycontainedtext")); //$NON-NLS-1$
185 			containedTextPanel.setBorder(titledBorder);
186 			containedTextPanel.setLayout(new BorderLayout());
187 			containedTextPanel.add(getContainedTextTextField(),
188 					BorderLayout.CENTER);
189 			containedTextPanel.add(getSortByTextCheckBox(), BorderLayout.WEST);
190 		}
191 		return containedTextPanel;
192 	}
193 
194 	/***
195 	 * @return Returns the dateChooser.
196 	 */
197 	protected JDateChooser getDateChooser() {
198 		if (dateChooser == null) {
199 			dateChooser = new JDateChooser();
200 			dateChooser.setDate(new Date());
201 		}
202 		return this.dateChooser;
203 	}
204 
205 	/***
206 	 * @return Returns the containedTextTextField.
207 	 */
208 	protected JTextField getContainedTextTextField() {
209 		if (containedTextTextField == null) {
210 			containedTextTextField = new JTextField();
211 		}
212 		return this.containedTextTextField;
213 	}
214 
215 	/***
216 	 * @return Returns the sortByDateCheckBox.
217 	 */
218 	protected JCheckBox getSortByDateCheckBox() {
219 		if (sortByDateCheckBox == null) {
220 			sortByDateCheckBox = new JCheckBox();
221 		}
222 		return sortByDateCheckBox;
223 	}
224 
225 	/***
226 	 * @return Returns the sortByTextCheckBox.
227 	 */
228 	protected JCheckBox getSortByTextCheckBox() {
229 		if (sortByTextCheckBox == null) {
230 			sortByTextCheckBox = new JCheckBox();
231 		}
232 		return sortByTextCheckBox;
233 	}
234 
235 }