View Javadoc

1   /**
2    * 
3    */
4   package sk.stuba.fiit.foo07.genex.gui;
5   
6   import java.sql.Connection;
7   import java.sql.SQLException;
8   import java.util.ArrayList;
9   
10  import javax.swing.table.DefaultTableModel;
11  
12  import sk.stuba.fiit.foo07.genex.beans.Question;
13  import sk.stuba.fiit.foo07.genex.beans.QuestionPoints;
14  import sk.stuba.fiit.foo07.genex.beans.Test;
15  import sk.stuba.fiit.foo07.genex.dao.QuestionDao;
16  import sk.stuba.fiit.foo07.genex.dao.QuestionDaoDerby;
17  import sk.stuba.fiit.foo07.genex.dao.QuestionTypeDao;
18  import sk.stuba.fiit.foo07.genex.dao.QuestionTypeDaoDerby;
19  
20  /**
21   * @author _mizu_
22   * 
23   */
24  public class NewTestTableModel extends DefaultTableModel {
25  
26      /**
27       * 
28       */
29      private static final long serialVersionUID = -3895037570823789366L;
30  
31      private int columnCount = 5;
32      private String[] columnNames = null;
33      private ArrayList<Question> questions;
34      private ArrayList<Float> points;
35      private QuestionTypeDao qtDao;
36      private QuestionDao qDao;
37  
38      public NewTestTableModel(Connection c) {
39          questions = new ArrayList<Question>();
40          points = new ArrayList<Float>();
41          qtDao = new QuestionTypeDaoDerby(c);
42          qDao = new QuestionDaoDerby(c);
43          initColumnNames();
44      }
45  
46      public NewTestTableModel(Connection c, Test test) throws SQLException {
47          qtDao = new QuestionTypeDaoDerby(c);
48          qDao = new QuestionDaoDerby(c);
49          initColumnNames();
50  
51          ArrayList<Question> qList = new ArrayList<Question>();
52          ArrayList<Float> pList = new ArrayList<Float>();
53          ArrayList<QuestionPoints> qpList = test.getQuestionPoints();
54  
55          int i = 0;
56          for (QuestionPoints qp : qpList) {
57              qList.add(i, qDao.getQuestionByID(qp.getQuestionId()));
58              pList.add(i, qp.getPoints());
59              i++;
60          }
61  
62          questions = qList;
63          points = pList;
64  
65      }
66  
67      public void setQuestions(ArrayList<Question> questions,
68              ArrayList<Float> points) throws Exception {
69          if (points != null && questions.size() != points.size())
70              throw new Exception("Length of questions and points must equals!");
71          this.questions = questions;
72          if (points == null) {
73              points = new ArrayList<Float>(questions.size());
74              for (Question q : questions)
75                  this.points.add((float) q.getDifficulty());
76          } else
77              this.points = points;
78          fireTableDataChanged();
79      }
80  
81      public void addQuestion(Question toAdd, Float points) {
82          questions.add(toAdd);
83          this.points.add(points);
84          fireTableDataChanged();
85      }
86  
87      public void addQuestions(ArrayList<Question> toAdd, ArrayList<Float> points)
88              throws Exception {
89          if (questions.size() != points.size())
90              throw new Exception("Length of questions and points must equals!");
91          questions.addAll(toAdd);
92          this.points.addAll(points);
93          fireTableDataChanged();
94      }
95  
96      public void removeQuestions(int[] indices) {
97          if (indices.length == 0)
98              return;
99  
100         for (int i = 0; i < indices.length; i++) {
101             questions.remove(indices[i] - i);
102             points.remove(indices[i] - i);
103         }
104         fireTableDataChanged();
105     }
106 
107     public void setQuestion(Question toSet, int index) {
108         questions.set(index, toSet);
109         fireTableDataChanged();
110     }
111 
112     public Question getQuestion(int index) {
113         return questions.get(index);
114     }
115 
116     private void exchange(int index1, int index2) {
117         Question temp = questions.get(index1);
118         questions.set(index1, questions.get(index2));
119         questions.set(index2, temp);
120         Float temp2 = points.get(index1);
121         points.set(index1, points.get(index2));
122         points.set(index2, temp2);
123     }
124 
125     public void reorder() {
126         int[] ind = new int[getQuestionCount()];
127         for (int i = 0; i < ind.length; i++)
128             ind[i] = i;
129         for (int i = 0; i < ind.length - 1; i++) {
130             int index = 1 + i + (int) (Math.random() * (ind.length - i - 1));
131             exchange(i, index);
132         }
133         fireTableDataChanged();
134     }
135 
136     public void moveUp(int index) {
137         if (index == 0)
138             return;
139         exchange(index - 1, index);
140         fireTableDataChanged();
141     }
142 
143     public void moveDown(int index) {
144         if (index == questions.size() - 1)
145             return;
146         exchange(index, index + 1);
147         fireTableDataChanged();
148     }
149 
150     public void initColumnNames() {
151         columnNames = new String[columnCount];
152 
153         columnNames[0] = new String("#"); // poradove cislo otazky
154         columnNames[1] = new String("Otázka");
155         columnNames[2] = new String("Typ");
156         columnNames[3] = new String("Náročnosť");
157         columnNames[4] = new String("Počet bodov");
158     }
159 
160     @Override
161     public int getColumnCount() {
162         return columnNames.length;
163     }
164 
165     @Override
166     public int getRowCount() {
167         return questions == null ? 0 : questions.size();
168     }
169 
170     @Override
171     public String getColumnName(int col) {
172         return columnNames[col];
173     }
174 
175     @Override
176     public void setValueAt(Object val, int row, int col) {
177         if (col != 4)
178             return;
179 
180         try {
181             points.set(row, new Float((String) val));
182         } catch (NumberFormatException e) {
183         }
184         fireTableCellUpdated(row, col);
185     }
186 
187     @Override
188     public Object getValueAt(int row, int col) {
189         switch (col) {
190         // poradove cislo
191         case 0:
192             return row + 1;
193 
194             // text otazky
195         case 1:
196             return questions.get(row);
197 
198             // typ
199         case 2:
200             //TODO
201             //pozor vynimky
202             String qt = null;
203             try {
204                 qt = qtDao.getQuestionTypeByQuestionID(
205                         questions.get(row).getQuestionID()).getName();
206             } catch (Exception e) {
207             }
208             if ("Single choice".equals(qt))
209                 return "S";
210             else if ("Multi choice".equals(qt))
211                 return "M";
212             else if ("True/False".equals(qt))
213                 return "TF";
214             else if ("Fill in".equals(qt))
215                 return "F";
216             else if ("Essay".equals(qt))
217                 return "E";
218             else
219                 return "Un";
220 
221             // narocnost
222         case 3:
223             return questions.get(row).getDifficulty();
224 
225             // pocet bodov
226         case 4:
227             return points.get(row);
228         }
229 
230         return "undefined";
231     }
232 
233     @Override
234     public boolean isCellEditable(int row, int col) {
235         if (col == 4)
236             return true;
237 
238         return false;
239     }
240 
241     public int getQuestionCount() {
242         return questions.size();
243     }
244 
245     public float getPointsSum() {
246         float sum = 0;
247         for (Float i : points)
248             sum += i;
249 
250         return sum;
251     }
252 
253     public float getAverageDiff() {
254         int diffSum = 0;
255         for (Question q : questions)
256             diffSum += q.getDifficulty();
257 
258         return (float) diffSum / questions.size();
259     }
260 
261     public ArrayList<Question> getQuestions() {
262         return questions;
263     }
264 
265     public ArrayList<Float> getPoints() {
266         return points;
267     }
268 }