Index: lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java diff -u lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java:1.2 lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java:1.3 --- lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java:1.2 Sat Feb 9 16:23:17 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java Thu Feb 21 17:16:57 2008 @@ -22,8 +22,11 @@ import javax.swing.SwingUtilities; import javax.swing.JTable; +import javax.swing.table.TableColumn; +import javax.swing.table.TableCellRenderer; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; +import java.awt.Component; /** * Le controleur de l'ui de modification de configuration. @@ -56,8 +59,11 @@ tableModel.addTableModelListener(getTableModelListener()); JTable table = ui.getMain(); table.setModel(tableModel); - table.setDefaultRenderer(Object.class, new ConfigTableRenderer(tableModel)); - table.getColumn(table.getColumnName(1)).setCellEditor(new ConfigTableEditor(getTableModel())); + initColumnSizes(tableModel,table); + ConfigTableRenderer renderer = new ConfigTableRenderer(tableModel); + table.getColumnModel().getColumn(0).setCellRenderer(renderer); + table.getColumnModel().getColumn(1).setCellRenderer(renderer); + table.getColumnModel().getColumn(1).setCellEditor(new ConfigTableEditor(getTableModel())); boolean action = tableModel.getType() != ConfigTableModel.TypeModel.tech; ui.getButons().setVisible(action); ui.getButons2().setVisible(!action); @@ -65,6 +71,30 @@ ui.pack(); } + protected void initColumnSizes(ConfigTableModel model,JTable table) { + TableColumn column; + Component comp; + int headerWidth; + int cellWidth; + + TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); + + for (int i = 0; i < model.getColumnCount(); i++) { + column = table.getColumnModel().getColumn(i); + + comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); + headerWidth = comp.getPreferredSize().width; + + comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, model.getColumnName(i),false, false, 0, i); + cellWidth = comp.getPreferredSize().width; + + { + System.out.println("Initializing width of column "+ i + ". "+ "headerWidth = " + headerWidth+ "; cellWidth = " + cellWidth); + } + column.setPreferredWidth(Math.max(headerWidth, cellWidth)); + } + } + public void doOk() { if (!isEmpty() && getTableModel().isModified()) { getTableModel().transfertModified(getConfig()); Index: lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java diff -u lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java:1.2 --- lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java:1.1.1.1 Sat Feb 9 15:05:37 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java Thu Feb 21 17:16:57 2008 @@ -97,7 +97,7 @@ } } } - this.type=type; + this.type = type; keys = ArrayUtil.toArray(props.keySet(), ConfigPropertyKey.class); } @@ -191,23 +191,27 @@ return columnIndex == 1 && !keys[rowIndex].isFinal(); } - @Override + @Override public void setValueAt(Object aValue, int row, int column) { if (column == 0) { return; } ConfigPropertyKey key = getKey(row); Object val; + String valStr = String.valueOf(aValue).trim(); try { - val = key.convert(key.getType(), aValue); + val = key.convert(key.getType(), valStr); + if (val !=null && val instanceof Integer) { + if (new Integer(0).equals(val) && !valStr.equals("0")) { + val = null; + } + } } catch (Exception e) { - val=null; + val = null; } - //System.out.println(_("new Value for {0} : {1} (oldValue:{2})", key.getKey(), val, key.getCurrentValue())); + //System.out.println(_("new Value for {0} : {1} --> {2} (oldValue:{3})", key.getKey(), valStr,val, key.getCurrentValue())); props.put(key, val); - if (column != 0) { - fireTableDataChanged(); - } + fireTableRowsUpdated(row,row); } } Index: lutincommandline/src/java/org/codelutin/option/ui/ConfigTableEditor.java diff -u lutincommandline/src/java/org/codelutin/option/ui/ConfigTableEditor.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/ui/ConfigTableEditor.java:1.2 --- lutincommandline/src/java/org/codelutin/option/ui/ConfigTableEditor.java:1.1.1.1 Sat Feb 9 15:05:37 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ConfigTableEditor.java Thu Feb 21 17:16:57 2008 @@ -18,11 +18,10 @@ * ##% */ package org.codelutin.option.ui; -import org.codelutin.option.ConfigPropertyKey; import org.codelutin.util.EnumEditor; -import javax.swing.JTable; import javax.swing.DefaultCellEditor; +import javax.swing.JTable; import javax.swing.event.CellEditorListener; import javax.swing.table.TableCellEditor; import java.awt.Component; @@ -33,7 +32,7 @@ * * @author chemit */ -public class ConfigTableEditor implements TableCellEditor { +public class ConfigTableEditor implements TableCellEditor { protected TableCellEditor delegate; protected ConfigTableModel model; @@ -42,59 +41,72 @@ this.model = model; } + public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { + delegate = findDelegate(table, model.getKey(row).getType()); + return delegate.getTableCellEditorComponent(table, value, isSelected, row, column); + } + public Object getCellEditorValue() { - return delegate == null ? null : delegate.getCellEditorValue(); + return !hasDelegate() ? null : delegate.getCellEditorValue(); } public boolean isCellEditable(EventObject anEvent) { - return delegate == null || delegate.isCellEditable(anEvent); + return !hasDelegate() || delegate.isCellEditable(anEvent); } public boolean shouldSelectCell(EventObject anEvent) { - return delegate != null && delegate.shouldSelectCell(anEvent); + return hasDelegate() && delegate.shouldSelectCell(anEvent); } public boolean stopCellEditing() { - return delegate == null || delegate.stopCellEditing(); + return !hasDelegate() || delegate.stopCellEditing(); } public void cancelCellEditing() { - if (delegate != null) { + if (hasDelegate()) { delegate.cancelCellEditing(); } } public void addCellEditorListener(CellEditorListener l) { - if (delegate != null) { + if (hasDelegate()) { delegate.addCellEditorListener(l); } } public void removeCellEditorListener(CellEditorListener l) { - if (delegate != null) { + if (hasDelegate()) { delegate.removeCellEditorListener(l); } } - public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { - if (column == 0) { - return null; - } - ConfigPropertyKey key = model.getKey(row); - Class type = key.getType(); - delegate = table.getDefaultEditor(type); + protected TableCellEditor findDelegate(JTable table, Class type) { + TableCellEditor delegate = table.getDefaultEditor(type); TableCellEditor defaultEditor = table.getDefaultEditor(Object.class); if (delegate == defaultEditor) { + // find not a specialized editor for the type if (type.isEnum()) { - // add a EnumEditor to table + // add a EnumEditor to table delegate = new DefaultCellEditor(EnumEditor.newEditor(type)); - table.setDefaultEditor(type,delegate); - } else { + table.setDefaultEditor(type, delegate); + } else if (type == Class.class) { + delegate = new ClassCellEditor(); + table.setDefaultEditor(type, delegate); + } //else if (type == File.class){ + // TODO a FileEditor + // table.setDefaultEditor(type, delegate); + //} + else { delegate = table.getDefaultEditor(String.class); } } - Component comp; - comp = delegate.getTableCellEditorComponent(table, value, isSelected, row, column); - return comp; + if (delegate == null) { + throw new IllegalStateException("could not find a editor for type +" + type); + } + return delegate; + } + + protected boolean hasDelegate() { + return delegate != null; } } Index: lutincommandline/src/java/org/codelutin/option/ui/ConfigTableRenderer.java diff -u lutincommandline/src/java/org/codelutin/option/ui/ConfigTableRenderer.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/ui/ConfigTableRenderer.java:1.2 --- lutincommandline/src/java/org/codelutin/option/ui/ConfigTableRenderer.java:1.1.1.1 Sat Feb 9 15:05:37 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ConfigTableRenderer.java Thu Feb 21 17:16:57 2008 @@ -21,8 +21,9 @@ import static org.codelutin.i18n.I18n._; import org.codelutin.option.ConfigPropertyKey; -import javax.swing.JTable; import javax.swing.JComponent; +import javax.swing.JTable; +import javax.swing.border.LineBorder; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import java.awt.Color; @@ -49,55 +50,59 @@ @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + //System.out.println(">>> ConfigColumnRenderer.getTableCellRendererComponent " + row + "/" + 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); + boolean isFinal = key.isFinal(); if (font == null) { font = table.getFont(); font2 = getFont().deriveFont(Font.ITALIC | Font.BOLD); } - String s = _("lutinutil.config.modified", key.getCurrentValue()); + Component cellRenderer = null; + // modified lines are selected if (column == 0) { - tooltip = key.getDescription(); - if (isModified) { - table.setFont(font2); - val = value + " *"; - tooltip += " [" + s + ']'; - } else { - table.setFont(font); - } + cellRenderer = getKeyCellRenderer(table, value, isModified, hasFocus, row, column, key, isValid); } else if (column == 1) { - if (isModified) { - tooltip += s; - } + cellRenderer = getValueCellRenderer(table, value, isModified, hasFocus, row, column, key, isValid); } - setToolTipText(tooltip); - TableCellRenderer defaultRenderer= this; - if (column==1) { - defaultRenderer = table.getDefaultRenderer(key.getType()); - if (defaultRenderer == null) { - defaultRenderer = this; - } + if (cellRenderer == null) { + throw new IllegalStateException("no renderer find for column " + column); + } + cellRenderer.setEnabled(!isFinal); + cellRenderer.setFont(isModified ? font2 : font); + return cellRenderer; + } + + protected Component getKeyCellRenderer(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column, ConfigPropertyKey key, boolean valid) { + String tooltip = key.getDescription(); + String s = _("lutinutil.config.modified", key.getCurrentValue()); + + if (isSelected) { + value = value + " *"; + tooltip += " [" + s + ']'; } - if (defaultRenderer == this) { - super.getTableCellRendererComponent(table, val, isModified, hasFocus, row, column); - } else { - JComponent rendererComponent = (JComponent) defaultRenderer.getTableCellRendererComponent(table, val, isModified, hasFocus, row, column); - rendererComponent.setToolTipText(tooltip); + if (!valid) { + String s2 = _("lutinutil.config.unvalid", key.getCurrentValue(), key.getType()); + tooltip += " (" + s2 + ")"; } - if (isValid) { - setForeground(col); + JComponent result = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + result.setToolTipText(tooltip); + result.setForeground(valid ? col : Color.RED); + return result; + } + + protected Component getValueCellRenderer(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column, ConfigPropertyKey key, boolean valid) { + TableCellRenderer defaultRenderer = table.getDefaultRenderer(key.getType()); + + JComponent result; + result = (JComponent) defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + if (!valid) { + result.setBorder(new LineBorder(Color.red)); } else { - setForeground(Color.RED); + result.setBorder(null); } - return (Component) defaultRenderer; + return result; } - } Index: lutincommandline/src/java/org/codelutin/option/ui/ClassCellEditor.java diff -u /dev/null lutincommandline/src/java/org/codelutin/option/ui/ClassCellEditor.java:1.1 --- /dev/null Thu Feb 21 17:17:02 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ClassCellEditor.java Thu Feb 21 17:16:57 2008 @@ -0,0 +1,95 @@ +/* +* ##% 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 javax.swing.DefaultCellEditor; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.event.CellEditorListener; +import javax.swing.table.TableCellEditor; +import java.awt.Component; +import java.util.EventObject; + +/** + * L'éditeur des valeurs des propriétés d'une configuration + * + * @author chemit + */ +public class ClassCellEditor implements TableCellEditor { + + protected TableCellEditor delegate; + + public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { + + String valStr = (value + "").trim(); + if (valStr.equals("null")) { + valStr = ""; + } else if (valStr.startsWith("class ")) { + valStr = valStr.substring(6); + } + Component comp; + comp = getDelegate().getTableCellEditorComponent(table, valStr, isSelected, row, column); + return comp; + } + + public Object getCellEditorValue() { + return !hasDelegate() ? null : delegate.getCellEditorValue(); + } + + public boolean isCellEditable(EventObject anEvent) { + return !hasDelegate() || delegate.isCellEditable(anEvent); + } + + public boolean shouldSelectCell(EventObject anEvent) { + return hasDelegate() && delegate.shouldSelectCell(anEvent); + } + + public boolean stopCellEditing() { + return !hasDelegate() || delegate.stopCellEditing(); + } + + public void cancelCellEditing() { + if (hasDelegate()) { + delegate.cancelCellEditing(); + } + } + + public void addCellEditorListener(CellEditorListener l) { + if (hasDelegate()) { + delegate.addCellEditorListener(l); + } + } + + public void removeCellEditorListener(CellEditorListener l) { + if (hasDelegate()) { + delegate.removeCellEditorListener(l); + } + } + + protected TableCellEditor getDelegate() { + if (delegate == null) { + delegate = new DefaultCellEditor(new JTextField()); + } + return delegate; + } + + protected boolean hasDelegate() { + return delegate != null; + } +} \ No newline at end of file