View Javadoc

1   package sk.stuba.fiit.foo07.genex.gui;
2   
3   import java.sql.Connection;
4   import java.util.ArrayList;
5   
6   import javax.swing.table.AbstractTableModel;
7   
8   import sk.stuba.fiit.foo07.genex.beans.Answer;
9   import sk.stuba.fiit.foo07.genex.beans.Question;
10  import sk.stuba.fiit.foo07.genex.dao.AnswerDao;
11  import sk.stuba.fiit.foo07.genex.dao.AnswerDaoDerby;
12  
13  public class CheckBoxTableModel extends AbstractTableModel {
14  
15      /**
16       * 
17       */
18      private static final long serialVersionUID = -7402691643396712897L;
19  
20      private String[] columnNames = new String[2];
21      private boolean singleSelect = true;
22      private AnswerDao aDao;
23      private ArrayList<Answer> answers;
24  
25      public CheckBoxTableModel(String t, String b) {
26          setColumnNames(t, b);
27          answers = new ArrayList<Answer>();
28      }
29  
30      public CheckBoxTableModel(String t, String b, boolean singleSelect) {
31          this(t, b);
32          this.singleSelect = singleSelect;
33      }
34  
35      @Override
36      public int getColumnCount() {
37          return columnNames.length;
38      }
39  
40      @Override
41      public int getRowCount() {
42          return answers.size();
43      }
44  
45      @Override
46      public Object getValueAt(int rowIndex, int columnIndex) {
47          Answer a = answers.get(rowIndex);
48  
49          return columnIndex == 0 ? a.getText() : a.getIsCorrect();
50      }
51  
52      @Override
53      public void setValueAt(Object value, int r, int c) {
54          Answer a = answers.get(r);
55  
56          if (c == 0) {
57              a.setText((String) value);
58          } else {
59              if (singleSelect && ((Boolean) value).booleanValue() == false)
60                  return;
61  
62              if (singleSelect) {
63                  for (Answer tmp : answers)
64                      tmp.setIsCorrect(false);
65              }
66              a.setIsCorrect((Boolean) value);
67          }
68  
69          fireTableDataChanged();
70      }
71  
72      @Override
73      public Class getColumnClass(int columnIndex) {
74          return columnIndex == 0 ? String.class : Boolean.class;
75      }
76  
77      @Override
78      public boolean isCellEditable(int r, int c) {
79          return true;
80      }
81  
82      @Override
83      public String getColumnName(int c) {
84          return columnNames[c];
85      }
86  
87      public void setColumnNames(String tColumn, String bColumn) {
88          columnNames[0] = new String(tColumn);
89          columnNames[1] = new String(bColumn);
90  
91          this.fireTableStructureChanged();
92      }
93  
94      public void addAnswer(Answer toAdd) {
95          answers.add(toAdd);
96          fireTableDataChanged();
97      }
98  
99      public Answer getAnswer(int r) {
100         return answers.get(r);
101     }
102 
103     public void removeAnswer(int r) {
104         answers.remove(r);
105         fireTableDataChanged();
106     }
107 
108     public ArrayList<Answer> getAnswers() {
109         return answers;
110     }
111 
112     public void setAnswers(ArrayList<Answer> answers) {
113         if (answers == null)
114             this.answers.clear();
115         else
116             this.answers = answers;
117         fireTableDataChanged();
118     }
119 
120     public void setAnswers(Question q, Connection c) {
121         if (q.getQuestionID() == null)
122             answers.clear();
123         else {
124             aDao = new AnswerDaoDerby(c);
125             //TODO 
126             //pridane odchytavanie vynimky (palo)
127             try {
128                 answers = aDao.getAnswersByQuestionID(q.getQuestionID());
129             } catch (Exception e) {
130             }
131         }
132         fireTableDataChanged();
133     }
134 
135     public void setSingleSelect(boolean singleSelect) {
136         // musime upravit data aby bol true iba jeden stlpec
137         if (!this.singleSelect && singleSelect) {
138             boolean first = true;
139             for (Answer bs : answers) {
140                 if (first)
141                     first = false;
142                 else
143                     bs.setIsCorrect(false);
144             }
145         }
146 
147         this.singleSelect = singleSelect;
148 
149     }
150 
151     public boolean isSingleSelect() {
152         return singleSelect;
153     }
154 
155     public void saveAnswers(Question q, Connection c) {
156         // predpoklada sa , ze otazka je v DB
157         //TODO 
158         //pridane odchytavanie vynimky (palo)
159         try {
160             aDao = new AnswerDaoDerby(c);
161             ArrayList<Answer> oldAnswers = aDao.getAnswersByQuestionID(q
162                     .getQuestionID());
163             for (Answer a : oldAnswers)
164                 aDao.deleteAnswer(a);
165 
166             aDao.addAnswersToQuestion(q.getQuestionID(), answers);
167         } catch (Exception e) {
168         }
169     }
170 }