View Javadoc

1   package org.xnap.commons.gui.factory;
2   
3   import java.awt.Insets;
4   import java.util.HashMap;
5   import java.util.Map;
6   import javax.swing.AbstractButton;
7   import javax.swing.Action;
8   import javax.swing.JButton;
9   import javax.swing.JCheckBox;
10  import javax.swing.JCheckBoxMenuItem;
11  import javax.swing.JComponent;
12  import javax.swing.JMenu;
13  import javax.swing.JMenuItem;
14  import javax.swing.JPopupMenu;
15  import javax.swing.JToggleButton;
16  import javax.swing.text.JTextComponent;
17  import org.xnap.commons.gui.TextFieldMenu;
18  import org.xnap.commons.gui.ToolBarButton;
19  import org.xnap.commons.gui.action.AbstractXNapAction;
20  import org.xnap.commons.gui.action.ToggleAction;
21  import org.xnap.commons.gui.completion.ClearCompletionModelAction;
22  import org.xnap.commons.gui.completion.Completion;
23  import org.xnap.commons.gui.completion.CompletionModeMenu;
24  import org.xnap.commons.gui.completion.DefaultCompletionModel;
25  import org.xnap.commons.gui.util.PopupListener;
26  
27  public class DefaultFactory implements Factory {
28  
29  	public final static String ENHANCED_TEXT_FIELD_MENU_KEY = "enhancedTextFieldMenu";
30  	
31  	private Map<Object, Object> properties;
32  
33  	public void addCompletionModeMenu(JTextComponent textComponent, Completion completion)
34  	{
35  		if (getProperty(ENHANCED_TEXT_FIELD_MENU_KEY) != null) {
36  			TextFieldMenu menu = new TextFieldMenu();
37  			menu.addSeparator();
38  			menu.add(new CompletionModeMenu(completion));
39  			textComponent.addMouseListener(new PopupListener(menu));
40  			if (completion.getModel() instanceof DefaultCompletionModel) {
41  				menu.addSeparator();
42  				ClearCompletionModelAction action 
43  					= new ClearCompletionModelAction((DefaultCompletionModel)completion.getModel());
44  				menu.add(createMenuItem(action));
45  			}
46  		}
47  		else {
48  			CompletionModeMenu menu = new CompletionModeMenu(completion);
49  			textComponent.addMouseListener(new PopupListener(menu));
50  		}		
51  	}
52  
53  	public void addCompletionModeMenu(JMenu menu, Completion completion)
54  	{
55  		menu.add(new CompletionModeMenu(completion));
56  	}
57  
58  	public AbstractButton createButton(Action action)
59  	{
60  		AbstractButton button = (action instanceof ToggleAction) ?
61  				new JToggleButton(action) : new JButton(action);
62  		AbstractXNapAction.initialize(button, action);
63  		return button;
64  	}
65  
66  	public AbstractButton createCheckBox(ToggleAction action)
67  	{
68  		AbstractButton button = new JCheckBox(action);
69  		AbstractXNapAction.initialize(button, action);
70  		return button;
71  	}
72  
73  	public AbstractButton createIconButton(Action action)
74  	{
75  		JButton button = new JButton(action);
76  		button.setMargin(new Insets(1, 1, 1, 1));
77  		AbstractXNapAction.initialize(button, action);
78  		return button;
79  	}
80  	
81  	public AbstractButton createToolBarButton(Action action)
82  	{
83  		ToolBarButton button = new ToolBarButton(action);
84  		AbstractXNapAction.initialize(button, action);
85  		return button;
86  	}
87  
88  	public JComponent createMenuItem(Action action) 
89  	{
90  		if (action == null) {
91  			return new JPopupMenu.Separator();
92  		}
93  		else if (action instanceof ToggleAction) {
94  			JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(action);
95  			AbstractXNapAction.initialize(menuItem, action);
96  			return menuItem;
97  		}
98  		/* TODO: implement ChooserAction
99  		else if (action instanceof SubmenuAction) {
100 			return ((SubmenuAction)action).createMenu();
101 		}
102 		*/
103 		else {
104 			JMenuItem menuItem = new JMenuItem(action);
105 			AbstractXNapAction.initialize(menuItem, action);
106 			return menuItem;
107 		}
108 	}
109 
110 	public Object getProperty(Object key)
111 	{
112 		if (properties == null) {
113 			return null;
114 		}
115 		return properties.get(key);
116 	}
117 	
118 	public void setProperty(Object key, Object value)
119 	{
120 		if (properties == null) {
121 			properties = new HashMap<Object, Object>();
122 		}
123 		properties.put(key, value);
124 	}
125 
126 }