View Javadoc

1   package com.explosion.utilities.preferences.editandrender.form;
2   
3   import java.awt.GridBagConstraints;
4   import java.awt.GridBagLayout;
5   import java.awt.Insets;
6   import java.awt.event.ActionEvent;
7   import java.awt.event.ActionListener;
8   
9   import javax.swing.JCheckBox;
10  import javax.swing.JLabel;
11  
12  import org.apache.log4j.LogManager;
13  import org.apache.log4j.Logger;
14  
15  import com.explosion.utilities.exception.ExceptionManagerFactory;
16  import com.explosion.utilities.preferences.Preference;
17  
18  
19  /*
20   * =============================================================================
21   * 
22   * Copyright 2004 Stephen Cowx
23   * 
24   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
25   * use this file except in compliance with the License. You may obtain a copy of
26   * the License at
27   * 
28   * http://www.apache.org/licenses/LICENSE-2.0
29   * 
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
32   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
33   * License for the specific language governing permissions and limitations under
34   * the License.
35   * 
36   * =============================================================================
37   */
38  
39  /***
40   * This class provides a way to edit and rebder boolean Preferences 
41   * @author Stephen Cowx
42   * @version
43   */
44  public class BooleanPreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
45  
46  	private JCheckBox checkBox = new JCheckBox();
47  	private JLabel label = new JLabel();
48  	private static Logger log = LogManager.getLogger(BooleanPreferenceEditorAndRenderer.class);
49  	
50  	/***
51  	 * Constructs an instance of this class 
52  	 * @param preference
53  	 */
54  	public BooleanPreferenceEditorAndRenderer(Preference preference)
55  	{
56      	super(preference);
57  		init();
58  	}
59  	
60  	/***
61  	 * This method initialises this renderer editor combination
62  	 */
63  	public void init()
64  	{
65  		this.setLayout(new GridBagLayout());
66  		
67  		//Initialise the layout and bits
68  		label.setText(getPreference().getLongName());
69  		checkBox.setSelected(((Boolean)getPreference().getValue()).booleanValue());
70  		
71  		//Add all of the bits to the panel
72  		this.add(label    , new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); 
73  		this.add(checkBox , new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
74  		
75  		//initialise the actions for updating everything
76  		checkBox.addActionListener(new ActionListener(){
77  			
78  		  public void actionPerformed(ActionEvent e)
79  		  {
80  		  	checkboxUpdated();
81  		  }
82  		});
83  	}
84  	
85  	/***
86  	 * @throws Exception
87  	 */
88  	private void checkboxUpdated()  {
89  		log.debug("Preference updated");
90  		try {
91  			if (checkBox.isSelected())
92  			{
93  				getPreference().setValue(new Boolean(true));
94  			}
95  			else
96  			{
97  				getPreference().setValue(new Boolean(false));
98  			}
99  		} catch (Exception e) {
100 			ExceptionManagerFactory.getExceptionManager().manageException(e, "Excaption caught while updating preference.");
101 		}
102 	}
103 		
104 }