View Javadoc

1   package sk.stuba.fiit.foo07.genex.gui;
2   
3   import java.sql.SQLException;
4   import java.util.ArrayList;
5   
6   import javax.swing.ImageIcon;
7   import javax.swing.table.AbstractTableModel;
8   
9   import sk.stuba.fiit.foo07.genex.beans.Category;
10  import sk.stuba.fiit.foo07.genex.beans.Question;
11  import sk.stuba.fiit.foo07.genex.dao.CategoryDao;
12  import sk.stuba.fiit.foo07.genex.dao.QuestionDao;
13  import sk.stuba.fiit.foo07.genex.dao.QuestionTypeDao;
14  import sk.stuba.fiit.foo07.genex.exceptions.QuestionInCategoryException;
15  
16  public class GenexTableModel extends AbstractTableModel {
17  
18      /**
19       * 
20       */
21      private static final long serialVersionUID = -5823071225893042256L;
22  
23      private int columnCount = 5;
24      private String[] columnNames = null;
25  
26      private Category actualCat;
27      private ImageIcon catIcon;
28      private ImageIcon qIcon;
29      private CategoryDao catDao;
30      private QuestionDao qDao;
31      private QuestionTypeDao qtDao;
32      ArrayList<Category> categories;
33      ArrayList<Question> questions;
34      private ArrayList<Object> copiedObjects;
35      private Category cutFromCategory;
36  
37      public GenexTableModel(ImageIcon catIcon, ImageIcon qIcon,
38              CategoryDao catDao, QuestionDao qDao, QuestionTypeDao qtDao) {
39          this.catIcon = catIcon;
40          this.qIcon = qIcon;
41          this.catDao = catDao;
42          this.qDao = qDao;
43          this.qtDao = qtDao;
44          //TODO 
45          //osetrit
46          try {
47              this.actualCat = catDao.getRootCategories().get(0);
48          } catch (Exception e) {
49          }
50          actualizeContent();
51  
52          initColumnNames();
53      }
54  
55      @Override
56      public void fireTableDataChanged() {
57          actualizeContent();
58          super.fireTableDataChanged();
59      }
60  
61      public void actualizeContent() {
62          try {
63              categories = catDao.getCategoriesByParentID(actualCat
64                      .getCategoryID());
65  
66              questions = qDao
67                      .getQuestionsByCategoryID(actualCat.getCategoryID());
68  
69          } catch (Exception e) {
70          }
71      }
72  
73      public void setActualCategory(Category newCat) {
74          if (newCat == null)
75              return;
76          actualCat = newCat;
77          this.fireTableDataChanged();
78      }
79  
80      public Category getActualCategory() {
81          return actualCat;
82      }
83  
84      public void initColumnNames() {
85          columnNames = new String[columnCount];
86  
87          columnNames[0] = new String("");
88          columnNames[1] = new String("Otázka");
89          columnNames[2] = new String("Typ");
90          columnNames[3] = new String("Náročnosť");
91          columnNames[4] = new String("Posledná úprava");
92      }
93  
94      public int getColumnCount() {
95          return columnNames.length;
96      }
97  
98      public int getRowCount() {
99          return categories.size() + questions.size();
100     }
101 
102     @Override
103     public String getColumnName(int col) {
104         return columnNames[col];
105     }
106 
107     public Object getValueAt(int row, int col) {
108         switch (col) {
109         // ikona
110         case 0:
111             return row < categories.size() ? catIcon : qIcon;
112 
113             // text otazky || nazov kategorie
114         case 1:
115             return row < categories.size() ? categories.get(row) : questions
116                     .get(row - categories.size());
117 
118             // typ
119         case 2:
120             if (row < categories.size()) {
121                 return "DIR";
122             }
123             //TODO
124             //pozor vynimky
125             String qt = null;
126             try {
127                 qt = qtDao.getQuestionTypeByQuestionID(
128                         questions.get(row - categories.size()).getQuestionID())
129                         .getName();
130 
131             } catch (Exception e) {
132             }
133             if ("Single choice".equalsIgnoreCase(qt))
134                 return "S";
135             else if ("Multi choice".equalsIgnoreCase(qt))
136                 return "M";
137             else if ("True/False".equalsIgnoreCase(qt))
138                 return "TF";
139             else if ("Fill in".equalsIgnoreCase(qt))
140                 return "F";
141             else if ("Essay".equalsIgnoreCase(qt))
142                 return "E";
143             else
144                 return "Un";
145 
146             // narocnost
147         case 3:
148             return row < categories.size() ? new Integer(-1) : questions.get(
149                     row - categories.size()).getDifficulty();
150 
151             // last update
152         case 4:
153             return row < categories.size() ? categories.get(row)
154                     .getLastUpdate() : questions.get(row - categories.size())
155                     .getLastUpdate();
156         }
157 
158         return "undefined";
159     }
160 
161     @Override
162     public boolean isCellEditable(int row, int col) {
163         return false;
164     }
165 
166     public Object getValue(int row) {
167         if (row < categories.size())
168             return categories.get(row);
169         else
170             return questions.get(row - categories.size());
171     }
172 
173     public void deleteItems(int[] selIndices) {
174         for (int i = 0; i < selIndices.length; i++) {
175             Object item = getValue(selIndices[i]);
176             try {
177                 if (item instanceof Category) {
178                     Category c = (Category) item;
179                     if (c.getName().equals("Kôš")
180                             || c.getName().equals("Konflikty"))
181                         continue;
182                     catDao.deleteCategory((Category) item);
183                 } else {
184                     qDao.deleteQuestionFromCategory((Question) item, actualCat);
185                 }
186             } catch (QuestionInCategoryException e) {
187                 // TODO notify user that category is not empty
188                 deleteCategory((Category) item);
189             } catch (SQLException e) {
190                 e.printStackTrace();
191             }
192             //TODO there should be other exceptions (PictureInPictureQuestionException)
193         }
194 
195         fireTableDataChanged();
196     }
197 
198     private void deleteCategory(Category c) {
199         try {
200             if (c.getName().equals("Kôš") || c.getName().equals("Konflikty"))
201                 return;
202             try {
203                 ArrayList<Category> children = catDao.getCategoriesByParentID(c
204                         .getCategoryID());
205                 for (Category cat : children) {
206                     deleteCategory(cat);
207                 }
208 
209                 catDao.deleteCategory(c);
210             } catch (QuestionInCategoryException e) {
211                 ArrayList<Question> qs = qDao.getQuestionsByCategoryID(c
212                         .getCategoryID());
213                 for (Question q : qs) {
214                     qDao.deleteQuestionFromCategory(q, c);
215                 }
216                 try {
217                     catDao.deleteCategory(c);
218                 } catch (QuestionInCategoryException ex) {
219                     e.printStackTrace();
220                     // TODO WTF? 
221                 }
222             }
223             categories.remove(c);
224         } catch (Exception e) {
225             e.printStackTrace();
226         }
227     }
228 
229     public void copyData(int[] selIndices) {
230         copiedObjects = new ArrayList<Object>(selIndices.length);
231         for (int i = 0; i < selIndices.length; i++) {
232             copiedObjects.add(getValue(selIndices[i]));
233         }
234         cutFromCategory = null;
235     }
236 
237     public void cutData(int[] selIndices) {
238         copyData(selIndices);
239         cutFromCategory = actualCat;
240     }
241 
242     public void pasteData() throws SQLException {
243         if (copiedObjects == null)
244             return;
245         for (Object o : copiedObjects) {
246             if (o instanceof Question) {
247                 Question q = (Question) o;
248                 qDao.copyQuestion(q.getQuestionID(), actualCat.getCategoryID());
249                 if (cutFromCategory != null) {
250                     qDao.deleteQuestionFromCategory(q, cutFromCategory);
251                 }
252             } else {
253                 Category c = (Category) o;
254                 Integer newID = catDao.copyCategory(c.getCategoryID(),
255                         actualCat);
256                 if (cutFromCategory != null) {
257                     deleteCategory(c);
258                 }
259                 c.setCategoryID(newID);
260             }
261         }
262         if (cutFromCategory != null) {
263             fireTableDataChanged();
264         }
265     }
266 }