Package reportgen.math.reference

Source Code of reportgen.math.reference.MathExpressionSubOptionPanel

/*
* MathExpressionOperatorPanel.java
*
* Created on 24 Ноябрь 2008 г., 15:48
*/

package reportgen.math.reference;

import reportgen.prototype.context.group.ExpressionEditorPanel;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ListCellRenderer;
import reportgen.math.MathExpression;
import reportgen.prototype.context.Context;
import reportgen.utils.Message;

/**
*
* @author  axe
*/
public abstract class MathExpressionSubOptionPanel<MAINOPTTYPE, SUBOPTTYPE,
        EXPTYPE extends MathExpression>
        extends ExpressionEditorPanel<EXPTYPE> {

    protected final Context context;
    protected EXPTYPE me;

    public MathExpressionSubOptionPanel(Window parent, Context context, EXPTYPE exp) {
        initComponents();
        this.context = context;
        this.me = exp;
        List<MAINOPTTYPE> options = getOptions(context);

        if(options == null) {
            Message.warning(parent, "Нет доступных вариантов");
            mainCombo.setEnabled(false);
        } else {
            mainCombo.setModel(new DefaultComboBoxModel(options.toArray()));
            ListCellRenderer mainCR = getMainCellRendeder();
            if(mainCR != null) {
                mainCombo.setRenderer(mainCR);
            }

            if(me != null
                    && options.contains(getOption(me))) {
                MAINOPTTYPE value = getOption(me);
                mainCombo.setSelectedItem(value);
                fillCols(value, getSubValue(me));
            } else {
                mainCombo.setSelectedItem(null);
            }
        }

        mainCombo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MAINOPTTYPE selection = (MAINOPTTYPE) mainCombo.getSelectedItem();
                if(selection != null) {
                    fillCols(selection, null);
                }
            }
        });

        valueCombo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SUBOPTTYPE item = (SUBOPTTYPE) valueCombo.getSelectedItem();
                if(item == null) {
                    return;
                }
                if(me == null) {
                    me = initNew(item);
                } else {
                    update(item);
                }
                fireExpressionChanged();
            }
        });
    }
   
    private void fillCols(MAINOPTTYPE mainOption, Object value) {
        List options = getSubOptions(mainOption);
        valueCombo.setModel(new DefaultComboBoxModel(options.toArray()));
        ListCellRenderer valueCR = getValueCellRendeder();
        if(valueCR != null) {
            valueCombo.setRenderer(valueCR);
        }

        valueCombo.setSelectedItem(value);
        valueCombo.setEnabled(true);
    }

    protected abstract List<MAINOPTTYPE> getOptions(Context context);
    protected abstract MAINOPTTYPE getOption(EXPTYPE expression);
    protected abstract List<SUBOPTTYPE> getSubOptions(MAINOPTTYPE type);
    protected abstract SUBOPTTYPE getSubValue(EXPTYPE expression);
    protected abstract EXPTYPE initNew(SUBOPTTYPE option);
    protected abstract void update(SUBOPTTYPE option);

    @Override
    public EXPTYPE getValue() {
        return me;
    }

    protected ListCellRenderer getMainCellRendeder() {
        return null;
    }
   
    protected ListCellRenderer getValueCellRendeder() {
        return null;
    }
   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        valueCombo = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        mainCombo = new javax.swing.JComboBox();

        jPanel2.setLayout(new java.awt.BorderLayout(5, 5));

        jLabel2.setText("Значение:");
        jPanel2.add(jLabel2, java.awt.BorderLayout.LINE_START);

        valueCombo.setEnabled(false);
        jPanel2.add(valueCombo, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.BorderLayout(5, 5));

        jLabel1.setText("Тип:");
        jPanel1.add(jLabel1, java.awt.BorderLayout.LINE_START);

        jPanel1.add(mainCombo, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(79, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox mainCombo;
    private javax.swing.JComboBox valueCombo;
    // End of variables declaration//GEN-END:variables

}
TOP

Related Classes of reportgen.math.reference.MathExpressionSubOptionPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.