下面列出了java.awt.event.HierarchyBoundsListener#javax.swing.ActionMap 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
}
}
private void init() {
initialized = true;
setDefaultRenderer(Object.class, new DefaultOutlineCellRenderer());
ActionMap am = getActionMap();
//make rows expandable with left/rigt arrow keys
Action a = am.get("selectNextColumn"); //NOI18N
am.put("selectNextColumn", new ExpandAction(true, a)); //NOI18N
a = am.get("selectPreviousColumn"); //NOI18N
am.put("selectPreviousColumn", new ExpandAction(false, a)); //NOI18N
getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (getSelectedRowCount() == 1) {
selectedRow = getSelectedRow();
} else {
selectedRow = -1;
}
}
});
}
public KeyStroke registerAction(String actionKey, Action action, ActionMap actionMap, InputMap inputMap) {
KeyStroke ks = null;
if (FIND_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_G, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
} else if (FIND_NEXT_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
} else if (FIND_PREV_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_F3, InputEvent.SHIFT_MASK);
} else if (FIND_SEL_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_F3, InputEvent.CTRL_MASK);
}
if (ks != null) {
actionMap.put(actionKey, action);
inputMap.put(ks, actionKey);
}
return ks;
}
/** Opened for the first time */
@Override
protected void componentOpened() {
Log.getLogger().entering("QueryBuilder", "componentOpened");
activateActions();
ActionMap map = getActionMap();
InputMap keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
installActions(map, keys);
// QueryBuilder does not need to listen to VSE, because it will notify us
// directly if something changes. The SqlCommandCustomizer needs to listen
// to VSE, because that's the only way it is notified of changes to the command
// sqlStatement.addPropertyChangeListener(sqlStatementListener) ;
// vse.addPropertyChangeListener(sqlStatementListener) ;
// do NOT force a parse here. It's done in componentShowing().
// populate( sqlStatement.getCommand()) ;
}
private JComponent getPresetPanel() {
final ActionMap actionMap = this.getContext().getActionMap(this.getClass(), this);
final JPanel presetPanel = new JPanel(new MigLayout("", "[grow, fill][]", ""));
presetPanel.setBorder(BorderFactory
.createTitledBorder(app.getResourceMap().getString("mainFrame.port_mapping_presets.title")));
portMappingPresets = new JList<>(new PresetListModel(app.getSettings()));
portMappingPresets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
portMappingPresets.setLayoutOrientation(JList.VERTICAL);
portMappingPresets.addListSelectionListener(e -> {
logger.trace("Selection of preset list has changed: {}", isPresetMappingSelected());
firePropertyChange(PROPERTY_PRESET_MAPPING_SELECTED, false, isPresetMappingSelected());
});
presetPanel.add(new JScrollPane(portMappingPresets), "spany 4, grow");
presetPanel.add(new JButton(actionMap.get(ACTION_CREATE_PRESET_MAPPING)), "wrap, sizegroup preset_buttons");
presetPanel.add(new JButton(actionMap.get(ACTION_EDIT_PRESET_MAPPING)), "wrap, sizegroup preset_buttons");
presetPanel.add(new JButton(actionMap.get(ACTION_REMOVE_PRESET_MAPPING)), "wrap, sizegroup preset_buttons");
presetPanel.add(new JButton(actionMap.get(ACTION_USE_PRESET_MAPPING)), "wrap, sizegroup preset_buttons");
return presetPanel;
}
public KeyStroke registerAction(String actionKey, Action action, ActionMap actionMap, InputMap inputMap) {
KeyStroke ks = null;
if (FIND_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK);
} else if (FIND_NEXT_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
} else if (FIND_PREV_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_F3, InputEvent.SHIFT_MASK);
} else if (FIND_SEL_ACTION_KEY.equals(actionKey)) {
ks = KeyStroke.getKeyStroke(KeyEvent.VK_F3, InputEvent.CTRL_MASK);
}
if (ks != null) {
actionMap.put(actionKey, action);
inputMap.put(ks, actionKey);
}
return ks;
}
/**
* Creates new form CompareResultImages
*/
public CompareResultImages() {
initComponents();
toReviewList.setModel(new DefaultListModel());
fillList();
setSize(800, 600);
setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
actualImage.setStretch(false);
referenceImage.setStretch(false);
acceptAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control D"));
InputMap keyMap = new ComponentInputMap(acceptButton);
keyMap.put(KeyStroke.getKeyStroke("control D"), "accept");
ActionMap actionMap = new ActionMapUIResource();
actionMap.put("accept", acceptAction);
SwingUtilities.replaceUIActionMap(acceptButton, actionMap);
SwingUtilities.replaceUIInputMap(acceptButton, JComponent.WHEN_IN_FOCUSED_WINDOW, keyMap);
}
/** Creates new form TemplatesPanel */
public TemplatesPanel (String pathToSelect) {
ActionMap map = getActionMap ();
map.put (DefaultEditorKit.copyAction, ExplorerUtils.actionCopy (getExplorerManager ()));
map.put (DefaultEditorKit.cutAction, ExplorerUtils.actionCut (getExplorerManager ()));
map.put (DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste (getExplorerManager ()));
map.put ("delete", ExplorerUtils.actionDelete (getExplorerManager (), true)); // NOI18N
initComponents ();
createTemplateView ();
treePanel.add (view, BorderLayout.CENTER);
associateLookup (ExplorerUtils.createLookup (getExplorerManager (), map));
initialize (pathToSelect);
}
public JComponent getComponent() {
if (panel == null) {
final BeanTreeView view = new BeanTreeView();
view.setRootVisible(false);
view.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
class Panel extends JPanel implements ExplorerManager.Provider, Lookup.Provider {
// Make sure action context works correctly:
private final Lookup lookup = ExplorerUtils.createLookup(manager, new ActionMap());
{
setLayout(new BorderLayout());
add(view, BorderLayout.CENTER);
}
public ExplorerManager getExplorerManager() {
return manager;
}
public Lookup getLookup() {
return lookup;
}
}
panel = new Panel();
}
return panel;
}
/** Finds appropriate map to work with.
* @return map from lookup or from activated TopComponent, null no available
*/
private ActionMap map() {
if (result == null) {
TopComponent tc = TopComponent.getRegistry().getActivated();
if (tc != null) {
return tc.getActionMap();
}
} else {
for (ActionMap am : result.allInstances()) {
return am;
}
}
return null;
}
public JComponent getComponent() {
if (panel == null) {
final PropertySheetView view = new PropertySheetView();
class Panel extends JPanel implements ExplorerManager.Provider, Lookup.Provider {
// Make sure action context works correctly:
private final Lookup lookup = ExplorerUtils.createLookup(manager, new ActionMap());
{
setLayout(new BorderLayout());
add(view, BorderLayout.CENTER);
}
public ExplorerManager getExplorerManager() {
return manager;
}
public Lookup getLookup() {
return lookup;
}
}
panel = new Panel();
}
return panel;
}
/**
* Registers all hotkeys processed by the graph search field.
*/
private void registerHotkeys() {
final ActionMap actionMap = ((JTextField) getEditor().getEditorComponent()).getActionMap();
final InputMap imap = ((JTextField) getEditor().getEditorComponent()).getInputMap();
setActionMap(actionMap);
setInputMap(JComponent.WHEN_FOCUSED, imap);
imap.put(HotKeys.GRAPH_SEARCH_NEXT_KEY.getKeyStroke(), "NEXT");
imap.put(HotKeys.GRAPH_SEARCH_NEXT_ZOOM_KEY.getKeyStroke(), "NEXT_ZOOM");
imap.put(HotKeys.GRAPH_SEARCH_PREVIOUS_KEY.getKeyStroke(), "PREVIOUS");
imap.put(HotKeys.GRAPH_SEARCH_PREVIOUS_ZOOM_KEY.getKeyStroke(), "PREVIOUS_ZOOM");
actionMap.put("NEXT", CActionProxy.proxy(new CActionHotKey("NEXT")));
actionMap.put("NEXT_ZOOM", CActionProxy.proxy(new CActionHotKey("NEXT_ZOOM")));
actionMap.put("PREVIOUS", CActionProxy.proxy(new CActionHotKey("PREVIOUS")));
actionMap.put("PREVIOUS_ZOOM", CActionProxy.proxy(new CActionHotKey("PREVIOUS_ZOOM")));
}
public void testSimplifiedActionMapChanges81117() {
MultiViewTopComponentLookup.InitialProxyLookup lookup = new MultiViewTopComponentLookup.InitialProxyLookup(new ActionMap());
Lookup.Result res = lookup.lookup(new Lookup.Template(ActionMap.class));
LookListener list = new LookListener();
list.resetCount();
res.addLookupListener(list);
assertEquals(1, res.allInstances().size());
assertEquals(0, list.getCount());
lookup.refreshLookup();
assertEquals(1, list.getCount());
assertEquals(1, res.allInstances().size());
MultiViewTopComponentLookup lookup2 = new MultiViewTopComponentLookup(new ActionMap());
res = lookup2.lookup(new Lookup.Template(ActionMap.class));
list = new LookListener();
list.resetCount();
res.addLookupListener(list);
assertEquals(1, res.allInstances().size());
assertEquals(0, list.getCount());
lookup2.setElementLookup(Lookups.fixed(new Object[] {new Object()} ));
assertEquals(1, list.getCount());
assertEquals(1, res.allInstances().size());
}
/*** Finds an action that we should delegate to
* @return the action or null
*/
private Action findAction() {
Collection<? extends ActionMap> c = result != null ? result.allInstances() : Collections.<ActionMap>emptySet();
if (!c.isEmpty()) {
Object key = delegate.getActionMapKey();
for (ActionMap map : c) {
Action action = map.get(key);
if (action != null) {
return action;
}
}
}
return null;
}
public ExplorerPanel(Node n) {
manager = new ExplorerManager();
manager.setRootContext(n);
ActionMap map = getActionMap();
map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
map.put("delete", ExplorerUtils.actionDelete(manager, true));
associateLookup(ExplorerUtils.createLookup (manager, map));
setLayout(new BorderLayout());
add(new BeanTreeView());
setName(n.getDisplayName());
}
private JComponent getMappingsPanel() {
// Mappings panel
final ActionMap actionMap = this.getContext().getActionMap(this.getClass(), this);
tableModel = new PortMappingsTableModel(app);
mappingsTable = new JTable(tableModel);
mappingsTable.setAutoCreateRowSorter(true);
mappingsTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
mappingsTable.setSize(new Dimension(400, 100));
mappingsTable.getSelectionModel().addListSelectionListener(
e -> firePropertyChange(PROPERTY_MAPPING_SELECTED, false, isMappingSelected()));
final JScrollPane mappingsTabelPane = new JScrollPane();
mappingsTabelPane.setViewportView(mappingsTable);
final JPanel mappingsPanel = new JPanel(new MigLayout("", "[fill,grow]", "[grow,fill][]"));
mappingsPanel.setName("port_mappings");
final Border panelBorder = BorderFactory
.createTitledBorder(app.getResourceMap().getString("mainFrame.port_mappings.title"));
mappingsPanel.setBorder(panelBorder);
mappingsPanel.add(mappingsTabelPane, "height 100::, span 2, wrap");
mappingsPanel.add(new JButton(actionMap.get(ACTION_REMOVE_MAPPINGS)), "");
mappingsPanel.add(new JButton(actionMap.get(ACTION_UPDATE_PORT_MAPPINGS)), "wrap");
return mappingsPanel;
}
/**
* This function is for debug purposes only. Prints out the
*
* @param actionKeys
* @param am
*/
public static void dumpActionKeys(Map<String, Object[]> actionKeys, ActionMap am) {
for (String key : actionKeys.keySet()) {
System.out.println(key + ":");
for (Object val : (Object[]) actionKeys.get(key))
System.out.println(" - " + val + " >> " + am.get(val));
}
}
static void blickActionMapImpl(ActionMap map, Component[] focus) {
assert EventQueue.isDispatchThread();
Object obj = Lookup.getDefault ().lookup (ContextGlobalProvider.class);
if (obj instanceof GlobalActionContextImpl) {
GlobalActionContextImpl g = (GlobalActionContextImpl)obj;
Lookup[] arr = {
map == null ? Lookup.EMPTY : Lookups.singleton (map),
Lookups.exclude (g.getLookup (), new Class[] { javax.swing.ActionMap.class }),
};
Lookup originalLkp = g.getLookup();
Lookup prev = temporary;
try {
temporary = new ProxyLookup (arr);
Lookup actionsGlobalContext = Utilities.actionsGlobalContext();
Object q = actionsGlobalContext.lookup (javax.swing.ActionMap.class);
assert q == map : dumpActionMapInfo(map, q, prev, temporary, actionsGlobalContext, originalLkp);
if (focus != null) {
setFocusOwner(focus[0]);
}
} finally {
temporary = prev;
// fire the changes about return of the values back
org.openide.util.Utilities.actionsGlobalContext ().lookup (javax.swing.ActionMap.class);
}
}
}
public ExplorerPanel() {
manager = new ExplorerManager();
ActionMap map = getActionMap();
map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
map.put("delete", ExplorerUtils.actionDelete(manager, true));
}
private void addShortcutForWindows() {
if (PacketProxyUtility.getInstance().isMac()) {
return;
}
JPanel p = (JPanel) getContentPane();
InputMap im = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = p.getActionMap();
int hotkey = (KeyEvent.CTRL_MASK);
registerTabShortcut(KeyEvent.VK_H, hotkey, im, am, Panes.HISTORY.ordinal());
registerTabShortcut(KeyEvent.VK_I, hotkey, im, am, Panes.INTERCEPT.ordinal());
registerTabShortcut(KeyEvent.VK_R, hotkey, im, am, Panes.REPEATER.ordinal());
registerTabShortcut(KeyEvent.VK_B, hotkey, im, am, Panes.BULKSENDER.ordinal());
registerTabShortcut(KeyEvent.VK_O, hotkey, im, am, Panes.OPTIONS.ordinal());
registerTabShortcut(KeyEvent.VK_L, hotkey, im, am, Panes.LOG.ordinal());
}
public static KeyStroke registerAction(String actionKey, Action action, ActionMap actionMap, InputMap inputMap) {
for (ActionsSupportProvider provider : Lookup.getDefault().lookupAll(ActionsSupportProvider.class)) {
KeyStroke ks = provider.registerAction(actionKey, action, actionMap, inputMap);
if (ks != null) return ks;
}
return null;
}
ActionListener treeActionAdapter(ActionMap map, String actionName) {
final Action selectPrevious = map.get(actionName);
return e -> {
e.setSource(tree);
selectPrevious.actionPerformed(e);
};
}
public GlobalSearchAction() {
super(true, GLOBAL_SEARCH);
InputMap inputMap = ApplicationFrame.getApplicationFrame().getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap actionMap = ApplicationFrame.getApplicationFrame().getRootPane().getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), GLOBAL_SEARCH);
actionMap.put(GLOBAL_SEARCH, this);
}
private void initActions () {
InputMap inputMap = getInputMap( WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
ActionMap actionMap = getActionMap();
Object action = recentLabel.getClientProperty("openAction");
if (action instanceof Action) {
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_R, KeyEvent.ALT_DOWN_MASK, false ), "messageHistory" ); //NOI18N
actionMap.put("messageHistory", (Action) action); //NOI18N
}
action = templatesLabel.getClientProperty("openAction");
if (action instanceof Action) {
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_L, KeyEvent.ALT_DOWN_MASK, false ), "messageTemplate" ); //NOI18N
actionMap.put("messageTemplate", (Action) action); //NOI18N
}
}
/**
* Sets the parent of the passed in ActionMap to be the audio action
* map.
*/
static void installAudioActionMap(ActionMap map) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
map.setParent(((BasicLookAndFeel)laf).getAudioActionMap());
}
}
public StatExecutionsTopComponent() {
initComponents();
setDisplayName(Bundle.CTL_StatExecutionsTopComponent());
setName(ViewResource.TOP_NAME_StatExecutionsTopComponent);
setToolTipText(Bundle.HINT_StatExecutionsTopComponent());
setBackground(Color.WHITE);
setLayout(new BorderLayout());
add(new BeanTreeView(),BorderLayout.CENTER);
ActionMap map = this.getActionMap();
map.put("delete", ExplorerUtils.actionDelete(exm, true));
associateLookup(ExplorerUtils.createLookup(exm, this.getActionMap()));
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Sets the parent of the passed in ActionMap to be the audio action
* map.
*/
static void installAudioActionMap(ActionMap map) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
map.setParent(((BasicLookAndFeel)laf).getAudioActionMap());
}
}
/**
* Register the default hotkeys of a graph view.
*
* @param parent Parent window used for dialogs.
* @param panel The panel where the view is shown.
* @param debuggerProvider Provides the debugger used by some hotkeys.
* @param searchField The search field that is shown in the graph panel.
* @param addressField The address field that is shown in the graph panel.
*/
public static void registerHotKeys(final JFrame parent, final CGraphPanel panel,
final IFrontEndDebuggerProvider debuggerProvider, final CGraphSearchField searchField,
final CGotoAddressField addressField) {
Preconditions.checkNotNull(parent, "IE01606: Parent argument can not be null");
Preconditions.checkNotNull(panel, "IE01607: Panel argument can not be null");
Preconditions.checkNotNull(searchField, "IE01608: Search field argument can not be null");
Preconditions.checkNotNull(addressField, "IE01609: Address field argument can not be null");
final InputMap inputMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
final ActionMap actionMap = panel.getActionMap();
inputMap.put(HotKeys.GRAPH_GOTO_ADDRESS_FIELD_KEY.getKeyStroke(), "GOTO_ADDRESS_FIELD");
actionMap.put("GOTO_ADDRESS_FIELD", new AbstractAction() {
/**
* Used for serialization.
*/
private static final long serialVersionUID = -8994014581850287793L;
@Override
public void actionPerformed(final ActionEvent event) {
addressField.requestFocusInWindow();
}
});
inputMap.put(HotKeys.GRAPH_SHOW_HOTKEYS_ACCELERATOR_KEY.getKeyStroke(), "SHOW_HOTKEYS");
actionMap.put("SHOW_HOTKEYS", new CShowHotkeysAction(parent));
registerSearchKeys(panel.getModel().getGraph().getView(), searchField, inputMap, actionMap);
registerDebuggerKeys(panel.getModel().getParent(), panel.getModel().getGraph(),
debuggerProvider, inputMap, actionMap);
}
/**
* Sets the parent of the passed in ActionMap to be the audio action
* map.
*/
static void installAudioActionMap(ActionMap map) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
map.setParent(((BasicLookAndFeel)laf).getAudioActionMap());
}
}