Author: tchemit Date: 2013-03-21 12:28:04 +0100 (Thu, 21 Mar 2013) New Revision: 663 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/663 Log: continue refactor Added: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyCellComponent.java Removed: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/FrequencyCellComponent.java Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java 2013-03-20 23:23:30 UTC (rev 662) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/SpeciesBatchUIHandler.java 2013-03-21 11:28:04 UTC (rev 663) @@ -45,7 +45,7 @@ import fr.ifremer.tutti.ui.swing.content.operation.catches.SampleCategoryComponent; import fr.ifremer.tutti.ui.swing.content.operation.catches.SampleCategoryType; import fr.ifremer.tutti.ui.swing.content.operation.catches.TableViewMode; -import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.FrequencyCellComponent; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyCellComponent; import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyRowModel; import fr.ifremer.tutti.ui.swing.content.operation.catches.species.split.CreateSpeciesBatchUI; import fr.ifremer.tutti.ui.swing.content.operation.catches.species.split.CreateSpeciesBatchUIModel; @@ -519,8 +519,8 @@ { // Number column (from frequencies) addColumnToModel(columnModel, - FrequencyCellComponent.newEditor(ui, computedDataColor), - FrequencyCellComponent.newRender(computedDataColor), + SpeciesFrequencyCellComponent.newEditor(ui, computedDataColor), + SpeciesFrequencyCellComponent.newRender(computedDataColor), SpeciesBatchTableModel.COMPUTED_NUMBER); } Deleted: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/FrequencyCellComponent.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/FrequencyCellComponent.java 2013-03-20 23:23:30 UTC (rev 662) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/FrequencyCellComponent.java 2013-03-21 11:28:04 UTC (rev 663) @@ -1,303 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency; - -/* - * #%L - * Tutti :: UI - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 Ifremer - * %% - * 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 3 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, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI; -import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIHandler; -import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchRowModel; -import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchTableModel; -import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchUI; -import fr.ifremer.tutti.ui.swing.util.TuttiComputedOrNotData; -import fr.ifremer.tutti.ui.swing.util.table.AbstractSelectTableAction; -import fr.ifremer.tutti.ui.swing.util.table.ColumnIdentifier; -import jaxx.runtime.SwingUtil; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import javax.swing.AbstractCellEditor; -import javax.swing.JTable; -import javax.swing.border.LineBorder; -import javax.swing.table.DefaultTableCellRenderer; -import javax.swing.table.TableCellEditor; -import javax.swing.table.TableCellRenderer; -import java.awt.Color; -import java.awt.Component; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.util.List; - -/** - * Component to render and edit frequency stuff from batch table. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.2 - */ -public class FrequencyCellComponent extends DefaultTableCellRenderer { - - private static final long serialVersionUID = 1L; - - public static final String ROW_INDEX = "rowIndex"; - - protected Color computedDataColor; - - /** Logger. */ - private static final Log log = - LogFactory.getLog(FrequencyCellComponent.class); - - public FrequencyCellComponent(Color computedDataColor) { - setHorizontalAlignment(CENTER); - setIcon(SwingUtil.createActionIcon("show-frequency")); - this.computedDataColor = computedDataColor; - } - - public void setComputedOrNotText(TuttiComputedOrNotData<Integer> data) { - String text; - - if (data != null && data.getData() != null) { - text = String.valueOf(data.getData()); - - } else if (data != null - && data.getComputedData() != null - && data.getComputedData() != 0) { - - String blue = Integer.toHexString(computedDataColor.getRGB()).substring(2); - text = "<html><em style='color: #" + blue + "'>" + data.getComputedData() + "</em></html>"; - - } else { - text = " - "; - } - setText(text); - setToolTipText(text); - } - - public static TableCellRenderer newRender(Color computedDataColor) { - return new FrequencyCellRenderer(computedDataColor); - } - - public static TableCellEditor newEditor(SpeciesBatchUI ui, Color computedDataColor) { - return new FrequencyCellEditor(ui, computedDataColor); - } - - public static class FrequencyCellEditor extends AbstractCellEditor implements TableCellEditor { - - private static final long serialVersionUID = 1L; - - protected final FrequencyCellComponent component; - - protected final SpeciesBatchUI ui; - - protected JTable table; - - protected SpeciesBatchTableModel tableModel; - - protected ColumnIdentifier<SpeciesBatchRowModel> columnIdentifier; - - protected SpeciesBatchRowModel editRow; - - protected Integer rowIndex; - - protected Integer columnIndex; - - public FrequencyCellEditor(SpeciesBatchUI ui, Color computedDataColor) { - this.ui = ui; - component = new FrequencyCellComponent(computedDataColor); - component.setBorder(new LineBorder(Color.BLACK)); - component.addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_ENTER || - e.getKeyCode() == KeyEvent.VK_SPACE) { - e.consume(); - startEdit(); - } - } - }); - - component.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - e.consume(); - startEdit(); - } - }); - } - - protected void startEdit() { - - Preconditions.checkNotNull(tableModel, "No table model assigned."); - - // open frequency dialog - - Preconditions.checkNotNull(editRow, "No editRow found."); - - if (log.isInfoEnabled()) { - log.info("Will edit frequencies for row: " + rowIndex); - } - - EditCatchesUI parent = SwingUtil.getParentContainer(ui, EditCatchesUI.class); - SpeciesFrequencyUI frequencyEditor = parent.getSpeciesTabFrequencyEditor(); - - frequencyEditor.getHandler().editBatch(editRow, this); - - // open frequency editor - parent.getHandler().setSpeciesSelectedCard(EditCatchesUIHandler.EDIT_FREQUENCY_CARD); - } - - public void validateEdition(SpeciesFrequencyUIModel frequencyModel) { - if (frequencyModel.isValid()) { - - // at close, synch back frequencies - List<SpeciesFrequencyRowModel> frequency = Lists.newArrayList(); - - if (frequencyModel.isSimpleCountingMode()) { - editRow.setNumber(frequencyModel.getSimpleCount()); - - } else { - // transfer rows to editor - - for (SpeciesFrequencyRowModel row : frequencyModel.getRows()) { - if (row.isValid()) { - - // can keep this row - frequency.add(row); - } - } - if (log.isInfoEnabled()) { - log.info("Push back " + frequency.size() + - " frequency to batch " + frequencyModel.getBatch()); - } - - // push back to batch - editRow.setNumber(null); - - } - - editRow.setFrequency(frequency); - // update frequencies total - ui.getHandler().updateTotalFromFrequencies(editRow); - - } - - int r = rowIndex; - int c = columnIndex; - - // stop edition - stopCellEditing(); - - // reselect this cell - AbstractSelectTableAction.doSelectCell(table, r, c); - table.requestFocus(); - } - - @Override - public Component getTableCellEditorComponent(JTable table, - Object value, - boolean isSelected, - int row, - int column) { - tableModel = (SpeciesBatchTableModel) table.getModel(); - this.table = table; - columnIdentifier = SpeciesBatchTableModel.COMPUTED_NUMBER; - if (log.isDebugEnabled()) { - log.debug("Set columnIdentifier (" + column + ") :: " + columnIdentifier.getPropertyName()); - } - TuttiComputedOrNotData<Integer> data = (TuttiComputedOrNotData<Integer>) value; - component.setComputedOrNotText(data); - - rowIndex = row; - columnIndex = column; - - editRow = tableModel.getEntry(row); - - return component; - } - - @Override - public Object getCellEditorValue() { - - Preconditions.checkNotNull(editRow, "No editRow found in editor."); - - Object result = null; - if (columnIdentifier == SpeciesBatchTableModel.COMPUTED_NUMBER) { - result = editRow.getComputedOrNotNumber(); - } - if (log.isDebugEnabled()) { - log.debug("editor value (" + columnIdentifier + "): " + result); - } - - return result; - } - -// @Override -// public boolean stopCellEditing() { -// boolean b = super.stopCellEditing(); -// if (b) { -// rowIndex = null; -// editRow = null; -// columnIndex = null; -// } -// return b; -// } - - @Override - public void cancelCellEditing() { - super.cancelCellEditing(); - rowIndex = null; - columnIndex = null; - editRow = null; - } - } - - public static class FrequencyCellRenderer implements TableCellRenderer { - - protected final FrequencyCellComponent component; - - public FrequencyCellRenderer(Color computedDataColor) { - component = new FrequencyCellComponent(computedDataColor); - } - - @Override - public Component getTableCellRendererComponent(JTable table, - Object value, - boolean isSelected, - boolean hasFocus, - int row, - int column) { - - TuttiComputedOrNotData<Integer> data = (TuttiComputedOrNotData<Integer>) value; - FrequencyCellComponent result = - (FrequencyCellComponent) component.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); - - boolean editable = table.isCellEditable(row, column); - result.setEnabled(editable); - result.setComputedOrNotText(data); - return result; - } - } -} Copied: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyCellComponent.java (from rev 662, trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/FrequencyCellComponent.java) =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyCellComponent.java (rev 0) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyCellComponent.java 2013-03-21 11:28:04 UTC (rev 663) @@ -0,0 +1,303 @@ +package fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency; + +/* + * #%L + * Tutti :: UI + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Ifremer + * %% + * 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 3 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, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI; +import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIHandler; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchRowModel; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchTableModel; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchUI; +import fr.ifremer.tutti.ui.swing.util.TuttiComputedOrNotData; +import fr.ifremer.tutti.ui.swing.util.table.AbstractSelectTableAction; +import fr.ifremer.tutti.ui.swing.util.table.ColumnIdentifier; +import jaxx.runtime.SwingUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.swing.AbstractCellEditor; +import javax.swing.JTable; +import javax.swing.border.LineBorder; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableCellRenderer; +import java.awt.Color; +import java.awt.Component; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.List; + +/** + * Component to render and edit frequency stuff from batch table. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.2 + */ +public class SpeciesFrequencyCellComponent extends DefaultTableCellRenderer { + + private static final long serialVersionUID = 1L; + + public static final String ROW_INDEX = "rowIndex"; + + protected Color computedDataColor; + + /** Logger. */ + private static final Log log = + LogFactory.getLog(SpeciesFrequencyCellComponent.class); + + public SpeciesFrequencyCellComponent(Color computedDataColor) { + setHorizontalAlignment(CENTER); + setIcon(SwingUtil.createActionIcon("show-frequency")); + this.computedDataColor = computedDataColor; + } + + public void setComputedOrNotText(TuttiComputedOrNotData<Integer> data) { + String text; + + if (data != null && data.getData() != null) { + text = String.valueOf(data.getData()); + + } else if (data != null + && data.getComputedData() != null + && data.getComputedData() != 0) { + + String blue = Integer.toHexString(computedDataColor.getRGB()).substring(2); + text = "<html><em style='color: #" + blue + "'>" + data.getComputedData() + "</em></html>"; + + } else { + text = " - "; + } + setText(text); + setToolTipText(text); + } + + public static TableCellRenderer newRender(Color computedDataColor) { + return new FrequencyCellRenderer(computedDataColor); + } + + public static TableCellEditor newEditor(SpeciesBatchUI ui, Color computedDataColor) { + return new FrequencyCellEditor(ui, computedDataColor); + } + + public static class FrequencyCellEditor extends AbstractCellEditor implements TableCellEditor { + + private static final long serialVersionUID = 1L; + + protected final SpeciesFrequencyCellComponent component; + + protected final SpeciesBatchUI ui; + + protected JTable table; + + protected SpeciesBatchTableModel tableModel; + + protected ColumnIdentifier<SpeciesBatchRowModel> columnIdentifier; + + protected SpeciesBatchRowModel editRow; + + protected Integer rowIndex; + + protected Integer columnIndex; + + public FrequencyCellEditor(SpeciesBatchUI ui, Color computedDataColor) { + this.ui = ui; + component = new SpeciesFrequencyCellComponent(computedDataColor); + component.setBorder(new LineBorder(Color.BLACK)); + component.addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER || + e.getKeyCode() == KeyEvent.VK_SPACE) { + e.consume(); + startEdit(); + } + } + }); + + component.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + e.consume(); + startEdit(); + } + }); + } + + protected void startEdit() { + + Preconditions.checkNotNull(tableModel, "No table model assigned."); + + // open frequency dialog + + Preconditions.checkNotNull(editRow, "No editRow found."); + + if (log.isInfoEnabled()) { + log.info("Will edit frequencies for row: " + rowIndex); + } + + EditCatchesUI parent = SwingUtil.getParentContainer(ui, EditCatchesUI.class); + SpeciesFrequencyUI frequencyEditor = parent.getSpeciesTabFrequencyEditor(); + + frequencyEditor.getHandler().editBatch(editRow, this); + + // open frequency editor + parent.getHandler().setSpeciesSelectedCard(EditCatchesUIHandler.EDIT_FREQUENCY_CARD); + } + + public void validateEdition(SpeciesFrequencyUIModel frequencyModel) { + if (frequencyModel.isValid()) { + + // at close, synch back frequencies + List<SpeciesFrequencyRowModel> frequency = Lists.newArrayList(); + + if (frequencyModel.isSimpleCountingMode()) { + editRow.setNumber(frequencyModel.getSimpleCount()); + + } else { + // transfer rows to editor + + for (SpeciesFrequencyRowModel row : frequencyModel.getRows()) { + if (row.isValid()) { + + // can keep this row + frequency.add(row); + } + } + if (log.isInfoEnabled()) { + log.info("Push back " + frequency.size() + + " frequency to batch " + frequencyModel.getBatch()); + } + + // push back to batch + editRow.setNumber(null); + + } + + editRow.setFrequency(frequency); + // update frequencies total + ui.getHandler().updateTotalFromFrequencies(editRow); + + } + + int r = rowIndex; + int c = columnIndex; + + // stop edition + stopCellEditing(); + + // reselect this cell + AbstractSelectTableAction.doSelectCell(table, r, c); + table.requestFocus(); + } + + @Override + public Component getTableCellEditorComponent(JTable table, + Object value, + boolean isSelected, + int row, + int column) { + tableModel = (SpeciesBatchTableModel) table.getModel(); + this.table = table; + columnIdentifier = SpeciesBatchTableModel.COMPUTED_NUMBER; + if (log.isDebugEnabled()) { + log.debug("Set columnIdentifier (" + column + ") :: " + columnIdentifier.getPropertyName()); + } + TuttiComputedOrNotData<Integer> data = (TuttiComputedOrNotData<Integer>) value; + component.setComputedOrNotText(data); + + rowIndex = row; + columnIndex = column; + + editRow = tableModel.getEntry(row); + + return component; + } + + @Override + public Object getCellEditorValue() { + + Preconditions.checkNotNull(editRow, "No editRow found in editor."); + + Object result = null; + if (columnIdentifier == SpeciesBatchTableModel.COMPUTED_NUMBER) { + result = editRow.getComputedOrNotNumber(); + } + if (log.isDebugEnabled()) { + log.debug("editor value (" + columnIdentifier + "): " + result); + } + + return result; + } + +// @Override +// public boolean stopCellEditing() { +// boolean b = super.stopCellEditing(); +// if (b) { +// rowIndex = null; +// editRow = null; +// columnIndex = null; +// } +// return b; +// } + + @Override + public void cancelCellEditing() { + super.cancelCellEditing(); + rowIndex = null; + columnIndex = null; + editRow = null; + } + } + + public static class FrequencyCellRenderer implements TableCellRenderer { + + protected final SpeciesFrequencyCellComponent component; + + public FrequencyCellRenderer(Color computedDataColor) { + component = new SpeciesFrequencyCellComponent(computedDataColor); + } + + @Override + public Component getTableCellRendererComponent(JTable table, + Object value, + boolean isSelected, + boolean hasFocus, + int row, + int column) { + + TuttiComputedOrNotData<Integer> data = (TuttiComputedOrNotData<Integer>) value; + SpeciesFrequencyCellComponent result = + (SpeciesFrequencyCellComponent) component.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + + boolean editable = table.isCellEditable(row, column); + result.setEnabled(editable); + result.setComputedOrNotText(data); + return result; + } + } +} Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java 2013-03-20 23:23:30 UTC (rev 662) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/species/frequency/SpeciesFrequencyUIHandler.java 2013-03-21 11:28:04 UTC (rev 663) @@ -37,8 +37,7 @@ import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI; import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIHandler; import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchRowModel; -import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchUIHandler; -import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.FrequencyCellComponent.FrequencyCellEditor; +import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyCellComponent.FrequencyCellEditor; import fr.ifremer.tutti.ui.swing.util.Cancelable; import fr.ifremer.tutti.ui.swing.util.TuttiBeanMonitor; import fr.ifremer.tutti.ui.swing.util.TuttiUI;