View Javadoc
1   /******************************************************************************
2    * RatingGraphPanelConfigure.java - 
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.4  2011/01/01 06:10:51  dick
10   * Javadoc.
11   *
12   * Revision 1.3  2010/03/03 13:12:21  inim
13   * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
14   *
15   * Revision 1.2  2010/03/03 12:19:49  inim
16   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
17   *
18   * Revision 1.1  2010/02/04 05:57:53  inim
19   * Mavenized project folder layout
20   *
21   * Revision 1.2  2010/01/29 23:38:35  dick
22   * Javadoc.
23   *
24   * Revision 1.1  2010/01/29 23:07:35  dick
25   * RatingGraphPanelConfigure becomes RatingGraphPanelConfigureDialog.
26   *
27   * Revision 1.1  2010/01/27 02:11:10  dick
28   * Empty RatingGraphPanelConfigure.
29   *
30   */
31  
32  /* 
33   * This program is free software: you can redistribute it and/or modify
34   * it under the terms of the GNU General Public License as published by
35   * the Free Software Foundation, either version 3 of the License, or
36   * (at your option) any later version.
37   *
38   * This program is distributed in the hope that it will be useful,
39   * but WITHOUT ANY WARRANTY; without even the implied warranty of
40   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41   * GNU General Public License for more details.
42   *
43   * You should have received a copy of the GNU General Public License
44   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
45   *
46   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
47   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
48   * 
49   */
50  package com.buckosoft.fibs.BuckoFIBS.gui;
51  
52  import java.awt.BorderLayout;
53  import java.awt.Dimension;
54  import java.awt.Frame;
55  import java.awt.GridBagConstraints;
56  import java.awt.GridBagLayout;
57  import java.awt.Insets;
58  
59  import javax.swing.ButtonGroup;
60  import javax.swing.JButton;
61  import javax.swing.JDialog;
62  import javax.swing.JFrame;
63  import javax.swing.JPanel;
64  import javax.swing.JRadioButton;
65  import javax.swing.JTextField;
66  import javax.swing.event.DocumentEvent;
67  import javax.swing.event.DocumentListener;
68  
69  import com.buckosoft.fibs.BuckoFIBS.BFProperties;
70  import com.buckosoft.fibs.BuckoFIBS.CommandDispatcher;
71  import com.buckosoft.fibs.domain.config.RatingGraphConfig;
72  
73  /**
74   * @author dick
75   * @since Jan 26, 2010
76   * @see 
77   */
78  /** Configure the Rating Graph Panel
79   * @author Dick Balaska
80   * @since 2010/01/26
81   * @version $Revision$ <br> $Date$
82   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/RatingGraphPanelConfigureDialog.java">cvs RatingGraphPanelConfigureDialog.java</a>
83   */
84  public class RatingGraphPanelConfigureDialog extends JDialog implements DocumentListener {
85  	private	final static boolean DEBUG = false;
86  	private	CommandDispatcher	commandDispatcher;
87  	private	BFProperties		properties;
88  
89  	private static final long serialVersionUID = 1L;
90  	private JPanel jContentPane = null;
91  	private JPanel jPanelGraphType = null;
92  	private JRadioButton jRadioButtonAllMatches = null;
93  	private JRadioButton jRadioButtonLastXMatches = null;
94  	private JTextField jTextFieldNumberOfMatches = null;
95  	private JRadioButton jRadioButtonFromDate = null;
96  	private JPanel jPanelOKCancel = null;
97  	private JButton jButtonOK = null;
98  	private JButton jButtonCancel = null;
99  	private	RatingGraphConfig ratingGraphConfig;
100 	
101 	/**
102 	 * @param owner
103 	 */
104 	public RatingGraphPanelConfigureDialog(Frame owner) {
105 		super(owner);
106 		initialize();
107 	}
108 
109 	/** Create and display the RatingGraphConfigure dialog.
110 	 * Just set and forget.  It's modal and manages ok and cancel on its own.
111 	 * @param owner Probably an instance of MainDialog
112 	 * @param commandDispatcher The BuckoFIBS commandDispatcher
113 	 */
114 	public RatingGraphPanelConfigureDialog(JFrame owner, CommandDispatcher commandDispatcher) {
115 		super(owner, true);
116 		this.commandDispatcher = commandDispatcher;
117 		this.properties = commandDispatcher.getProperties();
118 		initialize();
119 		this.setLocationRelativeTo(owner);
120 
121 		BFProperties props = this.commandDispatcher.getProperties();
122 		ratingGraphConfig = props.getRatingGraphConfig().clone();
123 		switch (ratingGraphConfig.getTypeAsInt()) {
124 		case 0:
125 			this.jRadioButtonAllMatches.getModel().setSelected(true);
126 			break;
127 		case 1:
128 			this.jRadioButtonLastXMatches.getModel().setSelected(true);
129 			break;
130 		case 2:
131 			this.jRadioButtonFromDate.getModel().setSelected(true);
132 			break;
133 		}
134 		this.jTextFieldNumberOfMatches.setText("" + props.getRatingGraphConfig().getMatchCount());
135 		this.jTextFieldNumberOfMatches.setEnabled(ratingGraphConfig.getType() == RatingGraphConfig.Type.displayLastXMatches);
136 	}
137 
138 	/** This method initializes this
139 	 * @return void
140 	 */
141 	private void initialize() {
142 		this.setSize(300, 200);
143 		this.setTitle("Configure the Rating Graph");
144 		this.setContentPane(getJContentPane());
145 	}
146 
147 	private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
148 		setVisible(false);
149 		RatingGraphConfig rgc = this.commandDispatcher.getProperties().getRatingGraphConfig();
150 		if (this.jRadioButtonAllMatches.getModel().isSelected())
151 			rgc.setTypeAsInt(0);
152 		if (this.jRadioButtonLastXMatches.getModel().isSelected())
153 			rgc.setTypeAsInt(1);
154 		if (this.jRadioButtonFromDate.getModel().isSelected())
155 			rgc.setTypeAsInt(2);
156 
157 		int i = -1;
158 		try {
159 			i = Integer.parseInt(this.jTextFieldNumberOfMatches.getText());
160 		} catch (NumberFormatException e) {
161 			i = -1;
162 		}
163 		if (i < 1)
164 			i = 1;
165 		rgc.setMatchCount(i);
166 		this.properties.setRatingGraphConfig(rgc);
167 		dispose();
168 	}
169 
170 	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
171 		setVisible(false);
172 		dispose();
173 	}
174 
175 	/** This method initializes jContentPane
176 	 * 
177 	 * @return javax.swing.JPanel
178 	 */
179 	private JPanel getJContentPane() {
180 		if (jContentPane == null) {
181 			BorderLayout borderLayout = new BorderLayout();
182 			borderLayout.setHgap(15);
183 			borderLayout.setVgap(11);
184 			jContentPane = new JPanel();
185 			jContentPane.setLayout(borderLayout);
186 			jContentPane.add(getJPanelGraphType(), BorderLayout.CENTER);
187 			jContentPane.add(getJPanelOKCancel(), BorderLayout.SOUTH);
188 		}
189 		return jContentPane;
190 	}
191 
192 	/** This method initializes jPanelGraphType	
193 	 * 	
194 	 * @return javax.swing.JPanel	
195 	 */
196 	private JPanel getJPanelGraphType() {
197 		if (jPanelGraphType == null) {
198 			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
199 			gridBagConstraints4.gridx = 0;
200 			gridBagConstraints4.gridy = 4;
201 			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
202 			gridBagConstraints3.gridx = 0;
203 			gridBagConstraints3.anchor = GridBagConstraints.WEST;
204 			gridBagConstraints3.gridy = 3;
205 			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
206 			gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
207 			gridBagConstraints2.gridy = 2;
208 			gridBagConstraints2.weightx = 1.0;
209 			gridBagConstraints2.anchor = GridBagConstraints.WEST;
210 			gridBagConstraints2.ipadx = 0;
211 			gridBagConstraints2.insets = new Insets(0, 22, 0, 0);
212 			gridBagConstraints2.gridx = 0;
213 			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
214 			gridBagConstraints1.gridx = 0;
215 			gridBagConstraints1.anchor = GridBagConstraints.WEST;
216 			gridBagConstraints1.gridy = 1;
217 			GridBagConstraints gridBagConstraints = new GridBagConstraints();
218 			gridBagConstraints.gridx = 0;
219 			gridBagConstraints.anchor = GridBagConstraints.WEST;
220 			gridBagConstraints.gridy = 0;
221 			jPanelGraphType = new JPanel();
222 			jPanelGraphType.setLayout(new GridBagLayout());
223 			jPanelGraphType.add(getJRadioButtonAllMatches(), gridBagConstraints);
224 			jPanelGraphType.add(getJRadioButtonLastXMatches(), gridBagConstraints1);
225 			jPanelGraphType.add(getJTextFieldNumberOfMatches(), gridBagConstraints2);
226 			jPanelGraphType.add(getJRadioButtonFromDate(), gridBagConstraints3);
227 			ButtonGroup group = new ButtonGroup();
228 		    group.add(getJRadioButtonAllMatches());
229 		    group.add(getJRadioButtonLastXMatches());
230 		    group.add(getJRadioButtonFromDate());
231 		}
232 		return jPanelGraphType;
233 	}
234 
235 	/** This method initializes jRadioButtonAllMatches	
236 	 * 	
237 	 * @return javax.swing.JRadioButton	
238 	 */
239 	private JRadioButton getJRadioButtonAllMatches() {
240 		if (jRadioButtonAllMatches == null) {
241 			jRadioButtonAllMatches = new JRadioButton();
242 			jRadioButtonAllMatches.setText("All Matches");
243 			jRadioButtonAllMatches.addActionListener(new java.awt.event.ActionListener() {
244 				public void actionPerformed(java.awt.event.ActionEvent e) {
245 					ratingGraphConfig.setType(RatingGraphConfig.Type.displayAllMatches);
246 					commandDispatcher.dispatch(CommandDispatcher.Command.SET_RATING_GRAPH_CONFIG, ratingGraphConfig);
247 				}
248 			});
249 		}
250 		return jRadioButtonAllMatches;
251 	}
252 
253 	/** This method initializes jRadioButtonLastXMatches	
254 	 * 	
255 	 * @return javax.swing.JRadioButton	
256 	 */
257 	private JRadioButton getJRadioButtonLastXMatches() {
258 		if (jRadioButtonLastXMatches == null) {
259 			jRadioButtonLastXMatches = new JRadioButton();
260 			jRadioButtonLastXMatches.setText("Last X Matches");
261 			jRadioButtonLastXMatches.addActionListener(new java.awt.event.ActionListener() {
262 				public void actionPerformed(java.awt.event.ActionEvent e) {
263 					ratingGraphConfig.setType(RatingGraphConfig.Type.displayLastXMatches);
264 					commandDispatcher.dispatch(CommandDispatcher.Command.SET_RATING_GRAPH_CONFIG, ratingGraphConfig);
265 				}
266 			});
267 			jRadioButtonLastXMatches
268 					.addChangeListener(new javax.swing.event.ChangeListener() {
269 						public void stateChanged(javax.swing.event.ChangeEvent e) {
270 							getJTextFieldNumberOfMatches().setEnabled(jRadioButtonLastXMatches.getModel().isSelected());
271 						}
272 					});
273 		}
274 		return jRadioButtonLastXMatches;
275 	}
276 
277 	/** This method initializes jTextFieldNumberOfMatches	
278 	 * 	
279 	 * @return javax.swing.JTextField	
280 	 */
281 	private JTextField getJTextFieldNumberOfMatches() {
282 		if (jTextFieldNumberOfMatches == null) {
283 			jTextFieldNumberOfMatches = new JTextField();
284 			jTextFieldNumberOfMatches.setPreferredSize(new Dimension(60, 20));
285 			jTextFieldNumberOfMatches.addKeyListener(new java.awt.event.KeyAdapter() {
286 				public void keyTyped(java.awt.event.KeyEvent e) {
287 					
288 				}
289 			});
290 			jTextFieldNumberOfMatches.getDocument().addDocumentListener(this);
291 		}
292 		return jTextFieldNumberOfMatches;
293 	}
294 
295 	/* (non-Javadoc)
296 	 * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
297 	 */
298 	@Override
299 	public void changedUpdate(DocumentEvent arg0) {
300 		updateFromTextChanged(arg0);
301 		
302 	}
303 
304 	/* (non-Javadoc)
305 	 * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
306 	 */
307 	@Override
308 	public void insertUpdate(DocumentEvent arg0) {
309 		updateFromTextChanged(arg0);
310 	}
311 
312 	/* (non-Javadoc)
313 	 * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
314 	 */
315 	@Override
316 	public void removeUpdate(DocumentEvent arg0) {
317 		updateFromTextChanged(arg0);
318 	}
319 
320 	private void updateFromTextChanged(DocumentEvent documentEvent) {
321 		int i = -1;
322 		try {
323 			i = Integer.parseInt(jTextFieldNumberOfMatches.getText());
324 		} catch (NumberFormatException e1) {
325 			i = -1;
326 		}
327 		if (i < 1)
328 			i = 1;
329 		if (DEBUG)
330 			System.out.println("docChanged(" + i + ")");
331 		ratingGraphConfig.setMatchCount(i);
332 		commandDispatcher.dispatch(CommandDispatcher.Command.SET_RATING_GRAPH_CONFIG, ratingGraphConfig);
333 
334 	}
335 
336 	/**
337 	 * This method initializes jRadioButtonFromDate	
338 	 * 	
339 	 * @return javax.swing.JRadioButton	
340 	 */
341 	private JRadioButton getJRadioButtonFromDate() {
342 		if (jRadioButtonFromDate == null) {
343 			jRadioButtonFromDate = new JRadioButton();
344 			jRadioButtonFromDate.setText("From Date");
345 			jRadioButtonFromDate.setEnabled(false);
346 			jRadioButtonFromDate.addActionListener(new java.awt.event.ActionListener() {
347 				public void actionPerformed(java.awt.event.ActionEvent e) {
348 					System.out.println("actionPerformed()"); // TODO Date does nothing.
349 				}
350 			});
351 		}
352 		return jRadioButtonFromDate;
353 	}
354 
355 	/**
356 	 * This method initializes jPanelOKCancel	
357 	 * 	
358 	 * @return javax.swing.JPanel	
359 	 */
360 	private JPanel getJPanelOKCancel() {
361 		if (jPanelOKCancel == null) {
362 			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
363 			gridBagConstraints6.gridx = 1;
364 			gridBagConstraints6.anchor = GridBagConstraints.EAST;
365 			gridBagConstraints6.insets = new Insets(0, 30, 0, 0);
366 			gridBagConstraints6.gridy = 5;
367 			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
368 			gridBagConstraints5.gridx = 0;
369 			gridBagConstraints5.weighty = 0.5;
370 			gridBagConstraints5.fill = GridBagConstraints.BOTH;
371 			gridBagConstraints5.anchor = GridBagConstraints.CENTER;
372 			gridBagConstraints5.insets = new Insets(0, 0, 0, 20);
373 			gridBagConstraints5.weightx = 0.0;
374 			gridBagConstraints5.gridy = 5;
375 			jPanelOKCancel = new JPanel();
376 			jPanelOKCancel.setLayout(new GridBagLayout());
377 			jPanelOKCancel.add(getJButtonOK(), gridBagConstraints5);
378 			jPanelOKCancel.add(getJButtonCancel(), gridBagConstraints6);
379 		}
380 		return jPanelOKCancel;
381 	}
382 
383 	/**
384 	 * This method initializes jButtonOK	
385 	 * 	
386 	 * @return javax.swing.JButton	
387 	 */
388 	private JButton getJButtonOK() {
389 		if (jButtonOK == null) {
390 			jButtonOK = new JButton();
391 			jButtonOK.setText("OK");
392 			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
393 				public void actionPerformed(java.awt.event.ActionEvent e) {
394 					okButtonActionPerformed(e);
395 				}
396 			});
397 		}
398 		return jButtonOK;
399 	}
400 
401 	/**
402 	 * This method initializes jButtonCancel	
403 	 * 	
404 	 * @return javax.swing.JButton	
405 	 */
406 	private JButton getJButtonCancel() {
407 		if (jButtonCancel == null) {
408 			jButtonCancel = new JButton();
409 			jButtonCancel.setText("Cancel");
410 			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
411 				public void actionPerformed(java.awt.event.ActionEvent e) {
412 					cancelButtonActionPerformed(e);
413 				}
414 			});
415 		}
416 		return jButtonCancel;
417 	}
418 
419 }  //  @jve:decl-index=0:visual-constraint="13,8"