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.refactoring.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jmi.reflect.RefObject;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import org.netbeans.modules.refactoring.api.AbstractRefactoring;
import org.netbeans.modules.refactoring.api.EncapsulateFieldRefactoring;
import org.netbeans.modules.refactoring.api.ui.ParametersPanel;
import org.netbeans.jmi.javamodel.ClassDefinition;
import org.netbeans.jmi.javamodel.EnumConstant;
import org.netbeans.jmi.javamodel.Field;
import org.openide.util.NbBundle;

/**
 * Panel used by Encapsulate Field refactoring. Contains components to
 * set parameters for the refactoring.
 *
 * @author  Pavel Flaska
 */
public class EncapsulateFieldPanel extends javax.swing.JPanel {
    
    Field[] refactoredObjs;
    DefaultTableModel model;
    
    private static final String modifierNames[] = {
        "public", // NOI18N
        "protected", // NOI18N
        "", // NOI18N
        "private" // NOI18N
    };
    
    private static final String[] columnNames = {
        "    ", // NOI18N 
        getString("LBL_ColField"),  // NOI18N
        getString("LBL_ColGetter"), // NOI18N
        getString("LBL_ColSetter")  // NOI18N
    };
    
    // modifier items in combo - indexes
    private static final int MOD_PUBLIC_INDEX = 0;
    private static final int MOD_PROTECTED_INDEX = 1;
    private static final int MOD_DEFAULT_INDEX = 2;
    private static final int MOD_PRIVATE_INDEX = 3;

    private static final Class[] columnTypes = new Class[] {
        java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
    };
    
    /** 
     * Creates new form EncapsulateFieldPanel.
     *
     * @param selectedObjects  array of selected objects
     */
    public EncapsulateFieldPanel(RefObject[] selectedObjects, final ParametersPanel parent) {
        refactoredObjs = initFields((Field) selectedObjects[0]);
        model = new TabM(columnNames, 0);
        Set selectedNames = new HashSet(selectedObjects.length);
        for (int i = 0; i < selectedObjects.length; i++) {
            selectedNames.add(((Field) selectedObjects[i]).getName());
        }
        for (int i = 0; i < refactoredObjs.length; i++) {
            Field field = refactoredObjs[i];
            String name = field.getName();
            model.addRow(new Object[] { 
                selectedNames.contains(name) ? Boolean.TRUE : Boolean.FALSE,
                name,
                EncapsulateFieldRefactoring.computeGetterName(field),
                EncapsulateFieldRefactoring.computeSetterName(field)
            });
        }
        initComponents();
        packRows(jTableFields);
        jTableFields.invalidate();
        jTableFields.repaint();
        model.addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                parent.stateChanged(null);
            }
        });
    }
    
    public EncapsulateFieldPanel() {
        model = new TabM(columnNames, 0);
        initComponents();
    }
    
    private int getMinimumRowHeight(JTable table, int rowIndex) {
        int height = table.getRowHeight();
    
        for (int c=0; cmax)
                max=h;
        }
        table.setRowHeight(max);
        table.getTableHeader().setPreferredSize(new Dimension(table.getTableHeader().getPreferredSize().width, max));
    }
    
    /**
     * Returns table model with data provided by user.
     *
     * @return  data provided in table by user
     */
    protected DefaultTableModel getTableModel() {
        return model;
    }
    
    /** 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 Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLblTitle = new javax.swing.JLabel();
        jScrollField = new javax.swing.JScrollPane();
        jTableFields = new javax.swing.JTable();
        jLblFieldVis = new javax.swing.JLabel();
        jComboField = new javax.swing.JComboBox();
        jLblAccessVis = new javax.swing.JLabel();
        jComboAccess = new javax.swing.JComboBox();
        jCheckAccess = new javax.swing.JCheckBox();

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

        setName(getString("LBL_TitleEncapsulateFields"));
        jLblTitle.setDisplayedMnemonic(getString("LBL_FieldList_MNEM").charAt(0));
        jLblTitle.setLabelFor(jTableFields);
        jLblTitle.setText(getString("LBL_FieldList"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jLblTitle, gridBagConstraints);

        jScrollField.setPreferredSize(new java.awt.Dimension(300, 200));
        jTableFields.setModel(model);
        TableColumn col = jTableFields.getColumnModel().getColumn(0);
        jTableFields.getTableHeader().setReorderingAllowed(false);
        int width = (int) jCheckAccess.getPreferredSize().getWidth();
        col.setPreferredWidth(width);
        col.setMinWidth(width);
        col.setMaxWidth(width);
        col.setResizable(false);
        jScrollField.setViewportView(jTableFields);
        jTableFields.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/ui/Bundle").getString("ACSD_jTableFields"));

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 12, 2);
        add(jScrollField, gridBagConstraints);

        jLblFieldVis.setDisplayedMnemonic(getString("LBL_FieldVis_MNEM").charAt(0));
        jLblFieldVis.setLabelFor(jComboField);
        jLblFieldVis.setText(getString("LBL_FieldVis"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 5, 5);
        add(jLblFieldVis, gridBagConstraints);

        jComboField.setModel(new DefaultComboBoxModel(modifierNames));
        jComboField.setSelectedIndex(3);
        jComboField.setPreferredSize(new java.awt.Dimension(100, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        add(jComboField, gridBagConstraints);
        jComboField.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/ui/Bundle").getString("ACSD_fieldModifiers"));

        jLblAccessVis.setDisplayedMnemonic(getString("LBL_AccessVis_MNEM").charAt(0));
        jLblAccessVis.setLabelFor(jComboAccess);
        jLblAccessVis.setText(getString("LBL_AccessVis"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 5);
        add(jLblAccessVis, gridBagConstraints);

        jComboAccess.setModel(new DefaultComboBoxModel(modifierNames));
        jComboAccess.setSelectedIndex(0);
        jComboAccess.setPreferredSize(new java.awt.Dimension(100, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jComboAccess, gridBagConstraints);
        jComboAccess.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/ui/Bundle").getString("ACSD_methodAcc"));

        jCheckAccess.setText(getString("LBL_AccessorsEven"));
        // NOI18N
        jCheckAccess.setSelected(true);
        jCheckAccess.setMnemonic(getString("LBL_AccessorsEven_MNEM").charAt(0));
        jCheckAccess.setMargin(new java.awt.Insets(12, 2, 2, 2));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(jCheckAccess, gridBagConstraints);
        jCheckAccess.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/ui/Bundle").getString("ACSD_useAccessors"));

    }//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckAccess;
    private javax.swing.JComboBox jComboAccess;
    private javax.swing.JComboBox jComboField;
    private javax.swing.JLabel jLblAccessVis;
    private javax.swing.JLabel jLblFieldVis;
    private javax.swing.JLabel jLblTitle;
    private javax.swing.JScrollPane jScrollField;
    private javax.swing.JTable jTableFields;
    // End of variables declaration//GEN-END:variables

    private static String getString(String key) {
        return NbBundle.getMessage(EncapsulateFieldPanel.class, key);
    }

    /**
     * Returns the array of all fields from class which contains
     * selectedField provided as a parameter.
     *
     * @param   selectedField field, whose class is used for obtaining
     *                        array of fields.
     * @return  array of all fields in a class.
     */
    private Field[] initFields(Field selectedField) {
        ClassDefinition cd = selectedField.getDeclaringClass();
        List result = new ArrayList();
        List features = cd.getFeatures();
        for (Iterator it = features.iterator(); it.hasNext(); ) {
            Object o = it.next();
            if ((o instanceof Field) && !(o instanceof EnumConstant)) {
                result.add(o);
            }
        }
        return (Field[]) result.toArray(new Field[0]);
    }
    
    public Field[] getAllFields() {
        return refactoredObjs;
    }
    
    protected int[] getModifiers() {
        int[] result = new int[2];
        JComboBox combo = jComboAccess;
        int i = 0;
        while (i < 2) {
            switch (combo.getSelectedIndex()) {
                case MOD_PRIVATE_INDEX: 
                    result[i] = Modifier.PRIVATE;
                    break;
                case MOD_DEFAULT_INDEX: 
                    result[i] = 0; /* no modifier */
                    break;
                case MOD_PROTECTED_INDEX: 
                    result[i] = Modifier.PROTECTED;
                    break;
                case MOD_PUBLIC_INDEX:
                    result[i] = Modifier.PUBLIC;
                    break;
            }
            combo = jComboField;
            i++;
        }
        return result;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // INNER CLASSES
    ////////////////////////////////////////////////////////////////////////////
    /**
     * The class is used by EncapsulateFieldPanel - it represents table model
     * used inside in jTable. It denies to edit first column, returns the
     * column classes (Boolean, String, String, String) etc.
     */
    private static class TabM extends DefaultTableModel {
        
        public TabM(Object[] columnNames, int rowCount) {
            super(columnNames, rowCount);
        }
        
        /**
         * Returns the appropriate class for column.
         *
         * @param  columnIndex  index of column for which we are looking for a class
         * @return  class which is used in the column
         */
        public Class getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }

        /**
         * We deny edit the field column (index 1), because field can't
         * be renamed when we encapsulate it.
         *
         * @param  row  doesn't matter
         * @param  column  for value 1, it returns false, otherwise true
         *
         * @return  true, if the cell is editable
         */
        public boolean isCellEditable(int row, int column) {
            return column != 1;
        }
    }
    // end INNER CLASSES
}
... 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.