Skip to content

Commit

Permalink
Bug Fix: Editing reprocessed count/account name/multiplier cells requ…
Browse files Browse the repository at this point in the history
…ire "enter" to update (Issue #410)
  • Loading branch information
GoldenGnu committed Aug 14, 2023
1 parent f1ecd11 commit 7f6d8d1
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import net.nikr.eve.jeveasset.Program;
import net.nikr.eve.jeveasset.data.api.accounts.ApiType;
import net.nikr.eve.jeveasset.data.api.accounts.DeprecatedOwner;
Expand Down Expand Up @@ -62,6 +66,7 @@ public AccountSeparatorTableCell(final AccountManagerDialog accountManagerDialog
defaultColor = jPanel.getBackground();

ListenerClass listener = new ListenerClass();
addCellEditorListener(listener);

jSeparatorLabel = new JLabel();
jSeparatorLabel.setBackground(jTable.getBackground());
Expand All @@ -85,12 +90,20 @@ public AccountSeparatorTableCell(final AccountManagerDialog accountManagerDialog
jDelete.addActionListener(actionListener);

jAccountName = new JTextField();
jAccountName.addFocusListener(listener);
jAccountName.setBorder(null);
jAccountName.setOpaque(false);
jAccountName.setBackground(Colors.COMPONENT_TRANSPARENT.getColor());
jAccountName.setActionCommand(AccountCellAction.ACCOUNT_NAME.name());
jAccountName.addActionListener(listener);
jAccountName.addKeyListener(listener);
jAccountName.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
jAccountName.selectAll();
}
@Override
public void focusLost(FocusEvent e) { }
});

jInvalidLabel = new JLabel(DialoguesAccount.get().accountInvalid());

Expand Down Expand Up @@ -208,18 +221,26 @@ protected void configure(final Separator<?> separator) {
}
}

private class ListenerClass implements FocusListener, ActionListener {
private void setAccountName(OwnerType owner) {
if (owner == null) {
return;
}
if (jAccountName.getText().isEmpty()) {
owner.setResetAccountName();
} else {
owner.setAccountName(jAccountName.getText());
}
accountManagerDialog.forceUpdate();
}

private class ListenerClass implements ActionListener, CellEditorListener, KeyListener {

private boolean update = true;

@Override
public void actionPerformed(final ActionEvent e) {
if (AccountCellAction.ACCOUNT_NAME.name().equals(e.getActionCommand())) {
OwnerType owner = (OwnerType) currentSeparator.first();
if (jAccountName.getText().isEmpty()) {
owner.setResetAccountName();
} else {
owner.setAccountName(jAccountName.getText());
}
jAccountName.transferFocus();
accountManagerDialog.forceUpdate();
stopCellEditing();
expandSeparator(true);
int index = jTable.getSelectedRow() + 1;
if (jTable.getRowCount() >= index) {
Expand All @@ -229,15 +250,40 @@ public void actionPerformed(final ActionEvent e) {
}

@Override
public void focusGained(final FocusEvent e) {
if (e.getSource() instanceof JTextField) {
jTable.setRowSelectionInterval(currentRow, currentRow);
jAccountName.selectAll();
public void editingStopped(ChangeEvent e) {
saveAccountName();
}

@Override
public void editingCanceled(ChangeEvent e) {
saveAccountName();
}

@Override
public void keyTyped(KeyEvent e) { }

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
update = false;
stopCellEditing();
}
}

@Override
public void focusLost(final FocusEvent e) { }
}
public void keyReleased(KeyEvent e) { }

private void saveAccountName() {
if (!update) {
update = true;
return;
}
OwnerType owner = (OwnerType) currentSeparator.first();
if (owner == null) { // handle 'late' rendering calls after this separator is invalid
return;
}
setAccountName(owner);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
import ca.odell.glazedlists.SeparatorList.Separator;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import net.nikr.eve.jeveasset.Program;
import net.nikr.eve.jeveasset.data.settings.ColorEntry;
import net.nikr.eve.jeveasset.data.settings.ColorSettings;
Expand All @@ -36,6 +41,7 @@ public enum ReprocessedCellAction {

private final JLabel jColor;
private final JButton jRemove;
private final JButton jRemoveTotal;
private final JIntegerField jCount;
private final JLabel jName;
private final JLabel jSellPriceLabel;
Expand All @@ -44,9 +50,14 @@ public enum ReprocessedCellAction {
private final JLabel jBatchSize;
private final JLabel jValueLabel;
private final JLabel jValue;
private final Program program;

public ReprocessedSeparatorTableCell(final JTable jTable, final SeparatorList<ReprocessedInterface> separatorList, final ActionListener actionListener) {
public ReprocessedSeparatorTableCell(final Program program, final JTable jTable, final SeparatorList<ReprocessedInterface> separatorList, final ActionListener actionListener) {
super(jTable, separatorList);
this.program = program;

ListenerClass listener = new ListenerClass();
addCellEditorListener(listener);

jColor = new JLabel();
jColor.setOpaque(true);
Expand All @@ -57,11 +68,17 @@ public ReprocessedSeparatorTableCell(final JTable jTable, final SeparatorList<Re
jRemove.setActionCommand(ReprocessedCellAction.REMOVE.name());
jRemove.addActionListener(actionListener);

jRemoveTotal = new JButton(TabsReprocessed.get().remove());
jRemoveTotal.setOpaque(false);
jRemoveTotal.setEnabled(false);
jRemoveTotal.setVisible(false);

jCount = new JIntegerField("1", DocumentFactory.ValueFlag.POSITIVE_AND_NOT_ZERO);
jCount.setActionCommand(ReprocessedCellAction.UPDATE_COUNT.name());
jCount.addActionListener(actionListener);
jCount.setHorizontalAlignment(JTextField.RIGHT);
jCount.setAutoSelectAll(true);
jCount.setHorizontalAlignment(JTextField.RIGHT);
jCount.setActionCommand(ReprocessedCellAction.UPDATE_COUNT.name());
jCount.addActionListener(listener);
jCount.addKeyListener(listener);

JLabel jCountLabel = new JLabel(TabsReprocessed.get().multiplierSign());

Expand All @@ -88,6 +105,7 @@ public ReprocessedSeparatorTableCell(final JTable jTable, final SeparatorList<Re
.addComponent(jColor, Program.getButtonsHeight() - 6, Program.getButtonsHeight() - 6, Program.getButtonsHeight() - 6)
.addGap(10)
.addComponent(jRemove, Program.getButtonsWidth(), Program.getButtonsWidth(), Program.getButtonsWidth())
.addComponent(jRemoveTotal, Program.getButtonsWidth(), Program.getButtonsWidth(), Program.getButtonsWidth())
.addGap(10)
.addComponent(jCount, 50, 50, 50)
.addComponent(jCountLabel)
Expand Down Expand Up @@ -116,6 +134,7 @@ public ReprocessedSeparatorTableCell(final JTable jTable, final SeparatorList<Re
.addComponent(jColor, Program.getButtonsHeight() - 6, Program.getButtonsHeight() - 6, Program.getButtonsHeight() - 6)
)
.addComponent(jRemove, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
.addComponent(jRemoveTotal, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
.addComponent(jName, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
.addComponent(jCount, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
.addComponent(jCountLabel, Program.getButtonsHeight(), Program.getButtonsHeight(), Program.getButtonsHeight())
Expand All @@ -136,7 +155,8 @@ protected void configure(Separator<?> separator) {
if (material == null) { // handle 'late' rendering calls after this separator is invalid
return;
}
jRemove.setEnabled(!material.getTotal().isGrandTotal());
jRemove.setVisible(!material.getTotal().isGrandTotal());
jRemoveTotal.setVisible(material.getTotal().isGrandTotal());
jName.setText(material.getTotal().getTypeName());
//Count
jCount.setText(String.valueOf(material.getTotal().getCount()));
Expand Down Expand Up @@ -171,4 +191,53 @@ protected void configure(Separator<?> separator) {
ColorSettings.config(jColor, ColorEntry.REPROCESSED_EQUAL);
}
}

private class ListenerClass implements ActionListener, CellEditorListener, KeyListener {

private boolean update = true;

@Override
public void actionPerformed(ActionEvent e) {
if (ReprocessedCellAction.UPDATE_COUNT.name().equals(e.getActionCommand())) {
stopCellEditing();
}
}

@Override
public void editingStopped(ChangeEvent e) {
saveMultiplier();
}

@Override
public void editingCanceled(ChangeEvent e) {
saveMultiplier();
}

@Override
public void keyTyped(KeyEvent e) { }

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
update = false;
stopCellEditing();
}
}

@Override
public void keyReleased(KeyEvent e) { }

private void saveMultiplier() {
if (!update) {
update = true;
return;
}
ReprocessedInterface reprocessed = (ReprocessedInterface) currentSeparator.first();
if (reprocessed == null) { // handle 'late' rendering calls after this separator is invalid
return;
}
program.getReprocessedTab().setCount(reprocessed, jCount);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ public ReprocessedTab(final Program program) {
tableModel = EventModels.createTableModel(separatorList, tableFormat);
//Table
jTable = new JReprocessedTable(program, tableModel, separatorList);
jTable.setSeparatorRenderer(new ReprocessedSeparatorTableCell(jTable, separatorList, listener));
jTable.setSeparatorEditor(new ReprocessedSeparatorTableCell(jTable, separatorList, listener));
jTable.setSeparatorRenderer(new ReprocessedSeparatorTableCell(program, jTable, separatorList, listener));
jTable.setSeparatorEditor(new ReprocessedSeparatorTableCell(program, jTable, separatorList, listener));
jTable.setCellSelectionEnabled(true);
PaddingTableCellRenderer.install(jTable, 3);
//Sorting
Expand Down Expand Up @@ -273,6 +273,27 @@ public void add(final Map<Item, Long> newItems) {
}
}

protected void setCount(ReprocessedInterface reprocessed, JTextField jCount) {
if (reprocessed == null) {
return;
}
ReprocessedTotal total = reprocessed.getTotal();
long count;
try {
count = Long.parseLong(jCount.getText());
} catch (NumberFormatException ex) {
count = 1;
}
if (count != total.getCount()) {
Item item = total.getItem();
if (!total.isGrandTotal()) {
items.put(item, count);
}
total.setCount(count);
tableModel.fireTableDataChanged();
}
}

private ReprocessedInterface getSelectedReprocessed() {
int index = jTable.getSelectedRow();
if (index < 0 || index >= tableModel.getRowCount()) {
Expand Down Expand Up @@ -356,28 +377,6 @@ public void actionPerformed(final ActionEvent e) {
items.put(selectedItem, 1L);
reprocessedData.addItem(eventList, selectedItem, 1L);
}
} else if (ReprocessedCellAction.UPDATE_COUNT.name().equals(e.getActionCommand())) {
Object source = e.getSource();
ReprocessedInterface reprocessed = getSelectedReprocessed();
if (reprocessed != null && source instanceof JTextField) {
ReprocessedTotal total = reprocessed.getTotal();
JTextField jCount = (JTextField) source;
long count;
try {
count = Long.parseLong(jCount.getText());
} catch (NumberFormatException ex) {
count = 1;
}
Item item = total.getItem();
Long previous = total.getCount();
if (count != previous) {
if (!total.isGrandTotal()) {
items.put(item, count);
}
total.setCount(count);
tableModel.fireTableDataChanged();
}
}
} else if (ReprocessedAction.IMPORT_EFT.name().equals(e.getActionCommand())) { //Add stockpile (EFT Import)
importText(TextImportType.EFT);
} else if (ReprocessedAction.IMPORT_ISK_PER_HOUR.name().equals(e.getActionCommand())) { //Add stockpile (Isk Per Hour)
Expand Down
Loading

0 comments on commit 7f6d8d1

Please # to comment.