Subversion Repositories public

Rev

Rev 87 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
87 luk 1
/*
2
 * TaskFrame.java - implementation of LWTT main application window
3
 *
89 luk 4
 * Copyright (c) 2006, 2007, 2008 Lukas Jelinek, http://www.aiken.cz
87 luk 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
 
89 luk 26
import java.util.*;
87 luk 27
import java.awt.*;
28
import javax.swing.*;
29
import javax.swing.table.*;
30
import javax.swing.event.*;
31
 
32
/**
33
 * This class represents the main application frame.
34
 * @author luk
35
 */
89 luk 36
public class TaskFrame extends JFrame implements ListSelectionListener {
87 luk 37
 
89 luk 38
    private TaskTableModel model = null;
87 luk 39
 
89 luk 40
    private Properties startSettings = null;
41
 
87 luk 42
    /**
43
     * Time consumption column width
44
     */
45
    public static final int CONS_COL_WIDTH = 180;
46
 
47
    /**
48
     * Creates new form TaskFrame
49
     */
50
    public TaskFrame() {
89 luk 51
        model = new TaskTableModel(this);
87 luk 52
        initComponents();
53
        TableColumn tc = jTable1.getColumnModel().getColumn(1);
54
        tc.setMaxWidth(CONS_COL_WIDTH);
55
        tc.setPreferredWidth(CONS_COL_WIDTH);
56
        jTable1.getSelectionModel().addListSelectionListener(this);
57
        updateButtons();
89 luk 58
 
59
        try {
60
            String xs = startSettings.getProperty("window.location.x");
61
            String ys = startSettings.getProperty("window.location.y");
62
            if (xs != null && ys != null) {
63
                setLocation(Integer.parseInt(xs), Integer.parseInt(ys));
64
            }
65
        } catch (NumberFormatException e) {
66
            JOptionPane.showMessageDialog(null, "Cannot load window location (bad format).", "Error", JOptionPane.ERROR_MESSAGE);
67
        }
68
 
69
        try {
70
            String ws = startSettings.getProperty("window.size.w");
71
            String hs = startSettings.getProperty("window.size.h");
72
            if (ws != null && hs != null) {
73
                setSize(Integer.parseInt(ws), Integer.parseInt(hs));
74
            }
75
        } catch (NumberFormatException e) {
76
            JOptionPane.showMessageDialog(null, "Cannot load window size (bad format).", "Error", JOptionPane.ERROR_MESSAGE);
77
        }
87 luk 78
    }
79
 
89 luk 80
 
81
    /**
82
     * Sets initial (start) settings.
83
     * @param props properties with initial settings
84
     */
85
    public void setStartSettings(Properties props) {
86
        startSettings = props;
87
    }
88
 
89
    /**
90
     * Updates the buttons' state.
91
     */
92
    private void updateButtons() {
87 luk 93
        int cnt = jTable1.getSelectedRowCount();
94
        if (cnt == 0) {
95
            startButton.setEnabled(false);
96
            stopButton.setEnabled(false);
97
            removeButton.setEnabled(false);
89 luk 98
            resetButton.setEnabled(false);
87 luk 99
        }
100
        else {
101
            int start = jTable1.getSelectedRow();
102
            int end = start + cnt - 1;
103
            removeButton.setEnabled(true);
89 luk 104
            resetButton.setEnabled(true);
87 luk 105
 
106
            int rcnt = 0;
107
            for (int i=start; i<=end; i++) {
108
                if (model.isRunning(i))
109
                    rcnt++;
110
            }
111
            startButton.setEnabled(rcnt < cnt);
112
            stopButton.setEnabled(rcnt > 0);
113
        }
114
    }
115
 
116
    /**
117
     * Updates the buttons according the current selection.
118
     * @param e list selection event
119
     */
120
    public void valueChanged(ListSelectionEvent e) {
121
        updateButtons();
122
    }
123
 
124
    /** This method is called from within the constructor to
125
     * initialize the form.
126
     * WARNING: Do NOT modify this code. The content of this method is
127
     * always regenerated by the Form Editor.
128
     */
89 luk 129
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
87 luk 130
    private void initComponents() {
89 luk 131
 
87 luk 132
        jSplitPane1 = new javax.swing.JSplitPane();
133
        jPanel1 = new javax.swing.JPanel();
134
        startButton = new javax.swing.JButton();
135
        stopButton = new javax.swing.JButton();
136
        addButton = new javax.swing.JButton();
137
        removeButton = new javax.swing.JButton();
89 luk 138
        resetButton = new javax.swing.JButton();
87 luk 139
        jScrollPane1 = new javax.swing.JScrollPane();
140
        jTable1 = new javax.swing.JTable();
141
 
142
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
143
        setTitle("LWTT");
144
        addWindowListener(new java.awt.event.WindowAdapter() {
145
            public void windowClosing(java.awt.event.WindowEvent evt) {
146
                TaskFrame.this.windowClosing(evt);
147
            }
148
        });
149
 
150
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
89 luk 151
 
87 luk 152
        jPanel1.setMinimumSize(new java.awt.Dimension(10, 25));
89 luk 153
 
87 luk 154
        startButton.setText("Start");
155
        startButton.addActionListener(new java.awt.event.ActionListener() {
156
            public void actionPerformed(java.awt.event.ActionEvent evt) {
157
                startPressed(evt);
158
            }
159
        });
160
 
161
        stopButton.setText("Stop");
162
        stopButton.addActionListener(new java.awt.event.ActionListener() {
163
            public void actionPerformed(java.awt.event.ActionEvent evt) {
164
                stopPressed(evt);
165
            }
166
        });
167
 
168
        addButton.setText("Add");
169
        addButton.addActionListener(new java.awt.event.ActionListener() {
170
            public void actionPerformed(java.awt.event.ActionEvent evt) {
171
                addPressed(evt);
172
            }
173
        });
174
 
175
        removeButton.setText("Remove");
176
        removeButton.addActionListener(new java.awt.event.ActionListener() {
177
            public void actionPerformed(java.awt.event.ActionEvent evt) {
178
                removePressed(evt);
179
            }
180
        });
181
 
89 luk 182
        resetButton.setText("Reset");
183
        resetButton.addActionListener(new java.awt.event.ActionListener() {
184
            public void actionPerformed(java.awt.event.ActionEvent evt) {
185
                resetPressed(evt);
186
            }
187
        });
188
 
87 luk 189
        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
190
        jPanel1.setLayout(jPanel1Layout);
191
        jPanel1Layout.setHorizontalGroup(
192
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
193
            .add(jPanel1Layout.createSequentialGroup()
194
                .add(startButton)
195
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
196
                .add(stopButton)
197
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
198
                .add(addButton)
199
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
200
                .add(removeButton)
89 luk 201
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
202
                .add(resetButton)
203
                .addContainerGap(251, Short.MAX_VALUE))
87 luk 204
        );
205
        jPanel1Layout.setVerticalGroup(
89 luk 206
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
207
            .add(startButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
208
            .add(stopButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
209
            .add(addButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
210
            .add(removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
211
            .add(resetButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
87 luk 212
        );
89 luk 213
 
87 luk 214
        jSplitPane1.setTopComponent(jPanel1);
215
 
216
        jScrollPane1.setPreferredSize(new java.awt.Dimension(454, 400));
217
        jScrollPane1.setVerifyInputWhenFocusTarget(false);
89 luk 218
 
87 luk 219
        jTable1.setModel(model);
220
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
89 luk 221
        jTable1.setDefaultRenderer(StringBuffer.class, new DefaultTableCellRenderer() {
87 luk 222
            private Color fg = Color.RED;
223
            private Color bg = new Color(255, 240, 240);
224
 
225
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
226
                Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
89 luk 227
 
87 luk 228
                if (JLabel.class.isAssignableFrom(c.getClass())) {
229
                    ((JLabel) c).setHorizontalAlignment(JLabel.TRAILING);
230
                }
231
 
232
                if (model.isRunning(row)) {
233
                    c.setForeground(fg);
234
                    if (!isSelected)
235
                    c.setBackground(bg);
236
                }
237
                else {
238
                    c.setForeground(Color.BLACK);
239
                    if (!isSelected)
240
                    c.setBackground(Color.WHITE);
241
                }
242
 
243
                return c;
244
            }
245
        });
246
        jScrollPane1.setViewportView(jTable1);
247
 
248
        jSplitPane1.setRightComponent(jScrollPane1);
249
 
250
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
251
        getContentPane().setLayout(layout);
252
        layout.setHorizontalGroup(
253
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
254
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
255
        );
256
        layout.setVerticalGroup(
257
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
258
            .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
259
        );
89 luk 260
 
87 luk 261
        pack();
262
    }// </editor-fold>//GEN-END:initComponents
263
 
89 luk 264
    private void resetPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetPressed
265
        int start = jTable1.getSelectedRow();
266
        int cnt = jTable1.getSelectedRowCount();
267
        if (cnt > 0)
268
            model.resetTasks(start, start + cnt - 1);
269
    }//GEN-LAST:event_resetPressed
270
 
87 luk 271
    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
272
        model.stopAllTasks();
273
        model.saveToFile();
274
    }//GEN-LAST:event_windowClosing
275
 
276
    private void removePressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePressed
277
        int start = jTable1.getSelectedRow();
278
        int cnt = jTable1.getSelectedRowCount();
279
        if (cnt > 0)
280
            model.removeTasks(start, start + cnt - 1);
281
 
282
    }//GEN-LAST:event_removePressed
283
 
284
    private void addPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPressed
285
        model.addNewTask();
286
    }//GEN-LAST:event_addPressed
287
 
288
    private void stopPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopPressed
289
        int start = jTable1.getSelectedRow();
290
        int cnt = jTable1.getSelectedRowCount();
291
        if (cnt > 0) {
292
            model.stopTasks(start, start + cnt - 1);
293
            updateButtons();
294
        }
295
    }//GEN-LAST:event_stopPressed
296
 
297
    private void startPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startPressed
298
        int start = jTable1.getSelectedRow();
299
        int cnt = jTable1.getSelectedRowCount();
300
        if (cnt > 0) {
301
            model.startTasks(start, start + cnt - 1);
302
            updateButtons();
303
        }
304
    }//GEN-LAST:event_startPressed
305
 
306
    // Variables declaration - do not modify//GEN-BEGIN:variables
307
    private javax.swing.JButton addButton;
308
    private javax.swing.JPanel jPanel1;
309
    private javax.swing.JScrollPane jScrollPane1;
310
    private javax.swing.JSplitPane jSplitPane1;
311
    private javax.swing.JTable jTable1;
312
    private javax.swing.JButton removeButton;
89 luk 313
    private javax.swing.JButton resetButton;
87 luk 314
    private javax.swing.JButton startButton;
315
    private javax.swing.JButton stopButton;
316
    // End of variables declaration//GEN-END:variables
317
 
318
}