54 |
import java.awt.event.MouseEvent; |
import java.awt.event.MouseEvent; |
55 |
|
|
56 |
import javax.swing.AbstractAction; |
import javax.swing.AbstractAction; |
57 |
|
import javax.swing.ActionMap; |
58 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
59 |
import javax.swing.CellRendererPane; |
import javax.swing.CellRendererPane; |
60 |
import javax.swing.InputMap; |
import javax.swing.InputMap; |
69 |
import javax.swing.event.ChangeEvent; |
import javax.swing.event.ChangeEvent; |
70 |
import javax.swing.event.MouseInputListener; |
import javax.swing.event.MouseInputListener; |
71 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
72 |
|
import javax.swing.plaf.InputMapUIResource; |
73 |
import javax.swing.plaf.TableUI; |
import javax.swing.plaf.TableUI; |
74 |
import javax.swing.table.TableCellRenderer; |
import javax.swing.table.TableCellRenderer; |
75 |
import javax.swing.table.TableColumn; |
import javax.swing.table.TableColumn; |
271 |
{ |
{ |
272 |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
273 |
InputMap ancestorMap = (InputMap)defaults.get("Table.ancestorInputMap"); |
InputMap ancestorMap = (InputMap)defaults.get("Table.ancestorInputMap"); |
274 |
|
InputMapUIResource parentInputMap = new InputMapUIResource(); |
275 |
|
// FIXME: The JDK uses a LazyActionMap for parentActionMap |
276 |
|
ActionMap parentActionMap = new ActionMap(); |
277 |
action = new TableAction(); |
action = new TableAction(); |
278 |
Object keys[] = ancestorMap.allKeys(); |
Object keys[] = ancestorMap.allKeys(); |
279 |
// Register the key bindings with the JTable. |
// Register key bindings in the UI InputMap-ActionMap pair |
280 |
// Note that we register key bindings with both the old and new modifier |
// Note that we register key bindings with both the old and new modifier |
281 |
// masks: InputEvent.SHIFT_MASK and InputEvent.SHIFT_DOWN_MASK and so on. |
// masks: InputEvent.SHIFT_MASK and InputEvent.SHIFT_DOWN_MASK and so on. |
282 |
for (int i = 0; i < keys.length; i++) |
for (int i = 0; i < keys.length; i++) |
283 |
{ |
{ |
284 |
table.registerKeyboardAction(action,(String)ancestorMap.get((KeyStroke)keys[i]), |
parentInputMap.put(KeyStroke.getKeyStroke |
285 |
KeyStroke.getKeyStroke |
(((KeyStroke)keys[i]).getKeyCode(), convertModifiers |
286 |
(((KeyStroke)keys[i]).getKeyCode(), convertModifiers(((KeyStroke)keys[i]).getModifiers())), |
(((KeyStroke)keys[i]).getModifiers())), |
287 |
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
(String)ancestorMap.get((KeyStroke)keys[i])); |
288 |
|
|
289 |
table.registerKeyboardAction(action,(String)ancestorMap.get((KeyStroke)keys[i]), |
parentInputMap.put(KeyStroke.getKeyStroke |
290 |
KeyStroke.getKeyStroke |
(((KeyStroke)keys[i]).getKeyCode(), |
291 |
(((KeyStroke)keys[i]).getKeyCode(), ((KeyStroke)keys[i]).getModifiers()), |
((KeyStroke)keys[i]).getModifiers()), |
292 |
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
(String)ancestorMap.get((KeyStroke)keys[i])); |
293 |
|
|
294 |
|
parentActionMap.put |
295 |
|
((String)ancestorMap.get((KeyStroke)keys[i]), new ActionListenerProxy |
296 |
|
(action, (String)ancestorMap.get((KeyStroke)keys[i]))); |
297 |
|
|
298 |
} |
} |
299 |
|
// Set the UI InputMap-ActionMap pair to be the parents of the |
300 |
|
// JTable's InputMap-ActionMap pair |
301 |
|
parentInputMap.setParent |
302 |
|
(table.getInputMap |
303 |
|
(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent()); |
304 |
|
parentActionMap.setParent(table.getActionMap().getParent()); |
305 |
|
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT). |
306 |
|
setParent(parentInputMap); |
307 |
|
table.getActionMap().setParent(parentActionMap); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
311 |
|
* This class is used to mimmic the behaviour of the JDK when registering |
312 |
|
* keyboard actions. It is the same as the private class used in JComponent |
313 |
|
* for the same reason. This class receives an action event and dispatches |
314 |
|
* it to the true receiver after altering the actionCommand property of the |
315 |
|
* event. |
316 |
|
*/ |
317 |
|
private static class ActionListenerProxy |
318 |
|
extends AbstractAction |
319 |
|
{ |
320 |
|
TableAction target; |
321 |
|
String bindingCommandName; |
322 |
|
|
323 |
|
public ActionListenerProxy(TableAction li, |
324 |
|
String cmd) |
325 |
|
{ |
326 |
|
target = li; |
327 |
|
bindingCommandName = cmd; |
328 |
|
} |
329 |
|
|
330 |
|
public void actionPerformed(ActionEvent e) |
331 |
|
{ |
332 |
|
ActionEvent derivedEvent = new ActionEvent(e.getSource(), |
333 |
|
e.getID(), |
334 |
|
bindingCommandName, |
335 |
|
e.getModifiers()); |
336 |
|
target.actionPerformed(derivedEvent); |
337 |
|
} |
338 |
} |
} |
339 |
|
|
340 |
/** |
/** |