alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core;

import java.util.Vector;
import java.util.Collections;
import java.util.Comparator;
import java.util.ResourceBundle;

import java.awt.*;
import javax.swing.text.Keymap;
import javax.swing.*;

import org.openide.DialogDescriptor;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

/** A panel used in SHortcutsEditor for editing shortcuts (list of shortcuts).
*
* @author Ian Formanek
*/
public class ShortcutsPanel extends javax.swing.JPanel {

    private Keymap map;
    private Vector strokes;

    private ShortcutsEditor shortcutsEditor;

    private ActionsPanel addActionsPanel;

    /** Creates new form ActionsPanel */
    public ShortcutsPanel(ShortcutsEditor shortcutsEditor) {
        ResourceBundle bundle;

        this.shortcutsEditor = shortcutsEditor;
        updateData ();

        initComponents ();
        initAccessibility();
        addButton.setMnemonic((bundle = NbBundle.getBundle(ShortcutsPanel.class)).getString("ActionsPanel.shortcutAddButton.text_Mnemonic").charAt(0));
        removeButton.setMnemonic(bundle.getString("ActionsPanel.shortcutRemoveButton.text_Mnemonic").charAt(0));

        buttonsPanel.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.setCellRenderer (new KeyStrokeRenderer ());
        shortcutsList.setSelectedIndices(new int[0]);
        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
        java.awt.GridBagConstraints gridBagConstraints;

        shortcutsScrollPane = new javax.swing.JScrollPane();
        shortcutsList = new javax.swing.JList (strokes);
        buttonsPanel = new javax.swing.JPanel();
        shortcutTextArea = new javax.swing.JTextArea();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        shortcutsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                shortcutsListValueChanged(evt);
            }
        });

        shortcutsScrollPane.setViewportView(shortcutsList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(shortcutsScrollPane, gridBagConstraints);

        buttonsPanel.setLayout(new java.awt.GridBagLayout());

        shortcutTextArea.setWrapStyleWord(true);
        shortcutTextArea.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ACS_ShortcutsPanel.shortcutTextArea.text.a11yName"));
        shortcutTextArea.setLineWrap(true);
        shortcutTextArea.setEditable(false);
        shortcutTextArea.setText(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ShortcutsPanel.shortcutTextArea.text"));
        shortcutTextArea.setBackground(buttonsPanel.getBackground());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        buttonsPanel.add(shortcutTextArea, gridBagConstraints);

        addButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ShortcutsPanel.addButton.text"));
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
        buttonsPanel.add(addButton, gridBagConstraints);

        removeButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/core/Bundle").getString("ShortcutsPanel.removeButton.text"));
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 0);
        buttonsPanel.add(removeButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
        add(buttonsPanel, gridBagConstraints);

    }//GEN-END:initComponents

    private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        if (addActionsPanel == null) {
            addActionsPanel = new ActionsPanel (true, shortcutsEditor);
            addActionsPanel.setBorder(new javax.swing.border.EmptyBorder(12, 12, 0, 11));
        }
        DialogDescriptor dd = new DialogDescriptor (addActionsPanel, org.openide.util.NbBundle.getBundle(ShortcutsPanel.class).getString("ShortcutsPanel.AddActionTitle"));
        java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dd);
        d.setVisible (true);
        if (dd.getValue() == DialogDescriptor.OK_OPTION) {
            updateData ();
            shortcutsList.setListData(strokes);
            shortcutsEditor.setModified (true);
        }
    }//GEN-LAST:event_addButtonActionPerformed

    private void shortcutsListValueChanged (javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_shortcutsListValueChanged
        updateButtons ();
    }//GEN-LAST:event_shortcutsListValueChanged

    private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        int[] selectedIndices = shortcutsList.getSelectedIndices();
        Object[] selected = shortcutsList.getSelectedValues();
        for (int i = 0; i < selected.length; i++) {
            map.removeKeyStrokeBinding((KeyStroke)selected[i]);
            strokes.remove (selected[i]);
        }
        if (selectedIndices.length == 1) {
            shortcutsList.setSelectedIndex (selectedIndices[0]);
        } else {
            shortcutsList.setSelectedIndices (new int[0]);
        }
        shortcutsList.revalidate ();
        shortcutsList.repaint ();
        shortcutsEditor.setModified (true);
    }//GEN-LAST:event_removeButtonActionPerformed

    private void updateData () {
        map = (Keymap)Lookup.getDefault ().lookup (Keymap.class);
        KeyStroke[] strokesArray = map.getBoundKeyStrokes();
        strokes = new Vector ();
        for (int i = 0; i < strokesArray.length; i++) {
            strokes.addElement (strokesArray[i]);
        }

        Collections.sort (strokes, new Comparator () {
                              public int compare (Object o1, Object o2) {
                                  int k1 = ((KeyStroke)o1).getKeyCode ();
                                  int k2 = ((KeyStroke)o2).getKeyCode ();
                                  if (k1 < k2) return -1;
                                  else if (k1 > k2) return 1;
                                  else {
                                      int m1 = ((KeyStroke)o1).getModifiers ();
                                      int m2 = ((KeyStroke)o2).getModifiers ();
                                      if (m1 < m2) return -1;
                                      else if (m1 > m2) return 1;
                                      else return 0;
                                  }
                              }
                          }
                         );
    }

    private void updateButtons () {
        removeButton.setEnabled(shortcutsList.getSelectedIndices().length > 0);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane shortcutsScrollPane;
    private javax.swing.JButton addButton;
    private javax.swing.JTextArea shortcutTextArea;
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JList shortcutsList;
    private javax.swing.JButton removeButton;
    // End of variables declaration//GEN-END:variables

    private void initAccessibility() {
        ResourceBundle bundle = NbBundle.getBundle(ShortcutsPanel.class);
        shortcutsList.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_ShortcutsPanel.shortcutsList"));
        shortcutsList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ShortcutsPanel.shortcutsList"));
        shortcutTextArea.getAccessibleContext().setAccessibleName(bundle.getString("ACS_ShortcutsPanel.shortcutTextArea.text.a11yName"));
        addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ShortcutsPanel.addButton.text.a11yDesc"));
        removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ShortcutsPanel.removeButton.text.a11yDesc"));
    }
    
    /** Sets focus to the default component */
    protected void setFocusDefault () {
        shortcutsList.requestFocus();
    }

    static class KeyStrokeRenderer extends DefaultListCellRenderer {
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent (list, value, index, isSelected, cellHasFocus);
            setText (" " + ShortcutsFolder.getKeyStrokeName ((KeyStroke)value)); // the leading space provides empty border on the left side of the list // NOI18N
            return this;
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.