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

package org.netbeans.modules.editor.java;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import org.openide.util.NbBundle;

/**
 * JTable with custom renderer, so second column looks editable (JComboBox).
 * Second column also has CellEditor (also a JComboBox).
 *
 * @author  eakle, Martin Roskanin
 */
public class FixDuplicateImportStmts extends javax.swing.JPanel{
    private PackagesTblModel tblModel = null;
    
    public FixDuplicateImportStmts(String[] simpleNames, String[][] choices, String[] defaults) {
        initComponents();
        initComponentsMore(simpleNames, choices, defaults);
        setAccessible();
    }

    private void initComponentsMore(String simpleNames[], String choices[][], String defaults[]) {
        tblModel = new PackagesTblModel(simpleNames.length);
        packagesTbl.setModel(tblModel);
        packagesTbl.setColumnSelectionInterval(1,1);        
        packagesTbl.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        packagesTbl.setSurrendersFocusOnKeystroke(true);
        

        for (int i=0; i~More than one class found in classpath for some Type Name in source.  Select class to use in import statement for each Type Name below:"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
        add(jLabel1, gridBagConstraints);

        packagesTbl.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"MyType", "com.foo.AllTypes"}, // NOI18N
                {"OtherType", "com.foo.AllTypes"}, // NOI18N
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "~Type Name", "~Class to Import" // NOI18N
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(packagesTbl);

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

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

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable packagesTbl;
    // End of variables declaration//GEN-END:variables
    
    // ------------------ Inner Classes -------------------------
    /*
     * Service information for a selected Organization
     */
    private static class PackagesTblModel extends javax.swing.table.DefaultTableModel {
        
        String columnNames[] = {
            getBundleString("FixDupImportStmts_TblCol1Hdr"), //NOI18N
            getBundleString("FixDupImportStmts_TblCol2Hdr")  //NOI18N
        };
        
        public PackagesTblModel(int rowCount) {
            super(rowCount, 2);
        }

        public String getColumnName(int col){
            return columnNames[col];
        }
        
        public boolean isCellEditable(int row, int col) {
            return (col == 1);
        }
                
        public String[] getSelections() {
            int rowCount = getRowCount();
            String ret[] = new String[rowCount];
            for (int i = 0; i=0; i-=2) {
                if (listeners[i]==CellEditorListener.class) {
                    // Lazily create the event:
                    if (changeEvent == null)
                        changeEvent = new ChangeEvent(this);
                    ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
                }
            }
        }
        
        protected void fireEditingCanceled() {
            // Guaranteed to return a non-null array
            Object[] listeners = listenerList.getListenerList();
            // Process the listeners last to first, notifying
            // those that are interested in this event
            for (int i = listeners.length-2; i>=0; i-=2) {
                if (listeners[i]==CellEditorListener.class) {
                    // Lazily create the event:
                    if (changeEvent == null)
                        changeEvent = new ChangeEvent(this);
                    ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
                }
            }
        }
        
        // implements javax.swing.table.TableCellEditor
        public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {
            editorComponent = (JComponent)value;
            return editorComponent;
        }
        
    } // End of class JComponentCellEditor

}
... 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.