Index: topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java diff -u topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java:1.4 topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java:1.5 --- topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java:1.4 Tue Jul 20 16:02:25 2004 +++ topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java Sun Aug 15 13:57:17 2004 @@ -20,11 +20,13 @@ import javax.swing.table.TableColumnModel; import org.codelutin.generator.Util; - -import org.codelutin.topia.TopiaException; -import org.codelutin.topia.TopiaQuery; import org.codelutin.topia.TopiaContext; import org.codelutin.topia.TopiaElement; +import org.codelutin.topia.TopiaEntityEvent; +import org.codelutin.topia.TopiaEntityListener; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.TopiaPersistenceService; +import org.codelutin.topia.TopiaQuery; /** * @author pineau @@ -32,12 +34,13 @@ * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ -public class TopiaTableModel extends AbstractTableModel implements TopiaElement { +public class TopiaTableModel extends AbstractTableModel implements TopiaElement, TopiaEntityListener { protected static String ASCENDING="ascending"; protected static String DESCENDING="descending"; protected TopiaContext context = null; + protected TopiaPersistenceService persistenceService = null; protected TopiaQuery query = null; protected String[] fields = null; protected String[] columnNames = null; @@ -47,33 +50,40 @@ public TopiaTableModel(){ } - public TopiaTableModel(TopiaContext context, TopiaQuery query){ - this(context, query, null, null, null); + public TopiaTableModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query) throws TopiaException { + this(context, persistenceService, query, null, null, null); } - public TopiaTableModel(TopiaContext context, TopiaQuery query, String[] fields) { - this(context, query, fields, fields, new String[fields.length]); + public TopiaTableModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query, String[] fields) throws TopiaException { + this(context, persistenceService, query, fields, fields, null); } - public TopiaTableModel(TopiaContext context, TopiaQuery query, String[] fields, String[] columnNames) { - this(context, query, fields, columnNames, new String[fields.length]); + public TopiaTableModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query, String[] fields, String[] columnNames) throws TopiaException { + this(context, persistenceService, query, fields, columnNames, null); } - public TopiaTableModel(TopiaContext context, TopiaQuery query, String[] fields, String[] columnNames, String[] columnOrders) { + public TopiaTableModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query, String[] fields, String[] columnNames, String[] columnOrders) throws TopiaException { this.context = context; this.query = query; this.fields = fields; this.columnNames = columnNames; this.columnOrders = columnOrders; - try { - // TODO lancer une invocation asynchrone pour la requete - // TODO plus besoin de faire une copie quand on aura une liste renvoyée ! - data = new ArrayList(context.getPersistenceHelper().find(query)); - } catch (TopiaException eee) { - Logger.getLogger("TopiaTableModel").log(Level.SEVERE, "Can't execute TopiaQuery "+query, eee); - } + setPersistenceService(persistenceService); + fetchData(); } + public void setPersistenceService(TopiaPersistenceService persistenceService) throws TopiaException { + if (this.persistenceService != null) { + this.persistenceService.removeTopiaEntityListener(this); + } + this.persistenceService = persistenceService; + this.persistenceService.addTopiaEntityListener(this); + } + + public TopiaPersistenceService getPersistenceService() { + return this.persistenceService; + } + public void setContext(TopiaContext context) throws TopiaException{ this.context = context; eventOccured(); @@ -83,28 +93,40 @@ return context; } - public void setQuery(TopiaQuery query) { + public void setQuery(TopiaQuery query) throws TopiaException { this.query = query; eventOccured(); } - public void setFields(String[] fields) { + public void setFields(String[] fields) throws TopiaException { this.fields = fields; eventOccured(); } - public void setColumnNames(String[] columnNames) { + public void setColumnNames(String[] columnNames) throws TopiaException { this.columnNames = columnNames; eventOccured(); } - public void setColumnOrders(String[] columnOrders) { + public void setColumnOrders(String[] columnOrders) throws TopiaException { this.columnOrders = columnOrders; eventOccured(); } - public void eventOccured() { + public void fetchData() throws TopiaException { + try { + // TODO lancer une invocation asynchrone pour la requete + // TODO plus besoin de faire une copie quand on aura une liste renvoyée ! + data = new ArrayList(context.getPersistenceHelper().find(query)); + } catch (TopiaException eee) { + Logger.getLogger("TopiaTableModel").log(Level.SEVERE, "Can't execute TopiaQuery "+query, eee); + throw new TopiaException ("TopiaTableModel : Can't execute TopiaQuery", eee); + } + } + + public void eventOccured() throws TopiaException { // TODO : rafraichissement nb column, data, ... + fetchData(); fireTableDataChanged(); } @@ -120,10 +142,10 @@ public Object getValueAt(int x, int y) { if (data == null) return null; - if (y >= getColumnCount()) { + if (0 <= y && y >= getColumnCount()) { return null; } - if (x >= getRowCount()) { + if (0 <= x && x >= getRowCount()) { return null; } Expression e = new Expression(data.get(x), "get"+Util.toUpperCaseFirstLetter(fields[y]), null); @@ -136,15 +158,15 @@ } public String getColumnName(int columnIndex) { - if (columnIndex >= getColumnCount()) { + if (0 <= columnIndex && columnIndex >= getColumnCount()) { return null; } - return fields[columnIndex]; + return columnNames[columnIndex]; } public Class getColumnClass(int columnIndex) { if (data == null) return null; - if (columnIndex >= getColumnCount()) { + if (0 <= columnIndex && columnIndex >= getColumnCount()) { return Object.class; } return getValueAt(0, columnIndex).getClass(); @@ -181,15 +203,53 @@ public Object getObjectAt(int x) { if (data == null) return null; - if (x >= getRowCount()) { + if (0 <= x && x >= getRowCount()) { return null; } return data.get(x); } - public void sortData() { + protected void sortData() { // TODO prise en compte du tableau d'ordre des column pour construire une query triée - eventOccured(); + try { + eventOccured(); + } catch (TopiaException e) { + // No trace that would pollute logs + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.TopiaEntityListener#entityAdded(org.codelutin.topia.TopiaEntityEvent) + */ + public void entityAdded(TopiaEntityEvent event) { + try { + eventOccured(); + } catch (TopiaException e) { + e.printStackTrace(); + // No trace that would pollute logs + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.TopiaEntityListener#entityModified(org.codelutin.topia.TopiaEntityEvent) + */ + public void entityModified(TopiaEntityEvent event) { + try { + eventOccured(); + } catch (TopiaException e) { + // No trace that would pollute logs + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.TopiaEntityListener#entityRemoved(org.codelutin.topia.TopiaEntityEvent) + */ + public void entityRemoved(TopiaEntityEvent event) { + try { + eventOccured(); + } catch (TopiaException e) { + // No trace that would pollute logs + } } }