Index: topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java diff -u /dev/null topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java:1.1 --- /dev/null Thu Jul 8 13:50:54 2004 +++ topia/src/java/org/codelutin/topia/ui/swing/TopiaComboBoxModel.java Thu Jul 8 13:50:49 2004 @@ -0,0 +1,65 @@ +/* + * Created on Jul 6, 2004 + * + * TODO To change the template for this generated file go to + * Window - Preferences - Java - Code Style - Code Templates + */ +package org.codelutin.topia.ui.swing; + +import java.util.List; + +import javax.swing.AbstractListModel; +import javax.swing.ComboBoxModel; + +import org.codelutin.topia.TopiaQuery; + +/** + * @author pineau + * + * TODO To change the template for this generated type comment go to Window - + * Preferences - Java - Code Style - Code Templates + */ +public class TopiaComboBoxModel extends AbstractListModel implements ComboBoxModel { + + protected TopiaQuery query = null; + protected List data = null; + protected Object selectedItem = null; + + public TopiaComboBoxModel(TopiaQuery query) { + this.query = query; + // lancer une invocation asynchrone pour la requete + } + + public void setQuery(TopiaQuery query) { + this.query = query; + eventOccured(); + } + + public void eventOccured() { + // TODO : rafraichissement nb column, data, ... + // TODO un peu d'optimisation eventuellement :-) ? + fireContentsChanged(this, 0, data.size()); + } + + public Object getSelectedItem() { + return selectedItem; + } + + public void setSelectedItem(Object selectedItem) { + this.selectedItem = selectedItem; + } + + public int getSize() { + if (data == null) return 0; + return data.size(); + } + + public Object getElementAt(int x) { + if (data == null) return null; + if (x >= getSize()) { + return null; + } + return data.get(x); + } + +} \ No newline at end of file Index: topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java diff -u /dev/null topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java:1.1 --- /dev/null Thu Jul 8 13:50:54 2004 +++ topia/src/java/org/codelutin/topia/ui/swing/TopiaTableModel.java Thu Jul 8 13:50:49 2004 @@ -0,0 +1,176 @@ +/* + * Created on Jul 5, 2004 + * + * TODO To change the template for this generated file go to + * Window - Preferences - Java - Code Style - Code Templates + */ +package org.codelutin.topia.ui.swing; + +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; + +import javax.swing.JTable; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableColumnModel; + +import org.codelutin.generator.Util; +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.TopiaQuery; + +/** + * @author pineau + * + * TODO To change the template for this generated type comment go to + * Window - Preferences - Java - Code Style - Code Templates + */ +public class TopiaTableModel extends AbstractTableModel { + + protected static String ASCENDING="ascending"; + protected static String DESCENDING="descending"; + + protected TopiaContext context = null; + protected TopiaQuery query = null; + protected String[] fields = null; + protected String[] columnNames = null; + protected String[] columnOrders = null; + protected List data = null; + + public TopiaTableModel(TopiaContext context, TopiaQuery query, String[] fields) { + this(context, query, fields, fields, new String[fields.length]); + } + + public TopiaTableModel(TopiaContext context, TopiaQuery query, String[] fields, String[] columnNames) { + this(context, query, fields, columnNames, new String[fields.length]); + } + + public TopiaTableModel(TopiaContext context, TopiaQuery query, String[] fields, String[] columnNames, String[] columnOrders) { + 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().execute(query)); + } catch (TopiaException eee) { + Logger.getLogger("TopiaTableModel").log(Level.SEVERE, "Can't execute TopiaQuery "+query, eee); + } + } + + public void setQuery(TopiaQuery query) { + this.query = query; + eventOccured(); + } + + public void setFields(String[] fields) { + this.fields = fields; + eventOccured(); + } + + public void setColumnNames(String[] columnNames) { + this.columnNames = columnNames; + eventOccured(); + } + + public void setColumnOrders(String[] columnOrders) { + this.columnOrders = columnOrders; + eventOccured(); + } + + public void eventOccured() { + // TODO : rafraichissement nb column, data, ... + fireTableDataChanged(); + } + + public int getColumnCount() { + return fields.length; + } + + public int getRowCount() { + if (data == null) return 0; + return data.size(); + } + + public Object getValueAt(int x, int y) { + if (data == null) return null; + if (y >= getColumnCount()) { + return null; + } + if (x >= getRowCount()) { + return null; + } + Expression e = new Expression(data.get(x), "get"+Util.toUpperCaseFirstLetter(fields[y]), null); + try { + return e.getValue(); + } catch (Exception eee) { + eee.printStackTrace(); + return null; + } + } + + public String getColumnName(int columnIndex) { + if (columnIndex >= getColumnCount()) { + return null; + } + return fields[columnIndex]; + } + + public Class getColumnClass(int columnIndex) { + if (data == null) return null; + if (columnIndex >= getColumnCount()) { + return Object.class; + } + return getValueAt(0, columnIndex).getClass(); + } + + public boolean isCellEditable(int rowIndex, int columnIndex) { + return false; + } + + public void setValueAt(Object aValue, int rowIndex, int columnIndex) { + } + + public void addMouseListenerToHeaderInTable(JTable table) { + final JTable tableView = table; + tableView.setColumnSelectionAllowed(false); + MouseAdapter listMouseListener = new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + 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 { + columnOrders[column] = ASCENDING; + } + sortData(); + } + } + }; + JTableHeader th = tableView.getTableHeader(); + th.addMouseListener(listMouseListener); + } + + public Object getObjectAt(int x) { + if (data == null) return null; + if (x >= getRowCount()) { + return null; + } + return data.get(x); + } + + public void sortData() { + // TODO prise en compte du tableau d'ordre des column pour construire une query triée + eventOccured(); + } + +}