1 15 package org.josql.contrib; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.SortedMap ; 20 21 import javax.swing.table.TableModel ; 22 23 import javax.swing.event.TableModelListener ; 24 import javax.swing.event.TableModelEvent ; 25 26 import org.josql.QueryExecutionException; 27 import org.josql.QueryParseException; 28 import org.josql.Query; 29 import org.josql.QueryResults; 30 31 import org.josql.internal.Utilities; 32 33 import org.josql.expressions.SelectItemExpression; 34 35 60 public class JoSQLSwingTableModel extends Query implements TableModel 61 { 62 63 private QueryResults results = null; 64 private List listeners = new ArrayList (); 65 66 public JoSQLSwingTableModel () 67 { 68 69 70 } 71 72 80 public void parse (String sql) 81 throws QueryParseException 82 { 83 84 this.results = null; 85 86 super.parse (sql); 87 88 if (this.isWantObjects ()) 89 { 90 91 throw new QueryParseException ("Only SQL statements that return columns (not the objects passed in) can be used."); 92 93 } 94 95 this.notifyListeners (new TableModelEvent (this, 96 TableModelEvent.HEADER_ROW)); 97 98 } 99 100 private void notifyListeners (TableModelEvent ev) 101 { 102 103 for (int i = 0; i < this.listeners.size (); i++) 104 { 105 106 TableModelListener l = (TableModelListener ) this.listeners.get (i); 107 108 l.tableChanged (ev); 109 110 } 111 112 } 113 114 125 public QueryResults reorder (List objs, 126 SortedMap dirs) 127 throws QueryExecutionException, 128 QueryParseException 129 { 130 131 this.results = super.reorder (objs, 133 dirs); 134 135 this.notifyListeners (new TableModelEvent (this)); 137 138 return this.results; 139 140 } 141 142 153 public QueryResults reorder (List objs, 154 String orderBys) 155 throws QueryParseException, 156 QueryExecutionException 157 { 158 159 this.results = super.reorder (objs, 160 orderBys); 161 162 this.notifyListeners (new TableModelEvent (this)); 164 165 return this.results; 166 167 } 168 169 179 public QueryResults execute (List l) 180 throws QueryExecutionException 181 { 182 183 this.results = super.execute (l); 184 185 this.notifyListeners (new TableModelEvent (this)); 187 188 return this.results; 189 190 } 191 192 197 public QueryResults getResults () 198 { 199 200 return this.results; 201 202 } 203 204 207 public void clearResults () 208 { 209 210 this.results = null; 211 212 } 213 214 220 public String getColumnName (int ind) 221 { 222 223 List cs = this.getColumns (); 224 225 if ((cs == null) 226 || 227 (ind > (cs.size () - 1)) 228 ) 229 { 230 231 return null; 232 233 } 234 235 SelectItemExpression s = (SelectItemExpression) cs.get (ind); 236 237 String al = s.getAlias (); 238 239 if (al == null) 240 { 241 242 return (ind + 1) + ""; 243 244 } 245 246 return al; 247 248 } 249 250 255 public Class getColumnClass (int i) 256 { 257 258 List cs = this.getColumns (); 259 260 if ((cs == null) 261 || 262 (i > (cs.size () - 1)) 263 ) 264 { 265 266 return null; 267 268 } 269 270 SelectItemExpression s = (SelectItemExpression) cs.get (i); 271 272 try 273 { 274 275 return Utilities.getObjectClass (s.getExpectedReturnType (this)); 276 277 } catch (Exception e) { 278 279 return null; 281 282 } 283 284 } 285 286 293 public Object getValueAt (int r, 294 int c) 295 { 296 297 if ((this.results == null) 298 || 299 (r > (this.results.getResults ().size () - 1)) 300 ) 301 { 302 303 return null; 304 305 } 306 307 Object o = this.results.getResults ().get (r); 308 309 if (o instanceof List ) 310 { 311 312 List l = (List ) o; 313 314 if (c > (l.size () - 1)) 315 { 316 317 return null; 318 319 } 320 321 return l.get (c); 322 323 } 324 325 if (c > 0) 326 { 327 328 return null; 329 330 } 331 332 return o; 333 334 } 335 336 344 public void setValueAt (Object v, 345 int r, 346 int c) 347 throws UnsupportedOperationException 348 { 349 350 throw new UnsupportedOperationException ("This method not supported for: " + 352 this.getClass ().getName ()); 353 354 } 355 356 363 public boolean isCellEditable (int r, 364 int c) 365 { 366 367 return false; 369 370 } 371 372 377 public int getRowCount () 378 { 379 380 if (this.results == null) 381 { 382 383 return 0; 384 385 } 386 387 return this.results.getResults ().size (); 388 389 } 390 391 396 public int getColumnCount () 397 { 398 399 if (this.getColumns () == null) 401 { 402 403 return 0; 404 405 } 406 407 return this.getColumns ().size (); 408 409 } 410 411 public void removeTableModelListener (TableModelListener l) 412 { 413 414 this.listeners.remove (l); 415 416 } 417 418 public void addTableModelListener (TableModelListener l) 419 { 420 421 if (this.listeners.contains (l)) 422 { 423 424 return; 425 426 } 427 428 this.listeners.add (l); 429 430 } 431 432 } 433 | Popular Tags |