1 package sk.stuba.fiit.foo07.genex.gui;
2
3 import java.awt.Color;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.event.ComponentEvent;
7 import java.awt.event.ComponentListener;
8 import java.awt.event.MouseAdapter;
9 import java.awt.event.MouseEvent;
10 import java.awt.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12 import java.sql.SQLException;
13 import java.sql.Timestamp;
14 import java.util.ArrayList;
15
16 import javax.swing.BorderFactory;
17 import javax.swing.ImageIcon;
18 import javax.swing.JButton;
19 import javax.swing.JLabel;
20 import javax.swing.JScrollPane;
21 import javax.swing.JSeparator;
22 import javax.swing.JTable;
23 import javax.swing.JTextField;
24 import javax.swing.JTextPane;
25 import javax.swing.SwingConstants;
26 import javax.swing.WindowConstants;
27 import javax.swing.border.TitledBorder;
28 import javax.swing.event.ListSelectionEvent;
29 import javax.swing.event.ListSelectionListener;
30 import javax.swing.event.TableModelEvent;
31 import javax.swing.event.TableModelListener;
32
33 import sk.stuba.fiit.foo07.genex.beans.Category;
34 import sk.stuba.fiit.foo07.genex.beans.Question;
35 import sk.stuba.fiit.foo07.genex.beans.QuestionPoints;
36 import sk.stuba.fiit.foo07.genex.beans.Test;
37 import sk.stuba.fiit.foo07.genex.common.ConnectionHelper;
38 import sk.stuba.fiit.foo07.genex.common.ResourceHelper;
39 import sk.stuba.fiit.foo07.genex.dao.CategoryDao;
40 import sk.stuba.fiit.foo07.genex.dao.CategoryDaoDerby;
41 import sk.stuba.fiit.foo07.genex.dao.TestDao;
42 import sk.stuba.fiit.foo07.genex.dao.TestDaoDerby;
43 import sk.stuba.fiit.foo07.genex.dao.UserDaoDerby;
44
45
46
47
48
49
50
51
52
53
54
55 public class NewTestDialog extends javax.swing.JDialog {
56
57
58
59 private static final long serialVersionUID = 8403446546144184388L;
60
61 private ResourceHelper resHelp;
62 private GenexTableModel genexModel;
63 private NewTestTableModel model;
64 private Category actualTestCategory;
65 private Test test;
66 private GenexGUI genex;
67
68 private JTextField jTxtSubject;
69 private JTextField jTxtName;
70 private JLabel jLabel6;
71 private JLabel jLabel5;
72 private JScrollPane jScrHint;
73 private JTextPane jTxtHint;
74 private JTable jTblQuestions;
75 private JLabel jLabel2;
76 private JLabel jLabel3;
77 private JLabel jLabel4;
78 private JButton jBtnAddQuestion;
79 private JButton jBtnEditQuestion;
80 private JButton jBtnRemove;
81 private JLabel jLblDiff;
82 private JLabel jLblPointsCount;
83 private JLabel jLblQuestionCount;
84 private JLabel jLabel1;
85 private JButton jBtnReorder;
86 private JButton jBtnItemsDown;
87 private JButton jBtnItemsUp;
88 private JSeparator jSeparator2;
89 private JButton jBtnOK;
90 private JButton jBtnCancel;
91 private JScrollPane jScrQuestions;
92 private JLabel jLblQuestions;
93 private JSeparator jSeparator1;
94
95 public NewTestDialog() {
96
97 }
98
99 public NewTestDialog(GenexGUI frame, NewTestTableModel model,
100 GenexTableModel genexModel, Category testCat, Test test) {
101 super();
102
103 resHelp = new ResourceHelper();
104 this.model = model;
105 this.genexModel = genexModel;
106 this.actualTestCategory = testCat;
107 this.test = test;
108 this.genex = frame;
109
110 initGUI();
111
112 if (test != null) {
113 jTxtName.setText(test.getName());
114 jTxtSubject.setText(test.getSubject());
115 setTitle("Upraviť test");
116 }
117
118 setStat();
119 hint("Info: Počet bodov môžete upraviť kliknutím na danú bunku.");
120 }
121
122 private void setStat() {
123 jLblQuestionCount.setText(new Integer(model.getQuestionCount())
124 .toString());
125 jLblPointsCount.setText(new Float(model.getPointsSum()).toString());
126 jLblDiff.setText(new Float(model.getAverageDiff()).toString());
127 }
128
129 private void initGUI() {
130 try {
131 {
132 getContentPane().setLayout(null);
133 this.setTitle("Nový test");
134 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
135 this.setResizable(false);
136 this.setLocationByPlatform(true);
137 this.addWindowListener(new WindowAdapter() {
138
139 @Override
140 public void windowClosed(WindowEvent e) {
141 genex.setNewTestButton(true);
142 genex.updateTestTree();
143 super.windowClosed(e);
144 }
145
146 @Override
147 public void windowOpened(WindowEvent e) {
148 genex.setNewTestButton(false);
149 super.windowOpened(e);
150 }
151 });
152 this.addComponentListener(new ComponentListener() {
153 @Override
154 public void componentHidden(ComponentEvent e) {
155 genex.setNewTestButton(true);
156 genex.updateTestTree();
157 }
158
159 @Override
160 public void componentMoved(ComponentEvent e) {
161 }
162
163 @Override
164 public void componentResized(ComponentEvent e) {
165 }
166
167 @Override
168 public void componentShown(ComponentEvent e) {
169 genex.setNewTestButton(false);
170 }
171
172 });
173 this.setModalityType(ModalityType.MODELESS);
174 {
175 jScrHint = new JScrollPane();
176 getContentPane().add(jScrHint);
177 jScrHint.setBounds(12, 12, 537, 64);
178 {
179 jTxtHint = new JTextPane();
180 jScrHint.setViewportView(jTxtHint);
181 jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
182 jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
183 jTxtHint.setEditable(false);
184 }
185 }
186 {
187 jSeparator1 = new JSeparator();
188 getContentPane().add(jSeparator1);
189 jSeparator1.setBounds(12, 82, 535, 10);
190 }
191 {
192 jLblQuestions = new JLabel();
193 getContentPane().add(jLblQuestions);
194 jLblQuestions.setText("Otázky testu");
195 jLblQuestions.setBounds(12, 140, 126, 14);
196 jLblQuestions.setForeground(new java.awt.Color(100, 0, 0));
197 }
198 {
199 jScrQuestions = new JScrollPane();
200 getContentPane().add(jScrQuestions);
201 jScrQuestions.setBounds(12, 160, 537, 255);
202 {
203 jTblQuestions = new JTable();
204 jScrQuestions.setViewportView(jTblQuestions);
205 jScrQuestions.getViewport().setBackground(
206 new Color(255, 255, 240));
207 jTblQuestions.setModel(model);
208 jTblQuestions.setBackground(new Color(255, 255, 240));
209 jTblQuestions.setShowGrid(false);
210 jTblQuestions.getColumnModel().getColumn(0)
211 .setPreferredWidth(20);
212 jTblQuestions.getColumnModel().getColumn(1)
213 .setPreferredWidth(300);
214 jTblQuestions.getColumnModel().getColumn(2)
215 .setPreferredWidth(20);
216 jTblQuestions.getColumnModel().getColumn(3)
217 .setPreferredWidth(80);
218 jTblQuestions.getColumnModel().getColumn(3)
219 .setCellRenderer(new ProgressCellRenderer());
220 jTblQuestions.getColumnModel().getColumn(4)
221 .setPreferredWidth(70);
222 jTblQuestions.getModel().addTableModelListener(
223 new TableModelListener() {
224 @Override
225 public void tableChanged(
226 TableModelEvent arg0) {
227 setStat();
228 }
229
230 });
231 jTblQuestions.addMouseListener(new MouseAdapter() {
232 @Override
233 public void mouseClicked(MouseEvent evt) {
234 int sel = jTblQuestions.getSelectedRow();
235 if (sel == -1)
236 return;
237 if (evt.getClickCount() == 2)
238 editQuestion();
239 }
240
241 });
242 jTblQuestions.getSelectionModel()
243 .addListSelectionListener(
244 new ListSelectionListener() {
245 @Override
246 public void valueChanged(
247 ListSelectionEvent e) {
248 int sel = jTblQuestions
249 .getSelectedRow();
250 if (sel == -1)
251 return;
252 hint(model.getQuestion(sel)
253 .getText());
254 }
255
256 });
257 }
258 }
259 {
260 jBtnCancel = new JButton();
261 getContentPane().add(jBtnCancel);
262 jBtnCancel.setText("Cancel");
263 jBtnCancel.setBounds(463, 553, 86, 21);
264 jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
265 jBtnCancel.addActionListener(new ActionListener() {
266 public void actionPerformed(ActionEvent evt) {
267 jBtnCancelActionPerformed(evt);
268 }
269 });
270 }
271 {
272 jBtnOK = new JButton();
273 getContentPane().add(jBtnOK);
274 jBtnOK.setText("OK");
275 jBtnOK.setBounds(372, 553, 86, 21);
276 jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
277 jBtnOK.addActionListener(new ActionListener() {
278 public void actionPerformed(ActionEvent evt) {
279 jBtnOKActionPerformed(evt);
280 }
281 });
282 }
283 {
284 jSeparator2 = new JSeparator();
285 getContentPane().add(jSeparator2);
286 jSeparator2.setBounds(12, 539, 537, 8);
287 }
288 {
289 jBtnItemsUp = new JButton();
290 getContentPane().add(jBtnItemsUp);
291 jBtnItemsUp.setIcon(new ImageIcon(new ResourceHelper()
292 .getResourceBytes("/icons/uparrow.png")));
293 jBtnItemsUp.setActionCommand("Hore");
294 jBtnItemsUp
295 .setHorizontalTextPosition(SwingConstants.CENTER);
296 jBtnItemsUp.setVerticalTextPosition(SwingConstants.BOTTOM);
297 jBtnItemsUp.setToolTipText("Hore");
298 jBtnItemsUp.setBounds(12, 421, 21, 21);
299 jBtnItemsUp.addActionListener(new ActionListener() {
300 public void actionPerformed(ActionEvent evt) {
301 jBtnItemsUpActionPerformed(evt);
302 }
303 });
304 }
305 {
306 jBtnItemsDown = new JButton();
307 getContentPane().add(jBtnItemsDown);
308 jBtnItemsDown.setIcon(new ImageIcon(new ResourceHelper()
309 .getResourceBytes("/icons/downarrow.png")));
310 jBtnItemsDown.setActionCommand("Dole");
311 jBtnItemsDown
312 .setHorizontalTextPosition(SwingConstants.CENTER);
313 jBtnItemsDown
314 .setVerticalTextPosition(SwingConstants.BOTTOM);
315 jBtnItemsDown.setToolTipText("Dole");
316 jBtnItemsDown.setBounds(38, 421, 21, 21);
317 jBtnItemsDown.addActionListener(new ActionListener() {
318 public void actionPerformed(ActionEvent evt) {
319 jBtnItemsDownActionPerformed(evt);
320 }
321 });
322 }
323 {
324 jBtnReorder = new JButton();
325 getContentPane().add(jBtnReorder);
326 jBtnReorder.setIcon(new ImageIcon(new ResourceHelper()
327 .getResourceBytes("/icons/refresh.png")));
328 jBtnReorder.setActionCommand("Preusporiada\u0165");
329 jBtnReorder
330 .setHorizontalTextPosition(SwingConstants.CENTER);
331 jBtnReorder.setVerticalTextPosition(SwingConstants.BOTTOM);
332 jBtnReorder.setToolTipText("Preusporiada\u0165");
333 jBtnReorder.setBounds(70, 421, 21, 21);
334 jBtnReorder.addActionListener(new ActionListener() {
335 public void actionPerformed(ActionEvent evt) {
336 jBtnReorderActionPerformed(evt);
337 }
338 });
339 }
340 {
341 jLabel1 = new JLabel();
342 getContentPane().add(jLabel1);
343 jLabel1.setBounds(12, 453, 537, 74);
344 TitledBorder border = BorderFactory
345 .createTitledBorder("Výsledný test");
346 border.setTitleColor(new Color(49, 106, 196));
347 jLabel1.setBorder(border);
348 }
349 {
350 jLabel2 = new JLabel();
351 getContentPane().add(jLabel2);
352 jLabel2.setText("Po\u010det otázok");
353 jLabel2.setBounds(29, 475, 134, 14);
354 jLabel2.setForeground(new java.awt.Color(100, 0, 0));
355 }
356 {
357 jLabel3 = new JLabel();
358 getContentPane().add(jLabel3);
359 jLabel3.setText("Po\u010det bodov");
360 jLabel3.setBounds(29, 501, 134, 14);
361 jLabel3.setForeground(new java.awt.Color(100, 0, 0));
362 }
363 {
364 jLabel4 = new JLabel();
365 getContentPane().add(jLabel4);
366 jLabel4.setText("Priemerná zlo\u017eitos\u0165 testu");
367 jLabel4.setBounds(294, 473, 166, 14);
368 jLabel4.setForeground(new java.awt.Color(100, 0, 0));
369 }
370 {
371 jLblQuestionCount = new JLabel();
372 getContentPane().add(jLblQuestionCount);
373 jLblQuestionCount.setText("0");
374 jLblQuestionCount.setBounds(192, 475, 38, 14);
375 jLblQuestionCount
376 .setHorizontalAlignment(SwingConstants.RIGHT);
377 jLblQuestionCount.setForeground(new java.awt.Color(0, 0,
378 255));
379 }
380 {
381 jLblPointsCount = new JLabel();
382 getContentPane().add(jLblPointsCount);
383 jLblPointsCount.setText("0");
384 jLblPointsCount.setBounds(192, 501, 38, 14);
385 jLblPointsCount
386 .setForeground(new java.awt.Color(0, 0, 255));
387 jLblPointsCount
388 .setHorizontalAlignment(SwingConstants.RIGHT);
389 }
390 {
391 jLblDiff = new JLabel();
392 getContentPane().add(jLblDiff);
393 jLblDiff.setText("1");
394 jLblDiff.setBounds(472, 473, 63, 14);
395 jLblDiff.setForeground(new java.awt.Color(0, 0, 255));
396 }
397 {
398 jBtnRemove = new JButton();
399 getContentPane().add(jBtnRemove);
400 jBtnRemove.setIcon(new ImageIcon(new ResourceHelper()
401 .getResourceBytes("/icons/remove.png")));
402 jBtnRemove.setHorizontalTextPosition(SwingConstants.CENTER);
403 jBtnRemove.setVerticalTextPosition(SwingConstants.BOTTOM);
404 jBtnRemove.setToolTipText("Odobra\u0165 otázku");
405 jBtnRemove.setBounds(528, 421, 21, 21);
406 jBtnRemove.addActionListener(new ActionListener() {
407 public void actionPerformed(ActionEvent evt) {
408 jBtnRemoveActionPerformed(evt);
409 }
410 });
411 }
412 {
413 jBtnEditQuestion = new JButton();
414 getContentPane().add(jBtnEditQuestion);
415 jBtnEditQuestion.setIcon(new ImageIcon(new ResourceHelper()
416 .getResourceBytes("/icons/menuedit.png")));
417 jBtnEditQuestion
418 .setHorizontalTextPosition(SwingConstants.CENTER);
419 jBtnEditQuestion
420 .setVerticalTextPosition(SwingConstants.BOTTOM);
421 jBtnEditQuestion.setToolTipText("Upravi\u0165 otázku");
422 jBtnEditQuestion.setBounds(502, 421, 21, 21);
423 jBtnEditQuestion.addActionListener(new ActionListener() {
424 public void actionPerformed(ActionEvent evt) {
425 jBtnEditQuestionActionPerformed(evt);
426 }
427 });
428 }
429 {
430 jBtnAddQuestion = new JButton();
431 getContentPane().add(jBtnAddQuestion);
432 jBtnAddQuestion.setIcon(new ImageIcon(new ResourceHelper()
433 .getResourceBytes("/icons/add.png")));
434 jBtnAddQuestion
435 .setHorizontalTextPosition(SwingConstants.CENTER);
436 jBtnAddQuestion
437 .setVerticalTextPosition(SwingConstants.BOTTOM);
438 jBtnAddQuestion.setToolTipText("Prida\u0165 otázku");
439 jBtnAddQuestion.setBounds(476, 421, 20, 21);
440 jBtnAddQuestion.addActionListener(new ActionListener() {
441 public void actionPerformed(ActionEvent evt) {
442 jBtnAddQuestionActionPerformed(evt);
443 }
444 });
445 }
446 {
447 jLabel5 = new JLabel();
448 getContentPane().add(jLabel5);
449 jLabel5.setText("Názov testu");
450 jLabel5.setBounds(12, 90, 79, 14);
451 jLabel5.setForeground(new java.awt.Color(100, 0, 0));
452 }
453 {
454 jLabel6 = new JLabel();
455 getContentPane().add(jLabel6);
456 jLabel6.setText("Predmet");
457 jLabel6.setBounds(12, 117, 79, 14);
458 jLabel6.setForeground(new java.awt.Color(100, 0, 0));
459 }
460 {
461 jTxtName = new JTextField();
462 getContentPane().add(jTxtName);
463 jTxtName.setBounds(103, 88, 446, 21);
464 jTxtName.setBackground(new java.awt.Color(255, 255, 240));
465 jTxtName.setForeground(new java.awt.Color(0, 0, 255));
466 }
467 {
468 jTxtSubject = new JTextField();
469 getContentPane().add(jTxtSubject);
470 jTxtSubject.setBounds(103, 114, 446, 21);
471 jTxtSubject
472 .setBackground(new java.awt.Color(255, 255, 240));
473 jTxtSubject.setForeground(new java.awt.Color(0, 0, 255));
474 }
475 }
476 this.setSize(573, 621);
477 } catch (Exception e) {
478 e.printStackTrace();
479 }
480 }
481
482 private void hint(String text) {
483 if (jTxtHint.getBackground() == GenexGUI.COMB1_BACKGROUND_COLOR) {
484 jTxtHint.setBackground(GenexGUI.COMB2_BACKGROUND_COLOR);
485 jTxtHint.setForeground(GenexGUI.COMB2_FOREGROUND_COLOR);
486 } else {
487 jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
488 jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
489 }
490 if (text.startsWith("Error:")) {
491 jTxtHint.setText(text.substring(6));
492 jTxtHint.setCaretPosition(0);
493 jTxtHint.insertIcon(new ImageIcon(resHelp
494 .getResourceBytes("/icons/error.png")));
495 } else if (text.startsWith("Warning:")) {
496 jTxtHint.setText(text.substring(8));
497 jTxtHint.setCaretPosition(0);
498 jTxtHint.insertIcon(new ImageIcon(resHelp
499 .getResourceBytes("/icons/warning.png")));
500 } else if (text.startsWith("Info:")) {
501 jTxtHint.setText(text.substring(5));
502 jTxtHint.setCaretPosition(0);
503 jTxtHint.insertIcon(new ImageIcon(resHelp
504 .getResourceBytes("/icons/info.png")));
505 } else {
506 jTxtHint.setText(text);
507 }
508 }
509
510 private void jBtnOKActionPerformed(ActionEvent evt) {
511
512 String name = jTxtName.getText().trim();
513 if (name.length() == 0) {
514 hint("Warning: Musíte zadať názov testu.");
515 return;
516 }
517
518 ArrayList<QuestionPoints> qp = new ArrayList<QuestionPoints>(model
519 .getQuestionCount());
520 ArrayList<Question> q = model.getQuestions();
521 ArrayList<Float> p = model.getPoints();
522
523 for (int i = 0; i < q.size(); i++) {
524 qp.add(new QuestionPoints(q.get(i).getQuestionID(), p.get(i)));
525 }
526
527 if (test == null)
528 test = new Test();
529 test.setName(name);
530 test.setSubject(jTxtSubject.getText());
531 test.setQuestionPoints(qp);
532 test.setCategoryID(actualTestCategory.getCategoryID());
533 test.setGenerated(new Timestamp(0));
534
535 try {
536 test.setUserID(new UserDaoDerby(ConnectionHelper.getConnection())
537 .getDefaultTUser().getUserID());
538 TestDao td = new TestDaoDerby(ConnectionHelper.getConnection());
539 if (test.getTestID() == null)
540 td.addTest(actualTestCategory.getCategoryID(), test);
541 else
542 td.updateTest(test);
543 } catch (SQLException e) {
544 hint("Error: " + e.getMessage());
545 }
546 setVisible(false);
547 }
548
549 private void jBtnCancelActionPerformed(ActionEvent evt) {
550 setVisible(false);
551 }
552
553 private void jBtnItemsUpActionPerformed(ActionEvent evt) {
554 questionsUp();
555 }
556
557 private void jBtnItemsDownActionPerformed(ActionEvent evt) {
558 questionsDown();
559 }
560
561 private void jBtnReorderActionPerformed(ActionEvent evt) {
562 reorder();
563 }
564
565 private void jBtnRemoveActionPerformed(ActionEvent evt) {
566 deleteQuestions();
567 }
568
569 private void jBtnEditQuestionActionPerformed(ActionEvent evt) {
570 editQuestion();
571 genexModel.fireTableDataChanged();
572 }
573
574 private void jBtnAddQuestionActionPerformed(ActionEvent evt) {
575 genex.requestFocus();
576 }
577
578 private void editQuestion() {
579 int sel = jTblQuestions.getSelectedRow();
580 if (sel == -1) {
581 hint("Warning: Najprv vyberte otázku, ktorú chcete upraviť.");
582 return;
583 }
584
585 try {
586 Question question = model.getQuestion(sel);
587 CategoryDao cd = new CategoryDaoDerby(ConnectionHelper
588 .getConnection());
589
590 ArrayList<Category> arr_categories = new ArrayList<Category>();
591 for (int cid : cd.getCategoriesIDByQuestionID(question
592 .getQuestionID())) {
593 arr_categories.add(cd.getCategoryByID(cid));
594 }
595
596 NewQuestionDialog nqd = new NewQuestionDialog(null, arr_categories,
597 question, ConnectionHelper.getConnection());
598 nqd.setLocationRelativeTo(null);
599 Question q = nqd.showDialog();
600 if (q != null)
601 model.setQuestion(q, sel);
602 setStat();
603 } catch (SQLException e) {
604 hint("Error: " + e.getMessage());
605 }
606 }
607
608 private void deleteQuestions() {
609 int[] sel = jTblQuestions.getSelectedRows();
610 if (sel.length == 0) {
611 hint("Warning: Najprv vyberte otázky, ktorú chcete odstrániť.");
612 return;
613 }
614 model.removeQuestions(sel);
615 setStat();
616 }
617
618 private int[] echoMove() {
619 int[] sel = jTblQuestions.getSelectedRows();
620 if (sel.length == 0) {
621 hint("Warning: Najprv vyberte otázky, ktorú chcete presunúť.");
622 return null;
623 }
624 return sel;
625 }
626
627 private void questionsUp() {
628 int[] sel = echoMove();
629 if (sel == null || sel[0] == 0)
630 return;
631 for (int i = 0; i < sel.length; i++) {
632 model.moveUp(sel[i]);
633 sel[i]--;
634 }
635 select(sel);
636 }
637
638 private void questionsDown() {
639 int[] sel = echoMove();
640 if (sel == null || sel[sel.length - 1] == model.getQuestionCount() - 1)
641 return;
642 for (int i = sel.length - 1; i >= 0; i--) {
643 model.moveDown(sel[i]);
644 sel[i]++;
645 }
646 select(sel);
647 }
648
649 private void select(int[] sel) {
650 for (int i = 0; i < sel.length; i++) {
651 jTblQuestions.getSelectionModel().addSelectionInterval(sel[i],
652 sel[i]);
653 }
654 }
655
656 private void reorder() {
657 model.reorder();
658 }
659
660 public NewTestTableModel getModel() {
661 return model;
662 }
663
664 public void setModel(NewTestTableModel model) {
665 this.model = model;
666 }
667
668 }