/*
 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.project;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.log4j.Logger;

/**
 * Frame to hold the list with all output elements.
 * 
 * @author Markus Kraetzig 
 */
public final class OutputFrame extends JFrame {
    private static final Logger log = Logger.getLogger(OutputFrame.class);

    private javax.swing.JSplitPane jSplitPane = null;

    private com.jstatcom.project.OutputList outputList = null;

    private com.jstatcom.project.OutputPanel outputPanel = null;

    private final List actualSelection = new ArrayList();

    private boolean doNotSetOutput = false;

    private javax.swing.JScrollPane jScrollPane = null;

    private javax.swing.JPanel jPanel = null;

    private javax.swing.JToolBar jToolBar = null;

    private javax.swing.JButton jButton = null;

    private javax.swing.JButton jButton1 = null;

    private javax.swing.JToggleButton jToggleButton = null;

    private javax.swing.JToggleButton jToggleButton1 = null;

    /**
     * Creates output frame.
     * 
     */
    public OutputFrame() {
        super();
        initialize();
    }

    private void initialize() {
        this.setContentPane(getJPanel());
        this.setTitle("Output Control [History On]");
        this.setSize(600, 485);
        URL url = getClass().getResource("/images/outcontrol.gif");
        if (url != null)
            setIconImage(new ImageIcon(url).getImage());
        ButtonGroup sortGroup = new ButtonGroup();
        sortGroup.add(getJToggleButton());
        sortGroup.add(getJToggleButton1());

    }

    /**
     * 
     * This method initializes jSplitPane
     * 
     * @return javax.swing.JSplitPane
     * 
     */
    private javax.swing.JSplitPane getJSplitPane() {
        if (jSplitPane == null) {
            jSplitPane = new javax.swing.JSplitPane();
            jSplitPane.setRightComponent(getOutputPanel());
            jSplitPane.setBorder(javax.swing.BorderFactory
                    .createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
            jSplitPane.setLeftComponent(getJScrollPane());
        }
        return jSplitPane;
    }

    /**
     * This method saves the order of the selection in the lists. It takes SHIFT
     * selection into account for adding several series at the same time. It is
     * needed to append the output elements in the correct order to the common
     * file view.
     */
    private void saveSelectionOrderToVector() {

        JList list = getOutputList();

        if (list.getSelectedIndex() < 0) {
            actualSelection.clear();
            return;
        }

        int start = 0;
        if (actualSelection.size() == 0)
            actualSelection.add(new Integer(list.getLeadSelectionIndex()));
        if (actualSelection.size() > 0)
            start = actualSelection.get(actualSelection.size() - 1).intValue();
        int end = list.getLeadSelectionIndex();
        // Checks if there is a SHIFT key selection of more than one series. All
        // series between the last selection and the new one must be added.
        if ((end - start) > 0)
            for (int i = start + 1; i <= end; i++) {
                Integer intI = new Integer(i);
                if (list.isSelectedIndex(i) && !actualSelection.contains(intI))
                    actualSelection.add(intI);
            }

        if ((end - start) < 0)
            for (int i = start - 1; i >= end; i--) {
                Integer intI = new Integer(i);
                if (list.isSelectedIndex(i) && !actualSelection.contains(intI))
                    actualSelection.add(intI);
            }

        // Check, if all series are selected that are in the vector.
        for (int i = 0; i < actualSelection.size(); i++)
            if (!list.isSelectedIndex(actualSelection.get(i).intValue())) {
                actualSelection.remove(i);
                // Because the vectorSize is smaller now.
                i--;
            }

        return;
    }

    /**
     * 
     * This method initializes outputByTimeTree
     * 
     * @return com.jstatcom.project.OutputByTimeTree
     * 
     */
    private com.jstatcom.project.OutputList getOutputList() {
        if (outputList == null) {
            outputList = com.jstatcom.project.OutputList.getInstance();
            outputList.setParent(this);
            outputList.addListSelectionListener(new ListSelectionListener() {

                public void valueChanged(ListSelectionEvent evt) {
                    if (doNotSetOutput)
                        return;
                    saveSelectionOrderToVector();
                    OutputData[] dataArray = new OutputData[actualSelection
                            .size()];
                    for (int i = 0; i < actualSelection.size(); i++) {
                        Integer element = actualSelection.get(i);
                        Object o = getOutputList().getModel().getElementAt(
                                element.intValue());
                        if (o instanceof OutputData)
                            dataArray[i] = (OutputData) o;
                    }
                    getOutputPanel().setOutput(dataArray);
                }
            });
        }
        return outputList;
    }

    /**
     * 
     * This method initializes outputPanel
     * 
     * @return com.jstatcom.project.OutputPanel
     * 
     */
    private com.jstatcom.project.OutputPanel getOutputPanel() {
        if (outputPanel == null) {
            outputPanel = new com.jstatcom.project.OutputPanel();
        }
        return outputPanel;
    }

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

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

    /**
     * 
     * This method initializes jToolBar
     * 
     * @return javax.swing.JToolBar
     * 
     */
    private javax.swing.JToolBar getJToolBar() {
        if (jToolBar == null) {
            jToolBar = new javax.swing.JToolBar();
            jToolBar.setPreferredSize(new java.awt.Dimension(18, 25));
            jToolBar.setFloatable(false);
            jToolBar.add(getJButton());
            jToolBar.add(getJButton1());
            jToolBar.add(getJToggleButton());
            jToolBar.add(getJToggleButton1());
        }
        return jToolBar;
    }

    /**
     * 
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     * 
     */
    private javax.swing.JButton getJButton() {
        if (jButton == null) {
            jButton = new javax.swing.JButton();
            jButton.setText("Clear Selected");
            jButton.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (JOptionPane.showConfirmDialog(OutputFrame.this,
                            "Remove selected output elements?",
                            "Clear Selected", JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
                        return;
                    }
                    getOutputList().removeSelected();
                }
            });

        }
        return jButton;
    }

    /**
     * 
     * This method initializes jButton1
     * 
     * @return javax.swing.JButton
     * 
     */
    private javax.swing.JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new javax.swing.JButton();
            jButton1.setText("Clear All");
            jButton1.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (JOptionPane.showConfirmDialog(OutputFrame.this,
                            "Remove all output elements?", "Clear All",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
                        return;
                    }
                    getOutputList().removeAll();
                }
            });

        }
        return jButton1;
    }

    /**
     * 
     * This method initializes jToggleButton
     * 
     * @return javax.swing.JToggleButton
     * 
     */
    private javax.swing.JToggleButton getJToggleButton() {
        if (jToggleButton == null) {
            jToggleButton = new javax.swing.JToggleButton();
            jToggleButton.setText("Sort By Time");
            jToggleButton.setSelected(true);
            jToggleButton
                    .addActionListener(new java.awt.event.ActionListener() {

                        public void actionPerformed(java.awt.event.ActionEvent e) {
                            doNotSetOutput = true;
                            try {
                                getOutputList().sortByTime();
                            } catch (Throwable ex) {
                                log.error("Sort by time failed", ex);
                            } finally {
                                doNotSetOutput = false;
                            }

                        }
                    });

        }
        return jToggleButton;
    }

    /**
     * 
     * This method initializes jToggleButton1
     * 
     * @return javax.swing.JToggleButton
     * 
     */
    private javax.swing.JToggleButton getJToggleButton1() {
        if (jToggleButton1 == null) {
            jToggleButton1 = new javax.swing.JToggleButton();
            jToggleButton1.setText("Sort By Name");
            jToggleButton1
                    .addActionListener(new java.awt.event.ActionListener() {

                        public void actionPerformed(java.awt.event.ActionEvent e) {
                            doNotSetOutput = true;
                            try {
                                getOutputList().sortByName();
                            } catch (Throwable ex) {
                                log.error("Sort by name failed", ex);
                            } finally {
                                doNotSetOutput = false;
                            }

                        }
                    });

        }
        return jToggleButton1;
    }

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

 

Ads help maintain this website.