View Javadoc

1   package org.xnap.commons.gui;
2   
3   import javax.swing.AbstractButton;
4   import javax.swing.Action;
5   import javax.swing.JComponent;
6   import javax.swing.JMenu;
7   import javax.swing.text.JTextComponent;
8   import org.xnap.commons.gui.action.ToggleAction;
9   import org.xnap.commons.gui.completion.Completion;
10  import org.xnap.commons.gui.completion.CompletionModel;
11  import org.xnap.commons.gui.factory.DefaultFactory;
12  import org.xnap.commons.gui.factory.Factory;
13  
14  public class Builder {
15  	
16  	private static Factory factory = new DefaultFactory();
17  
18  	/***
19  	 * Sets the factory that is used by the builder.
20  	 * @param factory can <code>null</code> then the {@link DefaultFactory}
21  	 * is set.
22  	 */
23  	public static void setFactory(Factory factory)
24  	{
25  		if (factory == null) {
26  			Builder.factory = new DefaultFactory();
27  		}
28  		else {
29  			Builder.factory = factory; 
30  		}
31  	}
32  	
33  	/***
34  	 * Returns the factory used by the builder. This method never returns
35  	 * <code>null</code>.
36  	 * 
37  	 * @return a valid factory
38  	 */
39  	public static Factory getFactory()
40  	{
41  		return factory;
42  	}
43  	
44  	/***
45  	 * Adds completion and a completion mode menu to a text component.
46  	 * <p>
47  	 * Completion is done for the whole text of the component and not for the
48  	 * last word before the cursor.
49  	 * <p>
50  	 * The completion mode menu is added as context menu to the text 
51  	 * component.
52  	 */
53  	public static Completion addCompletion(JTextComponent textComponent)
54  	{
55  		Completion completion = new Completion(textComponent);
56  		// TODO menu reference is lost so orientation can't be applied to it
57  		factory.addCompletionModeMenu(textComponent, completion);
58  		return completion;
59  	}
60  	
61  	/***
62  	 * Adds completion and a completion mode menu to a text component.
63  	 * <p>
64  	 * Completion is done for the whole text of the component and not for the
65  	 * last word before the cursor.
66  	 * <p>
67  	 * The completion mode menu is added as context menu to the text 
68  	 * component.
69  	 */
70  	public static Completion addCompletion(JTextComponent textComponent,
71  										   CompletionModel model)
72  	{
73  		Completion completion = new Completion(textComponent, model);
74  		// TODO menu reference is lost so orientation can't be applied to it
75  		factory.addCompletionModeMenu(textComponent, completion);
76  		return completion;
77  	}
78  
79  	/***
80  	 * Adds completion and a completion mode menu to a text component.
81  	 * <p>
82  	 * Completion is done for the whole text of the component and not for the
83  	 * last word before the cursor.
84  	 * <p>
85  	 * The completion mode menu is added as a submenu to the given menu.
86  	 */
87  	public static Completion addCompletion(JTextComponent textComponent,
88  										   JMenu menu)
89  	{
90  		Completion completion = new Completion(textComponent);
91  		// TODO menu reference is lost so orientation can't be applied to it
92  		factory.addCompletionModeMenu(menu, completion);
93  		return completion;
94  	}
95  	
96  	/***
97  	 * Adds completion and a completion mode menu to a text component.
98  	 * <p>
99  	 * Completion is done for the whole text of the component and not for the
100 	 * last word before the cursor.
101 	 * <p>
102 	 * The completion mode menu is added as a submenu to the given menu.
103 	 */
104 	public static Completion addCompletion(JTextComponent textComponent,
105 										   JMenu menu, CompletionModel model)
106 	{
107 		Completion completion = new Completion(textComponent, model);
108 		// TODO menu reference is lost so orientation can't be applied to it
109 		factory.addCompletionModeMenu(menu, completion);
110 		return completion;
111 	}
112 
113 	public static AbstractButton createButton(Action action)
114 	{
115 		return factory.createButton(action);
116 	}
117 
118 	public static AbstractButton createCheckBox(ToggleAction action)
119 	{
120 		return factory.createCheckBox(action);
121 	}
122 
123 	public static AbstractButton createIconButton(Action action)
124 	{
125 		return factory.createIconButton(action);
126 	}
127 	
128 	public static AbstractButton createToolBarButton(Action action)
129 	{
130 		return factory.createToolBarButton(action);
131 	}
132 
133 	public static JComponent createMenuItem(Action action) 
134 	{
135 		return factory.createMenuItem(action);
136 	}
137 	
138 	public static void setProperty(Object key, Object value)
139 	{
140 		factory.setProperty(key, value);
141 	}
142 	
143 }