Subversion Repositories public

Rev

Rev 89 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 * TaskFrame.java - implementation of LWTT main application window
 *
 * Copyright (c) 2006, 2007, 2008 Lukas Jelinek, http://www.aiken.cz
 *
 * ==========================================================================
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ==========================================================================
 */


package cz.aiken.util.lwtt;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

/**
 * This class represents the main application frame.
 * @author luk
 */

public class TaskFrame extends JFrame implements ListSelectionListener {
   
    private TaskTableModel model = null;
   
    private Properties startSettings = null;
   
    /**
     * Time consumption column width
     */

    public static final int CONS_COL_WIDTH = 180;
   
    /**
     * Creates new form TaskFrame
     */

    public TaskFrame() {
        model = new TaskTableModel(this);
        initComponents();
        TableColumn tc = jTable1.getColumnModel().getColumn(1);
        tc.setMaxWidth(CONS_COL_WIDTH);
        tc.setPreferredWidth(CONS_COL_WIDTH);
        jTable1.getSelectionModel().addListSelectionListener(this);
        updateButtons();
       
        try {
            String xs = startSettings.getProperty("window.location.x");
            String ys = startSettings.getProperty("window.location.y");
            if (xs != null && ys != null) {
                setLocation(Integer.parseInt(xs), Integer.parseInt(ys));
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Cannot load window location (bad format).", "Error", JOptionPane.ERROR_MESSAGE);
        }
       
        try {
            String ws = startSettings.getProperty("window.size.w");
            String hs = startSettings.getProperty("window.size.h");
            if (ws != null && hs != null) {
                setSize(Integer.parseInt(ws), Integer.parseInt(hs));
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Cannot load window size (bad format).", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
   
   
    /**
     * Sets initial (start) settings.
     * @param props properties with initial settings
     */

    public void setStartSettings(Properties props) {
        startSettings = props;
    }
   
    /**
     * Updates the buttons' state.
     */

    private void updateButtons() {
        int cnt = jTable1.getSelectedRowCount();
        if (cnt == 0) {
            startButton.setEnabled(false);
            stopButton.setEnabled(false);
            removeButton.setEnabled(false);
            resetButton.setEnabled(false);
        }
        else {
            int start = jTable1.getSelectedRow();
            int end = start + cnt - 1;
            removeButton.setEnabled(true);
            resetButton.setEnabled(true);
           
            int rcnt = 0;
            for (int i=start; i<=end; i++) {
                if (model.isRunning(i))
                    rcnt++;
            }
            startButton.setEnabled(rcnt < cnt);
            stopButton.setEnabled(rcnt > 0);
        }
    }
   
    /**
     * Updates the buttons according the current selection.
     * @param e list selection event
     */

    public void valueChanged(ListSelectionEvent e) {
        updateButtons();
    }
   
    /** 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.
     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        startButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LWTT");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                TaskFrame.this.windowClosing(evt);
            }
        });

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel1.setMinimumSize(new java.awt.Dimension(10, 25));

        startButton.setText("Start");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startPressed(evt);
            }
        });

        stopButton.setText("Stop");
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopPressed(evt);
            }
        });

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPressed(evt);
            }
        });

        removeButton.setText("Remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removePressed(evt);
            }
        });

        resetButton.setText("Reset");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetPressed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(startButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(stopButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(removeButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(resetButton)
                .addContainerGap(251, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
            .add(startButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
            .add(stopButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
            .add(addButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
            .add(removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
            .add(resetButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        jSplitPane1.setTopComponent(jPanel1);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(454, 400));
        jScrollPane1.setVerifyInputWhenFocusTarget(false);

        jTable1.setModel(model);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jTable1.setDefaultRenderer(StringBuffer.class, new DefaultTableCellRenderer() {
            private Color fg = Color.RED;
            private Color bg = new Color(255, 240, 240);

            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (JLabel.class.isAssignableFrom(c.getClass())) {
                    ((JLabel) c).setHorizontalAlignment(JLabel.TRAILING);
                }

                if (model.isRunning(row)) {
                    c.setForeground(fg);
                    if (!isSelected)
                    c.setBackground(bg);
                }
                else {
                    c.setForeground(Color.BLACK);
                    if (!isSelected)
                    c.setBackground(Color.WHITE);
                }

                return c;
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jSplitPane1.setRightComponent(jScrollPane1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void resetPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetPressed
        int start = jTable1.getSelectedRow();
        int cnt = jTable1.getSelectedRowCount();
        if (cnt > 0)
            model.resetTasks(start, start + cnt - 1);
    }//GEN-LAST:event_resetPressed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        model.stopAllTasks();
        model.saveToFile();
    }//GEN-LAST:event_windowClosing

    private void removePressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePressed
        int start = jTable1.getSelectedRow();
        int cnt = jTable1.getSelectedRowCount();
        if (cnt > 0)
            model.removeTasks(start, start + cnt - 1);
           
    }//GEN-LAST:event_removePressed

    private void addPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPressed
        model.addNewTask();
    }//GEN-LAST:event_addPressed

    private void stopPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopPressed
        int start = jTable1.getSelectedRow();
        int cnt = jTable1.getSelectedRowCount();
        if (cnt > 0) {
            model.stopTasks(start, start + cnt - 1);
            updateButtons();
        }
    }//GEN-LAST:event_stopPressed

    private void startPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startPressed
        int start = jTable1.getSelectedRow();
        int cnt = jTable1.getSelectedRowCount();
        if (cnt > 0) {
            model.startTasks(start, start + cnt - 1);
            updateButtons();
        }
    }//GEN-LAST:event_startPressed
         
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton startButton;
    private javax.swing.JButton stopButton;
    // End of variables declaration//GEN-END:variables
   
}