|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.core; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.HashMap; import java.util.Collections; import javax.swing.Action; import javax.swing.DefaultListModel; import javax.swing.KeyStroke; import javax.swing.event.*; import javax.swing.text.Keymap; import javax.swing.tree.*; import javax.swing.JDialog; import org.openide.*; import org.openide.cookies.InstanceCookie; import org.openide.loaders.DataObject; import org.openide.loaders.DataFolder; import org.openide.loaders.InstanceDataObject; import org.openide.util.HelpCtx; import org.openide.util.Lookup; /** * * @author Ian Formanek */ public class ActionsPanel extends javax.swing.JPanel { private static final java.util.ResourceBundle bundle = org.openide.util.NbBundle.getBundle(ActionsPanel.class); private ShortcutsEditor shortcutsEditor; private HashMap actionToNode = new HashMap (41); private DefaultTreeModel model; private DefaultTreeSelectionModel treeSelectionModel; private Action selectedAction; private DefaultListModel shortcutsModel; private HashMap nameToStroke = new HashMap (11); /** Creates new form ActionsPanel */ public ActionsPanel(boolean canChangeKey, ShortcutsEditor shortcutsEditor) { this.shortcutsEditor = shortcutsEditor; initComponents (); initAccessibility (); shortcutAddButton.setMnemonic(bundle.getString("ActionsPanel.shortcutAddButton.text_Mnemonic").charAt(0)); shortcutRemoveButton.setMnemonic(bundle.getString("ActionsPanel.shortcutRemoveButton.text_Mnemonic").charAt(0)); shortcutPanel.setBorder (new javax.swing.border.CompoundBorder( new javax.swing.border.TitledBorder( new javax.swing.border.EtchedBorder(), bundle.getString ("ActionsPanel.ShortcutsTitle")), new javax.swing.border.EmptyBorder(new java.awt.Insets(12, 12, 11, 11)))); shortcutsList.setModel(shortcutsModel = new DefaultListModel ()); treeSelectionModel = new DefaultTreeSelectionModel (); treeSelectionModel.addTreeSelectionListener ( new TreeSelectionListener () { public void valueChanged (TreeSelectionEvent evt) { TreePath[] paths = actionsTree.getSelectionPaths (); if ((paths != null) && (paths.length == 1)) { TreeNode node = (TreeNode) paths[0].getLastPathComponent (); if ((node != null) && (node instanceof ActionNode)) { updateSelectedAction (((ActionNode)node).getAction ()); return; } } updateSelectedAction (null); } } ); treeSelectionModel.setSelectionMode (DefaultTreeSelectionModel.SINGLE_TREE_SELECTION); actionsTree.setModel (model = new DefaultTreeModel (createActionsRootNode ())); actionsTree.setSelectionModel (treeSelectionModel); actionsTree.setShowsRootHandles(true); actionsTree.putClientProperty("JTree.lineStyle", "Angled"); // NOI18N updateButtons (); HelpCtx.setHelpIDString (this, ActionsPanel.class.getName ()); } void setAction (Action action) { TreeNode tn = (TreeNode)actionToNode.get (action.getClass ()); if (tn != null) { TreePath tp = new TreePath (model.getPathToRoot (tn)); treeSelectionModel.setSelectionPath(tp); actionsTree.scrollPathToVisible(tp); updateSelectedAction (action); } } private void updateSelectedAction (Action action) { selectedAction = action; Keymap map = (Keymap)Lookup.getDefault().lookup(Keymap.class); KeyStroke[] strokes = map.getKeyStrokesForAction(action); shortcutsModel.removeAllElements (); nameToStroke.clear (); for (int i = 0; i < strokes.length; i++) { String keyName = ShortcutsFolder.getKeyText (strokes[i]); nameToStroke.put (keyName, strokes[i]); shortcutsModel.addElement (keyName); } updateButtons (); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the FormEditor. */ private void initComponents() {//GEN-BEGIN:initComponents actionsPanel = new javax.swing.JPanel(); actionsScrollPane = new javax.swing.JScrollPane(); actionsTree = new javax.swing.JTree(); shortcutPanel = new javax.swing.JPanel(); shortcutsScrollPane = new javax.swing.JScrollPane(); shortcutsList = new javax.swing.JList(); shortcutsButtonsPanel = new javax.swing.JPanel(); shortcutAddButton = new javax.swing.JButton(); shortcutRemoveButton = new javax.swing.JButton(); setLayout(new java.awt.GridBagLayout()); java.awt.GridBagConstraints gridBagConstraints1; actionsPanel.setLayout(new java.awt.BorderLayout()); actionsTree.setRootVisible(false); actionsScrollPane.setViewportView(actionsTree); actionsPanel.add(actionsScrollPane, java.awt.BorderLayout.CENTER); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints1.weightx = 1.0; gridBagConstraints1.weighty = 1.0; add(actionsPanel, gridBagConstraints1); shortcutPanel.setLayout(new java.awt.GridBagLayout()); java.awt.GridBagConstraints gridBagConstraints2; shortcutsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { shortcutsListValueChanged(evt); } }); shortcutsScrollPane.setViewportView(shortcutsList); gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints2.weightx = 1.0; shortcutPanel.add(shortcutsScrollPane, gridBagConstraints2); shortcutsButtonsPanel.setLayout(new java.awt.GridBagLayout()); java.awt.GridBagConstraints gridBagConstraints3; shortcutAddButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ActionsPanel.shortcutAddButton.text")); shortcutAddButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { shortcutAddButtonActionPerformed(evt); } }); gridBagConstraints3 = new java.awt.GridBagConstraints(); gridBagConstraints3.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL; shortcutsButtonsPanel.add(shortcutAddButton, gridBagConstraints3); shortcutRemoveButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ActionsPanel.shortcutRemoveButton.text")); shortcutRemoveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { shortcutRemoveButtonActionPerformed(evt); } }); gridBagConstraints3 = new java.awt.GridBagConstraints(); gridBagConstraints3.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints3.insets = new java.awt.Insets(5, 0, 0, 0); gridBagConstraints3.anchor = java.awt.GridBagConstraints.NORTH; shortcutsButtonsPanel.add(shortcutRemoveButton, gridBagConstraints3); gridBagConstraints2 = new java.awt.GridBagConstraints(); gridBagConstraints2.insets = new java.awt.Insets(0, 12, 0, 0); shortcutPanel.add(shortcutsButtonsPanel, gridBagConstraints2); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints1.insets = new java.awt.Insets(12, 0, 0, 0); gridBagConstraints1.weightx = 1.0; add(shortcutPanel, gridBagConstraints1); }//GEN-END:initComponents private void shortcutRemoveButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shortcutRemoveButtonActionPerformed Object[] selectedItems = shortcutsList.getSelectedValues(); Keymap map = (Keymap)Lookup.getDefault().lookup(Keymap.class); for (int i = 0; i < selectedItems.length; i++) { KeyStroke ks = (KeyStroke)nameToStroke.get (selectedItems[i]); if (ks != null) { shortcutsEditor.removeShortcut(ks, map.getAction(ks)); map.removeKeyStrokeBinding(ks); } } updateSelectedAction (selectedAction); shortcutsEditor.setModified (true); updateTree (); }//GEN-LAST:event_shortcutRemoveButtonActionPerformed private void shortcutAddButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shortcutAddButtonActionPerformed KeyStroke existingStroke = null; boolean shortcutAccepted = false; Keymap map = (Keymap)Lookup.getDefault().lookup(Keymap.class); while (true) { ShortcutEnterPanel sep = new ShortcutEnterPanel (existingStroke); DialogDescriptor dd = new DialogDescriptor (sep, bundle.getString ("ActionsPanel.AddShortcut")); JDialog addshort = (JDialog)org.openide.DialogDisplayer.getDefault().createDialog(dd); addshort.setVisible(true); if (dd.getValue().equals (DialogDescriptor.OK_OPTION)) { existingStroke = sep.getShortcut (); if (existingStroke.getKeyCode() == 0 && existingStroke.getModifiers() == 0) return; Action a = map.getAction (existingStroke); if (a != null) { String text = java.text.MessageFormat.format ( bundle.getString ("FMT_ActionsPanel.AlreadyBound"), new Object[] { a.getValue(Action.NAME) } ); NotifyDescriptor nd = new NotifyDescriptor.Confirmation (text); Object result = org.openide.DialogDisplayer.getDefault ().notify (nd); if (result.equals (NotifyDescriptor.YES_OPTION)) { shortcutAccepted = true; // yes, will replace eisting shortcut with new one shortcutsEditor.removeShortcut(existingStroke, a); } else if (result.equals (NotifyDescriptor.NO_OPTION)) { return; // no, will keep the old shortcut } // else open the shortcut selector again } else { shortcutAccepted = true; } } else { return; // adding cancelled } if (shortcutAccepted) { map.addActionForKeyStroke(existingStroke, selectedAction); shortcutsEditor.addShortcut(existingStroke, selectedAction); updateSelectedAction (selectedAction); shortcutsEditor.setModified (true); updateTree (); return; } } }//GEN-LAST:event_shortcutAddButtonActionPerformed private void shortcutsListValueChanged (javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_shortcutsListValueChanged updateButtons (); }//GEN-LAST:event_shortcutsListValueChanged /** Sets focus to the default component */ private void updateButtons () { shortcutAddButton.setEnabled (selectedAction != null); shortcutRemoveButton.setEnabled ((selectedAction != null) && (shortcutsList.getSelectedIndices().length > 0)); } void updateTree () { // next two lines are hack to force the Tree to invalidate sizes of renderes actionsTree.setShowsRootHandles(false); actionsTree.setShowsRootHandles(true); actionsTree.repaint (); actionsTree.revalidate (); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel actionsPanel; private javax.swing.JScrollPane actionsScrollPane; private javax.swing.JTree actionsTree; private javax.swing.JPanel shortcutPanel; private javax.swing.JScrollPane shortcutsScrollPane; private javax.swing.JList shortcutsList; private javax.swing.JPanel shortcutsButtonsPanel; private javax.swing.JButton shortcutAddButton; private javax.swing.JButton shortcutRemoveButton; // End of variables declaration//GEN-END:variables // ----------------------------------------------------------------------------- // TreeNode for actions tree TreeNode createActionsRootNode () { final ArrayList actionGroups = new ArrayList (); DataFolder actionsFolder = NbPlaces.getDefault().actions (); DataObject[] actionsChildren = actionsFolder.getChildren (); for (int i = 0; i < actionsChildren.length; i++) { if (actionsChildren[i] instanceof DataFolder) actionGroups.add (new ActionsGroupNode ((DataFolder)actionsChildren[i], null)); } return new TreeNode () { public TreeNode getChildAt(int childIndex) { return (TreeNode) actionGroups.get(childIndex); } public int getChildCount() { return actionGroups.size (); } public TreeNode getParent() { return null; } public int getIndex(TreeNode node) { return actionGroups.indexOf (node); } public boolean getAllowsChildren() { return true; } public boolean isLeaf() { return false; } public java.util.Enumeration children() { return Collections.enumeration (actionGroups); } }; } protected void setFocusDefault () { actionsTree.requestFocus(); } private void initAccessibility() { java.util.ResourceBundle bundle = org.openide.util.NbBundle.getBundle(ActionsPanel.class); actionsTree.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_ActionsPanel.actionsTree")); actionsTree.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ActionsPanel.actionsTree")); shortcutsList.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_ActionsPanel.shortcutsList")); shortcutsList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ActionsPanel.shortcutsList")); shortcutAddButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ShortcutsPanel.addButton.text.a11yDesc")); shortcutRemoveButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ShortcutsPanel.removeButton.text.a11yDesc")); getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ActionsPanel")); } class ActionsGroupNode implements TreeNode { private DataFolder folder; private ArrayList actions; private TreeNode parent; ActionsGroupNode (DataFolder folder, TreeNode parent) { this.folder = folder; this.parent = parent; DataObject[] children = folder.getChildren (); actions = new ArrayList (children.length); for (int i = 0; i < children.length; i++) { InstanceCookie ic = (InstanceCookie)children[i].getCookie(InstanceCookie.class); if (ic != null) { try { Class instClass = ic.instanceClass (); if (Action.class.isAssignableFrom (instClass)) { TreeNode tn = new ActionNode ((Action)ic.instanceCreate(), ActionsGroupNode.this); actionToNode.put (instClass, tn); actions.add (tn); } } catch (Exception e) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); } catch (LinkageError le) { ErrorManager.getDefault().notify(le); } } if (children[i] instanceof DataFolder) { TreeNode tn = new ActionsGroupNode ((DataFolder)children[i], ActionsGroupNode.this); actions.add (tn); } } } public TreeNode getChildAt(int childIndex) { return (TreeNode) actions.get(childIndex); } public int getChildCount() { return actions.size (); } public TreeNode getParent() { return parent; } public int getIndex(TreeNode node) { return actions.indexOf (node); } public boolean getAllowsChildren() { return true; } public boolean isLeaf() { return false; } public java.util.Enumeration children() { return Collections.enumeration (actions); } public String toString () { return folder.getNodeDelegate().getDisplayName (); } } static class ActionNode implements TreeNode { private Action action; private TreeNode parent; ActionNode (Action action, TreeNode parent) { this.action = action; this.parent = parent; } Action getAction () { return action; } public TreeNode getChildAt(int childIndex) { return null; } public int getChildCount() { return 0; } public TreeNode getParent() { return parent; } public int getIndex(TreeNode node) { return -1; } public boolean getAllowsChildren() { return false; } public boolean isLeaf() { return true; } public java.util.Enumeration children() { return null; } public String toString () { return ShortcutsFolder.getActionName (action); } } static class ShortcutEnterPanel extends javax.swing.JPanel { private javax.swing.JLabel shortcutLabel; private javax.swing.JTextField shortcutField; private KeyStroke defaultStroke; private int currentKeyCode; private int currentModifiers; ShortcutEnterPanel (KeyStroke stroke) { defaultStroke = stroke; shortcutLabel = new javax.swing.JLabel (bundle.getString ("ActionsPanel.ShortcutLabel")); shortcutField = new javax.swing.JTextField (); shortcutLabel.setDisplayedMnemonic(bundle.getString("ActionsPanel.ShortcutLabel_Mnemonic").charAt(0)); shortcutLabel.setLabelFor(shortcutField); this.setLayout (new java.awt.GridBagLayout ()); java.awt.GridBagConstraints gridBagConstraints1; this.setBorder (new javax.swing.border.EmptyBorder(new java.awt.Insets(8, 8, 8, 8))); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.insets = new java.awt.Insets (0, 0, 0, 8); this.add (shortcutLabel, gridBagConstraints1); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints1.weightx = 1.0; this.add (shortcutField, gridBagConstraints1); this.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ActionsPanel.Dialog")); shortcutField.addKeyListener (new java.awt.event.KeyAdapter () { public void keyPressed (java.awt.event.KeyEvent evt) { shortcutFieldKeyPressed (evt); } public void keyReleased (java.awt.event.KeyEvent evt) { shortcutFieldKeyReleased (evt); } public void keyTyped (java.awt.event.KeyEvent evt) { shortcutFieldKeyTyped (evt); } } ); if (defaultStroke != null) { shortcutField.setText(ShortcutsFolder.getKeyText (defaultStroke)); currentKeyCode = defaultStroke.getKeyCode(); currentModifiers = defaultStroke.getModifiers(); } else shortcutField.setText (""); // NOI18N //initAccessibility shortcutLabel.setLabelFor(shortcutField); shortcutField.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ACS_ActionsPanel.ShortcutLabel.a11yName")); shortcutField.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ACS_ActionsPanel.ShortcutLabel.a11yDesc")); } public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension (300, 50); } KeyStroke getShortcut () { return KeyStroke.getKeyStroke (currentKeyCode, currentModifiers); } private void shortcutFieldKeyReleased (java.awt.event.KeyEvent evt) { evt.consume(); switch (currentKeyCode) { case KeyEvent.VK_ALT: case KeyEvent.VK_ALT_GRAPH: case KeyEvent.VK_CONTROL: case KeyEvent.VK_SHIFT: case KeyEvent.VK_META: // Not finished entering key if (defaultStroke != null) { shortcutField.setText(ShortcutsFolder.getKeyText (defaultStroke)); } else { shortcutField.setText(""); } break; } } private void shortcutFieldKeyTyped (java.awt.event.KeyEvent evt) { evt.consume(); } private void shortcutFieldKeyPressed (java.awt.event.KeyEvent evt) { evt.consume(); currentKeyCode = evt.getKeyCode(); currentModifiers = evt.getModifiers(); shortcutField.setText(ShortcutsFolder.getKeyText (currentKeyCode, currentModifiers)); } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.