ca.uhn.hunit.swing.ui.util.BeanDefinitionForm.java Source code

Java tutorial

Introduction

Here is the source code for ca.uhn.hunit.swing.ui.util.BeanDefinitionForm.java

Source

/**
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
 * specific language governing rights and limitations under the License.
 *
 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
 * 2001.  All Rights Reserved.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
 * applicable instead of those above.  If you wish to allow use of your version of this
 * file only under the terms of the GPL and not to allow others to use your version
 * of this file under the MPL, indicate your decision by deleting  the provisions above
 * and replace  them with the notice and other provisions required by the GPL License.
 * If you do not delete the provisions above, a recipient may use your version of
 * this file under either the MPL or the GPL.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JmsInterfaceForm.java
 *
 * Created on 9-Oct-2009, 7:30:51 PM
 */
package ca.uhn.hunit.swing.ui.util;

import ca.uhn.hunit.l10n.Colours;
import ca.uhn.hunit.util.TypedValueListTableModel;

import org.apache.commons.logging.Log;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;

/**
 *
 * @author James
 */
public class BeanDefinitionForm extends JPanel {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    private static final long serialVersionUID = 1;

    //~ Instance fields ------------------------------------------------------------------------------------------------

    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField myConnectionFactoryTextBox;
    private javax.swing.JScrollPane myConstructorArgsScrollBox;
    private javax.swing.JTable myConstructorArgsTable;
    private javax.swing.JComboBox myConstructorComboBox;
    private Class<?> mySelectedClass;
    private IBeanDefinitionController myController;
    private Log myLog;
    private MyConstructorComboBoxModel myConstructorComboBoxModel;
    private TypedValueListTableModel myConstructorArgsTableModel;

    //~ Constructors ---------------------------------------------------------------------------------------------------

    /** Creates new form JmsInterfaceForm */
    public BeanDefinitionForm() {
        initComponents();
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        myConnectionFactoryTextBox = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        myConstructorArgsScrollBox = new javax.swing.JScrollPane();
        myConstructorArgsTable = new javax.swing.JTable();
        myConstructorComboBox = new javax.swing.JComboBox();

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel5.setText("Username");

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ca/uhn/hunit/l10n/UiStrings"); // NOI18N
        jLabel1.setText(bundle.getString("beandef.classname")); // NOI18N

        myConnectionFactoryTextBox.setText("jTextField2");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel2.setText(bundle.getString("beandef.constructor")); // NOI18N

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel4.setText("Constructor Args");

        myConstructorArgsTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Value", "Type" }));
        myConstructorArgsScrollBox.setViewportView(myConstructorArgsTable);

        myConstructorComboBox.setModel(
                new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 131,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(myConstructorArgsScrollBox,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                                        .addContainerGap())
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        layout.createSequentialGroup()
                                                .addComponent(myConnectionFactoryTextBox,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                                                .addGap(34, 34, 34))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        layout.createSequentialGroup()
                                                .addComponent(myConstructorComboBox, 0, 251, Short.MAX_VALUE)
                                                .addContainerGap()))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(myConnectionFactoryTextBox, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        21, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(myConstructorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(myConstructorArgsScrollBox, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    } // </editor-fold>//GEN-END:initComponents
      // Variables declaration - do not modify//GEN-BEGIN:variables

    // End of variables declaration//GEN-END:variables
    public void setController(IBeanDefinitionController theController) {
        myController = theController;

        myConstructorArgsTableModel = theController.getConstructorArgsTableModel();
        myConstructorArgsTable.setModel(myConstructorArgsTableModel);

        myConstructorComboBoxModel = new MyConstructorComboBoxModel();
        myConstructorComboBox.setModel(myConstructorComboBoxModel);

        mySelectedClass = theController.getInitialClass();

        if (mySelectedClass != null) {
            myConnectionFactoryTextBox.setText(mySelectedClass.getName());
        } else {
            myConnectionFactoryTextBox.setText("");
        }

        myConstructorComboBoxModel.updateForClass(mySelectedClass);

        myConstructorComboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Class<?>> types = myConstructorComboBoxModel.getSelectedTypes();
                myConstructorArgsTableModel.setTypes(types);
            }
        });

        myConnectionFactoryTextBox.getDocument().addUndoableEditListener(new UndoableEditListener() {
            @Override
            public void undoableEditHappened(UndoableEditEvent e) {
                final Class<?> clazz;

                try {
                    clazz = Class.forName(myConnectionFactoryTextBox.getText());
                    myConnectionFactoryTextBox.setBackground(Colours.getTextFieldOk());
                    myController.setSelectedClass(clazz);
                } catch (PropertyVetoException ex) {
                    myLog.error(ex.getMessage());
                    myConnectionFactoryTextBox.setBackground(Colours.getTextFieldError());
                } catch (ClassNotFoundException ex) {
                    myLog.error(ex.getMessage());
                    myConnectionFactoryTextBox.setBackground(Colours.getTextFieldError());
                }
            }
        });
    }

    //~ Inner Classes --------------------------------------------------------------------------------------------------

    private class MyConstructorComboBoxModel extends DefaultComboBoxModel {
        private static final long serialVersionUID = 1L;
        List<List<Class<?>>> myParameterTypes = new ArrayList<List<Class<?>>>();

        /**
         * Returns the type array containing the selected types
         */
        public List<Class<?>> getSelectedTypes() {
            int selectedIndex = getIndexOf(getSelectedItem());

            return myParameterTypes.get(selectedIndex);
        }

        public void updateForClass(Class<?> theClass) {
            removeAllElements();
            myParameterTypes.clear();

            if (theClass == null) {
                return;
            }

            for (Constructor<?> next : theClass.getConstructors()) {
                StringBuffer nextDesc = new StringBuffer();
                Class<?>[] parameterTypes = next.getParameterTypes();

                for (Class<?> clazz : parameterTypes) {
                    if (nextDesc.length() > 0) {
                        nextDesc.append(", ");
                    }

                    nextDesc.append(clazz.getName());
                }

                addElement(nextDesc.toString());

                final List<Class<?>> nextTypeList = Arrays.asList(parameterTypes);
                myParameterTypes.add(nextTypeList);
            }
        }
    }
}