-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewTasks.java
More file actions
347 lines (301 loc) · 14.3 KB
/
ViewTasks.java
File metadata and controls
347 lines (301 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package todolistdesktopapp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author waqasali
*/
public class ViewTasks extends javax.swing.JFrame {
private ArrayList<String> tasksArray;
private String oldData;
private DefaultTableModel model;
/**
* Creates new form ViewTasks
*/
public ViewTasks() {
initComponents();
tasksArray = new ArrayList<>();
pnlUpdate.setVisible(false);
model = (DefaultTableModel)tblUpdate.getModel();
// load tasks from file
try{
FileReader fr = new FileReader("tasks.txt");
BufferedReader br = new BufferedReader(fr);
String line;
int taskNumber = 1;
while((line = br.readLine())!=null)
{
tasksArray.add(line);
String arr[] = line.split(",");
Object task[] = new Object[]{taskNumber, arr[0], arr[1]};
model.addRow(task);
taskNumber++;
}
br.close();
fr.close();
}catch(IOException e)
{
displayMessage(e.getMessage());
}
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tblUpdate = new javax.swing.JTable();
btnNewTask = new javax.swing.JButton();
btnUpdateTask = new javax.swing.JButton();
btnDeleteTask = new javax.swing.JButton();
pnlUpdate = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
tfTitle = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
taDesc = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
btnUpdate = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tblUpdate.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Task #", "Title", "Description"
}
) {
boolean[] canEdit = new boolean [] {
false, true, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tblUpdate);
btnNewTask.setText("New Task");
btnNewTask.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNewTaskActionPerformed(evt);
}
});
btnUpdateTask.setText("Update Task");
btnUpdateTask.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateTaskActionPerformed(evt);
}
});
btnDeleteTask.setText("Delete Task");
btnDeleteTask.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteTaskActionPerformed(evt);
}
});
jLabel1.setText("Title");
taDesc.setColumns(20);
taDesc.setRows(5);
jScrollPane2.setViewportView(taDesc);
jLabel2.setText("Desc");
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
btnCancel.setText("Cancel");
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
javax.swing.GroupLayout pnlUpdateLayout = new javax.swing.GroupLayout(pnlUpdate);
pnlUpdate.setLayout(pnlUpdateLayout);
pnlUpdateLayout.setHorizontalGroup(
pnlUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlUpdateLayout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(pnlUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(pnlUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tfTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(pnlUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnUpdate)
.addComponent(btnCancel))
.addGap(36, 36, 36))
);
pnlUpdateLayout.setVerticalGroup(
pnlUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlUpdateLayout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(pnlUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(tfTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnUpdate))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
.addGroup(pnlUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(btnCancel)))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(pnlUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnNewTask, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnUpdateTask, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDeleteTask, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(68, 68, 68))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(pnlUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(btnNewTask, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnUpdateTask, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnDeleteTask, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btnUpdateTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateTaskActionPerformed
pnlUpdate.setVisible(true);
int row = tblUpdate.getSelectedRow();
if(row!=-1){
oldData = model.getValueAt(row, 1).toString()+","+model.getValueAt(row, 2).toString();
tfTitle.setText(model.getValueAt(row, 1).toString());
taDesc.setText(model.getValueAt(row, 2).toString());
}
else
{
displayMessage("Select Row First");
}
// TODO add your handling code here:
}//GEN-LAST:event_btnUpdateTaskActionPerformed
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
pnlUpdate.setVisible(false);
// TODO add your handling code here:
}//GEN-LAST:event_btnCancelActionPerformed
private void btnNewTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewTaskActionPerformed
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewTask().setVisible(true);
}
});
dispose();
// TODO add your handling code here:
}//GEN-LAST:event_btnNewTaskActionPerformed
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
int index;
for(int i=0; i<tasksArray.size(); i++)
{
if(tasksArray.get(i).equals(oldData))
{
tasksArray.remove(i);
tasksArray.add(i, tfTitle.getText().toString()+","
+taDesc.getText().toString());
break;
}
}
writeArrayListIntoFile(tasksArray);
displayMessage("Task Updated Successfully");
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ViewTasks().setVisible(true);
}
});
dispose();
// TODO add your handling code here:
}//GEN-LAST:event_btnUpdateActionPerformed
private void btnDeleteTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteTaskActionPerformed
int row = tblUpdate.getSelectedRow();
if(row!=-1)
{
oldData = model.getValueAt(row, 1)+","
+model.getValueAt(row, 2);
tasksArray.remove(oldData);
writeArrayListIntoFile(tasksArray);
displayMessage("Row Deleted Successfully");
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ViewTasks().setVisible(true);
}
});
dispose();
}
else
{
displayMessage("Select Row First");
}
// TODO add your handling code here:
}//GEN-LAST:event_btnDeleteTaskActionPerformed
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnDeleteTask;
private javax.swing.JButton btnNewTask;
private javax.swing.JButton btnUpdate;
private javax.swing.JButton btnUpdateTask;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JPanel pnlUpdate;
private javax.swing.JTextArea taDesc;
private javax.swing.JTable tblUpdate;
private javax.swing.JTextField tfTitle;
// End of variables declaration//GEN-END:variables
private void writeArrayListIntoFile(ArrayList<String> tasksArray) {
try{
FileWriter fw = new FileWriter("tasks.txt");
BufferedWriter bw = new BufferedWriter(fw);
for(int i=0; i<tasksArray.size(); i++)
{
bw.write(tasksArray.get(i));
bw.newLine();
}
bw.close();
fw.close();
}catch(IOException e)
{
displayMessage(e.getMessage());
// JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
}
private void displayMessage(String msg) {
JOptionPane.showMessageDialog(rootPane, msg);
}
}