Rev 89 | 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 | } |
||
93 | luk | 114 | |
115 | propsButton.setEnabled(cnt == 1); |
||
87 | luk | 116 | } |
117 | |||
118 | /** |
||
119 | * Updates the buttons according the current selection. |
||
120 | * @param e list selection event |
||
121 | */ |
||
122 | public void valueChanged(ListSelectionEvent e) { |
||
123 | updateButtons(); |
||
124 | } |
||
125 | |||
126 | /** This method is called from within the constructor to |
||
127 | * initialize the form. |
||
128 | * WARNING: Do NOT modify this code. The content of this method is |
||
129 | * always regenerated by the Form Editor. |
||
130 | */ |
||
89 | luk | 131 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
87 | luk | 132 | private void initComponents() { |
89 | luk | 133 | |
87 | luk | 134 | jSplitPane1 = new javax.swing.JSplitPane(); |
135 | jPanel1 = new javax.swing.JPanel(); |
||
136 | startButton = new javax.swing.JButton(); |
||
137 | stopButton = new javax.swing.JButton(); |
||
138 | addButton = new javax.swing.JButton(); |
||
139 | removeButton = new javax.swing.JButton(); |
||
89 | luk | 140 | resetButton = new javax.swing.JButton(); |
93 | luk | 141 | propsButton = new javax.swing.JButton(); |
87 | luk | 142 | jScrollPane1 = new javax.swing.JScrollPane(); |
143 | jTable1 = new javax.swing.JTable(); |
||
144 | |||
145 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
||
146 | setTitle("LWTT"); |
||
147 | addWindowListener(new java.awt.event.WindowAdapter() { |
||
148 | public void windowClosing(java.awt.event.WindowEvent evt) { |
||
149 | TaskFrame.this.windowClosing(evt); |
||
150 | } |
||
151 | }); |
||
152 | |||
153 | jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); |
||
89 | luk | 154 | |
87 | luk | 155 | jPanel1.setMinimumSize(new java.awt.Dimension(10, 25)); |
89 | luk | 156 | |
87 | luk | 157 | startButton.setText("Start"); |
158 | startButton.addActionListener(new java.awt.event.ActionListener() { |
||
159 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
||
160 | startPressed(evt); |
||
161 | } |
||
162 | }); |
||
163 | |||
164 | stopButton.setText("Stop"); |
||
165 | stopButton.addActionListener(new java.awt.event.ActionListener() { |
||
166 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
||
167 | stopPressed(evt); |
||
168 | } |
||
169 | }); |
||
170 | |||
171 | addButton.setText("Add"); |
||
172 | addButton.addActionListener(new java.awt.event.ActionListener() { |
||
173 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
||
174 | addPressed(evt); |
||
175 | } |
||
176 | }); |
||
177 | |||
178 | removeButton.setText("Remove"); |
||
179 | removeButton.addActionListener(new java.awt.event.ActionListener() { |
||
180 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
||
181 | removePressed(evt); |
||
182 | } |
||
183 | }); |
||
184 | |||
89 | luk | 185 | resetButton.setText("Reset"); |
186 | resetButton.addActionListener(new java.awt.event.ActionListener() { |
||
187 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
||
188 | resetPressed(evt); |
||
189 | } |
||
190 | }); |
||
191 | |||
93 | luk | 192 | propsButton.setText("Properties..."); |
193 | propsButton.setActionCommand("Properties"); |
||
194 | propsButton.addActionListener(new java.awt.event.ActionListener() { |
||
195 | public void actionPerformed(java.awt.event.ActionEvent evt) { |
||
196 | propsPressed(evt); |
||
197 | } |
||
198 | }); |
||
199 | |||
87 | luk | 200 | org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); |
201 | jPanel1.setLayout(jPanel1Layout); |
||
202 | jPanel1Layout.setHorizontalGroup( |
||
203 | jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
||
204 | .add(jPanel1Layout.createSequentialGroup() |
||
205 | .add(startButton) |
||
206 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
||
207 | .add(stopButton) |
||
208 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
||
209 | .add(addButton) |
||
210 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
||
211 | .add(removeButton) |
||
89 | luk | 212 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
213 | .add(resetButton) |
||
93 | luk | 214 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) |
215 | .add(propsButton) |
||
216 | .addContainerGap(152, Short.MAX_VALUE)) |
||
87 | luk | 217 | ); |
218 | jPanel1Layout.setVerticalGroup( |
||
89 | luk | 219 | jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) |
220 | .add(startButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE) |
||
221 | .add(stopButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE) |
||
222 | .add(addButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE) |
||
223 | .add(removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE) |
||
224 | .add(resetButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE) |
||
93 | luk | 225 | .add(propsButton) |
87 | luk | 226 | ); |
89 | luk | 227 | |
87 | luk | 228 | jSplitPane1.setTopComponent(jPanel1); |
229 | |||
230 | jScrollPane1.setPreferredSize(new java.awt.Dimension(454, 400)); |
||
231 | jScrollPane1.setVerifyInputWhenFocusTarget(false); |
||
89 | luk | 232 | |
87 | luk | 233 | jTable1.setModel(model); |
234 | jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION); |
||
89 | luk | 235 | jTable1.setDefaultRenderer(StringBuffer.class, new DefaultTableCellRenderer() { |
87 | luk | 236 | private Color fg = Color.RED; |
237 | private Color bg = new Color(255, 240, 240); |
||
238 | |||
239 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
||
240 | Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
||
89 | luk | 241 | |
87 | luk | 242 | if (JLabel.class.isAssignableFrom(c.getClass())) { |
243 | ((JLabel) c).setHorizontalAlignment(JLabel.TRAILING); |
||
244 | } |
||
245 | |||
246 | if (model.isRunning(row)) { |
||
247 | c.setForeground(fg); |
||
248 | if (!isSelected) |
||
249 | c.setBackground(bg); |
||
250 | } |
||
251 | else { |
||
252 | c.setForeground(Color.BLACK); |
||
253 | if (!isSelected) |
||
254 | c.setBackground(Color.WHITE); |
||
255 | } |
||
256 | |||
257 | return c; |
||
258 | } |
||
259 | }); |
||
260 | jScrollPane1.setViewportView(jTable1); |
||
261 | |||
262 | jSplitPane1.setRightComponent(jScrollPane1); |
||
263 | |||
264 | org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); |
||
265 | getContentPane().setLayout(layout); |
||
266 | layout.setHorizontalGroup( |
||
267 | layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
||
268 | .add(org.jdesktop.layout.GroupLayout.TRAILING, jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE) |
||
269 | ); |
||
270 | layout.setVerticalGroup( |
||
271 | layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) |
||
272 | .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE) |
||
273 | ); |
||
89 | luk | 274 | |
87 | luk | 275 | pack(); |
276 | }// </editor-fold>//GEN-END:initComponents |
||
277 | |||
89 | luk | 278 | private void resetPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetPressed |
279 | int start = jTable1.getSelectedRow(); |
||
280 | int cnt = jTable1.getSelectedRowCount(); |
||
281 | if (cnt > 0) |
||
282 | model.resetTasks(start, start + cnt - 1); |
||
283 | }//GEN-LAST:event_resetPressed |
||
284 | |||
87 | luk | 285 | private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing |
286 | model.stopAllTasks(); |
||
287 | model.saveToFile(); |
||
288 | }//GEN-LAST:event_windowClosing |
||
289 | |||
290 | private void removePressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePressed |
||
291 | int start = jTable1.getSelectedRow(); |
||
292 | int cnt = jTable1.getSelectedRowCount(); |
||
293 | if (cnt > 0) |
||
294 | model.removeTasks(start, start + cnt - 1); |
||
295 | |||
296 | }//GEN-LAST:event_removePressed |
||
297 | |||
298 | private void addPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPressed |
||
299 | model.addNewTask(); |
||
300 | }//GEN-LAST:event_addPressed |
||
301 | |||
302 | private void stopPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopPressed |
||
303 | int start = jTable1.getSelectedRow(); |
||
304 | int cnt = jTable1.getSelectedRowCount(); |
||
305 | if (cnt > 0) { |
||
306 | model.stopTasks(start, start + cnt - 1); |
||
307 | updateButtons(); |
||
308 | } |
||
309 | }//GEN-LAST:event_stopPressed |
||
310 | |||
311 | private void startPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startPressed |
||
312 | int start = jTable1.getSelectedRow(); |
||
313 | int cnt = jTable1.getSelectedRowCount(); |
||
314 | if (cnt > 0) { |
||
315 | model.startTasks(start, start + cnt - 1); |
||
316 | updateButtons(); |
||
317 | } |
||
318 | }//GEN-LAST:event_startPressed |
||
93 | luk | 319 | |
320 | private void propsPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_propsPressed |
||
321 | TaskPropetiesDialog d = new TaskPropetiesDialog(this, true); |
||
322 | int start = jTable1.getSelectedRow(); |
||
323 | int cnt = jTable1.getSelectedRowCount(); |
||
324 | if (cnt == 1) { |
||
325 | Task t = model.getTask(start); |
||
326 | d.setPrice(t.getPrice()); |
||
327 | d.setVisible(true); |
||
328 | if (d.getReturnStatus() == TaskPropetiesDialog.RET_OK) { |
||
329 | t.setPrice(d.getPrice()); |
||
330 | model.fireTableCellUpdated(start, 2); |
||
331 | } |
||
332 | } |
||
333 | }//GEN-LAST:event_propsPressed |
||
87 | luk | 334 | |
335 | // Variables declaration - do not modify//GEN-BEGIN:variables |
||
336 | private javax.swing.JButton addButton; |
||
337 | private javax.swing.JPanel jPanel1; |
||
338 | private javax.swing.JScrollPane jScrollPane1; |
||
339 | private javax.swing.JSplitPane jSplitPane1; |
||
340 | private javax.swing.JTable jTable1; |
||
93 | luk | 341 | private javax.swing.JButton propsButton; |
87 | luk | 342 | private javax.swing.JButton removeButton; |
89 | luk | 343 | private javax.swing.JButton resetButton; |
87 | luk | 344 | private javax.swing.JButton startButton; |
345 | private javax.swing.JButton stopButton; |
||
346 | // End of variables declaration//GEN-END:variables |
||
347 | |||
348 | } |