|
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.modules.editor.options; import java.awt.Dialog; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Window; import java.awt.event.*; import java.text.MessageFormat; import java.util.*; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.*; import org.openide.*; import org.openide.util.NbBundle; import org.openide.util.HelpCtx; /** * Component for visual editing of Map of macroiations. When you enter new * macroiation with the already used macro, it will replace the existing one. * macroiations with empty expanded form are perfectly valid, but macroiations * with empty macro field are simply ignored. * * @author David Konecny */ public class MacrosEditorPanel extends javax.swing.JPanel { PairStringModel model; // The master we talk to about changes in map private MacrosEditor editor; private FontSizeTable macrosTable; /** Creates new form MacrosEditorPanel */ public MacrosEditorPanel(MacrosEditor editor) { this.editor = editor; model = new PairStringModel(); initComponents (); macrosTable = new FontSizeTable(); macrosTable.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(8, 8, 8, 8))); macrosTable.setModel(model); macrosTable.setShowVerticalLines(false); macrosTable.setShowHorizontalLines(false); macrosTable.setSelectionMode( DefaultListSelectionModel.SINGLE_SELECTION ); // Set the width of columns to 30% and 70% TableColumnModel col = macrosTable.getColumnModel(); col.getColumn( 0 ).setMaxWidth( 3000 ); col.getColumn( 0 ).setPreferredWidth( 30 ); col.getColumn( 1 ).setMaxWidth( 7000 ); col.getColumn( 1 ).setPreferredWidth( 70 ); macrosPane.setViewportView(macrosTable); getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP")); // NOI18N macrosTable.getAccessibleContext().setAccessibleName(getBundleString("ACSN_MEP_Table")); // NOI18N macrosTable.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Table")); // NOI18N addButton.setMnemonic(getBundleString("MEP_Add_Mnemonic").charAt(0)); // NOI18N editButton.setMnemonic(getBundleString("MEP_Edit_Mnemonic").charAt(0)); // NOI18N removeButton.setMnemonic(getBundleString("MEP_Remove_Mnemonic").charAt(0)); // NOI18N addButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Add")); // NOI18N editButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Edit")); // NOI18N removeButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Remove")); // NOI18N enableButtons(false); macrosTable.registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent evt) { SwingUtilities.getAncestorOfClass(Window.class, MacrosEditorPanel.this).hide(); }}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED ); macrosTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting()) return; // XXX - hack because of bugparade's 4801274 if (macrosTable.getRowCount() == 0){ enableButtons(false); return; } // valid fix of #35096 ListSelectionModel lsm = (ListSelectionModel)e.getSource(); enableButtons(!lsm.isSelectionEmpty()); } }); } private void enableButtons(boolean enable){ editButton.setEnabled(enable); removeButton.setEnabled(enable); } private String getBundleString(String s) { return NbBundle.getMessage(MacrosEditorPanel.class, s); } /** * Fill in editor with initial values */ public void setValue( Map m ) { HashMap hm; if (m != null) hm = new HashMap(m); else hm = new HashMap(); if (hm.containsKey(null)) { hm.remove(null); } // Our model is the one and only holding data model.setData( new TreeMap( hm ) ); // select first item, just to have something selected if( model.getRowCount() > 0 ) macrosTable.setRowSelectionInterval( 0, 0 ); } /** * Take the result of users modifications */ public Map getValue() { return model.getData(); } /** * Tell the editor (and in round the system), that user've changed * macros mapping. */ private void notifyEditor() { if( editor != null ) editor.customEditorChange(); } private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; macrosPane = new javax.swing.JScrollPane(); addButton = new javax.swing.JButton(); editButton = new javax.swing.JButton(); removeButton = new javax.swing.JButton(); setLayout(new java.awt.GridBagLayout()); setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(12, 12, 11, 11))); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridheight = 4; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12); add(macrosPane, gridBagConstraints); addButton.setText(getBundleString( "MEP_Add" )); addButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0); add(addButton, gridBagConstraints); editButton.setText(getBundleString( "MEP_Edit" )); editButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { editButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0); add(editButton, gridBagConstraints); removeButton.setText(getBundleString( "MEP_Remove" )); 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 = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; add(removeButton, gridBagConstraints); }//GEN-END:initComponents private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed String[] macro = getMacro( null ); // If user canceled entering, do noting if( macro == null ) return; int index = model.putPair( macro ); // can silently replace existing mapping macrosTable.setRowSelectionInterval( index, index ); notifyEditor(); }//GEN-LAST:event_addButtonActionPerformed private void editButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed int index = macrosTable.getSelectedRow(); if( index != -1 ) { // is something selected? String[] pair = model.getPair( index ); pair = getMacro( pair ); if( pair != null ) { model.removePair( index ); index = model.putPair( pair ); macrosTable.setRowSelectionInterval( index, index ); notifyEditor(); } } }//GEN-LAST:event_editButtonActionPerformed private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed int index = macrosTable.getSelectedRow(); if( index != -1 ) { // is something selected? model.removePair( index ); if( index >= model.getRowCount() ) index--; if( index >= 0 ) macrosTable.setRowSelectionInterval( index, index ); notifyEditor(); } }//GEN-LAST:event_removeButtonActionPerformed /** * Creates a dialog asking user for pair of Strings. * @param macro value to be preset in dialog, or |
... 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.