1 package sk.stuba.fiit.foo07.genex.gui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.event.ItemEvent;
6 import java.awt.event.ItemListener;
7 import java.awt.event.MouseAdapter;
8 import java.awt.event.MouseEvent;
9 import java.sql.Connection;
10 import java.sql.SQLException;
11 import java.sql.Timestamp;
12 import java.util.ArrayList;
13
14 import javax.swing.ComboBoxModel;
15 import javax.swing.DefaultComboBoxModel;
16 import javax.swing.DefaultListModel;
17 import javax.swing.ImageIcon;
18 import javax.swing.JButton;
19 import javax.swing.JComboBox;
20 import javax.swing.JFrame;
21 import javax.swing.JLabel;
22 import javax.swing.JList;
23 import javax.swing.JScrollPane;
24 import javax.swing.JSeparator;
25 import javax.swing.JTable;
26 import javax.swing.JTextArea;
27 import javax.swing.JTextPane;
28 import javax.swing.SwingConstants;
29 import javax.swing.WindowConstants;
30 import javax.swing.event.ListSelectionEvent;
31 import javax.swing.event.ListSelectionListener;
32
33 import sk.stuba.fiit.foo07.genex.beans.Answer;
34 import sk.stuba.fiit.foo07.genex.beans.Category;
35 import sk.stuba.fiit.foo07.genex.beans.Keyword;
36 import sk.stuba.fiit.foo07.genex.beans.Picture;
37 import sk.stuba.fiit.foo07.genex.beans.Question;
38 import sk.stuba.fiit.foo07.genex.beans.QuestionType;
39 import sk.stuba.fiit.foo07.genex.common.ResourceHelper;
40 import sk.stuba.fiit.foo07.genex.dao.CategoryDao;
41 import sk.stuba.fiit.foo07.genex.dao.CategoryDaoDerby;
42 import sk.stuba.fiit.foo07.genex.dao.KeywordDao;
43 import sk.stuba.fiit.foo07.genex.dao.KeywordDaoDerby;
44 import sk.stuba.fiit.foo07.genex.dao.PictureDao;
45 import sk.stuba.fiit.foo07.genex.dao.PictureDaoDerby;
46 import sk.stuba.fiit.foo07.genex.dao.QuestionDao;
47 import sk.stuba.fiit.foo07.genex.dao.QuestionDaoDerby;
48 import sk.stuba.fiit.foo07.genex.dao.QuestionTypeDao;
49 import sk.stuba.fiit.foo07.genex.dao.QuestionTypeDaoDerby;
50 import sk.stuba.fiit.foo07.genex.dao.UserDao;
51 import sk.stuba.fiit.foo07.genex.dao.UserDaoDerby;
52
53
54
55
56
57
58
59
60
61
62
63
64 public class NewQuestionDialog extends javax.swing.JDialog {
65
66
67
68 private static final long serialVersionUID = 1105455711110093349L;
69 private ResourceHelper resHelp;
70 private ImageIcon iconEdit;
71 private ImageIcon iconAdd;
72 private ImageIcon iconDelete;
73 private CheckBoxTableModel model;
74 private Question ret;
75 private JButton jBtnExtend;
76 private ArrayList<Category> actCategories;
77 private QuestionDao qDao;
78 private PictureDao pDao;
79 private KeywordDao kDao;
80 private QuestionTypeDao qtDao;
81 private UserDao uDao;
82 private ArrayList<QuestionType> questionTypes;
83 private Connection connection;
84 private DefaultListModel keywords;
85 private DefaultListModel pictures;
86
87 private JScrollPane jSclHint;
88 private JList jLstKeywords;
89 private JButton jBtnNewPicture;
90 private JButton jBtnDeletePicture;
91 private JButton jBtnDeleteKeyword;
92 private JButton jBtnNewKeyword;
93 private JLabel jLabel6;
94 private JList jLstPictures;
95 private JLabel jLabel5;
96 private JScrollPane jScrollPane4;
97 private JButton jBtnOK;
98 private JLabel jLabel2;
99 private JTable jTblAnswers;
100 private JScrollPane jScrollPane2;
101 private JComboBox jCbxDifficulty;
102 private JScrollPane jScrollPane3;
103 private ProgressCellRenderer jPbrDifficulty;
104 private JLabel jLabel4;
105 private JComboBox jCbxQuestionType;
106 private JLabel jLabel3;
107 private JButton jBtnDeleteAnswer;
108 private JButton jBtnNewAnswer;
109 private JButton jBtnEditAnswer;
110 private JSeparator jSeparator2;
111 private JSeparator jSeparator1;
112 private JTextArea jTxtQuestionText;
113 private JScrollPane jScrollPane1;
114 private JLabel jLabel1;
115 private JButton jBtnCancel;
116 private JTextPane jTxtHint;
117
118 public NewQuestionDialog(JFrame frame, ArrayList<Category> actual,
119 Question question, Connection c) {
120 super(frame);
121
122 try {
123 qDao = new QuestionDaoDerby(c);
124 pDao = new PictureDaoDerby(c);
125 qtDao = new QuestionTypeDaoDerby(c);
126 kDao = new KeywordDaoDerby(c);
127 uDao = new UserDaoDerby(c);
128 } catch (SQLException e) {
129 System.out
130 .println("SQLException: Can't create Dao instance. Connection=="
131 + c);
132 e.printStackTrace();
133 }
134
135 resHelp = new ResourceHelper();
136 connection = c;
137 actCategories = actual;
138 keywords = new DefaultListModel();
139 pictures = new DefaultListModel();
140
141
142
143
144 try {
145 questionTypes = qtDao.getAllQuestionTypes();
146 } catch (Exception e) {
147 }
148
149 model = new CheckBoxTableModel("Text", "Správna");
150
151 initGUI(c);
152
153 if (question != null) {
154 setTitle("Upraviť otázku");
155 ret = question;
156 fillFields(ret);
157 jBtnExtend.setEnabled(true);
158 } else {
159 setTitle("Nová otázka");
160 ret = new Question();
161 jBtnExtend.setEnabled(false);
162 }
163 CategoryDao cDao = new CategoryDaoDerby(c);
164 try {
165 if (question == null)
166 throw new SQLException("OK");
167 ArrayList<Integer> categoriesReal = cDao
168 .getCategoriesIDByQuestionID(question.getQuestionID());
169
170 if (categoriesReal.size() > 1) {
171 StringBuffer cats = new StringBuffer();
172
173 for (Integer catID : categoriesReal) {
174 Category cat = cDao.getCategoryByID(catID);
175 if (catID != categoriesReal.get(0)) {
176 cats.append(", ");
177 }
178 cats.append(cat);
179 }
180
181 StringBuffer newCategories = new StringBuffer();
182 for (Category cat : actCategories) {
183 if (cat != actCategories.get(0)) {
184 newCategories.append(", ");
185 }
186 newCategories.append(cat);
187 }
188
189 hint("Warning: Upravujete otázku ktorá sa nachádza v kategóriách: "
190 + cats
191 + "\nKategória/e pre novú otázku: "
192 + newCategories);
193 } else
194 hint("Info: Kliknutím na odpoveď sa v tomto poli zobrazí jej text.");
195
196 } catch (SQLException ex) {
197 hint("Info: Kliknutím na odpoveď sa v tomto poli zobrazí jej text.");
198 }
199 }
200
201 private void initGUI(Connection connection) {
202 try {
203 iconEdit = new ImageIcon(resHelp
204 .getResourceBytes("/icons/edit.png"));
205 iconAdd = new ImageIcon(resHelp.getResourceBytes("/icons/add.png"));
206 iconDelete = new ImageIcon(resHelp
207 .getResourceBytes("/icons/remove.png"));
208
209
210 this.setTitle("Nová otázka");
211 getContentPane().setLayout(null);
212 this.setResizable(false);
213 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
214 setLocationByPlatform(true);
215
216 jSclHint = new JScrollPane();
217 getContentPane().add(jSclHint);
218 jSclHint.setBounds(10, 11, 549, 64);
219
220 jTxtHint = new JTextPane();
221 jSclHint.setViewportView(jTxtHint);
222 jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
223 jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
224 jTxtHint.setEditable(false);
225 jTxtHint.setPreferredSize(new java.awt.Dimension(509, 40));
226
227
228
229 jBtnOK = new JButton();
230 getContentPane().add(jBtnOK);
231 jBtnOK.setText("OK");
232 jBtnOK.setBounds(399, 488, 77, 23);
233 jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
234 jBtnOK.addActionListener(new ActionListener() {
235 public void actionPerformed(ActionEvent evt) {
236 jBtnOKActionPerformed(evt);
237 }
238 });
239
240
241 jBtnCancel = new JButton();
242 getContentPane().add(jBtnCancel);
243 jBtnCancel.setText("Cancel");
244 jBtnCancel.setBounds(482, 488, 77, 23);
245 jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
246 jBtnCancel.addActionListener(new ActionListener() {
247 public void actionPerformed(ActionEvent evt) {
248 jBtnCancelActionPerformed(evt);
249 }
250 });
251
252
253 jLabel1 = new JLabel();
254 getContentPane().add(jLabel1);
255 jLabel1.setText("Text otázky:");
256 jLabel1.setBounds(10, 89, 264, 14);
257 jLabel1.setForeground(new java.awt.Color(100, 0, 0));
258
259
260 jScrollPane1 = new JScrollPane();
261 getContentPane().add(jScrollPane1);
262 jScrollPane1.setBounds(10, 109, 264, 120);
263 jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
264
265 jTxtQuestionText = new JTextArea();
266 jScrollPane1.setViewportView(jTxtQuestionText);
267 jTxtQuestionText.setBackground(new java.awt.Color(255, 255, 240));
268
269
270
271 jSeparator1 = new JSeparator();
272 getContentPane().add(jSeparator1);
273 jSeparator1.setBounds(10, 477, 549, 11);
274
275
276 jSeparator2 = new JSeparator();
277 getContentPane().add(jSeparator2);
278 jSeparator2.setBounds(10, 80, 549, 9);
279
280
281 jLabel2 = new JLabel();
282 getContentPane().add(jLabel2);
283 jLabel2.setText("Odpovede:");
284 jLabel2.setBounds(10, 293, 264, 14);
285 jLabel2.setForeground(new java.awt.Color(100, 0, 0));
286
287
288 jScrollPane2 = new JScrollPane();
289 getContentPane().add(jScrollPane2);
290 jScrollPane2.setBounds(10, 313, 265, 122);
291
292
293 jTblAnswers = new JTable(model);
294 jTblAnswers.setBackground(new java.awt.Color(255, 255, 240));
295 jTblAnswers.getColumnModel().getColumn(1).setMaxWidth(50);
296 jTblAnswers.getColumnModel().getColumn(1).setResizable(false);
297 jTblAnswers.getColumnModel().getColumn(0).setResizable(false);
298 jTblAnswers.setShowGrid(false);
299 jScrollPane2.setViewportView(jTblAnswers);
300 jScrollPane2.getViewport().setBackground(
301 new java.awt.Color(255, 255, 240));
302 jTblAnswers.addMouseListener(new MouseAdapter() {
303 @Override
304 public void mouseClicked(MouseEvent e) {
305 int index = jTblAnswers.getSelectedRow();
306 if (index == -1)
307 return;
308 hint(model.getAnswer(index).toString());
309 }
310 });
311
312
313
314 jBtnEditAnswer = new JButton();
315 getContentPane().add(jBtnEditAnswer);
316 jBtnEditAnswer.setBounds(229, 446, 21, 21);
317 jBtnEditAnswer.setIcon(iconEdit);
318 jBtnEditAnswer.setVerticalTextPosition(SwingConstants.BOTTOM);
319 jBtnEditAnswer.setHorizontalTextPosition(SwingConstants.CENTER);
320 jBtnEditAnswer.setToolTipText("Upravi\u0165");
321 jBtnEditAnswer.setActionCommand("Upravi\u0165");
322 jBtnEditAnswer.addActionListener(new ActionListener() {
323 public void actionPerformed(ActionEvent evt) {
324 jBtnEditAnswerActionPerformed(evt);
325 }
326 });
327
328
329 jBtnNewAnswer = new JButton();
330 getContentPane().add(jBtnNewAnswer);
331 jBtnNewAnswer.setBounds(203, 446, 21, 21);
332 jBtnNewAnswer.setIcon(iconAdd);
333 jBtnNewAnswer.setVerticalTextPosition(SwingConstants.BOTTOM);
334 jBtnNewAnswer.setHorizontalTextPosition(SwingConstants.CENTER);
335 jBtnNewAnswer.setToolTipText("Nová");
336 jBtnNewAnswer.setActionCommand("Nová");
337 jBtnNewAnswer.addActionListener(new ActionListener() {
338 public void actionPerformed(ActionEvent evt) {
339 jBtnNewAnswerActionPerformed(evt);
340 }
341 });
342
343
344 jBtnDeleteAnswer = new JButton();
345 getContentPane().add(jBtnDeleteAnswer);
346 jBtnDeleteAnswer.setBounds(255, 446, 21, 21);
347 jBtnDeleteAnswer.setIcon(iconDelete);
348 jBtnDeleteAnswer.setVerticalTextPosition(SwingConstants.BOTTOM);
349 jBtnDeleteAnswer.setHorizontalTextPosition(SwingConstants.CENTER);
350 jBtnDeleteAnswer.setToolTipText("Odstráni\u0165");
351 jBtnDeleteAnswer.setActionCommand("Odstráni\u0165");
352 jBtnDeleteAnswer.addActionListener(new ActionListener() {
353 public void actionPerformed(ActionEvent evt) {
354 jBtnDeleteAnswerActionPerformed(evt);
355 }
356 });
357
358
359 jLabel3 = new JLabel();
360 getContentPane().add(jLabel3);
361 jLabel3.setText("Typ otázky:");
362 jLabel3.setBounds(10, 240, 264, 14);
363 jLabel3.setForeground(new java.awt.Color(100, 0, 0));
364
365
366 ComboBoxModel jCbxQuestionTypeModel = new DefaultComboBoxModel(
367 questionTypes.toArray());
368 jCbxQuestionType = new JComboBox();
369 getContentPane().add(jCbxQuestionType);
370 jCbxQuestionType.setModel(jCbxQuestionTypeModel);
371 jCbxQuestionType.setBounds(10, 260, 264, 20);
372 jCbxQuestionType.setBackground(new java.awt.Color(255, 255, 240));
373 jCbxQuestionType.addItemListener(new ItemListener() {
374 public void itemStateChanged(ItemEvent evt) {
375 jCbxQuestionTypeItemStateChanged(evt);
376 }
377 });
378
379
380 jLabel4 = new JLabel();
381 getContentPane().add(jLabel4);
382 jLabel4.setText("Zlo\u017eitos\u0165 otázky:");
383 jLabel4.setBounds(294, 89, 175, 14);
384 jLabel4.setForeground(new java.awt.Color(100, 0, 0));
385
386
387 jPbrDifficulty = new ProgressCellRenderer();
388
389 getContentPane().add(jPbrDifficulty);
390 jPbrDifficulty.setBounds(294, 109, 207, 20);
391
392
393 jScrollPane3 = new JScrollPane();
394 getContentPane().add(jScrollPane3);
395 jScrollPane3.setBounds(294, 160, 265, 120);
396 jScrollPane3.setBackground(new java.awt.Color(255, 255, 255));
397
398 jLstKeywords = new JList(keywords);
399 jScrollPane3.setViewportView(jLstKeywords);
400 jLstKeywords.setBackground(new java.awt.Color(255, 255, 240));
401 jLstKeywords.addMouseListener(new MouseAdapter() {
402 @Override
403 public void mouseClicked(MouseEvent evt) {
404 if (evt.getClickCount() == 1)
405 hint("Info: Dvojklikom môžete vybrané kľúčové slovo vymazať.");
406 else
407 jBtnDeleteKeywordActionPerformed(new ActionEvent(
408 jLstKeywords, 0, null));
409 }
410 });
411
412
413
414
415 ComboBoxModel jTfdDifficultyModel = new DefaultComboBoxModel(
416 new String[] { "1", "2", "3", "4", "5" });
417 jCbxDifficulty = new JComboBox();
418 getContentPane().add(jCbxDifficulty);
419
420 jCbxDifficulty.setModel(jTfdDifficultyModel);
421 jCbxDifficulty.setBounds(511, 107, 48, 20);
422 jCbxDifficulty.setBackground(new java.awt.Color(255, 255, 240));
423 jCbxDifficulty.addActionListener(new ActionListener() {
424 public void actionPerformed(ActionEvent evt) {
425 jTfdDifficultyActionPerformed(evt);
426 }
427 });
428
429
430
431 jScrollPane4 = new JScrollPane();
432 getContentPane().add(jScrollPane4);
433 jScrollPane4.setBounds(294, 313, 265, 122);
434 jScrollPane4.setBackground(new java.awt.Color(255, 255, 255));
435
436 jLstPictures = new JList(pictures);
437 jScrollPane4.setViewportView(jLstPictures);
438 jLstPictures.setBackground(new java.awt.Color(255, 255, 240));
439 jLstPictures.addListSelectionListener(new ListSelectionListener() {
440 public void valueChanged(ListSelectionEvent evt) {
441 if (jLstPictures.getSelectedIndex() != -1)
442 hint(((Picture) pictures.get(jLstPictures
443 .getSelectedIndex())).getDescription());
444 }
445 });
446 jLstPictures.addMouseListener(new MouseAdapter() {
447 @Override
448 public void mouseClicked(MouseEvent evt) {
449 if (evt.getClickCount() == 1)
450 hint("Info: Dvojklikom môžete vybraný obrázok vymazať.");
451 else
452 jBtnDeletePictureActionPerformed(new ActionEvent(
453 jLstKeywords, 0, null));
454 }
455 });
456
457
458
459 jLabel5 = new JLabel();
460 getContentPane().add(jLabel5);
461 jLabel5.setText("K\u013eú\u010dové slová:");
462 jLabel5.setBounds(294, 138, 267, 14);
463 jLabel5.setForeground(new java.awt.Color(100, 0, 0));
464
465
466 jLabel6 = new JLabel();
467 getContentPane().add(jLabel6);
468 jLabel6.setText("Obrázky:");
469 jLabel6.setBounds(294, 293, 183, 14);
470 jLabel6.setForeground(new java.awt.Color(100, 0, 0));
471
472
473 jBtnNewKeyword = new JButton();
474 getContentPane().add(jBtnNewKeyword);
475 jBtnNewKeyword.setBounds(514, 284, 21, 21);
476 jBtnNewKeyword.setIcon(iconAdd);
477 jBtnNewKeyword.setVerticalTextPosition(SwingConstants.BOTTOM);
478 jBtnNewKeyword.setHorizontalTextPosition(SwingConstants.CENTER);
479 jBtnNewKeyword.setToolTipText("Nové");
480 jBtnNewKeyword.setActionCommand("ksNové");
481 jBtnNewKeyword.addActionListener(new ActionListener() {
482 public void actionPerformed(ActionEvent evt) {
483 jBtnNewKeywordActionPerformed(evt);
484 }
485 });
486
487
488 jPbrDifficulty.addMouseListener(new ProgressbarMouseHandler(
489 jPbrDifficulty, jCbxDifficulty));
490
491
492
493
494
495
496 jBtnDeleteKeyword = new JButton();
497 getContentPane().add(jBtnDeleteKeyword);
498 jBtnDeleteKeyword.setBounds(539, 284, 21, 21);
499 jBtnDeleteKeyword.setIcon(iconDelete);
500 jBtnDeleteKeyword.setVerticalTextPosition(SwingConstants.BOTTOM);
501 jBtnDeleteKeyword.setHorizontalTextPosition(SwingConstants.CENTER);
502 jBtnDeleteKeyword.setToolTipText("Odstráni\u0165");
503 jBtnDeleteKeyword.setActionCommand("ksOdstráni\u0165");
504 jBtnDeleteKeyword.addActionListener(new ActionListener() {
505 public void actionPerformed(ActionEvent evt) {
506 jBtnDeleteKeywordActionPerformed(evt);
507 }
508 });
509
510
511 jBtnNewPicture = new JButton();
512 getContentPane().add(jBtnNewPicture);
513 jBtnNewPicture.setBounds(514, 446, 21, 21);
514 jBtnNewPicture.setIcon(iconAdd);
515 jBtnNewPicture.setVerticalTextPosition(SwingConstants.BOTTOM);
516 jBtnNewPicture.setHorizontalTextPosition(SwingConstants.CENTER);
517 jBtnNewPicture.setToolTipText("Nový");
518 jBtnNewPicture.setActionCommand("picNový");
519 jBtnNewPicture.addActionListener(new ActionListener() {
520 public void actionPerformed(ActionEvent evt) {
521 jBtnNewPictureActionPerformed(evt);
522 }
523 });
524
525
526 jBtnDeletePicture = new JButton();
527 getContentPane().add(jBtnDeletePicture);
528 jBtnDeletePicture.setBounds(539, 446, 21, 21);
529 jBtnDeletePicture.setIcon(iconDelete);
530 jBtnDeletePicture.setVerticalTextPosition(SwingConstants.BOTTOM);
531 jBtnDeletePicture.setHorizontalTextPosition(SwingConstants.CENTER);
532 jBtnDeletePicture.setToolTipText("Odstráni\u0165");
533 jBtnDeletePicture.setActionCommand("picOdstráni\u0165");
534 {
535 jBtnExtend = new JButton();
536 getContentPane().add(jBtnExtend);
537 jBtnExtend.setText("Ulo\u017ei\u0165 ako novú");
538 jBtnExtend.setBounds(229, 488, 144, 23);
539 jBtnExtend.setMnemonic(java.awt.event.KeyEvent.VK_U);
540 jBtnExtend.addActionListener(new ActionListener() {
541 public void actionPerformed(ActionEvent evt) {
542 jBtnExtendActionPerformed(evt);
543 }
544 });
545 }
546 jBtnDeletePicture.addActionListener(new ActionListener() {
547 public void actionPerformed(ActionEvent evt) {
548 jBtnDeletePictureActionPerformed(evt);
549 }
550 });
551
552
553 this.setSize(577, 551);
554 } catch (Exception e) {
555 e.printStackTrace();
556 }
557 }
558
559 private void jTfdDifficultyActionPerformed(ActionEvent evt) {
560 jPbrDifficulty.setIntegerValue(jCbxDifficulty.getSelectedIndex() + 1);
561 }
562
563 private void jBtnNewAnswerActionPerformed(ActionEvent evt) {
564 NewAnswerDialog nad = new NewAnswerDialog(this, null);
565 nad.setLocationRelativeTo(null);
566 Answer a = nad.showDialog();
567 if (a == null)
568 return;
569
570 if (a.getText().length() == 0)
571 hint("Warning: Musíte zadať text odpovede!");
572 else
573 model.addAnswer(a);
574 }
575
576 private void jBtnEditAnswerActionPerformed(ActionEvent evt) {
577
578 int sel = jTblAnswers.getSelectedRow();
579
580 if (sel == -1) {
581 hint("Warning: Musíte vybrať odpoveď, ktorú chcete upraviť!");
582 return;
583 }
584
585 NewAnswerDialog nad = new NewAnswerDialog(this, model.getAnswer(sel));
586 nad.setLocationRelativeTo(null);
587 Answer a = nad.showDialog();
588 if (a == null)
589 return;
590
591 if (a.getText().length() == 0)
592 hint("Warning: Musíte zadať text odpovede!");
593 else
594 model.setValueAt(a.getText(), sel, 0);
595 }
596
597 private void jBtnDeleteAnswerActionPerformed(ActionEvent evt) {
598
599 int sel = jTblAnswers.getSelectedRow();
600 if (sel != -1)
601 model.removeAnswer(sel);
602 else
603 hint("Warning: Musíte vybrať odpoveď, ktorú chcete odstrániť!");
604 }
605
606 private boolean hasKeyword(Keyword k) {
607 for (int i = 0; i < keywords.size(); i++)
608 if (k.getText().equals(((Keyword) (keywords.get(i))).getText()))
609 return true;
610
611 return false;
612 }
613
614 private boolean hasPicture(Picture p) {
615 for (int i = 0; i < pictures.size(); i++)
616 if (p.getPictureID() == ((Picture) pictures.get(i)).getPictureID())
617 return true;
618
619 return false;
620 }
621
622 private void jBtnNewKeywordActionPerformed(ActionEvent evt) {
623 boolean hasDuplicity = false;
624 NewKeywordDialog nkd = new NewKeywordDialog(this, connection);
625 nkd.setLocationRelativeTo(null);
626 Keyword[] k = nkd.showDialog();
627 if (k != null) {
628 for (int i = 0; i < k.length; i++)
629 if (hasKeyword(k[i]) == false)
630 keywords.addElement(k[i]);
631 else {
632 hasDuplicity = true;
633 }
634 }
635 if (hasDuplicity)
636 hint("Info: Niektoré kľúčové slová neboli pridané, lebo sa v zozname už nachádzajú.");
637 else
638 hint("Info: Všetky vybrané kľúčové slová boli úspešne pridané.");
639 }
640
641 private void jBtnDeleteKeywordActionPerformed(ActionEvent evt) {
642 int sel = jLstKeywords.getSelectedIndex();
643
644 if (sel == -1) {
645 hint("Warning: Najprv musíte vybrať kľúčové slová, ktoré chcete odstrániť.");
646 return;
647 }
648
649 while (sel != -1) {
650 keywords.remove(sel);
651 sel = jLstKeywords.getSelectedIndex();
652 }
653 }
654
655 private void jBtnNewPictureActionPerformed(ActionEvent evt) {
656 boolean hasDuplicity = false;
657 NewPictureDialog npd = new NewPictureDialog(this, connection);
658 npd.setLocationRelativeTo(null);
659 Picture[] pixs = npd.showDialog();
660
661 if (pixs == null)
662 return;
663
664 for (int i = 0; i < pixs.length; i++) {
665 if (hasPicture(pixs[i]) == false)
666 pictures.addElement(pixs[i]);
667 else {
668 hasDuplicity = true;
669 }
670 }
671
672 if (hasDuplicity)
673 hint("Info: Niektoré obrázky neboli pridané, lebo sa v zozname už nachádzajú.");
674 else
675 hint("Info: Všetky vybrané obrázky boli úspešne pridané.");
676 }
677
678 private void jBtnDeletePictureActionPerformed(ActionEvent evt) {
679 int sel = jLstPictures.getSelectedIndex();
680
681 if (sel == -1) {
682 hint("Warning: Najprv musíte vybrať obrázky, ktoré chcete odstrániť.");
683 return;
684 }
685
686 while (sel != -1) {
687 pictures.remove(sel);
688 sel = jLstPictures.getSelectedIndex();
689 }
690 }
691
692 private void jBtnCancelActionPerformed(ActionEvent evt) {
693
694 ret = null;
695 setVisible(false);
696 }
697
698 private void jBtnOKActionPerformed(ActionEvent evt) {
699
700 ret.setText(jTxtQuestionText.getText());
701 ret.setQuestionTypeID(questionTypes.get(
702 jCbxQuestionType.getSelectedIndex()).getQuestionTypeID());
703
704 ret.setCreated(new Timestamp(0));
705 ret.setLastUpdate(new Timestamp(0));
706
707
708 try {
709 ret.setUserID(uDao.getDefaultTUser().getUserID());
710 } catch (Exception e) {
711 }
712 ret.setDifficulty(jCbxDifficulty.getSelectedIndex() + 1);
713
714
715 try {
716 if (evt == null) {
717
718 if (actCategories.size() != 0) {
719 for (Category c : actCategories) {
720 qDao.addQuestion(c.getCategoryID(), ret);
721 }
722 } else {
723 qDao.createQuestion(ret);
724 }
725 } else {
726 if (ret.getQuestionID() != null) {
727 qDao.updateQuestion(ret.getQuestionID(), ret);
728 } else {
729 qDao.addQuestion(actCategories.get(0).getCategoryID(), ret);
730 }
731 }
732 } catch (Exception e) {
733 hint("Error: " + e.getLocalizedMessage());
734 return;
735 }
736
737 model.saveAnswers(ret, connection);
738
739
740 int i, size = keywords.size();
741
742
743 try {
744 kDao.deleteAllKeywordsFromQuestion(ret.getQuestionID());
745 for (i = 0; i < size; i++)
746 kDao.addKeyword(ret.getQuestionID(), (Keyword) keywords.get(i));
747 } catch (Exception e) {
748 hint("Error: " + e.getLocalizedMessage());
749 return;
750 }
751
752 ArrayList<Picture> pixs = null;
753
754
755 try {
756 pixs = pDao.getPicturesByQuestionID(ret.getQuestionID());
757
758 for (Picture p : pixs)
759 pDao.deletePictureFromQuestion(p.getPictureID(), ret
760 .getQuestionID());
761 for (i = 0; i < pictures.size(); i++)
762 pDao.addPictureToQuestion(((Picture) pictures.get(i))
763 .getPictureID(), ret.getQuestionID());
764 } catch (Exception e) {
765 hint("Error: " + e.getLocalizedMessage());
766 return;
767 }
768 setVisible(false);
769 }
770
771 private void hint(String text) {
772 if (jTxtHint.getBackground() == GenexGUI.COMB1_BACKGROUND_COLOR) {
773 jTxtHint.setBackground(GenexGUI.COMB2_BACKGROUND_COLOR);
774 jTxtHint.setForeground(GenexGUI.COMB2_FOREGROUND_COLOR);
775 } else {
776 jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
777 jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
778 }
779 if (text.startsWith("Error:")) {
780 jTxtHint.setText(text.substring(6));
781 jTxtHint.setCaretPosition(0);
782 jTxtHint.insertIcon(new ImageIcon(resHelp
783 .getResourceBytes("/icons/error.png")));
784 } else if (text.startsWith("Warning:")) {
785 jTxtHint.setText(text.substring(8));
786 jTxtHint.setCaretPosition(0);
787 jTxtHint.insertIcon(new ImageIcon(resHelp
788 .getResourceBytes("/icons/warning.png")));
789 } else if (text.startsWith("Info:")) {
790 jTxtHint.setText(text.substring(5));
791 jTxtHint.setCaretPosition(0);
792 jTxtHint.insertIcon(new ImageIcon(resHelp
793 .getResourceBytes("/icons/info.png")));
794 } else {
795 jTxtHint.setText(text);
796 }
797 }
798
799 public Question showDialog() {
800 this.setModalityType(ModalityType.DOCUMENT_MODAL);
801
802 setVisible(true);
803 return ret;
804 }
805
806 public void fillFields(Question q) {
807 jTxtQuestionText.setText(q.getText());
808
809 for (int i = 0; i < questionTypes.size(); i++)
810 if (questionTypes.get(i).getQuestionTypeID() == q
811 .getQuestionTypeID())
812 jCbxQuestionType.setSelectedIndex(i);
813
814 model.setAnswers(q, connection);
815
816 jCbxDifficulty.setSelectedIndex(q.getDifficulty() - 1);
817 jPbrDifficulty.setIntegerValue(q.getDifficulty());
818
819 keywords.clear();
820
821
822 try {
823 ArrayList<Keyword> keys = kDao.getKeywordsByQuestionID(q
824 .getQuestionID());
825 for (Keyword key : keys)
826 keywords.addElement(key);
827 } catch (Exception e) {
828 }
829 ArrayList<Picture> pixs = null;
830
831
832 try {
833 pixs = pDao.getPicturesByQuestionID(q.getQuestionID());
834 for (Picture pic : pixs)
835 pictures.addElement(pic);
836 } catch (Exception e) {
837 }
838 jLstKeywords.updateUI();
839 jLstPictures.updateUI();
840 jTxtQuestionText.setCaretPosition(0);
841 }
842
843 private void jCbxQuestionTypeItemStateChanged(ItemEvent evt) {
844 QuestionType qType = (QuestionType) jCbxQuestionType.getSelectedItem();
845 hint(qType.getDescription());
846 String qt = qType.getName();
847
848 if ("Single choice".equals(qt)) {
849 jTblAnswers.setEnabled(true);
850 model.setSingleSelect(true);
851 jBtnNewAnswer.setEnabled(true);
852 jBtnEditAnswer.setEnabled(true);
853 jBtnDeleteAnswer.setEnabled(true);
854 } else if ("Multi choice".equals(qt)) {
855 jTblAnswers.setEnabled(true);
856 model.setSingleSelect(false);
857 jBtnNewAnswer.setEnabled(true);
858 jBtnEditAnswer.setEnabled(true);
859 jBtnDeleteAnswer.setEnabled(true);
860 } else if ("True/False".equals(qt)) {
861 jTblAnswers.setEnabled(true);
862 model.setSingleSelect(true);
863 model.setAnswers(null);
864 model.addAnswer(new Answer("Áno", true, 0, null));
865 model.addAnswer(new Answer("Nie", false, 0, null));
866 jBtnNewAnswer.setEnabled(false);
867 jBtnEditAnswer.setEnabled(true);
868 jBtnDeleteAnswer.setEnabled(false);
869 } else if ("Fill in".equals(qt)) {
870 jTblAnswers.setEnabled(false);
871 model.setSingleSelect(false);
872 model.setAnswers(null);
873 jBtnNewAnswer.setEnabled(false);
874 jBtnEditAnswer.setEnabled(false);
875 jBtnDeleteAnswer.setEnabled(false);
876 } else if ("Essay".equals(qt)) {
877 model.setAnswers(null);
878 jTblAnswers.setEnabled(false);
879 jBtnNewAnswer.setEnabled(false);
880 jBtnEditAnswer.setEnabled(false);
881 jBtnDeleteAnswer.setEnabled(false);
882 }
883
884 model.fireTableDataChanged();
885 }
886
887 private void jBtnExtendActionPerformed(ActionEvent evt) {
888
889
890 if (ret.getDerivedFromID() == null) {
891 ret.setDerivedFromID(ret.getQuestionID());
892 }
893
894 jBtnOKActionPerformed(null);
895 }
896 }