Subversion Repositories public

Rev

Rev 89 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
87 luk 1
/*
2
 * TaskFrame.java - implementation of LWTT main application window
3
 *
4
 * Copyright (c) 2006 Lukas Jelinek, http://www.aiken.cz
5
 *
6
 * ==========================================================================
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License Version 2 as
10
 * published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 *
21
 * ==========================================================================
22
 */
23
 
24
package cz.aiken.util.lwtt;
25
 
26
import java.awt.*;
27
import javax.swing.*;
28
import javax.swing.table.*;
29
import javax.swing.event.*;
30
 
31
/**
32
 * This class represents the main application frame.
33
 * @author luk
34
 */
35
public class TaskFrame extends javax.swing.JFrame implements ListSelectionListener {
36
 
37
    private TaskTableModel model = new TaskTableModel();
38
 
39
    /**
40
     * Time consumption column width
41
     */
42
    public static final int CONS_COL_WIDTH = 180;
43
 
44
    /**
45
     * Creates new form TaskFrame
46
     */
47
    public TaskFrame() {
48
        initComponents();
49
        TableColumn tc = jTable1.getColumnModel().getColumn(1);
50
        tc.setMaxWidth(CONS_COL_WIDTH);
51
        tc.setPreferredWidth(CONS_COL_WIDTH);
52
        jTable1.getSelectionModel().addListSelectionListener(this);
53
        updateButtons();
54
    }
55
 
56
    void updateButtons() {
57
        int cnt = jTable1.getSelectedRowCount();
58
        if (cnt == 0) {
59
            startButton.setEnabled(false);
60
            stopButton.setEnabled(false);
61
            removeButton.setEnabled(false);
62
        }
63
        else {
64
            int start = jTable1.getSelectedRow();
65
            int end = start + cnt - 1;
66
            removeButton.setEnabled(true);
67
 
68
            int rcnt = 0;
69
            for (int i=start; i<=end; i++) {
70
                if (model.isRunning(i))
71
                    rcnt++;
72
            }
73
            startButton.setEnabled(rcnt < cnt);
74
            stopButton.setEnabled(rcnt > 0);
75
        }
76
    }
77
 
78
    /**
79
     * Updates the buttons according the current selection.
80
     * @param e list selection event
81
     */
82
    public void valueChanged(ListSelectionEvent e) {
83
        updateButtons();
84
    }
85
 
86
    /** This method is called from within the constructor to
87
     * initialize the form.
88
     * WARNING: Do NOT modify this code. The content of this method is
89
     * always regenerated by the Form Editor.
90
     */
91
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
92
    private void initComponents() {
93
        jSplitPane1 = new javax.swing.JSplitPane();
94
        jPanel1 = new javax.swing.JPanel();
95
        startButton = new javax.swing.JButton();
96
        stopButton = new javax.swing.JButton();
97
        addButton = new javax.swing.JButton();
98
        removeButton = new javax.swing.JButton();
99
        jScrollPane1 = new javax.swing.JScrollPane();
100
        jTable1 = new javax.swing.JTable();
101
 
102
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
103
        setTitle("LWTT");
104
        addWindowListener(new java.awt.event.WindowAdapter() {
105
            public void windowClosing(java.awt.event.WindowEvent evt) {
106
                TaskFrame.this.windowClosing(evt);
107
            }
108
        });
109
 
110
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
111
        jPanel1.setMinimumSize(new java.awt.Dimension(10, 25));
112
        startButton.setText("Start");
113
        startButton.addActionListener(new java.awt.event.ActionListener() {
114
            public void actionPerformed(java.awt.event.ActionEvent evt) {
115
                startPressed(evt);
116
            }
117
        });
118
 
119
        stopButton.setText("Stop");
120
        stopButton.addActionListener(new java.awt.event.ActionListener() {
121
            public void actionPerformed(java.awt.event.ActionEvent evt) {
122
                stopPressed(evt);
123
            }
124
        });
125
 
126
        addButton.setText("Add");
127
        addButton.addActionListener(new java.awt.event.ActionListener() {
128
            public void actionPerformed(java.awt.event.ActionEvent evt) {
129
                addPressed(evt);
130
            }
131
        });
132
 
133
        removeButton.setText("Remove");
134
        removeButton.addActionListener(new java.awt.event.ActionListener() {
135
            public void actionPerformed(java.awt.event.ActionEvent evt) {
136
                removePressed(evt);
137
            }
138
        });
139
 
140
        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
141
        jPanel1.setLayout(jPanel1Layout);
142
        jPanel1Layout.setHorizontalGroup(
143
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
144
            .add(jPanel1Layout.createSequentialGroup()
145
                .add(startButton)
146
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
147
                .add(stopButton)
148
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
149
                .add(addButton)
150
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
151
                .add(removeButton)
152
                .addContainerGap(310, Short.MAX_VALUE))
153
        );
154
        jPanel1Layout.setVerticalGroup(
155
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
156
            .add(org.jdesktop.layout.GroupLayout.BASELINE, startButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
157
            .add(org.jdesktop.layout.GroupLayout.BASELINE, stopButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
158
            .add(org.jdesktop.layout.GroupLayout.BASELINE, addButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
159
            .add(org.jdesktop.layout.GroupLayout.BASELINE, removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
160
        );
161
        jSplitPane1.setTopComponent(jPanel1);
162
 
163
        jScrollPane1.setPreferredSize(new java.awt.Dimension(454, 400));
164
        jScrollPane1.setVerifyInputWhenFocusTarget(false);
165
        jScrollPane1.setViewport(null);
166
        jTable1.setModel(model);
167
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
168
        jTable1.setDefaultRenderer(Long.class, new DefaultTableCellRenderer() {
169
            private Color fg = Color.RED;
170
            private Color bg = new Color(255, 240, 240);
171
 
172
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
173
                Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
174
                if (JLabel.class.isAssignableFrom(c.getClass())) {
175
                    ((JLabel) c).setHorizontalAlignment(JLabel.TRAILING);
176
                }
177
 
178
                if (model.isRunning(row)) {
179
                    c.setForeground(fg);
180
                    if (!isSelected)
181
                    c.setBackground(bg);
182
                }
183
                else {
184
                    c.setForeground(Color.BLACK);
185
                    if (!isSelected)
186
                    c.setBackground(Color.WHITE);
187
                }
188
 
189
                return c;
190
            }
191
        });
192
        jScrollPane1.setViewportView(jTable1);
193
 
194
        jSplitPane1.setRightComponent(jScrollPane1);
195
 
196
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
197
        getContentPane().setLayout(layout);
198
        layout.setHorizontalGroup(
199
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
200
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
201
        );
202
        layout.setVerticalGroup(
203
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
204
            .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
205
        );
206
        pack();
207
    }// </editor-fold>//GEN-END:initComponents
208
 
209
    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
210
        model.stopAllTasks();
211
        model.saveToFile();
212
    }//GEN-LAST:event_windowClosing
213
 
214
    private void removePressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePressed
215
        int start = jTable1.getSelectedRow();
216
        int cnt = jTable1.getSelectedRowCount();
217
        if (cnt > 0)
218
            model.removeTasks(start, start + cnt - 1);
219
 
220
    }//GEN-LAST:event_removePressed
221
 
222
    private void addPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPressed
223
        model.addNewTask();
224
    }//GEN-LAST:event_addPressed
225
 
226
    private void stopPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopPressed
227
        int start = jTable1.getSelectedRow();
228
        int cnt = jTable1.getSelectedRowCount();
229
        if (cnt > 0) {
230
            model.stopTasks(start, start + cnt - 1);
231
            updateButtons();
232
        }
233
    }//GEN-LAST:event_stopPressed
234
 
235
    private void startPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startPressed
236
        int start = jTable1.getSelectedRow();
237
        int cnt = jTable1.getSelectedRowCount();
238
        if (cnt > 0) {
239
            model.startTasks(start, start + cnt - 1);
240
            updateButtons();
241
        }
242
    }//GEN-LAST:event_startPressed
243
 
244
    // Variables declaration - do not modify//GEN-BEGIN:variables
245
    private javax.swing.JButton addButton;
246
    private javax.swing.JPanel jPanel1;
247
    private javax.swing.JScrollPane jScrollPane1;
248
    private javax.swing.JSplitPane jSplitPane1;
249
    private javax.swing.JTable jTable1;
250
    private javax.swing.JButton removeButton;
251
    private javax.swing.JButton startButton;
252
    private javax.swing.JButton stopButton;
253
    // End of variables declaration//GEN-END:variables
254
 
255
}