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.ItemEvent;
7 import java.awt.event.ItemListener;
8 import java.awt.event.MouseAdapter;
9 import java.awt.event.MouseEvent;
10 import java.sql.Connection;
11 import java.sql.SQLException;
12 import java.util.ArrayList;
13 import java.util.LinkedList;
14
15 import javax.swing.BorderFactory;
16 import javax.swing.ButtonGroup;
17 import javax.swing.ComboBoxModel;
18 import javax.swing.DefaultComboBoxModel;
19 import javax.swing.DefaultListModel;
20 import javax.swing.ImageIcon;
21 import javax.swing.JButton;
22 import javax.swing.JCheckBox;
23 import javax.swing.JComboBox;
24 import javax.swing.JFrame;
25 import javax.swing.JLabel;
26 import javax.swing.JList;
27 import javax.swing.JRadioButton;
28 import javax.swing.JScrollPane;
29 import javax.swing.JSeparator;
30 import javax.swing.JSpinner;
31 import javax.swing.JTextField;
32 import javax.swing.JTextPane;
33 import javax.swing.JTree;
34 import javax.swing.SpinnerNumberModel;
35 import javax.swing.SwingConstants;
36 import javax.swing.border.TitledBorder;
37 import javax.swing.event.ChangeEvent;
38 import javax.swing.event.ChangeListener;
39 import javax.swing.tree.DefaultMutableTreeNode;
40 import javax.swing.tree.TreePath;
41
42 import sk.stuba.fiit.foo07.genex.beans.Category;
43 import sk.stuba.fiit.foo07.genex.beans.Keyword;
44 import sk.stuba.fiit.foo07.genex.beans.Test;
45 import sk.stuba.fiit.foo07.genex.common.ResourceHelper;
46 import sk.stuba.fiit.foo07.genex.dao.CategoryDao;
47 import sk.stuba.fiit.foo07.genex.dao.CategoryDaoDerby;
48 import sk.stuba.fiit.foo07.genex.dao.QuestionDao;
49 import sk.stuba.fiit.foo07.genex.dao.QuestionDaoDerby;
50 import sk.stuba.fiit.foo07.genex.dao.QuestionTypeDao;
51 import sk.stuba.fiit.foo07.genex.dao.QuestionTypeDaoDerby;
52 import sk.stuba.fiit.foo07.genex.exceptions.NotEnoughQuestionsException;
53 import sk.stuba.fiit.foo07.genex.generator.TestGenerator;
54
55
56
57
58
59
60
61
62
63
64
65 public class GenerateTestDialog extends javax.swing.JDialog {
66
67
68
69
70 private static final long serialVersionUID = 92194243772639229L;
71
72 private ResourceHelper resHelp;
73 private DefaultMutableTreeNode rootNode;
74 private DefaultListModel keywords;
75 private Connection connection;
76 private CategoryDao cDao;
77 private QuestionDao qDao;
78 private QuestionTypeDao qtDao;
79 private CheckBoxTreeCellRenderer treeRenderer;
80
81 private Test generatedTest;
82
83 private ArrayList<Integer> difficultiesCount;
84 private LinkedList<Integer> questionCategoryIDs;
85 private ArrayList<Integer> keywordIDs;
86 private LinkedList<Integer> questionTypeIDs;
87 private int questionCount;
88 private int pointsSummDesired;
89
90 private JScrollPane jScrollPane1;
91 private JScrollPane jScrollPane2;
92 private JScrollPane jScrollPane3;
93 private JLabel jLabel6;
94 private JLabel jLabel5;
95 private JCheckBox jChbEssay;
96 private JList jLstKeywords;
97 private JLabel jLabel12;
98 private ButtonGroup buttonGroup1;
99 private JSpinner jSpinner5;
100 private JSpinner jSpinner4;
101 private JSpinner jSpinner3;
102 private JSpinner jSpinner2;
103 private JSpinner jSpinner1;
104 private JLabel jLabel11;
105 private JLabel jLabel10;
106 private JLabel jLabel9;
107 private JLabel jLabel8;
108 private JLabel jLabel7;
109 private JRadioButton jRadioButtonComplexityCount;
110 private JRadioButton jRadioButtonAvgComplexity;
111 private JLabel jLabel2;
112 private JComboBox jCbxDiff;
113 private ProgressCellRenderer jPbrDiff;
114 private JButton jBtnNewKeyword;
115 private JButton jBtnDeletePicture;
116 private JCheckBox jChbPicturesToo;
117 private JCheckBox jChbFillIn;
118 private JCheckBox jChbTrueFalse;
119 private JCheckBox jChbMulti;
120 private JCheckBox jChbSingle;
121 private JTextField jTfdPoints;
122 private JLabel jLabel4;
123 private JTextField jTfdQuestionCount;
124 private JLabel jLabel3;
125 private JLabel jLblQCount;
126 private JLabel jLabel1;
127 private JButton jBtnOK;
128 private JButton jBtnCancel;
129 private JTree jTreeCategories;
130 private JSeparator jSeparator2;
131 private JSeparator jSeparator1;
132 private JTextPane jTxtHint;
133
134 private int retVal = javax.swing.JOptionPane.CANCEL_OPTION;
135
136
137
138
139 public Test getGeneratedTest() {
140 return generatedTest;
141 }
142
143 public GenerateTestDialog(JFrame frame, Connection c) {
144 super(frame);
145
146 resHelp = new ResourceHelper();
147 connection = c;
148 try {
149 cDao = new CategoryDaoDerby(c);
150 qDao = new QuestionDaoDerby(c);
151 qtDao = new QuestionTypeDaoDerby(c);
152 Category root = cDao.getRootCategories().get(0);
153 rootNode = new DefaultMutableTreeNode(new CheckBoxCategory(root));
154 keywords = new DefaultListModel();
155 createTree(rootNode, root.getCategoryID());
156 } catch (SQLException e) {
157 hint("Error: " + e.getMessage());
158 }
159 initGUI();
160 }
161
162 private void createTree(DefaultMutableTreeNode node, Integer cID) {
163 ArrayList<Category> cats = null;
164 try {
165 cats = cDao.getCategoriesByParentID(cID);
166 for (Category subc : cats) {
167 CheckBoxCategory sc = new CheckBoxCategory(subc);
168 DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(sc);
169 node.add(subNode);
170 createTree(subNode, sc.getCategoryID());
171 }
172 } catch (SQLException e) {
173 hint(e.getMessage());
174 }
175 }
176
177 private void initGUI() {
178 try {
179
180 this.setTitle("Generova\u0165 test");
181 getContentPane().setLayout(null);
182 this.setResizable(false);
183 setLocationByPlatform(true);
184
185 jScrollPane1 = new JScrollPane();
186 getContentPane().add(jScrollPane1);
187 jScrollPane1.setBounds(10, 11, 710, 56);
188
189 jTxtHint = new JTextPane();
190 jScrollPane1.setViewportView(jTxtHint);
191 hint("Info: Hints");
192 jTxtHint.setEditable(false);
193 jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
194 jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
195 jTxtHint.setPreferredSize(new java.awt.Dimension(682, 53));
196
197
198
199 jSeparator1 = new JSeparator();
200 getContentPane().add(jSeparator1);
201 jSeparator1.setBounds(10, 73, 710, 12);
202
203
204 jSeparator2 = new JSeparator();
205 getContentPane().add(jSeparator2);
206 jSeparator2.setBounds(14, 435, 702, 6);
207
208
209 jScrollPane2 = new JScrollPane();
210 getContentPane().add(jScrollPane2);
211 jScrollPane2.setBounds(10, 106, 208, 297);
212
213 jTreeCategories = new JTree(rootNode);
214 jScrollPane2.setViewportView(jTreeCategories);
215 treeRenderer = new CheckBoxTreeCellRenderer();
216 jTreeCategories.setCellRenderer(treeRenderer);
217 jTreeCategories.setEditable(false);
218 jTreeCategories.setBackground(new java.awt.Color(255, 255, 240));
219 jTreeCategories.setPreferredSize(new java.awt.Dimension(170, 214));
220 jTreeCategories.addMouseListener(new MouseAdapter() {
221 @Override
222 public void mouseClicked(MouseEvent evt) {
223 jTreeCategoriesMouseClicked(evt);
224 }
225 });
226
227
228
229 jBtnCancel = new JButton();
230 getContentPane().add(jBtnCancel);
231 jBtnCancel.setText("Cancel");
232 jBtnCancel.setBounds(606, 447, 111, 23);
233 jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
234 jBtnCancel.addActionListener(new ActionListener() {
235 public void actionPerformed(ActionEvent evt) {
236 jBtnCancelActionPerformed(evt);
237 }
238 });
239
240
241 jBtnOK = new JButton();
242 getContentPane().add(jBtnOK);
243 jBtnOK.setText("OK");
244 jBtnOK.setBounds(490, 447, 111, 23);
245 jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
246 jBtnOK.addActionListener(new ActionListener() {
247 public void actionPerformed(ActionEvent evt) {
248 jBtnOKActionPerformed(evt);
249 }
250 });
251
252
253 jLabel1 = new JLabel();
254 getContentPane().add(jLabel1);
255 jLabel1.setText("Po\u010det vybraných otázok");
256 jLabel1.setBounds(12, 409, 155, 14);
257 jLabel1.setForeground(new java.awt.Color(100, 0, 0));
258
259
260 jLblQCount = new JLabel();
261 getContentPane().add(jLblQCount);
262 jLblQCount.setText("0");
263 jLblQCount.setBounds(179, 409, 41, 14);
264 jLblQCount.setHorizontalAlignment(SwingConstants.RIGHT);
265 jLblQCount.setForeground(new java.awt.Color(0, 0, 255));
266
267
268 jLabel3 = new JLabel();
269 getContentPane().add(jLabel3);
270 jLabel3.setText("Po\u010det otázok");
271 jLabel3.setBounds(508, 105, 91, 14);
272 jLabel3.setForeground(new java.awt.Color(100, 0, 0));
273
274
275 jTfdQuestionCount = new JTextField();
276 getContentPane().add(jTfdQuestionCount);
277 jTfdQuestionCount.setText("0");
278 jTfdQuestionCount.setBounds(681, 99, 35, 18);
279 jTfdQuestionCount.setHorizontalAlignment(SwingConstants.RIGHT);
280 jTfdQuestionCount.setBackground(new java.awt.Color(255, 255, 240));
281 jTfdQuestionCount.setForeground(new java.awt.Color(0, 0, 255));
282
283
284 jLabel4 = new JLabel();
285 getContentPane().add(jLabel4);
286 jLabel4.setText("Po\u010det bodov");
287 jLabel4.setBounds(508, 125, 84, 14);
288 jLabel4.setForeground(new java.awt.Color(100, 0, 0));
289
290
291 jTfdPoints = new JTextField();
292 getContentPane().add(jTfdPoints);
293 jTfdPoints.setText("0");
294 jTfdPoints.setBounds(681, 123, 35, 18);
295 jTfdPoints.setHorizontalAlignment(SwingConstants.RIGHT);
296 jTfdPoints.setBackground(new java.awt.Color(255, 255, 240));
297 jTfdPoints.setForeground(new java.awt.Color(0, 0, 255));
298 jTfdPoints.addMouseListener(new MouseAdapter() {
299 @Override
300 public void mouseClicked(MouseEvent evt) {
301 hint("Info: Ak je hodnota poľa rovná nule, počet bodov pre otázky vo vygenerovanom teste bude \n rovný ich obtiažnosti.");
302 }
303 });
304
305
306 jChbSingle = new JCheckBox();
307 getContentPane().add(jChbSingle);
308 jChbSingle.setText("Single choice");
309 jChbSingle.setBounds(517, 173, 111, 23);
310 jChbSingle.setSelected(true);
311
312
313 jChbMulti = new JCheckBox();
314 getContentPane().add(jChbMulti);
315 jChbMulti.setText("Multi choice");
316 jChbMulti.setBounds(517, 195, 117, 23);
317 jChbMulti.setSelected(true);
318
319
320 jChbTrueFalse = new JCheckBox();
321 getContentPane().add(jChbTrueFalse);
322 jChbTrueFalse.setText("True/False");
323 jChbTrueFalse.setBounds(518, 219, 117, 23);
324 jChbTrueFalse.setSelected(true);
325
326
327 jChbFillIn = new JCheckBox();
328 getContentPane().add(jChbFillIn);
329 jChbFillIn.setText("Fill in");
330 jChbFillIn.setBounds(634, 173, 74, 23);
331 jChbFillIn.setSelected(true);
332
333
334 jChbEssay = new JCheckBox();
335 getContentPane().add(jChbEssay);
336 jChbEssay.setText("Essay");
337 jChbEssay.setBounds(634, 195, 69, 26);
338 jChbEssay.setSelected(true);
339
340
341 jLabel5 = new JLabel();
342 getContentPane().add(jLabel5);
343 jLabel5.setBounds(508, 153, 208, 95);
344 TitledBorder border = BorderFactory
345 .createTitledBorder("Typy otázok");
346 border.setTitleColor(new Color(49, 106, 196));
347 jLabel5.setBorder(border);
348 jLabel5.setHorizontalAlignment(SwingConstants.LEFT);
349 jLabel5.setHorizontalTextPosition(SwingConstants.LEFT);
350 jLabel5.setVerticalAlignment(SwingConstants.TOP);
351 jLabel5.setVerticalTextPosition(SwingConstants.TOP);
352
353
354 jLabel6 = new JLabel();
355 getContentPane().add(jLabel6);
356 jLabel6.setText("K\u013eú\u010dové slová:");
357 jLabel6.setBounds(509, 254, 161, 15);
358 jLabel6.setForeground(new java.awt.Color(100, 0, 0));
359
360
361 jScrollPane3 = new JScrollPane();
362 getContentPane().add(jScrollPane3);
363
364 jScrollPane3.setBounds(508, 275, 210, 123);
365 {
366 jChbPicturesToo = new JCheckBox();
367 getContentPane().add(jChbPicturesToo);
368 jChbPicturesToo.setText("Vybera\u0165 aj otázky s obrázkami");
369 jChbPicturesToo.setBounds(258, 407, 250, 18);
370 jChbPicturesToo.setForeground(new java.awt.Color(100, 0, 0));
371 jChbPicturesToo.setSelected(true);
372 jChbPicturesToo.setMnemonic(java.awt.event.KeyEvent.VK_V);
373 }
374 {
375 jBtnDeletePicture = new JButton();
376 getContentPane().add(jBtnDeletePicture);
377 jBtnDeletePicture.setIcon(new ImageIcon(new ResourceHelper()
378 .getResourceBytes("/icons/remove.png")));
379 jBtnDeletePicture.setActionCommand("picOdstráni\u0165");
380 jBtnDeletePicture
381 .setHorizontalTextPosition(SwingConstants.CENTER);
382 jBtnDeletePicture
383 .setVerticalTextPosition(SwingConstants.BOTTOM);
384 jBtnDeletePicture.setToolTipText("Odstráni\u0165");
385 jBtnDeletePicture.setBounds(696, 404, 21, 21);
386 jBtnDeletePicture.addActionListener(new ActionListener() {
387 public void actionPerformed(ActionEvent evt) {
388 jBtnDeleteKeywordActionPerformed(evt);
389 }
390 });
391 }
392 {
393 jBtnNewKeyword = new JButton();
394 getContentPane().add(jBtnNewKeyword);
395 jBtnNewKeyword.setIcon(new ImageIcon(new ResourceHelper()
396 .getResourceBytes("/icons/add.png")));
397 jBtnNewKeyword.setActionCommand("ksNové");
398 jBtnNewKeyword.setHorizontalTextPosition(SwingConstants.CENTER);
399 jBtnNewKeyword.setVerticalTextPosition(SwingConstants.BOTTOM);
400 jBtnNewKeyword.setToolTipText("Nové");
401 jBtnNewKeyword.setBounds(670, 404, 21, 21);
402 jBtnNewKeyword.addActionListener(new ActionListener() {
403 public void actionPerformed(ActionEvent evt) {
404 jBtnNewKeywordActionPerformed(evt);
405 }
406 });
407 }
408 {
409 ComboBoxModel jTfdDifficultyModel = new DefaultComboBoxModel(
410 new String[] { "NA", "1", "2", "3", "4", "5" });
411 jCbxDiff = new JComboBox();
412 getContentPane().add(jCbxDiff);
413 jCbxDiff.setModel(jTfdDifficultyModel);
414 jCbxDiff.setBounds(435, 163, 45, 21);
415 jCbxDiff.setBackground(new java.awt.Color(255, 255, 240));
416 jCbxDiff.setForeground(new java.awt.Color(0, 0, 255));
417 jCbxDiff.addActionListener(new ActionListener() {
418 public void actionPerformed(ActionEvent evt) {
419 int indexSelected = jCbxDiff.getSelectedIndex();
420 if (indexSelected == 0) {
421 disableProgressBar();
422 } else {
423 enableProgressBar();
424 jPbrDiff.setIntegerValue(indexSelected);
425 }
426 }
427 });
428 }
429 {
430 jPbrDiff = new ProgressCellRenderer();
431 getContentPane().add(jPbrDiff);
432 jPbrDiff.setBounds(259, 163, 170, 21);
433
434 jPbrDiff.setIntegerValue(0);
435 jPbrDiff.setFocusable(false);
436 jPbrDiff.setRequestFocusEnabled(false);
437 jPbrDiff.addMouseListener(new ProgressbarMouseHandler(jPbrDiff,
438 jCbxDiff, 1));
439 }
440 {
441 jLstKeywords = new JList();
442
443 jScrollPane3.setViewportView(jLstKeywords);
444
445 jLstKeywords.setModel(keywords);
446 jLstKeywords.setBackground(new java.awt.Color(255, 255, 240));
447 jLstKeywords.setBounds(228, 408, 267, 93);
448 jLstKeywords.setPreferredSize(new java.awt.Dimension(145, 80));
449 jLstKeywords.addMouseListener(new MouseAdapter() {
450 @Override
451 public void mouseClicked(MouseEvent evt) {
452 if (evt.getClickCount() == 1)
453 hint("Info: Dvojklikom môžete vybrané kľúčové slovo vymazať.");
454 else
455 jBtnDeleteKeywordActionPerformed(new ActionEvent(
456 jLstKeywords, 0, null));
457 }
458 });
459 }
460 {
461 jLabel2 = new JLabel();
462 getContentPane().add(jLabel2);
463 jLabel2.setBounds(226, 99, 274, 304);
464 border = BorderFactory.createTitledBorder("Spôsob generovania");
465 border.setTitleColor(new Color(49, 106, 196));
466 jLabel2.setBorder(border);
467 }
468 {
469 jRadioButtonAvgComplexity = new JRadioButton();
470 getContentPane().add(jRadioButtonAvgComplexity);
471 jRadioButtonAvgComplexity
472 .setText("Priemerná zlo\u017eitos\u0165 otázok");
473 jRadioButtonAvgComplexity.setBounds(240, 135, 214, 19);
474 jRadioButtonAvgComplexity.setForeground(new java.awt.Color(100,
475 0, 0));
476 jRadioButtonAvgComplexity.addItemListener(new ItemListener() {
477 public void itemStateChanged(ItemEvent evt) {
478 jRadioButton1ItemStateChanged(evt);
479 }
480 });
481 getButtonGroup1().add(jRadioButtonAvgComplexity);
482 getButtonGroup1().setSelected(
483 jRadioButtonAvgComplexity.getModel(), true);
484 }
485 {
486 jRadioButtonComplexityCount = new JRadioButton();
487 getContentPane().add(jRadioButtonComplexityCount);
488 jRadioButtonComplexityCount
489 .setText("Ur\u010denie po\u010dtu otázok s obtia\u017enos\u0165ou");
490 jRadioButtonComplexityCount.setBounds(240, 206, 250, 19);
491 jRadioButtonComplexityCount.setForeground(new java.awt.Color(
492 100, 0, 0));
493 jRadioButtonComplexityCount.addItemListener(new ItemListener() {
494
495 public void itemStateChanged(ItemEvent evt) {
496 jRadioButton2ItemStateChanged(evt);
497 }
498 });
499 getButtonGroup1().add(jRadioButtonComplexityCount);
500 }
501 {
502 jLabel7 = new JLabel();
503 getContentPane().add(jLabel7);
504 jLabel7.setText("obtia\u017enos\u0165 1");
505 jLabel7.setBounds(260, 237, 84, 15);
506 jLabel7.setForeground(new java.awt.Color(0, 0, 0));
507 }
508 {
509 jLabel8 = new JLabel();
510 getContentPane().add(jLabel8);
511 jLabel8.setText("obtia\u017enos\u0165 2");
512 jLabel8.setBounds(260, 268, 82, 15);
513 jLabel8.setForeground(new java.awt.Color(0, 0, 0));
514 }
515 {
516 jLabel9 = new JLabel();
517 getContentPane().add(jLabel9);
518 jLabel9.setText("obtia\u017enos\u0165 3");
519 jLabel9.setBounds(260, 299, 85, 15);
520 jLabel9.setForeground(new java.awt.Color(0, 0, 0));
521 }
522 {
523 jLabel10 = new JLabel();
524 getContentPane().add(jLabel10);
525 jLabel10.setText("obtia\u017enos\u0165 4");
526 jLabel10.setBounds(258, 330, 87, 15);
527 jLabel10.setForeground(new java.awt.Color(0, 0, 0));
528 }
529 {
530 jLabel11 = new JLabel();
531 getContentPane().add(jLabel11);
532 jLabel11.setText("obtia\u017enos\u0165 5");
533 jLabel11.setBounds(258, 361, 87, 15);
534 jLabel11.setForeground(new java.awt.Color(0, 0, 0));
535 }
536 {
537 SpinnerNumberModel jSpinnerModel = new SpinnerNumberModel();
538 jSpinnerModel.setMinimum(0);
539 jSpinner1 = new JSpinner();
540 getContentPane().add(jSpinner1);
541 jSpinner1.setModel(jSpinnerModel);
542 jSpinner1.setBounds(349, 234, 55, 22);
543 jSpinner1.addChangeListener(new ChangeListener() {
544 public void stateChanged(ChangeEvent evt) {
545 jSpinnerStateChanged(evt);
546 }
547 });
548 jSpinner1.setEnabled(false);
549 jSpinner1.getEditor().setBackground(
550 new java.awt.Color(255, 255, 240));
551 }
552 {
553 SpinnerNumberModel jSpinnerModel = new SpinnerNumberModel();
554 jSpinnerModel.setMinimum(0);
555 jSpinner2 = new JSpinner();
556 getContentPane().add(jSpinner2);
557 jSpinner2.setModel(jSpinnerModel);
558 jSpinner2.setBounds(349, 265, 55, 22);
559 jSpinner2.addChangeListener(new ChangeListener() {
560 public void stateChanged(ChangeEvent evt) {
561 jSpinnerStateChanged(evt);
562 }
563 });
564 jSpinner2.setEnabled(false);
565 jSpinner2.getEditor().setBackground(
566 new java.awt.Color(255, 255, 240));
567 }
568 {
569 SpinnerNumberModel jSpinnerModel = new SpinnerNumberModel();
570 jSpinnerModel.setMinimum(0);
571 jSpinner3 = new JSpinner();
572 getContentPane().add(jSpinner3);
573 jSpinner3.setModel(jSpinnerModel);
574 jSpinner3.setBounds(349, 296, 55, 22);
575 jSpinner3.addChangeListener(new ChangeListener() {
576 public void stateChanged(ChangeEvent evt) {
577 jSpinnerStateChanged(evt);
578 }
579 });
580 jSpinner3.setEnabled(false);
581 jSpinner3.getEditor().setBackground(
582 new java.awt.Color(255, 255, 240));
583 }
584 {
585 SpinnerNumberModel jSpinnerModel = new SpinnerNumberModel();
586 jSpinnerModel.setMinimum(0);
587 jSpinner4 = new JSpinner();
588 getContentPane().add(jSpinner4);
589 jSpinner4.setModel(jSpinnerModel);
590 jSpinner4.setBounds(349, 327, 55, 22);
591 jSpinner4.addChangeListener(new ChangeListener() {
592 public void stateChanged(ChangeEvent evt) {
593 jSpinnerStateChanged(evt);
594 }
595 });
596 jSpinner4.setEnabled(false);
597 jSpinner4.getEditor().setBackground(
598 new java.awt.Color(255, 255, 240));
599 }
600 {
601 SpinnerNumberModel jSpinnerModel = new SpinnerNumberModel();
602 jSpinnerModel.setMinimum(0);
603 jSpinner5 = new JSpinner();
604 getContentPane().add(jSpinner5);
605 getContentPane().add(getJLabel12());
606 jSpinner5.setModel(jSpinnerModel);
607 jSpinner5.setBounds(349, 358, 55, 22);
608 jSpinner5.addChangeListener(new ChangeListener() {
609 public void stateChanged(ChangeEvent evt) {
610 jSpinnerStateChanged(evt);
611 }
612 });
613 jSpinner5.setEnabled(false);
614 jSpinner5.getEditor().setBackground(
615 new java.awt.Color(255, 255, 240));
616 }
617
618
619
620
621 this.setSize(734, 511);
622 } catch (Exception e) {
623 e.printStackTrace();
624 }
625 }
626
627 private void hint(String text) {
628 if (jTxtHint.getBackground() == GenexGUI.COMB1_BACKGROUND_COLOR) {
629 jTxtHint.setBackground(GenexGUI.COMB2_BACKGROUND_COLOR);
630 jTxtHint.setForeground(GenexGUI.COMB2_FOREGROUND_COLOR);
631 } else {
632 jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
633 jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
634 }
635 if (text.startsWith("Error:")) {
636 jTxtHint.setText(text.substring(6));
637 jTxtHint.setCaretPosition(0);
638 jTxtHint.insertIcon(new ImageIcon(resHelp
639 .getResourceBytes("/icons/error.png")));
640 } else if (text.startsWith("Warning:")) {
641 jTxtHint.setText(text.substring(8));
642 jTxtHint.setCaretPosition(0);
643 jTxtHint.insertIcon(new ImageIcon(resHelp
644 .getResourceBytes("/icons/warning.png")));
645 } else if (text.startsWith("Info:")) {
646 jTxtHint.setText(text.substring(5));
647 jTxtHint.setCaretPosition(0);
648 jTxtHint.insertIcon(new ImageIcon(resHelp
649 .getResourceBytes("/icons/info.png")));
650 } else {
651 jTxtHint.setText(text);
652 }
653 }
654
655 private void getSelectedCategories(DefaultMutableTreeNode node) {
656 CheckBoxCategory cc = (CheckBoxCategory) node.getUserObject();
657 if (cc.isSelected()) {
658 questionCategoryIDs.add(cc.getCategoryID());
659 }
660 for (int i = 0; i < node.getChildCount(); i++) {
661 getSelectedCategories((DefaultMutableTreeNode) node.getChildAt(i));
662 }
663 }
664
665 private void getQuestionTypes() {
666 try {
667 if (jChbTrueFalse.isSelected()) {
668 questionTypeIDs.add(qtDao.getQuestionTypeByName(jChbTrueFalse
669 .getText()));
670 }
671 if (jChbSingle.isSelected()) {
672 questionTypeIDs.add(qtDao.getQuestionTypeByName(jChbSingle
673 .getText()));
674 }
675 if (jChbMulti.isSelected()) {
676 questionTypeIDs.add(qtDao.getQuestionTypeByName(jChbMulti
677 .getText()));
678 }
679 if (jChbEssay.isSelected()) {
680 questionTypeIDs.add(qtDao.getQuestionTypeByName(jChbEssay
681 .getText()));
682 }
683 if (jChbFillIn.isSelected()) {
684 questionTypeIDs.add(qtDao.getQuestionTypeByName(jChbFillIn
685 .getText()));
686 }
687 } catch (SQLException sqle) {
688 hint("Error: Chyba databázy: " + sqle.getMessage());
689 }
690
691 }
692
693 private void getSelectedKeywords() {
694 Keyword k;
695 for (int i = 0; i < keywords.getSize(); i++) {
696 k = (Keyword) keywords.get(i);
697 keywordIDs.add(k.getKeywordID());
698 }
699 }
700
701 private void jBtnOKActionPerformed(ActionEvent evt) {
702 retVal = javax.swing.JOptionPane.OK_OPTION;
703 questionCategoryIDs = new LinkedList<Integer>();
704 keywordIDs = new ArrayList<Integer>(keywords.size());
705 questionTypeIDs = new LinkedList<Integer>();
706
707
708 for (int i = 0; i < rootNode.getChildCount(); i++) {
709 getSelectedCategories((DefaultMutableTreeNode) rootNode
710 .getChildAt(i));
711 }
712 if (questionCategoryIDs.size() == 0) {
713 hint("Warning: Musí byť vybraná aspoň jedna kategória!");
714 return;
715 }
716
717 getSelectedKeywords();
718
719 getQuestionTypes();
720
721 try {
722 questionCount = new Integer(jTfdQuestionCount.getText());
723 } catch (NumberFormatException e1) {
724 hint("Warning: Počet otázok musí byť celé číslo!");
725 return;
726 }
727 if (questionCount <= 0) {
728 hint("Warning: Počet otazok v teste musi byť väčší ako 0!");
729 return;
730 }
731
732 try {
733 pointsSummDesired = new Integer(jTfdPoints.getText());
734 } catch (NumberFormatException e1) {
735 hint("Warning: Počet bodov musí byť celé číslo!");
736 return;
737 }
738 if (pointsSummDesired < 0) {
739 hint("Warning: Počet bodov za test musi byť väčší rovné nule!");
740 return;
741 }
742
743 try {
744 if (jRadioButtonComplexityCount.isSelected()) {
745 int sum = 0;
746 int count = 0;
747 difficultiesCount = new ArrayList<Integer>(5);
748
749 count = ((SpinnerNumberModel) jSpinner1.getModel()).getNumber()
750 .intValue();
751 difficultiesCount.add(0, count);
752 sum += count;
753 count = ((SpinnerNumberModel) jSpinner2.getModel()).getNumber()
754 .intValue();
755 difficultiesCount.add(1, count);
756 sum += count;
757 count = ((SpinnerNumberModel) jSpinner3.getModel()).getNumber()
758 .intValue();
759 difficultiesCount.add(2, count);
760 sum += count;
761 count = ((SpinnerNumberModel) jSpinner4.getModel()).getNumber()
762 .intValue();
763 difficultiesCount.add(3, count);
764 sum += count;
765 count = ((SpinnerNumberModel) jSpinner5.getModel()).getNumber()
766 .intValue();
767 difficultiesCount.add(4, count);
768 sum += count;
769
770 if (sum > questionCount) {
771 hint("Warning: Počet otázok je menší ako súčet počtov jednotlivých obtiažností");
772 return;
773 }
774
775 TestGenerator tg = new TestGenerator(connection,
776 new ArrayList<Integer>(questionCategoryIDs),
777 keywordIDs, new ArrayList<Integer>(questionTypeIDs),
778 questionCount, pointsSummDesired, jChbPicturesToo
779 .isSelected(), difficultiesCount);
780
781 tg.assembleTest();
782 generatedTest = tg.getGeneratedTest();
783 } else {
784 TestGenerator tg = new TestGenerator(connection,
785 new ArrayList<Integer>(questionCategoryIDs),
786 keywordIDs, new ArrayList<Integer>(questionTypeIDs),
787 jCbxDiff.getSelectedIndex(), questionCount,
788 pointsSummDesired, jChbPicturesToo.isSelected());
789
790 tg.generateTest();
791 generatedTest = tg.getGeneratedTest();
792 }
793 } catch (NotEnoughQuestionsException e) {
794 hint("Error: " + e.getMessage());
795 return;
796 } catch (SQLException e) {
797 hint("Error: Chyba v databáze: " + e.getMessage());
798 return;
799 }
800
801 setVisible(false);
802 }
803
804 private void jBtnCancelActionPerformed(ActionEvent evt) {
805 retVal = javax.swing.JOptionPane.CANCEL_OPTION;
806
807 setVisible(false);
808 }
809
810 public int showDialog() {
811 setModalityType(ModalityType.APPLICATION_MODAL);
812
813 setVisible(true);
814
815 return retVal;
816 }
817
818 public void setQuestionCount(int count) {
819 System.out.println("" + count);
820 jLblQCount.setText("" + count);
821 }
822
823 private boolean hasKeyword(Keyword k) {
824 for (int i = 0; i < keywords.size(); i++)
825 if (k.getText().equals(((Keyword) (keywords.get(i))).getText()))
826 return true;
827
828 return false;
829 }
830
831 private void jBtnNewKeywordActionPerformed(ActionEvent evt) {
832 boolean hasDuplicity = false;
833 NewKeywordDialog nkd = new NewKeywordDialog(this, connection);
834 nkd.deactivateNewKeywordAdding();
835 nkd.setLocationRelativeTo(null);
836 Keyword[] k = nkd.showDialog();
837 if (k != null) {
838 for (int i = 0; i < k.length; i++)
839 if (hasKeyword(k[i]) == false)
840 keywords.addElement(k[i]);
841 else {
842 hasDuplicity = true;
843 }
844 }
845 if (hasDuplicity)
846 hint("Info: Niektoré kľúčové slová neboli pridané, lebo sa v zozname už nachádzajú.");
847 else
848 hint("Info: Všetky vybrané kľúčové slová boli úspešne pridané.");
849 }
850
851 private void jBtnDeleteKeywordActionPerformed(ActionEvent evt) {
852 int sel = jLstKeywords.getSelectedIndex();
853
854 if (sel == -1) {
855 hint("Warning: Najprv musíte vybrať kľúčové slová, ktoré chcete odstrániť.");
856 return;
857 }
858
859 while (sel != -1) {
860 keywords.remove(sel);
861 sel = jLstKeywords.getSelectedIndex();
862 }
863 }
864
865 private int getQuestionCount(DefaultMutableTreeNode from) {
866 int pocet = 0;
867
868 CheckBoxCategory cc = (CheckBoxCategory) from.getUserObject();
869 try {
870 if (cc.isSelected())
871 pocet += qDao.getQuestionCount(cc.getCategoryID());
872 int count = from.getChildCount();
873
874 for (int i = 0; i < count; i++)
875
876 pocet += getQuestionCount((DefaultMutableTreeNode) from
877 .getChildAt(i));
878 } catch (SQLException e) {
879 hint("Error: Zobrazený súčet otázok v kategóriách zrejme nie je správny: "
880 + e.getMessage());
881 }
882
883 return pocet;
884 }
885
886 private void setSubCategoriesSelected(DefaultMutableTreeNode from) {
887 CheckBoxCategory sc = (CheckBoxCategory) from.getUserObject();
888 sc.setSelected(true);
889 int cc = from.getChildCount();
890
891 for (int i = 0; i < cc; i++)
892 setSubCategoriesSelected((DefaultMutableTreeNode) from
893 .getChildAt(i));
894 }
895
896 private void jTreeCategoriesMouseClicked(MouseEvent evt) {
897 if (evt.getClickCount() == 2)
898 return;
899
900 TreePath tp = jTreeCategories
901 .getPathForLocation(evt.getX(), evt.getY());
902 if (tp == null)
903 return;
904 DefaultMutableTreeNode sel = (DefaultMutableTreeNode) tp
905 .getLastPathComponent();
906
907 CheckBoxCategory cc = (CheckBoxCategory) sel.getUserObject();
908 cc.setSelected(!cc.isSelected());
909 if (cc.isSelected())
910 setSubCategoriesSelected(sel);
911
912 setQuestionCount(getQuestionCount(rootNode));
913
914 jTreeCategories.updateUI();
915 }
916
917 private ButtonGroup getButtonGroup1() {
918 if (buttonGroup1 == null) {
919 buttonGroup1 = new ButtonGroup();
920 }
921 return buttonGroup1;
922 }
923
924 private void disableProgressBar() {
925 jCbxDiff.setSelectedIndex(0);
926 jPbrDiff.setIntegerValue(0);
927
928
929
930
931
932 }
933
934 private void enableProgressBar() {
935
936
937
938 }
939
940 private void jRadioButton1ItemStateChanged(ItemEvent evt) {
941 if (evt.getStateChange() == ItemEvent.SELECTED) {
942 jCbxDiff.setEnabled(true);
943 enableProgressBar();
944 } else {
945 jCbxDiff.setEnabled(false);
946 disableProgressBar();
947 }
948 }
949
950 private void jRadioButton2ItemStateChanged(ItemEvent evt) {
951 if (evt.getStateChange() == ItemEvent.SELECTED) {
952 jSpinner1.setEnabled(true);
953 jSpinner2.setEnabled(true);
954 jSpinner3.setEnabled(true);
955 jSpinner4.setEnabled(true);
956 jSpinner5.setEnabled(true);
957 } else {
958 jSpinner1.setEnabled(false);
959 jSpinner2.setEnabled(false);
960 jSpinner3.setEnabled(false);
961 jSpinner4.setEnabled(false);
962 jSpinner5.setEnabled(false);
963 }
964 }
965
966 private void jSpinnerStateChanged(ChangeEvent evt) {
967 int sum = 0;
968
969 sum += ((SpinnerNumberModel) jSpinner1.getModel()).getNumber()
970 .intValue();
971 sum += ((SpinnerNumberModel) jSpinner2.getModel()).getNumber()
972 .intValue();
973 sum += ((SpinnerNumberModel) jSpinner3.getModel()).getNumber()
974 .intValue();
975 sum += ((SpinnerNumberModel) jSpinner4.getModel()).getNumber()
976 .intValue();
977 sum += ((SpinnerNumberModel) jSpinner5.getModel()).getNumber()
978 .intValue();
979
980 if (sum > Integer.parseInt(jTfdQuestionCount.getText())) {
981 jTfdQuestionCount.setText(String.valueOf(sum));
982 }
983 }
984
985 private JLabel getJLabel12() {
986 if (jLabel12 == null) {
987 jLabel12 = new JLabel();
988 jLabel12.setText("Kategórie");
989 jLabel12.setBounds(12, 86, 72, 14);
990 jLabel12.setForeground(new java.awt.Color(100, 0, 0));
991 }
992 return jLabel12;
993 }
994 }