View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Felix Berger
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library 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 GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  
21  package org.xnap.commons.settings;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.xnap.commons.gui.completion.Completion;
26  import org.xnap.commons.gui.completion.CompletionMode;
27  import org.xnap.commons.gui.completion.CompletionModeFactory;
28  import org.xnap.commons.gui.completion.CompletionModeListener;
29  
30  
31  /***
32   * This class manages the serialization of the active completion mode for a 
33   * {@link Completion} object.
34   * 
35   * A CompletionModeSetting can be used as follows:
36   * 
37   * <pre>
38   * JTextField jtf = new JTextField();
39   * Completion comp = new Completion(jtf);
40   * CompletionModeSetting cms = 
41   * new CompletionModeSetting("prefsKey", 
42   * 	ManualCompletionMode.class.getName(), comp);
43   * </pre>
44   * 
45   * 
46   * 
47   * @author Felix Berger
48   */
49  public class CompletionModeSetting extends StringSetting 
50  	implements CompletionModeListener
51  {
52  
53  	private static Log logger = LogFactory.getLog(CompletionModeSetting.class);
54  	
55  	private Completion comp;
56  	
57  	/***
58  	 * Creates a CompletionModeSetting for a completion object.
59  	 * @param key the settings key for the properties file 
60  	 * @param defaultMode the mode which is initially set for the completion
61  	 * object
62  	 * @param comp 
63  	 */
64  	public CompletionModeSetting(SettingResource backend, String key, 
65  			Class defaultMode, Completion comp)
66  		
67  	{
68  		super(backend, key, defaultMode.getName());
69  		this.comp = comp;
70  		setMode();
71  		comp.addCompletionModeListener(this);
72  	}
73  
74  	/***
75  	 * Reverts the completion mode setting to the default mode and sets it
76  	 * for the completion object.
77  	 */
78  	@Override
79  	public void revert()
80  	{
81  		super.revert();
82  		setMode();
83  	}
84  	
85  	private void setMode()
86  	{
87  		try {
88  			CompletionMode mode 
89  				= CompletionModeFactory.createCompletionMode(getValue());
90  			comp.setMode(mode);
91  		} catch (IllegalArgumentException iae) {
92  			logger.debug(iae);
93  		}
94  		catch (Exception e) {
95  			logger.debug(e);
96  		}
97  	}
98  	
99  	/*
100 	 * Sets the new mode.
101 	 */
102 	public void modeChanged(Class oldMode,  Class newMode)
103 	{
104 		logger.info("mode changed from " + oldMode.getName() + " to " 
105 				+ newMode.getName());
106 		setValue(newMode.getName());		
107 	}
108 }