Index: topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java diff -u topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java:1.3 topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java:1.4 --- topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java:1.3 Tue Jul 20 16:02:25 2004 +++ topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java Wed Sep 15 14:36:04 2004 @@ -6,15 +6,18 @@ */ package org.codelutin.topia.ui.swing; -import java.util.List; - import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; +import org.codelutin.topia.AsynchronousLoader; +import org.codelutin.topia.AsynchronousLoaderEvent; +import org.codelutin.topia.AsynchronousLoaderListener; import org.codelutin.topia.TopiaContext; -import org.codelutin.topia.TopiaQuery; -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 @@ -23,40 +26,75 @@ * Preferences - Java - Code Style - Code Templates */ public class TopiaComboBoxModel extends AbstractListModel -implements ComboBoxModel, TopiaElement { +implements ComboBoxModel, TopiaEntityListener, AsynchronousLoaderListener { - protected TopiaQuery query = null; protected TopiaContext context = null; - protected List data = null; + protected TopiaPersistenceService persistenceService = null; + protected TopiaQuery query = null; protected Object selectedItem = null; - public TopiaComboBoxModel() { - } + protected AsynchronousLoader loader = null; - public TopiaComboBoxModel(TopiaContext context, TopiaQuery query) { + public TopiaComboBoxModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query) throws TopiaException { this.query = query; this.context = context; - // lancer une invocation asynchrone pour la requete + setPersistenceService(persistenceService); + fetchData(); } - public void setContext(TopiaContext context) throws TopiaException{ + public void setPersistenceService(TopiaPersistenceService persistenceService) throws TopiaException { + if (this.persistenceService != null) { + this.persistenceService.removeTopiaEntityListener(this); + } + this.persistenceService = persistenceService; + this.persistenceService.addTopiaEntityListener(this); + } + + public void fetchData() throws TopiaException { + loader = context.getPersistenceHelper().findAsynchronously(query); + loader.addListener(this); + System.out.println("Fetching asyncronously for "+query); // TODO remove this +// 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("TopiaComboBoxModel").log(Level.SEVERE, "Can't execute TopiaQuery "+query, eee); +// throw new TopiaException ("TopiaComboBoxModel : Can't execute TopiaQuery", eee); +// } + } + +// public void fetchDataInRange(int startIndex, int endIndex) throws TopiaException { +// try { +// Object[] fetchedObjects = context.getPersistenceHelper().findInRange(query, startIndex, endIndex).toArray(); +// int insertionIndex = startIndex; +// for (int i = 0; i < fetchedObjects.length; i++) { +// data[insertionIndex++] = fetchedObjects[i]; +// } +// } catch (TopiaException eee) { +// Logger.getLogger("TopiaComboBoxModel").log(Level.SEVERE, "Can't execute TopiaQuery "+query, eee); +// throw new TopiaException ("TopiaComboBoxModel : Can't execute TopiaQuery", eee); +// } +// } + + + public void setContext(TopiaContext context) throws TopiaException { this.context = context; eventOccured(); } - public TopiaContext getContext(){ + public TopiaContext getContext() { return context; } - public void setQuery(TopiaQuery query) { + public void setQuery(TopiaQuery query) throws TopiaException { this.query = query; eventOccured(); } - public void eventOccured() { - // TODO : rafraichissement nb column, data, ... - // TODO un peu d'optimisation eventuellement :-) ? - fireContentsChanged(this, 0, data.size()); + public void eventOccured() throws TopiaException { + fetchData(); + fireContentsChanged(this, 0, getSize()); } public Object getSelectedItem() { @@ -65,19 +103,55 @@ public void setSelectedItem(Object selectedItem) { this.selectedItem = selectedItem; + //fireContentsChanged(this, 0, data.length); } - + public int getSize() { - if (data == null) return 0; - return data.size(); + if (loader == null) return 0; + return loader.size(); } - public Object getElementAt(int x) { - if (data == null) return null; - if (x >= getSize()) { + public Object getElementAt(int rowIndex) { + if (loader == null) return null; + if (0 > rowIndex || rowIndex >= getSize()) { return null; } - return data.get(x); + Object row = loader.getObjectAt(rowIndex); + return row; + } + + public void entityAdded(TopiaEntityEvent event) { + try { + eventOccured(); + } catch (TopiaException e) { + // No trace that would pollute logs + } + } + + public void entityModified(TopiaEntityEvent event) { + try { + eventOccured(); + } catch (TopiaException e) { + // No trace that would pollute logs + } + } + + public void entityRemoved(TopiaEntityEvent event) { + try { + eventOccured(); + } catch (TopiaException e) { + // No trace that would pollute logs + } + } + + public void dataLoaded(AsynchronousLoaderEvent event) { + if (event.getType() == AsynchronousLoaderEvent.SIZE_IS_AVAILABLE) { + fireContentsChanged(this, 0, getSize()); + } else if (event.getType() == AsynchronousLoaderEvent.OBJECTS_LOADED) { + int startIndex = event.getStartIndex(); + int endIndex = event.getEndIndex(); + fireContentsChanged(this, startIndex, endIndex); + } } } Index: topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java diff -u topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java:1.6 topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java:1.7 --- topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java:1.6 Thu Sep 2 15:42:54 2004 +++ topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java Wed Sep 15 14:36:04 2004 @@ -9,8 +9,6 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.beans.Expression; -import java.util.ArrayList; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -20,8 +18,11 @@ import javax.swing.table.TableColumnModel; import org.codelutin.generator.Util; +import org.codelutin.topia.AsynchronousLoader; +import org.codelutin.topia.AsynchronousLoaderEvent; +import org.codelutin.topia.AsynchronousLoaderListener; import org.codelutin.topia.TopiaContext; -import org.codelutin.topia.TopiaElement; +import org.codelutin.topia.TopiaEntity; import org.codelutin.topia.TopiaEntityEvent; import org.codelutin.topia.TopiaEntityListener; import org.codelutin.topia.TopiaException; @@ -30,39 +31,47 @@ /** * @author pineau - * - * TODO To change the template for this generated type comment go to - * Window - Preferences - Java - Code Style - Code Templates + * + * 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, TopiaEntityListener { +public class TopiaTableModel extends AbstractTableModel implements + TopiaEntityListener, AsynchronousLoaderListener { - protected static String ASCENDING="ascending"; - protected static String DESCENDING="descending"; + 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; + protected String[] columnOrders = null; - protected List data = null; - public TopiaTableModel(){ - } + protected AsynchronousLoader loader = null; - public TopiaTableModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query) throws TopiaException { - this(context, persistenceService, query, null, null, null); - } - - public TopiaTableModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query, String[] fields) throws TopiaException { + public TopiaTableModel(TopiaContext context, + TopiaPersistenceService persistenceService, TopiaQuery query, + String[] fields) throws TopiaException { this(context, persistenceService, query, fields, fields, null); } - public TopiaTableModel(TopiaContext context, TopiaPersistenceService persistenceService, TopiaQuery query, String[] fields, String[] columnNames) throws TopiaException { + 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, TopiaPersistenceService persistenceService, TopiaQuery query, String[] fields, String[] columnNames, String[] columnOrders) throws TopiaException { + 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; @@ -72,7 +81,8 @@ fetchData(); } - public void setPersistenceService(TopiaPersistenceService persistenceService) throws TopiaException { + public void setPersistenceService(TopiaPersistenceService persistenceService) + throws TopiaException { if (this.persistenceService != null) { this.persistenceService.removeTopiaEntityListener(this); } @@ -83,13 +93,12 @@ public TopiaPersistenceService getPersistenceService() { return this.persistenceService; } - public void setContext(TopiaContext context) throws TopiaException{ this.context = context; eventOccured(); } - public TopiaContext getContext(){ + public TopiaContext getContext() { return context; } @@ -104,6 +113,15 @@ } public void setColumnNames(String[] columnNames) throws TopiaException { + if (columnNames.length != fields.length) { + Logger.getLogger("TopiaTableModel").log( + Level.WARNING, + "Can't set " + columnNames + + " columnNames as size does not match " + fields + + " fields size"); + throw new TopiaException( + "TopiaTableModel : Can't set columnNames as size does not match fields size"); + } this.columnNames = columnNames; eventOccured(); } @@ -114,15 +132,39 @@ } 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); - } - } + loader = context.getPersistenceHelper().findAsynchronously(query); + loader.addListener(this); + System.out.println("Fetching asyncronously for "+query); // TODO remove this + // 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 fetchDataInRange(int startIndex, int endIndex) throws + // TopiaException { + // try { + // Object[] fetchedObjects = + // context.getPersistenceHelper().findInRange(query, startIndex, + // endIndex).toArray(); + // int insertionIndex = startIndex; + // for (int i = 0; i < fetchedObjects.length; i++) { + // data[insertionIndex++] = fetchedObjects[i]; + // } + // } 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, ... @@ -131,24 +173,32 @@ } public int getColumnCount() { - if (fields == null) return 0; + if (fields == null) + return 0; return fields.length; } public int getRowCount() { - if (data == null) return 0; - return data.size(); + if (loader == null) + return 0; + return loader.size(); } - public Object getValueAt(int x, int y) { - if (data == null) return null; - if (0 <= y && y >= getColumnCount()) { + public Object getValueAt(int rowIndex, int columnIndex) { + if (loader == null) + return null; + if (0 > columnIndex || columnIndex >= getColumnCount()) { return null; } - if (0 <= x && x >= getRowCount()) { + if (0 > rowIndex || rowIndex >= getRowCount()) { return null; } - Expression e = new Expression(data.get(x), "get"+Util.toUpperCaseFirstLetter(fields[y]), null); + Object row = loader.getObjectAt(rowIndex); + if (row == null) { + return null; // nothing loaded for that yet + } + Expression e = new Expression(row, "get" + + Util.toUpperCaseFirstLetter(fields[columnIndex]), null); try { return e.getValue(); } catch (Exception eee) { @@ -158,18 +208,26 @@ } public String getColumnName(int columnIndex) { - if (0 <= columnIndex && columnIndex >= getColumnCount()) { + if (0 > columnIndex || columnIndex >= getColumnCount()) { return null; } return columnNames[columnIndex]; } public Class getColumnClass(int columnIndex) { - if (data == null) return null; - if (0 <= columnIndex && columnIndex >= getColumnCount()) { + if (loader == null) + return Object.class; + if (0 > columnIndex || columnIndex >= getColumnCount()) { + return Object.class; + } + Object value = getValueAt(0, columnIndex); + if (value == null) { return Object.class; } - return getValueAt(0, columnIndex).getClass(); + if (value instanceof TopiaEntity) { + return TopiaEntity.class; + } + return value.getClass(); } public boolean isCellEditable(int rowIndex, int columnIndex) { @@ -184,14 +242,17 @@ tableView.setColumnSelectionAllowed(false); MouseAdapter listMouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { + if (columnOrders == null) { + columnOrders = new String[columnOrders.length]; + } TableColumnModel columnModel = tableView.getColumnModel(); int viewColumn = columnModel.getColumnIndexAtX(e.getX()); int column = tableView.convertColumnIndexToModel(viewColumn); if (e.getClickCount() == 1 && column != -1) { - if (ASCENDING.equals(columnOrders[column])) { - columnOrders[column] = DESCENDING; - } else { + if (DESCENDING.equals(columnOrders[column])) { columnOrders[column] = ASCENDING; + } else { + columnOrders[column] = DESCENDING; } sortData(); } @@ -202,15 +263,17 @@ } public Object getObjectAt(int x) { - if (data == null) return null; - if (0 <= x && x >= getRowCount()) { + if (loader == null) + return null; + if (0 > x || x >= getRowCount()) { return null; } - return data.get(x); + return loader.getObjectAt(x); } protected void sortData() { - // TODO prise en compte du tableau d'ordre des column pour construire une query triée + // TODO prise en compte du tableau d'ordre des column pour construire + // une query triée try { eventOccured(); } catch (TopiaException e) { @@ -218,21 +281,15 @@ } } - /* (non-Javadoc) - * @see org.codelutin.topia.TopiaEntityListener#entityAdded(org.codelutin.topia.TopiaEntityEvent) - */ public void entityAdded(TopiaEntityEvent event) { try { eventOccured(); } catch (TopiaException e) { - e.printStackTrace(); + 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(); @@ -241,9 +298,6 @@ } } - /* (non-Javadoc) - * @see org.codelutin.topia.TopiaEntityListener#entityRemoved(org.codelutin.topia.TopiaEntityEvent) - */ public void entityRemoved(TopiaEntityEvent event) { try { eventOccured(); @@ -252,4 +306,14 @@ } } -} + public void dataLoaded(AsynchronousLoaderEvent event) { + if (event.getType() == AsynchronousLoaderEvent.SIZE_IS_AVAILABLE) { + fireTableDataChanged(); + } else if (event.getType() == AsynchronousLoaderEvent.OBJECTS_LOADED) { + int startIndex = event.getStartIndex(); + int endIndex = event.getEndIndex(); + fireTableRowsUpdated(startIndex, endIndex); + } + } + +} \ No newline at end of file