|
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.util.*; import javax.swing.*; import javax.swing.table.*; import org.openide.*; import org.openide.util.NbBundle; import org.openide.util.HelpCtx; import org.openide.NotifyDescriptor; import java.text.MessageFormat; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; /** * Component for visual editing of Map of abbreviations. When you enter new * abbreviation with the already used abbrev, it will replace the existing one. * abbreviations with empty expanded form are perfectly valid, but abbreviations * with empty abbrev field are simply ignored. * * @author Petr Nejedly */ public class AbbrevsEditorPanel extends javax.swing.JPanel { PairStringModel model; // The master we talk to about changes in map private AbbrevsEditor editor; private FontSizeTable abbrevsTable; /** Creates new form AbbrevsEditorPanel */ public AbbrevsEditorPanel( AbbrevsEditor editor ) { this.editor = editor; model = new PairStringModel(); initComponents (); abbrevsTable = new FontSizeTable(); abbrevsTable.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(8, 8, 8, 8))); abbrevsTable.setModel(model); abbrevsTable.setShowHorizontalLines(false); abbrevsTable.setShowVerticalLines(false); abbrevsTable.setSelectionMode( DefaultListSelectionModel.SINGLE_SELECTION ); // Set the width of columns to 30% and 70% TableColumnModel col = abbrevsTable.getColumnModel(); col.getColumn( 0 ).setMaxWidth( 3000 ); col.getColumn( 0 ).setPreferredWidth( 30 ); col.getColumn( 1 ).setMaxWidth( 7000 ); col.getColumn( 1 ).setPreferredWidth( 70 ); abbrevsPane.setViewportView(abbrevsTable); getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP")); // NOI18N abbrevsTable.getAccessibleContext().setAccessibleName(getBundleString("ACSN_AEP_Table")); // NOI18N abbrevsTable.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Table")); // NOI18N addButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Add")); // NOI18N editButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Edit")); // NOI18N removeButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Remove")); // NOI18N enableButtons(false); abbrevsTable.registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent evt) { SwingUtilities.getAncestorOfClass(Window.class, AbbrevsEditorPanel.this).hide(); }}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED ); abbrevsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting()) return; // XXX - hack because of bugparade's 4801274 if (abbrevsTable.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(AbbrevsEditorPanel.class, s); } /** * Fill in editor with initial values */ public void setValue( Map m ) { // Our model is the one and only holding data if (m != null) model.setData( new TreeMap( m ) ); else model.setData( new TreeMap() ); // select first item, just to have something selected if( model.getRowCount() > 0 ) abbrevsTable.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 * abbrevs mapping. */ private void notifyEditor() { if( editor != null ) editor.customEditorChange(); } private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; abbrevsPane = 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(abbrevsPane, gridBagConstraints); addButton.setMnemonic(getBundleString("AEP_Add_Mnemonic").charAt (0)); addButton.setText(getBundleString( "AEP_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.setMnemonic(getBundleString("AEP_Edit_Mnemonic").charAt (0)); editButton.setText(getBundleString( "AEP_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.setMnemonic(getBundleString("AEP_Remove_Mnemonic").charAt (0)); removeButton.setText(getBundleString( "AEP_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[] abbrev = getAbbrev( null ); // If user canceled entering, do noting if( abbrev == null ) return; int index = model.putPair( abbrev ); // can silently replace existing mapping abbrevsTable.setRowSelectionInterval( index, index ); notifyEditor(); }//GEN-LAST:event_addButtonActionPerformed private void editButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed int index = abbrevsTable.getSelectedRow(); if( index != -1 ) { // is something selected? String[] pair = model.getPair( index ); pair = getAbbrev( pair ); if( pair != null ) { model.removePair( index ); index = model.putPair( pair ); abbrevsTable.setRowSelectionInterval( index, index ); notifyEditor(); } } }//GEN-LAST:event_editButtonActionPerformed private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed int index = abbrevsTable.getSelectedRow(); if( index != -1 ) { // is something selected? model.removePair( index ); if( index >= model.getRowCount() ) index--; if( index >= 0 ) abbrevsTable.setRowSelectionInterval( index, index ); notifyEditor(); } }//GEN-LAST:event_removeButtonActionPerformed /** * Creates a dialog asking user for pair of Strings. * @param abbrev 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.