Index: lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java diff -u lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java:1.8 lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java:1.9 --- lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java:1.8 Fri Mar 17 17:31:10 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java Wed Mar 22 19:38:47 2006 @@ -24,9 +24,9 @@ * * @author Benjamin Poussin * - * @version $Revision: 1.8 $ + * @version $Revision: 1.9 $ * - * Mise a jour: $Date: 2006/03/17 17:31:10 $ par : $Author: bpoussin $ + * Mise a jour: $Date: 2006/03/22 19:38:47 $ par : $Author: bpoussin $ */ package org.codelutin.math.matrix.gui; @@ -44,6 +44,7 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -58,6 +59,8 @@ import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.UIManager; +import javax.swing.event.TableModelEvent; +import javax.swing.event.TableModelListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.JTableHeader; @@ -66,197 +69,13 @@ import org.codelutin.math.matrix.MatrixFactory; import org.codelutin.math.matrix.MatrixHelper; import org.codelutin.math.matrix.MatrixND; - -/** - * Extension de AbstractTableModel pour definir un TableModel avec une MatrixND - * comme support d'information. - * - * @todo changer les 'matrice' en 'matrix', s'il y a des methodes public avec - * les nom 'matice' les laisser mais les mettres depreciées - */ -class MatrixTableModel extends AbstractTableModel { - - /** */ - private static final long serialVersionUID = 983978774901981167L; - - protected MatrixND m; - - /** par defaut, la matrice est editable. */ - protected boolean enabled = true; - - /** - * TableModel basee sur une MatrixND a une ou deux dimensions. Pour le - * moment les matrices de plus de 2 dimensions ne sont pas geree. - *

- * Pour les matrices 1D : - *

- * La premiere dimension represente les colonnes. - *

- * Pour les matrices 2D : - *

- * La premiere dimension represente les lignes. - *

- * La deuxieme dimension represente les colonnes. - * - * @param m Matrice a afficher dans la table - */ - public MatrixTableModel(MatrixND m) throws MatrixException { - this.m = m; - if (m.getNbDim() > 2) { - throw new MatrixException( - "matrix with more than 2 dimension not supported."); - } - } - - /** - * Retourne une representation String de la semantique de l'element elem de - * la dimension dim - * - * @param dim la dimension dans lequel on recherche l'element - * @param elem l'element de la dimension a prendre - * @return une chaine representant l'element. Si l'element est null, la - * chaine vide est retourné - */ - protected String getSemantic(int dim, int elem) { - Object o = m.getSemantics(dim).get(elem); - return (o == null) ? "" : o.toString(); - } - - public String getColumnName(int column) { - if (m.getNbDim() > 1) { - if (column == 0) { - return ""; - } else { - return getSemantic(1, column - 1); - } - } else { - return getSemantic(0, column); - } - } - - /** - * @return Le nombre de lignes de la table. - */ - public int getRowCount() { - if (m == null || m.getNbDim() < 1) - return 0; - else if (m.getNbDim() > 1) - return m.getDim(0); - else - return 1; - } - - /** - * @return Le nombre de colonnes de la table. - */ - public int getColumnCount() { - if (m == null || m.getNbDim() < 1) { - return 0; - } else if (m.getNbDim() > 1) { - return m.getDim(1) + 1; - } else { - return m.getDim(0); - } - } - - /** - * @param row La ligne - * @param column La colonnes - * @return L'Object correspondant dans la matrice. - */ - public Object getValueAt(int row, int column) { - // column 0 donc on veut la semantique pour l'afficher - if (column == 0 && m.getNbDim() > 1) { - return getSemantic(0, row); - } else if (m.getNbDim() == 1) { - return "" + m.getValue(column); - } else { // m.getNbDim() == 2 - return "" + m.getValue(row, column - 1); - } - } - - /** - * @param obj L'objet a inserer dans la matrice. - * @param row La ligne - * @param column La colonnes - */ - public void setValueAt(Object obj, int row, int column) { - double val = 0; - - if (m.getNbDim() == 1) { - val = m.getValue(column); - } else { // m.getNbDim() == 2 - val = m.getValue(row, column - 1); - } - - try { - val = Double.parseDouble((String) obj); - } catch (Exception eee) { - Logger.getLogger(getClass().getName() + ".setValueAt").log( - Level.FINE, - "La nouvelle valeur n'est pas convertible en double: " - + obj, eee); - } - - if (m.getNbDim() == 1) { - m.setValue(column, val); - } else { // m.getNbDim() == 2 - m.setValue(row, column - 1, val); - } - fireTableDataChanged(); - } - - /** - * Le modele est rendu editable. - */ - public boolean isCellEditable(int row, int column) { - if (column == 0 && m.getNbDim() > 1) - return false; - return enabled; - } - - /** - * Permet de rendre la table editable ou non . - */ - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - /** - * Par defaut, la classe de la colonne est du type Function.class - */ - public Class getColumnClass(int column) { - return String.class; - } -} - -class MatrixCellRendere extends DefaultTableCellRenderer { - - /** */ - private static final long serialVersionUID = 6537813058357761914L; - - public Component getTableCellRendererComponent(JTable table, Object value, - boolean isSelected, boolean hasFocus, int row, int column) { - super.getTableCellRendererComponent(table, value, isSelected, hasFocus, - row, column); - if (table != null) { - JTableHeader header = table.getTableHeader(); - if (header != null) { - setForeground(header.getForeground()); - setBackground(header.getBackground()); - setFont(header.getFont()); - } - } - setBorder(UIManager.getBorder("TableHeader.cellBorder")); - return this; - } -} +import org.codelutin.util.ListenerSet; /** * JPanel contenant une JTable pour afficher une Matrice a une ou deux * dimension. */ -public class MatrixPanelEditor extends JPanel { // MatrixPanelEditor +public class MatrixPanelEditor extends JPanel implements TableModelListener { // MatrixPanelEditor /** */ private static final long serialVersionUID = 2097859265435050946L; @@ -265,6 +84,8 @@ private final static int DEFAULT_HEIGHT = 150; + protected ListenerSet listeners = new ListenerSet(); + protected JTable table; protected MatrixND m; @@ -273,7 +94,11 @@ protected JScrollPane editArea; - protected JTextArea text; +// protected JTextArea text; + + protected boolean linearModel = false; + + protected boolean linearModelShowDefault = false; /** Boolean to autorize table editing. */ protected boolean enabled = true; @@ -324,7 +149,15 @@ protected MatrixFactory getFactory() { return MatrixFactory.getInstance(); } - + + public void addMatrixListener(MatrixPanelListener l) { + listeners.add(l); + } + + public void removeMatrixPanelListener(MatrixPanelListener l) { + listeners.remove(l); + } + protected void initObjet() { setLayout(new BorderLayout()); editArea = new JScrollPane(); @@ -360,6 +193,37 @@ return bEdit; } + + /** + * @return Returns the linearModel. + */ + public boolean isLinearModel() { + return this.linearModel; + } + + /** + * @param linearModel The linearModel to set. + */ + public void setLinearModel(boolean linearModel) { + this.linearModel = linearModel; + initObject(m); + } + + /** + * @return Returns the linearModelShowDefault. + */ + public boolean isLinearModelShowDefault() { + return this.linearModelShowDefault; + } + + /** + * @param linearModelShowDefault The linearModelShowDefault to set. + */ + public void setLinearModelShowDefault(boolean linearModelShowDefault) { + this.linearModelShowDefault = linearModelShowDefault; + initObject(m); + } + /** * Get the value of dimensionEdit. * @@ -386,34 +250,51 @@ protected void initObject(MatrixND m) { if (m == null) { editArea.setViewportView(null); - } else if (m.getNbDim() <= 2) { + } else { // if (m.getNbDim() <= 2) { // pour les matrices 1D et 2D - table = new JTable(); + table = getTable(); editArea.setViewportView(table); - table.setModel(tableModel = new MatrixTableModel(m)); - if (table.getColumnCount() > 0 && m.getNbDim() > 1) { - table.getColumnModel().getColumn(0).setCellRenderer( - new MatrixCellRendere()); - } + if (isLinearModel()) { + tableModel = new MatrixTableModelLinear(m, isLinearModelShowDefault()); + } else { + tableModel = new MatrixTableModelND(m); + } + table.getModel().removeTableModelListener(this); + tableModel.addTableModelListener(this); + table.setModel(tableModel); + table.setDefaultRenderer(String.class, tableModel.getMatrixCellRenderer()); +// if (table.getColumnCount() > 0 && m.getNbDim() > 1) { +// table.getColumnModel().getColumn(0).setCellRenderer( +// tableModel.getMatrixCellRenderer()); +// } +// text = null; + } +// else { +// // pour les matrices 3D et plus +// text = new JTextArea(); +// editArea.setViewportView(text); +// text.setText(MatrixHelper.encodeToXML(m)); +// table = null; +// editArea.setColumnHeaderView(null); +// } + setEnabled(enabled); + repaint(); + } + + public JTable getTable() { + if (table == null) { + table = new JTable(); table.registerKeyboardAction(getCopyAction(), "Copy", KeyStroke .getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK), JComponent.WHEN_FOCUSED); - text = null; - } else { - // pour les matrices 3D et plus - text = new JTextArea(); - editArea.setViewportView(text); - text.setText(MatrixHelper.encodeToXML(m)); - table = null; - editArea.setColumnHeaderView(null); } - setEnabled(enabled); - repaint(); + return table; } - + public void setMatrix(MatrixND m) throws MatrixException { initObject(m); this.m = m; + fireEvent(); } /** @@ -424,13 +305,13 @@ } public MatrixND getMatrix() { - if (m == null) { - return m; - } else if (m.getNbDim() <= 2) { +// if (m == null) { return m; - } else { - return MatrixHelper.decodeFromXML(text.getText()); - } +// } else if (m.getNbDim() <= 2) { +// return m; +// } else { +// return MatrixHelper.decodeFromXML(text.getText()); +// } } /** * @deprecated use setMatrix @@ -446,9 +327,9 @@ if (tableModel != null) { tableModel.setEnabled(enabled); } - if (text != null) { - text.setEditable(enabled); - } +// if (text != null) { +// text.setEditable(enabled); +// } // si la table n'est pas editable, inutile de laisser le bouton // de creation de matrice. if (!enabled && dimensionEdit) { @@ -485,6 +366,21 @@ clipboard.setContents(contents, contents); } + /* (non-Javadoc) + * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent) + */ + public void tableChanged(TableModelEvent e) { + fireEvent(); + } + + protected void fireEvent() { + MatrixPanelEvent e = new MatrixPanelEvent(this); + for(Iterator i=listeners.iterator(); i.hasNext();) { + MatrixPanelListener l = (MatrixPanelListener)i.next(); + l.matrixChanged(e); + } + } + /** * Une petite fonction main pour le test... */ @@ -493,6 +389,7 @@ MatrixPanelEditor ed = null; try { ed = new MatrixPanelEditor(true); + ed.setLinearModel(true); frame.getContentPane().add(ed); // MatriceND m = new MatriceNDImpl(new int[]{4,4}); @@ -505,9 +402,14 @@ List sem1 = Arrays.asList(new String[] { "toto", "titi", "tutu" }); List sem2 = Arrays.asList(new String[] { "tata", "tete", "tyty" }); - MatrixND m = MatrixFactory.getInstance().create( - new List[] { sem1, sem2 }); - + List sem3 = Arrays.asList(new String[] { "riri", "fifi", "loulou" }); + MatrixND m = MatrixFactory.getInstance().create("name", + new List[] { sem1, sem2, sem3 }, new String[]{"dim1", "dim2", "dim3"}); + + m.setValue(0, 0, 0, 1); + m.setValue(0, 1, 0, 2); + m.setValue(0, 0, 1, 3); + ed.setMatrix(m); // ed.setEnabled(false); } catch (MatrixException e) { Index: lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEvent.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEvent.java:1.1 --- /dev/null Wed Mar 22 19:38:52 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelEvent.java Wed Mar 22 19:38:47 2006 @@ -0,0 +1,59 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MatrixPanelEvent.java + * + * Created: 21 mars 2006 14:53:25 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/03/22 19:38:47 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix.gui; + +import java.util.EventObject; + +import org.codelutin.math.matrix.MatrixND; + + +/** + * @author poussin + * + */ + +public class MatrixPanelEvent extends EventObject { + + /** + * @param source + */ + public MatrixPanelEvent(MatrixPanelEditor source) { + super(source); + } + + public MatrixND getMatrix() { + MatrixND result = ((MatrixPanelEditor)getSource()).getMatrix(); + return result; + } +} + + Index: lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelListener.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelListener.java:1.1 --- /dev/null Wed Mar 22 19:38:52 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixPanelListener.java Wed Mar 22 19:38:47 2006 @@ -0,0 +1,51 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MatrixPanelListener.java + * + * Created: 21 mars 2006 14:54:05 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/03/22 19:38:47 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix.gui; + + +/** + * @author poussin + * + */ + +public interface MatrixPanelListener { + + /** + * called when matrix change + * + * @param e + */ + public void matrixChanged(MatrixPanelEvent e); + +} + + Index: lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModel.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModel.java:1.1 --- /dev/null Wed Mar 22 19:38:52 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModel.java Wed Mar 22 19:38:47 2006 @@ -0,0 +1,53 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MatrixTableModel.java + * + * Created: 22 mars 2006 12:53:22 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/03/22 19:38:47 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix.gui; + +import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableModel; + +import org.codelutin.math.matrix.MatrixND; + + +/** + * @author poussin + * + */ + +public interface MatrixTableModel extends TableModel { + + public void setMatrix(MatrixND m); + public void setEnabled(boolean enabled); + public TableCellRenderer getMatrixCellRenderer(); + +} + + Index: lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModelLinear.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModelLinear.java:1.1 --- /dev/null Wed Mar 22 19:38:52 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModelLinear.java Wed Mar 22 19:38:47 2006 @@ -0,0 +1,275 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MatrixTableModelLinear.java + * + * Created: 22 mars 2006 12:11:46 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/03/22 19:38:47 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix.gui; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Font; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JTable; +import javax.swing.UIManager; +import javax.swing.border.Border; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableCellRenderer; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.math.matrix.MatrixIterator; +import org.codelutin.math.matrix.MatrixND; +import org.codelutin.math.matrix.gui.MatrixTableModelND.MatrixCellRenderer; + + +/** + * @author poussin + * + */ + +public class MatrixTableModelLinear extends AbstractTableModel implements MatrixTableModel { + + /** + * Logger for this class + */ + private static final Log log = LogFactory + .getLog(MatrixTableModelLinear.class); + + protected boolean enabled = true; + protected MatrixND m = null; + protected boolean showDefault = false; + protected double defaultValue = 0; + protected List mappingRowSems = new ArrayList(); + protected TableCellRenderer renderer = null; + + public MatrixTableModelLinear(MatrixND m, boolean showDefault) { + this.showDefault = showDefault; + setMatrix(m); + } + + public void setMatrix(MatrixND m) { + this.m = m; + computeMapping(); + fireTableStructureChanged(); + } + + protected void computeMapping() { + mappingRowSems.clear(); + for (MatrixIterator i=m.iterator(); i.next();) { + Object [] sems = i.getSemanticsCoordinates(); + double value = i.getValue(); + if (showDefault || value != defaultValue) { + mappingRowSems.add(sems); + } + } + } + + /** + * @return Returns the enabled. + */ + public boolean isEnabled() { + return this.enabled; + } + + /** + * @param enabled The enabled to set. + */ + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + /** + * @return Returns the showDefault. + */ + public boolean isShowDefault() { + return this.showDefault; + } + + /** + * @param showDefault The showDefault to set. + */ + public void setShowDefault(boolean showDefault) { + this.showDefault = showDefault; + computeMapping(); + fireTableDataChanged(); + } + + /** + * @return Returns the defaultValue. + */ + public double getDefaultValue() { + return this.defaultValue; + } + + /** + * @param defaultValue The defaultValue to set. + */ + public void setDefaultValue(double defaultValue) { + this.defaultValue = defaultValue; + computeMapping(); + fireTableDataChanged(); + } + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getRowCount() + */ + public int getRowCount() { + return mappingRowSems.size(); + } + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getColumnCount() + */ + public int getColumnCount() { + return m.getNbDim() + 1; + } + + /* (non-Javadoc) + * @see javax.swing.table.TableModel#getValueAt(int, int) + */ + public Object getValueAt(int rowIndex, int columnIndex) { + Object result = null; + if (columnIndex < m.getNbDim()) { + result = mappingRowSems.get(rowIndex)[columnIndex]; + } else { + result = m.getValue(mappingRowSems.get(rowIndex)); + } + return result; + } + + /* (non-Javadoc) + * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int) + */ + @Override + public boolean isCellEditable(int rowIndex, int columnIndex) { + return isEnabled() && columnIndex == m.getNbDim(); + } + + /* (non-Javadoc) + * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int) + */ + @Override + public void setValueAt(Object aValue, int rowIndex, int columnIndex) { + try { + double val = Double.parseDouble((String) aValue); + Object [] sems = mappingRowSems.get(rowIndex); + m.setValue(sems, val); + } catch (Exception eee) { + log.debug("La nouvelle valeur n'est pas convertible en double: " + + aValue, eee); + } + + } + + /* (non-Javadoc) + * @see javax.swing.table.AbstractTableModel#getColumnName(int) + */ + @Override + public String getColumnName(int column) { + String result = null; + if (column < m.getNbDim()) { + result = m.getDimensionName(column); + } else { + result = ""; + } + return result; + } + + /** + * Par defaut, la classe de la colonne est du type Function.class + */ + public Class getColumnClass(int column) { + return String.class; + } + + /** + * @return + */ + public TableCellRenderer getMatrixCellRenderer() { + if (renderer == null) { + renderer = new MatrixCellRenderer(this); + } + return renderer; + } + + + class MatrixCellRenderer extends DefaultTableCellRenderer { + + protected MatrixTableModelLinear model = null; + protected Color bg = null; + protected Color fg = null; + protected Font font = null; + protected Border border = null; + + public MatrixCellRenderer(MatrixTableModelLinear model) { + this.model = model; + bg = getBackground(); + fg = getForeground(); + font = getFont(); + border = getBorder(); + } + + /** */ + private static final long serialVersionUID = 6537813058357761914L; + + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) { + super.getTableCellRendererComponent(table, value, isSelected, hasFocus, + row, column); + setToolTipText(getText()); + + if (column < model.m.getNbDim()) { + if (table != null) { + JTableHeader header = table.getTableHeader(); + if (header != null) { + setForeground(header.getForeground()); + setBackground(header.getBackground()); + setFont(header.getFont()); + } + } + + setBorder(UIManager.getBorder("TableHeader.cellBorder")); + } else { + setBackground(bg); + setForeground(fg); + setFont(font); + setBorder(border); + } + + return this; + } + } + +} + + Index: lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModelND.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModelND.java:1.1 --- /dev/null Wed Mar 22 19:38:53 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/gui/MatrixTableModelND.java Wed Mar 22 19:38:47 2006 @@ -0,0 +1,367 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MatrixTableModel.java + * + * Created: 21 mars 2006 19:01:27 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/03/22 19:38:47 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix.gui; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Font; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.swing.JTable; +import javax.swing.UIManager; +import javax.swing.border.Border; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableCellRenderer; + +import org.codelutin.math.matrix.MatrixException; +import org.codelutin.math.matrix.MatrixND; + + +/** + * @author poussin + * + */ + +public /** + * Extension de AbstractTableModel pour definir un TableModel avec une MatrixND + * comme support d'information. + * + * @todo changer les 'matrice' en 'matrix', s'il y a des methodes public avec + * les nom 'matice' les laisser mais les mettres depreciées + */ +class MatrixTableModelND extends AbstractTableModel implements MatrixTableModel { + + /** */ + private static final long serialVersionUID = 983978774901981167L; + + protected MatrixND m; + + /** nombre de ligne ajouté */ + protected int addRow = 0; + /** nombre de colone ajouté */ + protected int addCol = 0; + + protected int [] multRowCol = null; + + /** par defaut, la matrice est editable. */ + protected boolean enabled = true; + + protected TableCellRenderer renderer = null; + + /** + * TableModel basee sur une MatrixND a une ou deux dimensions. Pour le + * moment les matrices de plus de 3 dimensions ne sont pas geree. + *

+ * Pour les matrices 1D : + *

+ * La premiere dimension represente les colonnes. + *

+ * Pour les matrices 2D : + *

+ * La premiere dimension represente les lignes. + *

+ * La deuxieme dimension represente les colonnes. + *

+ * Pour les matrices 3D : + *

+ * La premiere dimension represente les lignes. + *

+ * La deuxieme dimension represente les colonnes. + *

+ * La troisieme dimension represente les lignes (dim1 x dim3). + * + * @param m Matrice a afficher dans la table + */ + public MatrixTableModelND(MatrixND m) throws MatrixException { + setMatrix(m); + } + + public void setMatrix(MatrixND m) { + this.m = m; + addRow = m.getNbDim() / 2; + addCol = (m.getNbDim() + 1) / 2; + + // calcule les coefficients multiplicateur pour la correspondance table/matrice + multRowCol = new int[m.getNbDim()]; + for (int i=multRowCol.length-1; i>=0; i--) { + if (i >= multRowCol.length - 2) { + multRowCol[i] = 1; + } else { + multRowCol[i] = multRowCol[i+2] * m.getDim(i+2); + } + } + } + + /** + * converti les coordonnées de la table en coordonnées pour la matrice + * + * @param row la ligne dans la table + * @param col la colonne dans la table + * @return les coordonnées equivalentes dans la matrice + */ + protected int [] tableToMatrix(int row, int col) { + int [] result = new int[m.getNbDim()]; + for (int i=0; i= addRow && column >= addCol) + || (m.getNbDim() == 1 && row >= 1) ) { + try { + double val = Double.parseDouble((String) obj); + int [] coord = null; + if (m.getNbDim() != 1) { +// coord = tableToMatrix(row - addRow + 1, column - addCol); // +1 pour le header + coord = tableToMatrix(row - addRow, column - addCol); + } else { + coord = new int[]{column}; + } + m.setValue(coord, val); + fireTableDataChanged(); + } catch (Exception eee) { + Logger.getLogger(getClass().getName() + ".setValueAt").log( + Level.FINE, + "La nouvelle valeur n'est pas convertible en double: " + + obj, eee); + } + } + } + + /** + * Le modele est rendu editable. + */ + public boolean isCellEditable(int row, int column) { + if (m.getNbDim() != 1 && (row < addRow || column < addCol)) { + return false; + } else if (m.getNbDim() == 1 && row < 1) { + return false; + } + return enabled; + } + + /** + * Permet de rendre la table editable ou non . + */ + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + /** + * Par defaut, la classe de la colonne est du type Function.class + */ + public Class getColumnClass(int column) { + return String.class; + } + + /** + * @return + */ + public TableCellRenderer getMatrixCellRenderer() { + if (renderer == null) { + renderer = new MatrixCellRenderer(this); + } + return renderer; + } + + + class MatrixCellRenderer extends DefaultTableCellRenderer { + + protected MatrixTableModelND model = null; + protected Color bg = null; + protected Color fg = null; + protected Font font = null; + protected Border border = null; + + public MatrixCellRenderer(MatrixTableModelND model) { + this.model = model; + bg = getBackground(); + fg = getForeground(); + font = getFont(); + border = getBorder(); + } + + /** */ + private static final long serialVersionUID = 6537813058357761914L; + + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) { + super.getTableCellRendererComponent(table, value, isSelected, hasFocus, + row, column); + setToolTipText(getText()); + + if ((model.m.getNbDim() != 1 && (row < model.addRow || column < model.addCol)) + || (model.m.getNbDim() == 1 && row < 1)) { + if (table != null) { + JTableHeader header = table.getTableHeader(); + if (header != null) { + setForeground(header.getForeground()); + setBackground(header.getBackground()); + setFont(header.getFont()); + } + } + + setBorder(UIManager.getBorder("TableHeader.cellBorder")); + } else { + setBackground(bg); + setForeground(fg); + setFont(font); + setBorder(border); + } + + return this; + } + } + +} + +