下面列出了javax.swing.JComponent#getInputMap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Registers a component for tooltip management.
* <p>
* This will register key bindings to show and hide the tooltip text
* only if <code>component</code> has focus bindings. This is done
* so that components that are not normally focus traversable, such
* as <code>JLabel</code>, are not made focus traversable as a result
* of invoking this method.
*
* @param component a <code>JComponent</code> object to add
* @see JComponent#isFocusTraversable
*/
protected void registerComponent(JComponent component) {
component.removeMouseListener(this);
component.addMouseListener(this);
component.removeMouseMotionListener(moveBeforeEnterListener);
component.addMouseMotionListener(moveBeforeEnterListener);
if (shouldRegisterBindings(component)) {
// register our accessibility keybindings for this component
// this will apply globally across L&F
// Post Tip: Ctrl+F1
// Unpost Tip: Esc and Ctrl+F1
InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = component.getActionMap();
if (inputMap != null && actionMap != null) {
//XXX remove
}
}
}
private void populateMenuOpenKeyboardShortcuts(JComponent component) {
/* In the future, it would be nice to include the shortcut from
o.n.modules.quicksearch.QuickSearchAction/QuickSearchComboBar here. That shortcut isn't set
via a regular InputMap, however, so a new cross-module API (maybe a client property that
could be set on QuickSearchComboBar) would be needed to let QuickSearchComboBar tell
AutoHidingMenuBar about its shortcut. */
InputMap im = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
if (im != null) {
KeyStroke keyStrokes[] = im.allKeys();
if (keyStrokes != null) {
for (KeyStroke keyStroke : keyStrokes) {
menuOpenKeyboardShortcuts.put(keyStroke,
"OpenMenu"); // Value doesn't matter, just use some non-null string.
}
}
}
// Don't descend into the actual menus.
if (!(component instanceof JMenu)) {
for (Component childComponent : component.getComponents()) {
if (childComponent instanceof JComponent)
populateMenuOpenKeyboardShortcuts((JComponent) childComponent);
}
}
}
private static void exchangeCommands(String[][] commandsToExchange,
final JComponent target, final JComponent source) {
InputMap targetBindings = target.getInputMap();
KeyStroke[] targetBindingKeys = targetBindings.allKeys();
ActionMap targetActions = target.getActionMap();
InputMap sourceBindings = source.getInputMap();
ActionMap sourceActions = source.getActionMap();
for (int i = 0; i < commandsToExchange.length; i++) {
String commandFrom = commandsToExchange[i][0];
String commandTo = commandsToExchange[i][1];
final Action orig = targetActions.get(commandTo);
if (orig == null) {
continue;
}
sourceActions.put(commandTo, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
orig.actionPerformed(new ActionEvent(target, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers()));
}
});
for (int j = 0; j < targetBindingKeys.length; j++) {
if (targetBindings.get(targetBindingKeys[j]).equals(commandFrom)) {
sourceBindings.put(targetBindingKeys[j], commandTo);
}
}
}
}
public static void removeKeyBinding(JComponent component, String key)
{
int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
InputMap inputMap = component.getInputMap(condition);
// According to the docs, null should remove the action, but it does
// not seem to work with Sun Java 1.4.2, new Object() works
inputMap.put(KeyStroke.getKeyStroke(key), new Object());
}
/** Register the accelerator key of an action at a component. */
public static void register(JComponent component, GuiAction action)
{
KeyStroke keyStroke =
(KeyStroke)action.getValue(AbstractAction.ACCELERATOR_KEY);
if (keyStroke != null)
{
String name = (String)action.getValue(AbstractAction.NAME);
InputMap inputMap =
component.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(keyStroke, name);
component.getActionMap().put(name, action);
}
}
/**
* Removes a component from tooltip control.
*
* @param component a <code>JComponent</code> object to remove
*/
protected void unregisterComponent(JComponent component) {
component.removeMouseListener(this);
component.removeMouseMotionListener(moveBeforeEnterListener);
if (shouldRegisterBindings(component)) {
InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = component.getActionMap();
if (inputMap != null && actionMap != null) {
//XXX remove
}
}
}
private void filterInputMap(JComponent component, int condition) {
InputMap imap = component.getInputMap(condition);
if (imap instanceof ComponentInputMap) {
imap = new F8FilterComponentInputMap(component, imap);
} else {
imap = new F8FilterInputMap(imap);
}
component.setInputMap(condition, imap);
}
private void addSpaceActionhandler(JComponent host) {
InputMap inputMap = host.getInputMap();
ActionMap actionMap = host.getActionMap();
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
String key = keyStroke.toString();
inputMap.put(keyStroke, key);
actionMap.put(key, invertCheckAction);
}
private void bindKeys (JComponent component)
{
InputMap inputMap;
// inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
// inputMap = component.getInputMap(JComponent.WHEN_FOCUSED); // Default
inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = component.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("UP"), "UpAction");
actionMap.put("UpAction", new UpAction());
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "DownAction");
actionMap.put("DownAction", new DownAction());
inputMap.put(KeyStroke.getKeyStroke("shift UP"), "ShiftUpAction");
actionMap.put("ShiftUpAction", new ShiftUpAction());
inputMap.put(KeyStroke.getKeyStroke("shift DOWN"), "ShiftDownAction");
actionMap.put("ShiftDownAction", new ShiftDownAction());
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "LeftAction");
actionMap.put("LeftAction", new LeftAction());
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "RightAction");
actionMap.put("RightAction", new RightAction());
inputMap.put(KeyStroke.getKeyStroke("shift LEFT"), "ShiftLeftAction");
actionMap.put("ShiftLeftAction", new ShiftLeftAction());
inputMap.put(KeyStroke.getKeyStroke("shift RIGHT"), "ShiftRightAction");
actionMap.put("ShiftRightAction", new ShiftRightAction());
}
public static void addToActionMap(JComponent component) {
ActionMap actionMap = component.getActionMap();
actionMap.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
actionMap.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
actionMap.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
actionMap.put(DeleteOperatorAction.getActionName(), new DeleteOperatorAction());
// only required if you have not set the menu accelerators
InputMap inputMap = component.getInputMap();
inputMap.put(KeyStroke.getKeyStroke("ctrl X"), TransferHandler.getCutAction().getValue(Action.NAME));
inputMap.put(KeyStroke.getKeyStroke("ctrl C"), TransferHandler.getCopyAction().getValue(Action.NAME));
inputMap.put(KeyStroke.getKeyStroke("ctrl V"), TransferHandler.getPasteAction().getValue(Action.NAME));
inputMap.put(KeyStroke.getKeyStroke("del"), DeleteOperatorAction.getActionName());
}
private JComponent createChatLog(NotificationChannelManager channelManager) {
JComponent chatLogArea = new ChatLogArea(channelManager).getComponent();
chatLogArea.setPreferredSize(new Dimension(screen.getWidth(), 171));
// Bind the tab changing keys of the chat log to global key map
InputMap input = chatLogArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
input.put(KeyStroke.getKeyStroke("control PAGE_UP"), "navigatePrevious");
input.put(KeyStroke.getKeyStroke("control PAGE_DOWN"), "navigateNext");
return chatLogArea;
}
/**
* Creates a new Options object.
*/
public Options ()
{
// Preload constant units
UnitManager.getInstance().preLoadUnits();
frame = new JFrame();
frame.setName("optionsFrame");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JComponent framePane = (JComponent) frame.getContentPane();
framePane.setLayout(new BorderLayout());
InputMap inputMap = framePane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap actionMap = framePane.getActionMap();
JToolBar toolBar = new JToolBar(JToolBar.HORIZONTAL);
framePane.add(toolBar, BorderLayout.NORTH);
// Dump button
JButton dumpButton = new JButton(dumping);
dumpButton.setName("optionsDumpButton");
toolBar.add(dumpButton);
// Check button
JButton checkButton = new JButton(checking);
checkButton.setName("optionsCheckButton");
toolBar.add(checkButton);
// Reset button
JButton resetButton = new JButton(resetting);
resetButton.setName("optionsResetButton");
toolBar.add(resetButton);
// Some space
toolBar.add(Box.createHorizontalStrut(100));
toolBar.add(new JLabel("Search:"));
// Back button
JButton backButton = new JButton(backSearch);
backButton.setName("optionsBackButton");
toolBar.add(backButton);
inputMap.put(KeyStroke.getKeyStroke("shift F3"), "backSearch");
actionMap.put("backSearch", backSearch);
// Search entry
searchField = new JTextField();
searchField.setMaximumSize(new Dimension(200, 28));
searchField.setName("optionsSearchField");
searchField.setHorizontalAlignment(JTextField.LEFT);
toolBar.add(searchField);
inputMap.put(KeyStroke.getKeyStroke("ctrl F"), "find");
actionMap.put("find", find);
searchField.getDocument().addDocumentListener(docListener);
// Forward button
JButton forwardButton = new JButton(forwardSearch);
forwardButton.setName("optionsForwardButton");
toolBar.add(forwardButton);
// Some space, message field
toolBar.add(Box.createHorizontalStrut(10));
toolBar.add(msgLabel);
// TreeTable
UnitModel unitModel = new UnitModel();
unitTreeTable = new UnitTreeTable(unitModel);
framePane.add(new JScrollPane(unitTreeTable), BorderLayout.CENTER);
// Needed to process user input when RETURN/ENTER is pressed
inputMap.put(KeyStroke.getKeyStroke("ENTER"), "forwardSearch");
inputMap.put(KeyStroke.getKeyStroke("F3"), "forwardSearch");
actionMap.put("forwardSearch", forwardSearch);
// Resources injection
ResourceMap resource = Application.getInstance().getContext().getResourceMap(getClass());
resource.injectComponents(frame);
// Make sure the search entry field gets the focus at creation time
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowOpened (WindowEvent e)
{
searchField.requestFocus();
}
});
}
/**
* Returns whether or not bindings should be registered on the given
* <code>JComponent</code>. This is implemented to return true if the
* tool tip manager has a binding in any one of the
* <code>InputMaps</code> registered under the condition
* <code>WHEN_FOCUSED</code>.
* <p>
* This does not use <code>isFocusTraversable</code> as
* some components may override <code>isFocusTraversable</code> and
* base the return value on something other than bindings. For example,
* <code>JButton</code> bases its return value on its enabled state.
*
* @param component the <code>JComponent</code> in question
*/
private boolean shouldRegisterBindings(JComponent component) {
InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
while (inputMap != null && inputMap.size() == 0) {
inputMap = inputMap.getParent();
}
return (inputMap != null);
}
private void init( final ReportDesignerContext reportDesignerContext ) {
if ( reportDesignerContext == null ) {
throw new NullPointerException();
}
setTitle( Messages.getString( "WelcomePane.title" ) ); // NON-NLS
this.reportDesignerContext = reportDesignerContext;
this.newReportAction = new NewReportAction();
this.newReportAction.setReportDesignerContext( reportDesignerContext );
this.closeActionListener = new CloseActionListener();
showOnStartupCheckbox = new JCheckBox(
Messages.getString( "WelcomePane.showAtStartup" ), WorkspaceSettings.getInstance().isShowLauncher() ); // NON-NLS
showOnStartupCheckbox.addActionListener( new TriggerShowWelcomePaneAction() );
backgroundImage = Toolkit.getDefaultToolkit().createImage(
IconLoader.class.getResource( "/org/pentaho/reporting/designer/core/icons/WelcomeBackground.png" ) ); // NON-NLS
final WaitingImageObserver obs = new WaitingImageObserver( backgroundImage );
obs.waitImageLoaded();
setResizable( false );
initGUI();
pack();
final JComponent contentPane = (JComponent) getContentPane();
final InputMap inputMap = contentPane.getInputMap();
final ActionMap actionMap = contentPane.getActionMap();
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), "cancel" ); // NON-NLS
actionMap.put( "cancel", new CloseActionListener() ); // NON-NLS
}
/**
* Adds support for plus and minus menu commands, typically for zooming in
* and out. This is designed for menu items with key accelerators using
* KeyEvent.VK_ADD and KeyEvent.VK_SUBTRACT (which are typically on the
* numerical key pad). It adds support for KeyEvent.VK_MINUS and
* KeyEvent.VK_PLUS, and KeyEvent.VK_EQUALS for keyboard layouts with the
* plus sign as secondary key for the equals.
*
* @param zoomInAction action to call when the + key and the menu shortcut
* key are pressed
* @param zoomOutAction action to call when the - key and the menu shortcut
* key are pressed
* @param component component that will receive events and store actions
*/
public static void zoomMenuCommands(Action zoomInAction, Action zoomOutAction, JComponent component) {
InputMap inputMap = component.getInputMap();
ActionMap actionMap = component.getActionMap();
zoomMenuCommands(inputMap, actionMap, zoomInAction, zoomOutAction);
}