GenSelectionPanel.java :  » J2EE » enhydra » org » enhydra » tool » codegen » wizard » Java Open Source

Java Open Source » J2EE » enhydra 
enhydra » org » enhydra » tool » codegen » wizard » GenSelectionPanel.java

/*
 * Enhydra Java Application Server Project
 *
 * The contents of this file are subject to the Enhydra 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 on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 *
 * 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 terms governing rights and limitations
 * under the License.
 *
 * The Initial Developer of the Enhydra Application Server is Lutris
 * Technologies, Inc. The Enhydra Application Server and portions created
 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
 * All Rights Reserved.
 *
 * Contributor(s):
 * Paul Mahar
 *
 */
package org.enhydra.tool.codegen.wizard;

// ToolBox imports
import org.enhydra.tool.ToolBoxInfo;
import org.enhydra.tool.codegen.CodeGen;
import org.enhydra.tool.codegen.GeneratorOption;
import org.enhydra.tool.codegen.GeneratorException;
import org.enhydra.tool.codegen.Generator;
import org.enhydra.tool.codegen.ValidationException;
import org.enhydra.tool.common.PathHandle;
import org.enhydra.tool.common.SwingUtil;

// Standard imports
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.*;
import java.beans.*;
import java.io.File;
import java.util.ResourceBundle;

/**
 * This class defines the type of panel you can add to an
 * OpenTool's BasicWizardPage or an CodeGenPage.
 */
public class GenSelectionPanel extends CodeGenPanel {

    //
    transient private GridBagLayout     layoutMain;
    transient private BorderLayout      layoutDescription;
    transient private JComboBox         comboGenerator;
    transient private JLabel            labelGenerator;
    transient private JPanel            panelDescription;
    transient private JTextArea         textDescription;
    transient private Generator[]       generators = new Generator[0];
    transient private LocalItemListener itemListener = null;
    transient private Generator         selection = null;
    transient private JPanel            panelFiller;

    /**
     * Create an CodeGenPanel for use in an OpenTool
     * BasicWizardPage or an CodeGenPage.
     */
    public GenSelectionPanel() {
        try {
            jbInit();
            pmInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Get the title to use on the current page.
     *
     * @return
     * A string to place at the top of a CodeGen wizard panel.
     */
    public String getPageTitle() {
        return "Select a component type";
    }

    /**
     * Get the instructions for selecting a generator.
     *
     * @return
     * A string to place below the page title.
     */
    public String getInstructions() {
        return "Select the type of component you would like "
               + "to generate. The description applies the the current "
               + "selection.";
    }

    /**
     * No-op method for selection page.
     */
    public void readOptionSet() {}

    /**
     * No-op method for selection page.
     */
    public void writeOptionSet() {}

    /**
     * Write values from the swing controls into the option array.
     */
    public void validateOptionSet() throws ValidationException {}

    /**
     * Get the list of available generators.
     *
     * @return
     * An array of generators from which a user can make a selection.
     */
    public Generator[] getGenerators() {
        return generators;
    }

    /**
     * Set the list of available generators.
     *
     * @param generators
     * An array of generators from which a user can make a selection.
     */
    public void setGenerators(Generator[] g) {
        generators = g;
        comboGenerator.removeAllItems();
        for (int i = 0; i < generators.length; i++) {
            comboGenerator.addItem(generators[i]);
        }
        comboGenerator.repaint();
    }

    /**
     * Get the generator selected by the user.
     *
     * @return
     * The generator to show options for and to use for generating a project.
     */
    public Generator getSelection() {
        return selection;
    }

    protected void setSelection(Generator sel) {
        ComboBoxModel model = comboGenerator.getModel();
        int           count = model.getSize();

        for (int i = 0; i < count; i++) {
            if (model.getElementAt(i) instanceof Generator) {
                Generator cursor = (Generator) model.getElementAt(i);

                if (cursor.getDescription().equalsIgnoreCase(sel.getDescription())) {
                    comboGenerator.setSelectedIndex(i);
                    break;
                }
            }
        }
    }

    // /
    // / PRIVATE METHODS
    // /
    private void jbInit() throws Exception {
        panelDescription =
            (JPanel) Beans.instantiate(getClass().getClassLoader(),
                                       JPanel.class.getName());
        labelGenerator =
            (JLabel) Beans.instantiate(getClass().getClassLoader(),
                                       JLabel.class.getName());
        textDescription =
            (JTextArea) Beans.instantiate(getClass().getClassLoader(),
                                          JTextArea.class.getName());
        comboGenerator =
            (JComboBox) Beans.instantiate(getClass().getClassLoader(),
                                          JComboBox.class.getName());
        layoutMain =
            (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
                                              GridBagLayout.class.getName());
        layoutDescription =
            (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
                                             BorderLayout.class.getName());
        panelFiller = (JPanel) Beans.instantiate(getClass().getClassLoader(),
                                                 JPanel.class.getName());
        labelGenerator.setMaximumSize(new Dimension(80, 20));
        labelGenerator.setMinimumSize(new Dimension(80, 20));
        labelGenerator.setPreferredSize(new Dimension(80, 20));
        labelGenerator.setLabelFor(comboGenerator);
        labelGenerator.setText("Component type:");
        textDescription.setDisabledTextColor(SystemColor.controlText);
        textDescription.setBackground(SystemColor.text);
        textDescription.setBounds(2, 2, 2, 2);
        textDescription.setMargin(new Insets(5, 5, 5, 5));
        textDescription.setEnabled(false);
        textDescription.setEditable(false);
        textDescription.setWrapStyleWord(true);
        textDescription.setFont(labelGenerator.getFont());
        textDescription.setLineWrap(true);
        panelDescription.setLayout(layoutDescription);
        panelDescription.setBorder(BorderFactory.createEtchedBorder());
        panelDescription.setMaximumSize(new Dimension(220, 70));
        panelDescription.setMinimumSize(new Dimension(220, 70));
        panelDescription.setPreferredSize(new Dimension(220, 70));
        panelDescription.add(textDescription, BorderLayout.CENTER);
        comboGenerator.setMaximumSize(new Dimension(100, 21));
        comboGenerator.setMinimumSize(new Dimension(100, 21));
        comboGenerator.setPreferredSize(new Dimension(100, 21));
        this.setLayout(layoutMain);
        this.add(panelFiller,
                 new GridBagConstraints(3, 0, 1, 1, 0.1, 0.1,
                                        GridBagConstraints.CENTER,
                                        GridBagConstraints.BOTH,
                                        new Insets(0, 0, 0, 0), 0, 0));
        this.add(comboGenerator,
                 new GridBagConstraints(1, 0, 2, 1, 0.5, 0.5,
                                        GridBagConstraints.WEST,
                                        GridBagConstraints.NONE,
                                        new Insets(5, 5, 5, 5), 100, 0));
        this.add(labelGenerator,
                 new GridBagConstraints(0, 0, 1, 1, 0.5, 0.5,
                                        GridBagConstraints.WEST,
                                        GridBagConstraints.NONE,
                                        new Insets(5, 10, 5, 0), 60, 15));
        this.add(panelDescription,
                 new GridBagConstraints(0, 1, 4, 1, 0.5, 0.5,
                                        GridBagConstraints.NORTHWEST,
                                        GridBagConstraints.VERTICAL,
                                        new Insets(5, 5, 5, 5), 150, 80));
    }

    private void pmInit() {
        itemListener = new LocalItemListener();
        comboGenerator.addItemListener(itemListener);
    }

    private class LocalItemListener implements ItemListener {
        public void itemStateChanged(ItemEvent event) {
            Object item = comboGenerator.getSelectedItem();

            if (item instanceof Generator) {
                selection = (Generator) item;
                textDescription.setText(selection.getDescription());
            } else {
                selection = null;
                textDescription.setText(new String());
            }
        }

    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.