View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Felix Berger
5    *  Copyright (C) 2005  Steffen Pingel
6    *
7    *  This library is free software; you can redistribute it and/or
8    *  modify it under the terms of the GNU Lesser General Public
9    *  License as published by the Free Software Foundation; either
10   *  version 2.1 of the License, or (at your option) any later version.
11   *
12   *  This library is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   *  Lesser General Public License for more details.
16   *
17   *  You should have received a copy of the GNU Lesser General Public
18   *  License along with this library; if not, write to the Free Software
19   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package org.xnap.commons.gui.completion;
22  
23  import java.lang.ref.WeakReference;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  /***
29   * This mode acts as a proxy for another mode that can be set as global
30   * default.
31   *
32   * @author Steffen Pingel
33   */
34  public class GlobalDefaultCompletionMode implements CompletionMode
35  {
36  	
37  	private static String defaultMode = DropDownListCompletionMode.class.getName();
38  	private static List<WeakReference<GlobalDefaultCompletionMode>> instances 
39  		= new ArrayList<WeakReference<GlobalDefaultCompletionMode>>();
40  	
41  	private CompletionMode mode;
42  	private Completion completion;
43  	
44  	public GlobalDefaultCompletionMode() 
45  	{
46  		instances.add(new WeakReference<GlobalDefaultCompletionMode>(this));
47  	}
48  	
49  	public void enable(Completion completion) 
50  	{
51  		try {
52  			mode = CompletionModeFactory.createCompletionMode(defaultMode);
53  		}
54  		catch (IllegalArgumentException iae) {
55  			throw iae;
56  		} 
57  		catch (Exception e) {
58  			// TODO now what
59  			e.printStackTrace();
60  			return;
61  		}
62  
63  		mode.enable(completion);
64  		this.completion = completion;
65  	}
66  
67  	public void disable() 
68  	{
69  		mode.disable();
70  		mode = null;
71  		completion = null;
72  	}
73  	
74  	public void updateMode() 
75  	{
76  		Completion savedCompletion = completion;
77  		if (savedCompletion != null) {
78  			disable();
79  			enable(savedCompletion);
80  		}
81  	}
82  	
83  	/***
84  	 * Returns the class name of the currently used completion mode.
85  	 */
86  	public static String getCompletionMode()
87  	{
88  		return defaultMode;
89  	}
90  	
91  	/***
92  	 * Calls to this method need to be Swing thread synchronized. 
93  	 * @param className must not be <code>null</code>, must not be
94  	 * the class name of DefaultCompletionMode itself.
95  	 * @throws IllegalArgumentException if <code>className</code> violates
96  	 * one of the requirements stated above
97  	 */
98  	public static void setCompletionMode(String className)
99  	{ 
100 		if (className == null) {
101 			throw new IllegalArgumentException("class name must not be null");
102 		}
103 		else if (className.equals(GlobalDefaultCompletionMode.class.getName())) {
104 			throw new IllegalArgumentException("delegate mode must not be default completion mode itself");
105 		}
106 		
107 		defaultMode = className;
108 		
109 		for (Iterator<WeakReference<GlobalDefaultCompletionMode>> it = instances.iterator(); it.hasNext();) {
110 			WeakReference<GlobalDefaultCompletionMode> instance = it.next();
111 			GlobalDefaultCompletionMode mode = instance.get();
112 			if (mode != null) {
113 				mode.updateMode();
114 			}
115 			else {
116 				it.remove();
117 			}
118 		}
119 	}
120 	
121 }