Author: sbavencoff Date: 2014-07-25 10:47:33 +0200 (Fri, 25 Jul 2014) New Revision: 3867 Url: http://forge.chorem.org/projects/lima/repository/revisions/3867 Log: fixes #973 : auto complete Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AutoCompleteTableCellEditor.java Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractColumn.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractLimaTable.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/Column.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/DescriptionColumn.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/VoucherColumn.java Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AutoCompleteTableCellEditor.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AutoCompleteTableCellEditor.java (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/AutoCompleteTableCellEditor.java 2014-07-25 08:47:33 UTC (rev 3867) @@ -0,0 +1,38 @@ +package org.chorem.lima.ui.celleditor; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator; + +import java.util.List; +import java.util.Map; + +/** + * @author Sylvain Bavencoff <bavencoff@codelutin.com> + */ +public class AutoCompleteTableCellEditor extends StringTableCellEditor { + + protected static Map<String, List<String>> precedingValuesById = Maps.newHashMap(); + + List<String> precedingValues; + + public AutoCompleteTableCellEditor(String id) { + precedingValues = precedingValuesById.get(id); + + if (precedingValues == null) { + precedingValues = Lists.newLinkedList(); + precedingValuesById.put(id, precedingValues); + } + + AutoCompleteDecorator.decorate(getComponent(), precedingValues, false); + } + + @Override + public String getCellEditorValue() { + String stringValue = super.getCellEditorValue().toString(); + if (!precedingValues.contains(stringValue)) { + precedingValues.add(stringValue); + } + return stringValue; + } +} Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractColumn.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractColumn.java 2014-07-24 12:48:39 UTC (rev 3866) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractColumn.java 2014-07-25 08:47:33 UTC (rev 3867) @@ -27,6 +27,8 @@ import org.chorem.lima.LimaConfig; import org.chorem.lima.util.ErrorHelper; +import javax.swing.table.TableCellEditor; + /** * @author Sylvain Bavencoff <bavencoff@codelutin.com> */ @@ -40,6 +42,8 @@ protected boolean editable; + protected TableCellEditor cellEditor; + protected ErrorHelper errorHelper; public AbstractColumn(Class<?> columnClass, String columnName, boolean editable) { @@ -87,4 +91,12 @@ public void setEditable(boolean editable) { this.editable = editable; } + + public TableCellEditor getCellEditor() { + return cellEditor; + } + + public void setCellEditor(TableCellEditor cellEditor) { + this.cellEditor = cellEditor; + } } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractLimaTable.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractLimaTable.java 2014-07-24 12:48:39 UTC (rev 3866) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/AbstractLimaTable.java 2014-07-25 08:47:33 UTC (rev 3867) @@ -40,9 +40,14 @@ import org.chorem.lima.ui.celleditor.StringTableCellEditor; import org.jdesktop.swingx.JXTable; -import javax.swing.*; +import javax.swing.AbstractAction; +import javax.swing.ActionMap; +import javax.swing.InputMap; +import javax.swing.JComponent; +import javax.swing.KeyStroke; import javax.swing.table.TableCellEditor; -import java.awt.*; +import javax.swing.table.TableModel; +import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; @@ -87,6 +92,20 @@ setShowHorizontalLines(false); } + @Override + public void setModel(TableModel model) { + super.setModel(model); + if (model instanceof AbstractLimaTableModel) { + for (int columnIndex = 0; columnIndex < getColumnModel().getColumnCount(); columnIndex++) { + Column column = ((AbstractLimaTableModel) model).getColumn(columnIndex); + TableCellEditor cellEditor = column.getCellEditor(); + if (cellEditor != null) { + getColumnModel().getColumn(columnIndex).setCellEditor(cellEditor); + } + } + } + } + protected void initNavigation() { InputMap inputMap= getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); @@ -100,8 +119,6 @@ clearSelection(); } }); - - } public H getHandler() { Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/Column.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/Column.java 2014-07-24 12:48:39 UTC (rev 3866) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/common/Column.java 2014-07-25 08:47:33 UTC (rev 3867) @@ -24,20 +24,24 @@ * #L% */ +import javax.swing.table.TableCellEditor; + /** * @author Sylvain Bavencoff <bavencoff@codelutin.com> */ public interface Column<T extends AbstractLimaTableModel> { - public Class<?> getColumnClass(); + Class<?> getColumnClass(); - public String getColumnName(); + String getColumnName(); - public Object getValueAt(int row); + Object getValueAt(int row); - public boolean isCellEditable(int row); + boolean isCellEditable(int row); - public boolean setValueAt(Object value, int row); + boolean setValueAt(Object value, int row); - public void setTableModel(T tableModel); + void setTableModel(T tableModel); + + TableCellEditor getCellEditor(); } Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/DescriptionColumn.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/DescriptionColumn.java 2014-07-24 12:48:39 UTC (rev 3866) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/DescriptionColumn.java 2014-07-25 08:47:33 UTC (rev 3867) @@ -25,6 +25,7 @@ */ import org.chorem.lima.entity.Entry; +import org.chorem.lima.ui.celleditor.AutoCompleteTableCellEditor; import org.chorem.lima.ui.common.AbstractColumn; import org.chorem.lima.ui.common.FinancialTransactionTableModel; @@ -37,6 +38,7 @@ public DescriptionColumn() { super(String.class, t("lima.ui.financialtransaction.description"), true); + setCellEditor(new AutoCompleteTableCellEditor("lima.ui.financialtransaction.description")); } @Override public Object getValueAt(int row) { Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/VoucherColumn.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/VoucherColumn.java 2014-07-24 12:48:39 UTC (rev 3866) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/VoucherColumn.java 2014-07-25 08:47:33 UTC (rev 3867) @@ -25,6 +25,7 @@ */ import org.chorem.lima.entity.Entry; +import org.chorem.lima.ui.celleditor.AutoCompleteTableCellEditor; import org.chorem.lima.ui.common.AbstractColumn; import org.chorem.lima.ui.common.FinancialTransactionTableModel; @@ -37,6 +38,7 @@ public VoucherColumn() { super(String.class, t("lima.ui.financialtransaction.voucher"), true); + setCellEditor(new AutoCompleteTableCellEditor("lima.ui.financialtransaction.voucher")); } @Override