1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.dialog; |
16 | |
17 | import java.awt.event.KeyEvent; |
18 | |
19 | import javax.swing.ActionMap; |
20 | import javax.swing.JButton; |
21 | import javax.swing.JComponent; |
22 | import javax.swing.JPanel; |
23 | import javax.swing.KeyStroke; |
24 | |
25 | import org.jdesktop.application.ApplicationContext; |
26 | import org.jdesktop.application.ResourceMap; |
27 | |
28 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; |
29 | |
30 | /** |
31 | * @author Jean-Francois Poilpret |
32 | */ |
33 | public abstract class AbstractPanel |
34 | extends JPanel |
35 | implements DialogPanel |
36 | { |
37 | protected AbstractPanel(String id) |
38 | { |
39 | setName(id); |
40 | } |
41 | |
42 | final public void setContext(ApplicationContext context) |
43 | { |
44 | // Initialize all properties |
45 | _context = context; |
46 | _application = (HiveGuiApplicationMain) _context.getApplication(); |
47 | _resourceMap = _context.getResourceMap(getClass(), AbstractPanel.class); |
48 | _actionMap = _context.getActionMap(AbstractPanel.class, this); |
49 | |
50 | // Setup layout for buttons and internationalize the whole panel |
51 | initLayout(); |
52 | initCommands(); |
53 | _resourceMap.injectComponents(this); |
54 | _resourceMap.injectFields(this); |
55 | } |
56 | |
57 | // Override if some of the panel layout needs access to properties |
58 | // such as _context, _application, _resourceMap (which are not available |
59 | // at construction time) |
60 | protected void initLayout() |
61 | { |
62 | } |
63 | |
64 | protected void initCommands() |
65 | { |
66 | _cancel = createButton("cancel", "cancel"); |
67 | _accept = createButton("ok", "accept"); |
68 | |
69 | // Set escape button |
70 | if (_cancel != null) |
71 | { |
72 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( |
73 | KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); |
74 | getActionMap().put("cancel", _cancel.getAction()); |
75 | } |
76 | |
77 | // Add the buttons to the layout |
78 | initCommandsLayout(); |
79 | } |
80 | |
81 | public JButton getDefaultButton() |
82 | { |
83 | if (_accept != null) |
84 | { |
85 | return _accept; |
86 | } |
87 | else |
88 | { |
89 | return _cancel; |
90 | } |
91 | } |
92 | |
93 | protected void initCommandsLayout() |
94 | { |
95 | if (_cancel == null && _accept == null) |
96 | { |
97 | return; |
98 | } |
99 | |
100 | // Depends on layout |
101 | CommandPanelAdder adder = CommandPanelAdderFactory.getAdder(this); |
102 | if (adder != null) |
103 | { |
104 | adder.addCommands(this, _accept, _cancel); |
105 | } |
106 | } |
107 | |
108 | final public JComponent getPanel() |
109 | { |
110 | return this; |
111 | } |
112 | |
113 | final public String getDialogTitle() |
114 | { |
115 | return String.format(_title, getTitleFormatArgs()); |
116 | } |
117 | |
118 | final public String getTitle() |
119 | { |
120 | return _title; |
121 | } |
122 | |
123 | final public void setTitle(String title) |
124 | { |
125 | _title = title; |
126 | } |
127 | |
128 | public void setParent(GenericDialog dialog) |
129 | { |
130 | _parent = dialog; |
131 | } |
132 | |
133 | final protected JButton createButton(String name, String actionName) |
134 | { |
135 | javax.swing.Action action = getAction(actionName); |
136 | if (action != null) |
137 | { |
138 | JButton button = new JButton(action); |
139 | button.setName(getName() + "-" + name); |
140 | return button; |
141 | } |
142 | else |
143 | { |
144 | return null; |
145 | } |
146 | } |
147 | |
148 | final protected javax.swing.Action getAction(String action) |
149 | { |
150 | return _actionMap.get(action); |
151 | } |
152 | |
153 | protected Object[] getTitleFormatArgs() |
154 | { |
155 | return EMPTY_ARGS; |
156 | } |
157 | |
158 | public void reset() |
159 | { |
160 | } |
161 | |
162 | public boolean canClose() |
163 | { |
164 | return true; |
165 | } |
166 | |
167 | // Utility property that can be used for automatic enabling of accept button |
168 | final public boolean isAcceptEnabled() |
169 | { |
170 | return _enabled; |
171 | } |
172 | |
173 | final public void setAcceptEnabled(boolean enabled) |
174 | { |
175 | boolean old = _enabled; |
176 | _enabled = enabled; |
177 | firePropertyChange("acceptEnabled", old, enabled); |
178 | } |
179 | |
180 | static final private Object[] EMPTY_ARGS = new Object[0]; |
181 | |
182 | protected ApplicationContext _context; |
183 | protected HiveGuiApplicationMain _application; |
184 | protected ResourceMap _resourceMap; |
185 | protected ActionMap _actionMap; |
186 | protected GenericDialog _parent; |
187 | |
188 | protected String _title = ""; |
189 | protected JButton _cancel; |
190 | protected JButton _accept; |
191 | |
192 | private boolean _enabled = true; |
193 | } |