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.KeyAdapter;
7   import java.awt.event.KeyEvent;
8   
9   import javax.swing.JLabel;
10  import javax.swing.JTextField;
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 text based preferences 
41   * @author Stephen Cowx
42   * @version
43   */
44  public class TextPreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
45  
46  	private JTextField textField = new JTextField();
47  	private JLabel label = new JLabel();
48  	private static Logger log = LogManager.getLogger(TextPreferenceEditorAndRenderer.class);
49  	
50  	/***
51  	 * Constructs an instance of this class 
52  	 * @param preference
53  	 */
54  	public TextPreferenceEditorAndRenderer(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  		textField.setText(getPreference().getValue().toString());
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(textField , 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  		textField.addKeyListener(new KeyAdapter(){
77  			public void keyReleased(KeyEvent e)
78  			{
79  				textUpdated();
80  			}
81  		});
82  	}
83  	
84  	/***
85  	 * @throws Exception
86  	 */
87  	private void textUpdated()  {
88  		log.debug("Preference updated");
89  		try {
90  			getPreference().setValue(textField.getText());	
91  		} catch (Exception e) {
92  			ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating preference.");
93  		}
94  	}
95  		
96  }