001 /* BEE - Book Editing Environment for PAVLOV
002 * Copyright (C) 2004 T.J. Willis
003 *
004 * This program is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU General Public License
006 * as published by the Free Software Foundation; either version 2
007 * of the License, or (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, write to the Free Software
016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017 *
018 * $Header: /cvsroot/pavlov/net/sourceforge/bee/ChapterTableModel.java,v 1.8 2004/06/24 07:56:15 tj_willis Exp $
019 */
020 package net.sourceforge.bee;
021
022 import javax.swing.table.*;
023 import java.util.*;
024 import java.lang.reflect.Array;
025
026 import net.sourceforge.pavlov.library.*;
027
028 /**
029 * A TableModel for displaying all the questions in a chapter.
030 *
031 * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
032 * @version $Id: ChapterTableModel.java,v 1.8 2004/06/24 07:56:15 tj_willis Exp $
033 */
034 public class ChapterTableModel
035 extends AbstractTableModel
036 {
037 private Question qs[];
038
039 // FIXME: what's all this package-private crap?
040 void sortByID()
041 {
042 Arrays.sort(qs,new QuestionIDComparator());
043 fireTableDataChanged();
044 }
045
046 void sortByText()
047 {
048 Arrays.sort(qs,new QuestionTextComparator());
049 fireTableDataChanged();
050 }
051 void sortByRightAnswer()
052 {
053 Arrays.sort(qs,new QuestionRightAnswerComparator());
054 fireTableDataChanged();
055 }
056
057 Question getQuestion(int row)
058 {
059 try {
060 return qs[row];
061 } catch (Exception e) {
062
063 }
064 return null;
065 }
066
067 void setQuestion(int row, Question q)
068 {
069 try {
070 qs[row] = q;
071 fireTableDataChanged();
072 } catch (Exception e) {
073
074 }
075 }
076
077
078 java.util.List<Question> getQuestions()
079 {
080 return (List<Question>)Arrays.asList(qs);
081 }
082
083 void setQuestions(java.util.List<Question> l)
084 {
085 Vector<Question> v = new Vector<Question>(l);
086 v.trimToSize();
087 qs = null;
088 qs = new Question[l.size()];
089 qs = (Question [])v.toArray(qs);
090 //fireTableRowsInserted(row,row);
091 fireTableDataChanged();
092 }
093
094 void insertQuestion(int row, Question q)
095 {
096 try {
097 java.util.List<Question> l = Arrays.asList(qs);
098 Vector<Question> v = new Vector<Question>(l);
099 //System.out.println("rc is : " + getRowCount());
100 v.insertElementAt(q,row);
101 v.trimToSize();
102 qs = (Question [])v.toArray(qs);
103 //System.out.println("rc is : " + getRowCount());
104 fireTableRowsInserted(row,row);
105 fireTableDataChanged();
106 } catch (Exception e) {
107 System.out.println(e);
108 }
109 }
110
111
112 // had a nicer implementation, but it didn't work
113 void deleteQuestions(int indices[])
114 {
115 //System.out.println("RC is " + getRowCount());
116 if(indices==null) return;
117 int x = Array.getLength(indices);
118 int y = Array.getLength(qs);
119 if(x>y) return;
120 ArrayList<Question> al = new ArrayList<Question>(y-x);
121 for(int i=0;i<y;i++){
122 if( qs[i]==null) continue;
123 boolean found=false;
124 for(int j=0;j<x;j++){
125 if(i==indices[j]){
126 found = true;
127 break;
128 }
129 }
130 if(!found) al.add(qs[i]);
131 }
132 qs = null;
133 qs = new Question[y-x];
134 qs = (Question [])(al.toArray(qs));
135 for(int j=0;j<x;j++)
136 fireTableRowsDeleted(indices[j],indices[j]);
137 fireTableStructureChanged();
138 fireTableDataChanged();
139 }
140
141 private Vector getQuestions(int indices[])
142 {
143 Vector<Question> v = new Vector<Question>();
144 int x = java.lang.reflect.Array.getLength(indices);
145 for(int i=0;i<x;i++)
146 {
147 v.add(getQuestion(indices[i]));
148 }
149
150 return v;
151 }
152
153 void deleteQuestion(int row)
154 {
155 int foo[] = new int[1];
156 foo[0] = row;
157 deleteQuestions(foo);
158 }
159
160
161 /**
162 * Creates a new <code>ChapterTableModel</code> instance.
163 *
164 * @param arr[] a <code>Question</code> value
165 */
166 public ChapterTableModel(Question arr[])
167 {
168 qs=arr;
169 }
170
171 /**
172 * Describe <code>getColumnClass</code> method here.
173 *
174 * @param i an <code>int</code> value
175 * @return a <code>Class</code> value
176 */
177 public Class getColumnClass(int i) {
178 String s = new String();
179 return s.getClass();
180 }
181
182 boolean validEntry(int row, int col)
183 {
184 String s = (String)getValueAt(row,col);
185 if(s==null) return false;
186 if(s.indexOf('<')>0) return false;
187 if(s.indexOf('&')>0) return false;
188 //if(s.indexOf('/')>0) return false;
189 //if(s.indexOf('\"')>0) return false;
190
191
192 // KISS Principle
193 // make sure id isn't used anywhere else
194 if(col==0){
195 int x= java.lang.reflect.Array.getLength(qs);
196 for(int i=0;i<x;i++){
197 if(i==row) continue;
198 if(s.equals(getValueAt(i,0)))
199 return false;
200 }
201 }
202 return true;
203 }
204
205 /**
206 * Returns number of columns for table.
207 *
208 * @return an <code>int</code> value
209 */
210 public int getColumnCount() { return 7; }
211 /**
212 * Returns number of questions in chapter.
213 *
214 * @return an <code>int</code> value
215 */
216 public int getRowCount() { return Array.getLength(qs);}
217 /**
218 * Implemented to always return true.
219 *
220 * @param rowIndex an <code>int</code> value
221 * @param columnIndex an <code>int</code> value
222 * @return a <code>boolean</code> value
223 */
224 public boolean isCellEditable(int rowIndex, int columnIndex) { return true;}
225
226 /**
227 * Picks the appropriate value out of the list of questions to
228 * display in the JTable.
229 *
230 * @param row an <code>int</code> value
231 * @param col an <code>int</code> value
232 * @return an <code>Object</code> value
233 */
234 public Object getValueAt(int row, int col)
235 {
236 try {
237 Question q = qs[row];
238 if(col == 0)
239 return q.getID();
240 else if (col == 1)
241 return q.getText();
242 else if (col == 2)
243 return q.getRightAnswer();
244 else if (col == 3)
245 {
246 if(q.getWrongAnswer(0)==null)
247 q.setWrongAnswer("a",0);
248 return q.getWrongAnswer(0);
249 }
250 else if (col == 4)
251 {
252 if(q.getWrongAnswer(1)==null)
253 q.setWrongAnswer(" ",1);
254 return q.getWrongAnswer(1);
255 }
256 else if (col == 5)
257 {
258 if(q.getWrongAnswer(2)==null)
259 q.setWrongAnswer(" ",2);
260 return q.getWrongAnswer(2);
261 }
262 else if (col == 6)
263 {
264 return q.getImageFile();
265 }
266 } catch (Exception e) {
267 System.out.println(e);
268 }
269 return null;
270 }
271
272
273 /**
274 * Sets the appropriate value in the question corresponding to the edited
275 * row.
276 *
277 * @param val an <code>Object</code> value
278 * @param row an <code>int</code> value
279 * @param col an <code>int</code> value
280 */
281 public void setValueAt(Object val, int row, int col)
282 {
283 try {
284 String s = val.toString();
285 Question q = qs[row];
286 if(col == 0)
287 q.setId(s);
288 else if (col == 1)
289 q.setText(s);
290 else if (col == 2)
291 q.setRightAnswer(s);
292 else if (col == 3)
293 q.setWrongAnswer(s,0);
294 else if (col == 4)
295 q.setWrongAnswer(s,1);
296 else if (col == 5)
297 q.setWrongAnswer(s,2);
298 else if (col == 6)
299 q.setImageFile(s);
300 } catch (Exception e) {
301 System.out.println(e);
302 }
303 }
304
305
306 /**
307 * Returns user-readable column names for JTable.
308 *
309 * @param i an <code>int</code> value
310 * @return a <code>String</code> value
311 */
312 public String getColumnName(int i)
313 {
314 if(i==0)
315 return "ID";
316 else if (i==1)
317 return "Question Text";
318 else if (i==2)
319 return "Correct Answer";
320 else if (i==3)
321 return "Incorrect Answer";
322 else if (i==4)
323 return "Incorrect Answer";
324 else if (i==5)
325 return "Incorrect Answer";
326 else if (i==6)
327 return "Diagram";
328 return null;
329 }
330
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349