View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
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  package org.xnap.commons.settings;
21  
22  import org.xnap.commons.gui.completion.Completion;
23  import org.xnap.commons.gui.completion.CompletionMode;
24  import org.xnap.commons.gui.completion.DefaultCompletionModel;
25  
26  /***
27   * @author Steffen Pingel
28   */
29  public class CompletionSettingDirector
30  {
31  	
32  	private ClassNameSetting<CompletionMode> modeSetting;
33  	// TODO shouldn't we write out the objects themselves, instead of their strings
34  	private StringArraySetting dataSetting;
35  
36  	public CompletionSettingDirector(SettingResource backstore, String key)
37  	{
38  		this.modeSetting = new ClassNameSetting<CompletionMode>(backstore, "completion." + key + ".mode", null);
39  		this.dataSetting = new StringArraySetting(backstore, "completion." + key + ".data", null);
40  	}
41  
42  	public void save(Completion completion) 
43  	{
44  		if (!(completion.getModel() instanceof DefaultCompletionModel)) {
45  			throw new IllegalArgumentException("completion.getModel() must inherit DefaultCompletionModel");
46  		}
47  
48  		modeSetting.setValue(completion.getMode());
49  		DefaultCompletionModel model = (DefaultCompletionModel)completion.getModel();
50  		Object[] objectData = model.toArray();
51  		String[] data = new String[objectData.length];
52  		for (int i = 0; i < objectData.length; i++) {
53  			data[i] = objectData[i].toString();
54  		}
55  		dataSetting.setValue(data);
56  	}
57  	
58  	public void restore(Completion completion)
59  	{
60  		if (!(completion.getModel() instanceof DefaultCompletionModel)) {
61  			throw new IllegalArgumentException("completion.getModel() must inherit DefaultCompletionModel");
62  		}
63  		
64  		CompletionMode mode = modeSetting.getValue();
65  		if (mode != null) {
66  			completion.setMode(mode);
67  		}
68  		String[] data = dataSetting.getValue();
69  		if (data != null) {
70  			DefaultCompletionModel model = (DefaultCompletionModel)completion.getModel();			
71  			model.insert(data);
72  		}
73  	}
74  	
75  }