Index: lutinutil/src/java/org/codelutin/option/ui/ConfigTableModel.java diff -u /dev/null lutinutil/src/java/org/codelutin/option/ui/ConfigTableModel.java:1.1 --- /dev/null Tue Jan 8 01:16:28 2008 +++ lutinutil/src/java/org/codelutin/option/ui/ConfigTableModel.java Tue Jan 8 01:16:20 2008 @@ -0,0 +1,163 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit +* +* 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. +* ##% */ +package org.codelutin.option.ui; + +import static org.codelutin.i18n.I18n._; +import org.codelutin.option.Config; +import org.codelutin.option.ConfigPropertyKey; +import org.codelutin.util.ArrayUtil; + +import javax.swing.event.TableModelListener; +import javax.swing.table.TableModel; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Un modèle générique pour une configuration générique + * + * @author chemit + */ +public class ConfigTableModel implements TableModel { + + private final String[] columnNames = {_("lutinutil.common.key"), _("lutinutil.common.value")}; + + private final Class[] columnClass = {String.class, Object.class}; + + protected ConfigPropertyKey[] keys; + protected Map, Object> props; + + public ConfigTableModel(Config config) { + props = new LinkedHashMap, Object>(); + for (ConfigPropertyKey key : config.getUniverse()) { + if (!key.isFinal()) { + props.put(key, key.getCurrentValue()); + } + } + for (ConfigPropertyKey key : config.getUniverse()) { + if (key.isFinal()) { + props.put(key, key.getCurrentValue()); + } + } + keys = ArrayUtil.toArray(props.keySet(),ConfigPropertyKey.class); + } + + protected ConfigPropertyKey getKey(int rowIndex) { + return keys[rowIndex]; + } + + public boolean isModified(int rowIndex) { + ConfigPropertyKey key = getKey(rowIndex); + return !key.equals(key.getCurrentValue(), props.get(key)); + } + + public boolean isValid(int rowIndex) { + ConfigPropertyKey key = getKey(rowIndex); + Object value = props.get(key); + return !key.isMandatory() || value != null; + } + + public boolean isModified() { + for (int i = 0; i < keys.length; i++) { + if (isModified(i)) { + return true; + } + } + return false; + } + + public boolean isValid() { + for (int i = 0; i < keys.length; i++) { + if (!isValid(i)) { + return false; + } + } + return true; + } + + @SuppressWarnings({"unchecked"}) + public void transfertModified() { + for (int i = 0; i < getRowCount(); i++) { + if (isModified(i)) { + ConfigPropertyKey key = (ConfigPropertyKey) getKey(i); + Object value = props.get(key); + System.out.println(_("transfert property {0} value {1}",key.getKey(),value)); + key.setCurrentValue(value); + } + } + } + public void reset() { + for (int i = 0; i < getRowCount(); i++) { + if (isModified(i)) { + ConfigPropertyKey key = getKey(i); + props.put(key, key.getCurrentValue()); + } + } + } + + public void addTableModelListener(TableModelListener l) { + + } + + public void removeTableModelListener(TableModelListener l) { + + } + + public int getRowCount() { + return keys.length; + } + + public int getColumnCount() { + return 2; + } + + public String getColumnName(int columnIndex) { + return columnNames[columnIndex]; + } + + public Class getColumnClass(int columnIndex) { + return columnClass[columnIndex]; + } + + public boolean isCellEditable(int rowIndex, int columnIndex) { + return columnIndex == 1 && !keys[rowIndex].isFinal(); + } + + public Object getValueAt(int row, int column) { + ConfigPropertyKey key = getKey(row); + if (column == 0) { + return key.getKey(); + } + return props.get(key); + } + + @SuppressWarnings({"unchecked"}) + public void setValueAt(Object aValue, int row, int column) { + if (column == 0) { + return; + } + ConfigPropertyKey key = getKey(row); + Object val = key.convert(key.getType(), aValue); + //System.out.println(_("new Value for {0} : {1} (oldValue:{2})", key.getKey(), val, key.getCurrentValue())); + props.put(key, val); + } + + public ConfigPropertyKey[] getKeys() { + return keys; + } +} Index: lutinutil/src/java/org/codelutin/option/ui/ConfigTableRenderer.java diff -u /dev/null lutinutil/src/java/org/codelutin/option/ui/ConfigTableRenderer.java:1.1 --- /dev/null Tue Jan 8 01:16:28 2008 +++ lutinutil/src/java/org/codelutin/option/ui/ConfigTableRenderer.java Tue Jan 8 01:16:21 2008 @@ -0,0 +1,88 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit +* +* 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. +* ##% */ +package org.codelutin.option.ui; + +import static org.codelutin.i18n.I18n._; +import org.codelutin.option.ConfigPropertyKey; + +import javax.swing.JTable; +import javax.swing.table.DefaultTableCellRenderer; +import java.awt.Color; +import java.awt.Component; +import java.awt.Font; + +/** + * le renderer pour l'ui de config + * + * @author chemit + */ +public class ConfigTableRenderer extends DefaultTableCellRenderer { + + protected ConfigTableModel model; + protected Color col; + protected Font font; + protected Font font2; + + public ConfigTableRenderer(ConfigTableModel config) { + this.model = config; + col = getForeground(); + } + + @Override + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + ConfigPropertyKey key = model.getKey(row); + boolean isModified = model.isModified(row); + boolean isValid = model.isValid(row); + boolean isFinal = key.isFinal(); + + Object val = value; + + String tooltip = ""; + + setEnabled(!isFinal); + if (font == null) { + font = table.getFont(); + font2 = getFont().deriveFont(Font.ITALIC | Font.BOLD); + } + String s = _("lutinutil.config.modified", key.getCurrentValue()); + if (column == 0) { + tooltip = key.getDescription(); + if (isModified) { + table.setFont(font2); + val = value + " *"; + tooltip += " [" + s + ']'; + } else { + table.setFont(font); + } + } else if (column == 1) { + if (isModified) { + tooltip += s; + } + } + setToolTipText(tooltip); + super.getTableCellRendererComponent(table, val, isModified, hasFocus, row, column); + if (isValid) { + setForeground(col); + } else { + setForeground(Color.RED); + } + return this; + } + +}