/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.component;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

/**
 * A dialog for setting user defined properties.
 * 
 * @author Markus Kraetzig 
 */
public final class AppSettingsDialog extends JDialog {
    private static TableCellEditor myEditor = new DefaultCellEditor(
            new JTextField()) {

        private JComboBox currentCombo = new JComboBox();

        private Object currentValue = null;

        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {

            currentValue = value;

            if (value instanceof Boolean) {
                currentCombo.removeAllItems();
                currentCombo.addItem(Boolean.valueOf(true));
                currentCombo.addItem(Boolean.valueOf(false));
                currentCombo.setSelectedItem(value);
                return currentCombo;
            }

            return super.getTableCellEditorComponent(table, value, isSelected,
                    row, column);

        }

        // input checking is done here
        public Object getCellEditorValue() {
            if (currentValue instanceof Boolean) {
                return currentCombo.getSelectedItem();
            }

            return super.getCellEditorValue();
        }

    };

    private AppSettings appSettings = new AppSettings();

    private JPanel jPanel = null;

    private JScrollPane jScrollPane = null;

    private JTable jTable = null;

    private JPanel jPanel1 = null;

    private JButton okButton = null;

    private JButton cancelButton = null;

    /**
     * This method initializes
     *  
     */
    public AppSettingsDialog(Frame frame) {
        super(frame, true);
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setContentPane(getJPanel());
        this.setTitle("Preferences");
        this.setSize(473, 243);

    }

    /**
     * This method initializes jPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel() {
        if (jPanel == null) {
            jPanel = new JPanel();
            jPanel.setLayout(new BorderLayout());
            jPanel.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
            jPanel.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
        }
        return jPanel;
    }

    /**
     * This method initializes jScrollPane
     * 
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setViewportView(getJTable());
        }
        return jScrollPane;
    }

    /**
     * This method initializes jTable
     * 
     * @return javax.swing.JTable
     */
    private JTable getJTable() {
        if (jTable == null) {
            jTable = new JTable();
            jTable.setRowHeight(25);
            jTable.setRowSelectionAllowed(false);
            jTable.setColumnSelectionAllowed(false);
            int gapWidth = 4;
            int gapHeight = 4;
            jTable.setIntercellSpacing(new Dimension(gapWidth, gapHeight));
            jTable.setDefaultEditor(Object.class, myEditor);
            jTable.setShowGrid(false);
            jTable.setCellSelectionEnabled(false);
        }
        return jTable;
    }

    /**
     * This method initializes jPanel1
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel1() {
        if (jPanel1 == null) {
            FlowLayout flowLayout1 = new FlowLayout();
            jPanel1 = new JPanel();
            jPanel1.setLayout(flowLayout1);
            jPanel1.setPreferredSize(new java.awt.Dimension(10, 50));
            jPanel1.setBorder(javax.swing.BorderFactory
                    .createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
            flowLayout1.setHgap(100);
            flowLayout1.setVgap(10);
            jPanel1.add(getOkButton(), null);
            jPanel1.add(getCancelButton(), null);
        }
        return jPanel1;
    }

    /**
     * This method initializes okButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getOkButton() {
        if (okButton == null) {
            okButton = new JButton();
            okButton.setText("OK");
            okButton.setPreferredSize(new java.awt.Dimension(100, 26));
            okButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    TableCellEditor ed = getJTable().getCellEditor();
                    if (ed != null)
                        ed.stopCellEditing();
                    setVisible(false);
                    appSettings = (AppSettings) getJTable().getModel();
                }
            });
        }
        return okButton;
    }

    /**
     * This method initializes cancelButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getCancelButton() {
        if (cancelButton == null) {
            cancelButton = new JButton();
            cancelButton.setText("Cancel");
            cancelButton.setPreferredSize(new java.awt.Dimension(100, 26));
            cancelButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    setVisible(false);
                }
            });
        }
        return cancelButton;
    }

    /**
     * Sets the application settings to be displayed.
     * 
     * @param appSettings
     * @throws IllegalArgumentException
     *             if (appSettings == null)
     */
    public void setAppSettings(AppSettings appSettings) {
        if (appSettings == null)
            throw new IllegalArgumentException("Argument was null.");
        this.appSettings = appSettings;
        // copy appSettings before because it might be cancelled
        getJTable().setModel(new AppSettings(appSettings));
    }

    /**
     * Gets the application settings that are currently set.
     * 
     * @return application settings
     */
    public AppSettings getAppSettings() {
        return appSettings;
    }

} //  @jve:decl-index=0:visual-constraint="10,10"

 

Ads help maintain this website.