Author: tchemit Date: 2013-10-14 20:30:59 +0200 (Mon, 14 Oct 2013) New Revision: 1298 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/1298 Log: refs #3328: [ERGO] Tri du tableau des esp?\195?\168ces (must always translate model and view model coordinate since using sorting on table) Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/CreateBenthosMelagAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosSubBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RenameBenthosBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/CreateSpeciesMelagAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddBenthosProtocolAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddSpeciesProtocolAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveBenthosProtocolAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveSpeciesProtocolAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/CreateBenthosMelagAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/CreateBenthosMelagAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/CreateBenthosMelagAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -68,7 +68,7 @@ public boolean prepareAction() throws Exception { boolean result = super.prepareAction(); - int[] selectedRowIndexes = handler.getTable().getSelectedRows(); + int[] selectedRowIndexes = SwingUtil.getSelectedModelRows(handler.getTable()); BenthosBatchTableModel tableModel = handler.getTableModel(); @@ -108,7 +108,7 @@ _("tutti.createBenthosMelag.error.title"), JOptionPane.ERROR_MESSAGE); - handler.getTable().getSelectionModel().setSelectionInterval(selectedRowIndex, selectedRowIndex); + SwingUtil.setSelectionInterval(handler.getTable(), selectedRowIndex); result = false; break; Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosBatchAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosBatchAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -28,8 +28,9 @@ import com.google.common.collect.Sets; import fr.ifremer.tutti.persistence.TuttiPersistence; import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; -import fr.ifremer.tutti.ui.swing.util.table.AbstractSelectTableAction; +import jaxx.runtime.SwingUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdesktop.swingx.JXTable; @@ -51,6 +52,13 @@ private static final Log log = LogFactory.getLog(RemoveBenthosBatchAction.class); + /** + * Selected row to treat. + * + * @since 2.8 + */ + protected int rowIndex; + public RemoveBenthosBatchAction(BenthosBatchUIHandler handler) { super(handler, false); } @@ -59,6 +67,8 @@ public boolean prepareAction() throws Exception { boolean result = super.prepareAction(); + rowIndex = -1; + if (result) { int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(), _("tutti.editBenthosBatch.action.removeBatch.confirm.message"), @@ -78,7 +88,7 @@ JXTable table = handler.getTable(); - int rowIndex = table.getSelectedRow(); + rowIndex = SwingUtil.getSelectedModelRow(table); Preconditions.checkState(rowIndex != -1, "Cant remove batch if no batch selected"); @@ -110,26 +120,29 @@ handler.collectChildren(selectedBatch, rowToRemove); - table.clearSelection(); +//FIXME TC-2013-10-14 Comment this line, don't see the point +// table.clearSelection(); // remove all rows from the model getModel().getRows().removeAll(rowToRemove); - // refresh table from parent batch row index to the end - tableModel.fireTableDataChanged(); +// // refresh table from parent batch row index to the end +// tableModel.fireTableDataChanged(); +// +// TuttiUIUtil.selectFirstCellOnFirstRowAndStopEditing(table); + } - if (!getModel().getRows().isEmpty()) { + @Override + public void postSuccessAction() { + super.postSuccessAction(); - // select first row - AbstractSelectTableAction.doSelectCell(table, 0, 0); + JXTable table = handler.getTable(); - } + // refresh table from parent batch row index to the end + handler.getTableModel().fireTableDataChanged(); - if (table.isEditing()) { - - // but no edit it - table.getCellEditor().stopCellEditing(); - } + // select parent batch row + TuttiUIUtil.selectFirstCellOnFirstRowAndStopEditing(table); } } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosSubBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosSubBatchAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RemoveBenthosSubBatchAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -28,8 +28,9 @@ import com.google.common.collect.Sets; import fr.ifremer.tutti.persistence.TuttiPersistence; import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; -import fr.ifremer.tutti.ui.swing.util.table.AbstractSelectTableAction; +import jaxx.runtime.SwingUtil; import org.jdesktop.swingx.JXTable; import javax.swing.JOptionPane; @@ -45,6 +46,13 @@ */ public class RemoveBenthosSubBatchAction extends AbstractTuttiAction<BenthosBatchUIModel, BenthosBatchUI, BenthosBatchUIHandler> { + /** + * Selected row to treat. + * + * @since 2.8 + */ + protected int rowIndex; + public RemoveBenthosSubBatchAction(BenthosBatchUIHandler handler) { super(handler, false); } @@ -53,6 +61,8 @@ public boolean prepareAction() throws Exception { boolean result = super.prepareAction(); + rowIndex = -1; + if (result) { int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(), _("tutti.editBenthosBatch.action.removeSubBatch.confirm.message"), @@ -72,7 +82,7 @@ JXTable table = handler.getTable(); - int rowIndex = table.getSelectedRow(); + rowIndex = SwingUtil.getSelectedModelRow(table); Preconditions.checkState(rowIndex != -1, "Cant remove sub batch if no batch selected"); @@ -90,7 +100,8 @@ handler.collectChildren(parentBatch, rowToRemove); - table.clearSelection(); +//FIXME TC-2013-10-14 Comment this line, don't see the point +// table.clearSelection(); // remove all rows from the model getModel().getRows().removeAll(rowToRemove); @@ -98,17 +109,29 @@ // remove childs from parent batch parentBatch.setChildBatch(null); +// // refresh table from parent batch row index to the end +// handler.getTableModel().fireTableDataChanged(); +// +// // select parent batch row +// AbstractSelectTableAction.doSelectCell(table, rowIndex, 0); +// +// if (table.isEditing()) { +// +// // but no edit it +// table.getCellEditor().stopCellEditing(); +// } + } + + @Override + public void postSuccessAction() { + super.postSuccessAction(); + + JXTable table = handler.getTable(); + // refresh table from parent batch row index to the end handler.getTableModel().fireTableDataChanged(); // select parent batch row - AbstractSelectTableAction.doSelectCell(table, rowIndex, 0); - - if (table.isEditing()) { - - // but no edit it - table.getCellEditor().stopCellEditing(); - } + TuttiUIUtil.selectFirstCellOnRow(table, rowIndex, true); } - } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RenameBenthosBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RenameBenthosBatchAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/benthos/RenameBenthosBatchAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -30,6 +30,7 @@ import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.service.PersistenceService; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; +import jaxx.runtime.SwingUtil; import org.jdesktop.swingx.JXTable; import java.io.Serializable; @@ -59,7 +60,8 @@ BenthosBatchTableModel tableModel = handler.getTableModel(); JXTable table = handler.getTable(); - BenthosBatchRowModel row = tableModel.getEntry(table.getSelectedRow()); + int selectedRowIndex = SwingUtil.getSelectedModelRow(table); + BenthosBatchRowModel row = tableModel.getEntry(selectedRowIndex); BenthosBatchUIModel model = handler.getModel(); List<Species> speciesList = Lists.newArrayList( @@ -90,7 +92,8 @@ BenthosBatchTableModel tableModel = handler.getTableModel(); JXTable table = handler.getTable(); - BenthosBatchRowModel row = tableModel.getEntry(table.getSelectedRow()); + int selectedRowIndex = SwingUtil.getSelectedModelRow(table); + BenthosBatchRowModel row = tableModel.getEntry(selectedRowIndex); PersistenceService persistenceService = getContext().getPersistenceService(); persistenceService.changeBenthosBatchSpecies(row.getId(), selectedSpecies); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/CreateSpeciesMelagAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/CreateSpeciesMelagAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/CreateSpeciesMelagAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -68,7 +68,7 @@ public boolean prepareAction() throws Exception { boolean result = super.prepareAction(); - int[] selectedRowIndexes = handler.getTable().getSelectedRows(); + int[] selectedRowIndexes = SwingUtil.getSelectedModelRows(handler.getTable()); SpeciesBatchTableModel tableModel = handler.getTableModel(); @@ -108,7 +108,7 @@ _("tutti.createSpeciesMelag.error.title"), JOptionPane.ERROR_MESSAGE); - handler.getTable().getSelectionModel().setSelectionInterval(selectedRowIndex, selectedRowIndex); + SwingUtil.setSelectionInterval(handler.getTable(), selectedRowIndex); result = false; break; Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesBatchAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -28,8 +28,9 @@ import com.google.common.collect.Sets; import fr.ifremer.tutti.persistence.TuttiPersistence; import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; -import fr.ifremer.tutti.ui.swing.util.table.AbstractSelectTableAction; +import jaxx.runtime.SwingUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdesktop.swingx.JXTable; @@ -78,7 +79,7 @@ JXTable table = handler.getTable(); - int rowIndex = table.getSelectedRow(); + int rowIndex = SwingUtil.getSelectedModelRow(table); Preconditions.checkState(rowIndex != -1, "Cant remove batch if no batch selected"); @@ -110,27 +111,39 @@ handler.collectChildren(selectedBatch, rowToRemove); - table.clearSelection(); +//FIXME TC-2013-10-14 Comment this line, don't see the point +// table.clearSelection(); // remove all rows from the model getModel().getRows().removeAll(rowToRemove); - // refresh table from parent batch row index to the end - tableModel.fireTableDataChanged(); +// // refresh table from parent batch row index to the end +// tableModel.fireTableDataChanged(); +// +// if (!getModel().getRows().isEmpty()) { +// +// // select first row +// AbstractSelectTableAction.doSelectCell(table, 0, 0); +// +// } +// +// if (table.isEditing()) { +// +// // but no edit it +// table.getCellEditor().stopCellEditing(); +// } + } - if (!getModel().getRows().isEmpty()) { + @Override + public void postSuccessAction() { + super.postSuccessAction(); - // select first row - AbstractSelectTableAction.doSelectCell(table, 0, 0); + JXTable table = handler.getTable(); - } + // refresh table from parent batch row index to the end + handler.getTableModel().fireTableDataChanged(); - if (table.isEditing()) { - - // but no edit it - table.getCellEditor().stopCellEditing(); - } - + // select parent batch row + TuttiUIUtil.selectFirstCellOnFirstRowAndStopEditing(table); } - } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RemoveSpeciesSubBatchAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -28,8 +28,9 @@ import com.google.common.collect.Sets; import fr.ifremer.tutti.persistence.TuttiPersistence; import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; -import fr.ifremer.tutti.ui.swing.util.table.AbstractSelectTableAction; +import jaxx.runtime.SwingUtil; import org.jdesktop.swingx.JXTable; import javax.swing.JOptionPane; @@ -45,6 +46,13 @@ */ public class RemoveSpeciesSubBatchAction extends AbstractTuttiAction<SpeciesBatchUIModel, SpeciesBatchUI, SpeciesBatchUIHandler> { + /** + * Selected row to treat. + * + * @since 2.8 + */ + protected int rowIndex; + public RemoveSpeciesSubBatchAction(SpeciesBatchUIHandler handler) { super(handler, false); } @@ -53,6 +61,8 @@ public boolean prepareAction() throws Exception { boolean result = super.prepareAction(); + rowIndex = -1; + if (result) { int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(), _("tutti.editSpeciesBatch.action.removeSubBatch.confirm.message"), @@ -72,7 +82,7 @@ JXTable table = handler.getTable(); - int rowIndex = table.getSelectedRow(); + rowIndex = SwingUtil.getSelectedModelRow(table); Preconditions.checkState(rowIndex != -1, "Cant remove sub batch if no batch selected"); @@ -90,7 +100,8 @@ handler.collectChildren(parentBatch, rowToRemove); - table.clearSelection(); +//FIXME TC-2013-10-14 Comment this line, don't see the point +// table.clearSelection(); // remove all rows from the model getModel().getRows().removeAll(rowToRemove); @@ -98,17 +109,29 @@ // remove childs from parent batch parentBatch.setChildBatch(null); +// // refresh table from parent batch row index to the end +// handler.getTableModel().fireTableDataChanged(); +// +// // select parent batch row +// TuttiUIUtil.selectFirstCellOnRow(table, rowIndex); +// +// if (table.isEditing()) { +// +// // but no edit it +// table.getCellEditor().stopCellEditing(); +// } + } + + @Override + public void postSuccessAction() { + super.postSuccessAction(); + + JXTable table = handler.getTable(); + // refresh table from parent batch row index to the end handler.getTableModel().fireTableDataChanged(); // select parent batch row - AbstractSelectTableAction.doSelectCell(table, rowIndex, 0); - - if (table.isEditing()) { - - // but no edit it - table.getCellEditor().stopCellEditing(); - } + TuttiUIUtil.selectFirstCellOnRow(table, rowIndex, true); } - } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/RenameSpeciesBatchAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -30,6 +30,7 @@ import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.service.PersistenceService; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; +import jaxx.runtime.SwingUtil; import org.jdesktop.swingx.JXTable; import java.io.Serializable; @@ -59,7 +60,8 @@ SpeciesBatchTableModel tableModel = handler.getTableModel(); JXTable table = handler.getTable(); - SpeciesBatchRowModel row = tableModel.getEntry(table.getSelectedRow()); + int selectedRowIndex = SwingUtil.getSelectedModelRow(table); + SpeciesBatchRowModel row = tableModel.getEntry(selectedRowIndex); SpeciesBatchUIModel model = handler.getModel(); List<Species> speciesList = Lists.newArrayList( @@ -89,7 +91,8 @@ SpeciesBatchTableModel tableModel = handler.getTableModel(); JXTable table = handler.getTable(); - SpeciesBatchRowModel row = tableModel.getEntry(table.getSelectedRow()); + int selectedRowIndex = SwingUtil.getSelectedModelRow(table); + SpeciesBatchRowModel row = tableModel.getEntry(selectedRowIndex); PersistenceService persistenceService = getContext().getPersistenceService(); persistenceService.changeSpeciesBatchSpecies(row.getId(), selectedSpecies); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddBenthosProtocolAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddBenthosProtocolAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddBenthosProtocolAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -27,6 +27,7 @@ import com.google.common.base.Preconditions; import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; +import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox; import java.util.Collection; @@ -47,6 +48,8 @@ protected Species species; + protected EditProtocolSpeciesRowModel newRow; + @Override public void doAction() throws Exception { @@ -70,22 +73,32 @@ Collection<Species> allSynonyms = getModel().getAllSynonyms(taxonIdStr); getModel().getAllSynonyms().removeAll(allSynonyms); - benthosComboBox.removeItem(species); - ui.getSpeciesComboBox().removeItem(species); +// benthosComboBox.removeItem(species); +// ui.getSpeciesComboBox().removeItem(species); // add new row to model (do it after combo stuff for ui best display) - EditProtocolSpeciesRowModel protocol = tableModel.createNewRow(); - protocol.setSpecies(species); - tableModel.addNewRow(protocol); - - int rowIndex = tableModel.getRowIndex(protocol); - int selectedRowIndex = handler.getBenthosTable().convertRowIndexToView(rowIndex); - handler.getBenthosTable().getSelectionModel().setSelectionInterval(selectedRowIndex, selectedRowIndex); + newRow = tableModel.createNewRow(); + newRow.setSpecies(species); + getModel().getBenthosRow().add(newRow); } @Override public void postSuccessAction() { super.postSuccessAction(); + + // update comboboxes + getUI().getBenthosComboBox().removeItem(species); + getUI().getSpeciesComboBox().removeItem(species); + + // fire row was inserted in table model + EditProtocolSpeciesTableModel tableModel = getHandler().getBenthosTableModel(); + tableModel.fireTableRowsInserted(newRow); + + // select this new row + int rowIndex = tableModel.getRowIndex(newRow); + SwingUtil.setSelectionInterval(handler.getBenthosTable(), rowIndex); + + // add notification String speciesStr = decorate(species); sendMessage(_("tutti.flash.info.benthos.add.to.protocol", speciesStr)); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddSpeciesProtocolAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddSpeciesProtocolAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/AddSpeciesProtocolAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -27,6 +27,7 @@ import com.google.common.base.Preconditions; import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; +import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox; import java.util.Collection; @@ -47,6 +48,8 @@ protected Species species; + protected EditProtocolSpeciesRowModel newRow; + @Override public void doAction() throws Exception { @@ -68,21 +71,33 @@ Collection<Species> allSynonyms = getModel().getAllSynonyms(taxonIdStr); getModel().getAllSynonyms().removeAll(allSynonyms); - speciesComboBox.getHandler().removeItem(species); - ui.getBenthosComboBox().removeItem(species); +// speciesComboBox.getHandler().removeItem(species); +// ui.getBenthosComboBox().removeItem(species); // add new row to model (do it after combo stuff for ui best display) - EditProtocolSpeciesRowModel protocol = tableModel.createNewRow(); - protocol.setSpecies(species); - tableModel.addNewRow(protocol); - int rowIndex = tableModel.getRowIndex(protocol); - int selectedRowIndex = handler.getSpeciesTable().convertRowIndexToView(rowIndex); - handler.getSpeciesTable().getSelectionModel().setSelectionInterval(selectedRowIndex, selectedRowIndex); + + newRow = tableModel.createNewRow(); + newRow.setSpecies(species); + getModel().getSpeciesRow().add(newRow); } @Override public void postSuccessAction() { super.postSuccessAction(); + + // update comboboxes + getUI().getBenthosComboBox().removeItem(species); + getUI().getSpeciesComboBox().removeItem(species); + + // fire row was inserted in table model + EditProtocolSpeciesTableModel tableModel = getHandler().getSpeciesTableModel(); + tableModel.fireTableRowsInserted(newRow); + + // select this new row + int rowIndex = tableModel.getRowIndex(newRow); + SwingUtil.setSelectionInterval(handler.getBenthosTable(), rowIndex); + + // add notification String speciesStr = decorate(species); sendMessage(_("tutti.flash.info.species.add.to.protocol", speciesStr)); Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveBenthosProtocolAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveBenthosProtocolAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveBenthosProtocolAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -26,13 +26,16 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; +import jaxx.runtime.SwingUtil; import javax.swing.JTable; import java.util.Collections; import java.util.List; +import java.util.Set; import static org.nuiton.i18n.I18n._; @@ -44,6 +47,14 @@ */ public class RemoveBenthosProtocolAction extends AbstractTuttiAction<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> { + + /** + * Set of removed species. + * + * @since 2.8 + */ + protected Set<Species> removedSpecies; + public RemoveBenthosProtocolAction(EditProtocolUIHandler handler) { super(handler, false); } @@ -56,57 +67,91 @@ // need to have a selection Preconditions.checkState(!table.getSelectionModel().isSelectionEmpty()); - // build the list of model row indexes from selected view row indexes - List<Integer> modelIndexes = Lists.newArrayList(); +// // build the list of model row indexes from selected view row indexes +// List<Integer> modelIndexes = Lists.newArrayList(); +// +// for (int index : SwingUtil.getSelectedModelRows(table)) { +// modelIndexes.add(index); +// } +// +// // sort it and reverse it (must delete first max row index) +// Collections.sort(modelIndexes); +// Collections.reverse(modelIndexes); - for (int index : table.getSelectedRows()) { - int modelRowIndex = table.convertRowIndexToModel(index); - modelIndexes.add(modelRowIndex); - } - - // sort it and reverse it (must delete first max row index) - Collections.sort(modelIndexes); - Collections.reverse(modelIndexes); - EditProtocolSpeciesTableModel tableModel = (EditProtocolSpeciesTableModel) table.getModel(); - for (Integer index : modelIndexes) { + removedSpecies = Sets.newHashSet(); - removeRow(tableModel, index); + EditProtocolUIModel model = getModel(); + + for (Integer rowIndex : SwingUtil.getSelectedModelRows(table)) { + + // get row to remove + EditProtocolSpeciesRowModel selectedRow = + tableModel.getEntry(rowIndex); + + // re-add all synonym of this taxon to the species / benthos combobox + Species species = selectedRow.getSpecies(); + removedSpecies.add(species); + + Integer taxonId = species.getReferenceTaxonId(); + List<Species> allSynonyms = Lists.newArrayList( + model.getAllSynonyms(String.valueOf(taxonId))); + allSynonyms.remove(species); + model.getAllSynonyms().addAll(allSynonyms); + + // remove the row from the model + model.getBenthosRow().remove(selectedRow); } - EditProtocolUIModel model = getModel(); - // reorder the list by name, otherwise, // all the species without a reftax code will be at the end - Collections.sort(model.getAllSynonyms(), TuttiEntities.SPECIES_BY_NAME_COMPARATOR); + Collections.sort(model.getAllSynonyms(), + TuttiEntities.SPECIES_BY_NAME_COMPARATOR); - table.clearSelection(); +// table.clearSelection(); } - protected void removeRow(EditProtocolSpeciesTableModel tableModel, int rowIndex) { +// protected void removeRow(EditProtocolSpeciesTableModel tableModel, int rowIndex) { +// +// EditProtocolSpeciesRowModel selectedRow = tableModel.getEntry(rowIndex); +// +// // re-add all synonym of this taxon to the species / benthos combobox +// Species species = selectedRow.getSpecies(); +// getUI().getBenthosComboBox().addItem(species); +// getUI().getSpeciesComboBox().addItem(species); +// +// EditProtocolUIModel model = getModel(); +// Integer taxonId = species.getReferenceTaxonId(); +// List<Species> allSynonyms = Lists.newArrayList( +// model.getAllSynonyms(String.valueOf(taxonId))); +// allSynonyms.remove(species); +// model.getAllSynonyms().addAll(allSynonyms); +// +// // remove the row from the model +// tableModel.removeRow(rowIndex); +// +// String speciesStr = decorate(selectedRow.getSpecies()); +// sendMessage(_("tutti.flash.info.benthos.remove.from.protocol", +// speciesStr)); +// } - EditProtocolSpeciesRowModel selectedRow = tableModel.getEntry(rowIndex); + @Override + public void postSuccessAction() { + super.postSuccessAction(); - // re-add all synonym of this taxon to the species / benthos combobox - Species species = selectedRow.getSpecies(); - getUI().getBenthosComboBox().addItem(species); - getUI().getSpeciesComboBox().addItem(species); + // update comboboxes + getUI().getBenthosComboBox().addItems(removedSpecies); + getUI().getSpeciesComboBox().addItems(removedSpecies); - EditProtocolUIModel model = getModel(); - Integer taxonId = species.getReferenceTaxonId(); - List<Species> allSynonyms = Lists.newArrayList( - model.getAllSynonyms(String.valueOf(taxonId))); - allSynonyms.remove(species); - model.getAllSynonyms().addAll(allSynonyms); + // fire table data changed + handler.getBenthosTableModel().fireTableDataChanged(); - // remove the row from the model - tableModel.removeRow(rowIndex); + // clear table selection + handler.getBenthosTable().clearSelection(); - String speciesStr = decorate(selectedRow.getSpecies()); - sendMessage(_("tutti.flash.info.benthos.remove.from.protocol", - speciesStr)); + // notify user + sendMessage(_("tutti.flash.info.benthos.remove.from.protocol")); } - } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveSpeciesProtocolAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveSpeciesProtocolAction.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/RemoveSpeciesProtocolAction.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -26,13 +26,16 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; +import jaxx.runtime.SwingUtil; import javax.swing.JTable; import java.util.Collections; import java.util.List; +import java.util.Set; import static org.nuiton.i18n.I18n._; @@ -44,6 +47,13 @@ */ public class RemoveSpeciesProtocolAction extends AbstractTuttiAction<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> { + /** + * Set of removed species. + * + * @since 2.8 + */ + protected Set<Species> removedSpecies; + public RemoveSpeciesProtocolAction(EditProtocolUIHandler handler) { super(handler, false); } @@ -56,33 +66,51 @@ // need to have a selection Preconditions.checkState(!table.getSelectionModel().isSelectionEmpty()); - // build the list of model row indexes from selected view row indexes - List<Integer> modelIndexes = Lists.newArrayList(); +// // build the list of model row indexes from selected view row indexes +// List<Integer> modelIndexes = Lists.newArrayList(); +// +// for (int index : SwingUtil.getSelectedModelRows(table)) { +// modelIndexes.add(index); +// } +// +// // sort it and reverse it (must delete first max row index) +// Collections.sort(modelIndexes); +// Collections.reverse(modelIndexes); - for (int index : table.getSelectedRows()) { - int modelRowIndex = table.convertRowIndexToModel(index); - modelIndexes.add(modelRowIndex); - } - - // sort it and reverse it (must delete first max row index) - Collections.sort(modelIndexes); - Collections.reverse(modelIndexes); - EditProtocolSpeciesTableModel tableModel = (EditProtocolSpeciesTableModel) table.getModel(); - for (Integer index : modelIndexes) { + EditProtocolUIModel model = getModel(); - removeRow(tableModel, index); + removedSpecies = Sets.newHashSet(); + + for (Integer rowIndex : SwingUtil.getSelectedModelRows(table)) { + +// removeRow(tableModel, index); + + // get row to remove + EditProtocolSpeciesRowModel selectedRow = + tableModel.getEntry(rowIndex); + + // re-add all synonym of this taxon to the species / benthos combobox + Species species = selectedRow.getSpecies(); + removedSpecies.add(species); + + Integer taxonId = species.getReferenceTaxonId(); + List<Species> allSynonyms = Lists.newArrayList( + model.getAllSynonyms(String.valueOf(taxonId))); + allSynonyms.remove(species); + model.getAllSynonyms().addAll(allSynonyms); + + // remove the row from the model + model.getSpeciesRow().remove(selectedRow); } - EditProtocolUIModel model = getModel(); - // reorder the list by name, otherwise, // all the species without a reftax code will be at the end Collections.sort(model.getAllSynonyms(), TuttiEntities.SPECIES_BY_NAME_COMPARATOR); - table.clearSelection(); +// table.clearSelection(); } protected void removeRow(EditProtocolSpeciesTableModel tableModel, int rowIndex) { @@ -109,4 +137,22 @@ speciesStr)); } + @Override + public void postSuccessAction() { + super.postSuccessAction(); + + // update comboboxes + getUI().getBenthosComboBox().addItems(removedSpecies); + getUI().getSpeciesComboBox().addItems(removedSpecies); + + // fire table data changed + handler.getBenthosTableModel().fireTableDataChanged(); + + // clear table selection + handler.getSpeciesTable().clearSelection(); + + // notify user + sendMessage(_("tutti.flash.info.species.remove.from.protocol")); + } + } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java 2013-10-14 17:42:10 UTC (rev 1297) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java 2013-10-14 18:30:59 UTC (rev 1298) @@ -32,6 +32,7 @@ import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.service.TuttiDecorator; import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableModel; +import jaxx.runtime.SwingUtil; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; @@ -46,6 +47,7 @@ import javax.swing.JPopupMenu; import javax.swing.JRadioButtonMenuItem; import javax.swing.JSeparator; +import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.table.JTableHeader; import java.awt.Color; @@ -305,8 +307,8 @@ return result; } - public static TuttiDecorator<Species> getSpeciesColumnDecorator(TableColumnExt tableColumn) { - TuttiDecorator<Species> decorator = (TuttiDecorator<Species>) + public static <E> TuttiDecorator<E> getSpeciesColumnDecorator(TableColumnExt tableColumn) { + TuttiDecorator<E> decorator = (TuttiDecorator<E>) tableColumn.getClientProperty(SPECIES_DECORATOR); return decorator; } @@ -352,6 +354,81 @@ } + public static void selectFirstCellOnFirstRowAndStopEditing(JXTable table) { + + // select first cell + doSelectCell(table, 0, 0); + + if (table.isEditing()) { + + // but no edit it + table.getCellEditor().stopCellEditing(); + } + } + + public static void selectFirstCellOnLastRow(JXTable table) { + + // select first cell + doSelectCell(table, table.getRowCount() - 1, 0); + } + + public static void selectFirstCellOnRow(JXTable table, int row, boolean stopEdit) { + + // select first cell + doSelectCell(table, row, 0); + + if (stopEdit && table.isEditing()) { + + table.getCellEditor().stopCellEditing(); + } + } + + public static void doSelectCell(JTable table, + int rowIndex, + int columnIndex) { + + int rowCount = table.getRowCount(); + if (rowCount == 0) { + + // no row, can not selected any cell + if (log.isWarnEnabled()) { + log.warn("No row in table, can not select any cell"); + } + return; + } + int columnCount = table.getColumnCount(); + if (columnCount == 0) { + + // no column, can not selected any cell + if (log.isWarnEnabled()) { + log.warn("No column in table, can not select any cell"); + } + return; + } + if (columnIndex > columnCount) { + if (log.isWarnEnabled()) { + log.warn(String.format("ColumnIndex: %s is more than columnCount %s", columnIndex, columnCount)); + } + columnIndex = columnCount - 1; + } + if (columnIndex < 0) { + columnIndex = 0; + } + if (rowIndex >= rowCount) { + if (log.isWarnEnabled()) { + log.warn(String.format("RowIndex: %s is more than rowCount %s", rowIndex, rowCount)); + } + rowIndex = rowCount - 1; + } + if (rowIndex < 0) { + rowIndex = 0; + } + + table.setColumnSelectionInterval(columnIndex, columnIndex); + table.setRowSelectionInterval(rowIndex, rowIndex); + table.editCellAt(rowIndex, columnIndex); + } + protected static class SpeciesDecoratorListener<R extends Serializable, T extends AbstractTuttiTableModel<R>> implements ActionListener { protected final JXTable table; @@ -393,11 +470,9 @@ T tableModel = (T) table.getModel(); // keep selected rows - int[] rowIndexes = table.getSelectedRows(); List<R> rowsToReSelect = Lists.newArrayList(); - for (int viewRowIndex : rowIndexes) { - int modelRowIndex = table.convertRowIndexToModel(viewRowIndex); - R row = tableModel.getEntry(modelRowIndex); + for (int rowIndex : SwingUtil.getSelectedModelRows(table)) { + R row = tableModel.getEntry(rowIndex); rowsToReSelect.add(row); } @@ -407,8 +482,7 @@ // reselect rows for (R row : rowsToReSelect) { int modelRowIndex = tableModel.getRowIndex(row); - int viewRowIndex = table.convertRowIndexToView(modelRowIndex); - table.addRowSelectionInterval(viewRowIndex, viewRowIndex); + SwingUtil.addRowSelectionInterval(table, modelRowIndex); } }