From 3fb377462775e9cefdf437488897d38a6e3f15ba Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 23 Mar 2015 12:57:52 +0100 Subject: [PATCH] lib/core installer: scrolling with arrow keys almost works --- .../ui/ContributedLibraryTableCell.java | 6 +-- .../ui/ContributedPlatformTableCell.java | 6 +-- .../contributions/ui/InstallerJDialog.java | 32 +++++++++---- .../ui/listeners/AbstractKeyListener.java | 23 +++++++++ .../ui/listeners/DelegatingKeyListener.java | 47 +++++++++++++++++++ 5 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 app/src/cc/arduino/contributions/ui/listeners/AbstractKeyListener.java create mode 100644 app/src/cc/arduino/contributions/ui/listeners/DelegatingKeyListener.java diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index 198ee83f6..63de0366d 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -35,6 +35,7 @@ import cc.arduino.contributions.libraries.ContributedLibraryComparator; import cc.arduino.contributions.libraries.filters.BuiltInPredicate; import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate; import cc.arduino.contributions.ui.InstallerTableCell; +import cc.arduino.contributions.ui.listeners.DelegatingKeyListener; import cc.arduino.utils.ReverseComparator; import com.google.common.base.Function; import com.google.common.base.Predicates; @@ -73,7 +74,6 @@ public class ContributedLibraryTableCell extends InstallerTableCell { private JComboBox versionToInstallChooser; private JButton downgradeButton; private JPanel buttonsPanel; - private Component removeButtonStrut; private JPanel inactiveButtonsPanel; private JLabel statusLabel; @@ -155,8 +155,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { buttonsPanel.add(installButton); buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(removeButton); - removeButtonStrut = Box.createHorizontalStrut(5); - buttonsPanel.add(removeButtonStrut); + buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(Box.createHorizontalStrut(15)); panel.add(buttonsPanel); @@ -211,6 +210,7 @@ public class ContributedLibraryTableCell extends InstallerTableCell { } } }); + description.addKeyListener(new DelegatingKeyListener(parentTable)); panel.add(description, 0); return description; } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java index ff151e616..881f27e8d 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java @@ -34,6 +34,7 @@ import cc.arduino.contributions.packages.ContributedBoard; import cc.arduino.contributions.packages.ContributedPlatform; import cc.arduino.contributions.packages.ContributedPlatformComparator; import cc.arduino.contributions.ui.InstallerTableCell; +import cc.arduino.contributions.ui.listeners.DelegatingKeyListener; import cc.arduino.utils.ReverseComparator; import com.google.common.base.Function; import com.google.common.base.Predicates; @@ -71,7 +72,6 @@ public class ContributedPlatformTableCell extends InstallerTableCell { private JComboBox versionToInstallChooser; private JButton downgradeButton; private JPanel buttonsPanel; - private Component removeButtonStrut; private JPanel inactiveButtonsPanel; private JLabel statusLabel; @@ -153,8 +153,7 @@ public class ContributedPlatformTableCell extends InstallerTableCell { buttonsPanel.add(installButton); buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(removeButton); - removeButtonStrut = Box.createHorizontalStrut(5); - buttonsPanel.add(removeButtonStrut); + buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(Box.createHorizontalStrut(15)); panel.add(buttonsPanel); @@ -209,6 +208,7 @@ public class ContributedPlatformTableCell extends InstallerTableCell { } } }); + description.addKeyListener(new DelegatingKeyListener(parentTable)); panel.add(description, 0); return description; } diff --git a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java index 91ec2822a..56fd4cd82 100644 --- a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java +++ b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java @@ -28,6 +28,7 @@ */ package cc.arduino.contributions.ui; +import cc.arduino.contributions.ui.listeners.AbstractKeyListener; import com.google.common.base.Predicate; import processing.app.Base; import processing.app.Theme; @@ -39,6 +40,7 @@ import javax.swing.table.TableColumnModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import static cc.arduino.contributions.packages.ui.ContributionIndexTableModel.DESCRIPTION_COL; @@ -78,8 +80,6 @@ public abstract class InstallerJDialog extends JDialog { protected Box errorMessageBox; private final JLabel errorMessage; - protected InstallerTableCell cellEditor; - public InstallerJDialog(Frame parent, String title, ModalityType applicationModal, String noConnectionErrorMessage) { super(parent, title, applicationModal); this.noConnectionErrorMessage = noConnectionErrorMessage; @@ -104,7 +104,9 @@ public abstract class InstallerJDialog extends JDialog { @Override protected void onFilter(String[] _filters) { filters = _filters; - cellEditor.stopCellEditing(); + if (contribTable.getCellEditor() != null) { + contribTable.getCellEditor().stopCellEditing(); + } contribModel.updateIndexFilter(categoryFilter, filters); } }; @@ -130,13 +132,23 @@ public abstract class InstallerJDialog extends JDialog { contribTable.setIntercellSpacing(new Dimension(0, 1)); contribTable.setShowVerticalLines(false); contribTable.setSelectionBackground(Theme.getColor("status.notice.bgcolor")); + contribTable.addKeyListener(new AbstractKeyListener() { + + @Override + public void keyReleased(KeyEvent keyEvent) { + if (keyEvent.getKeyCode() != keyEvent.VK_DOWN && keyEvent.getKeyCode() != KeyEvent.VK_UP) { + return; + } + + contribTable.editCellAt(contribTable.getSelectedRow(), contribTable.getSelectedColumn()); + } + }); { TableColumnModel tcm = contribTable.getColumnModel(); TableColumn col = tcm.getColumn(DESCRIPTION_COL); col.setCellRenderer(createCellRenderer()); - cellEditor = createCellEditor(); - col.setCellEditor(cellEditor); + col.setCellEditor(createCellEditor()); col.setResizable(true); } @@ -241,8 +253,10 @@ public abstract class InstallerJDialog extends JDialog { categoryChooser.setEnabled(!visible); contribTable.setEnabled(!visible); errorMessageBox.setVisible(false); - cellEditor.setEnabled(!visible); - cellEditor.setStatus(status); + if (contribTable.getCellEditor() != null) { + ((InstallerTableCell) contribTable.getCellEditor()).setEnabled(!visible); + ((InstallerTableCell) contribTable.getCellEditor()).setStatus(status); + } } protected ActionListener categoryChooserActionListener = new ActionListener() { @@ -252,7 +266,9 @@ public abstract class InstallerJDialog extends JDialog { DropdownItem selected = (DropdownItem) categoryChooser.getSelectedItem(); if (categoryFilter == null || !categoryFilter.equals(selected)) { categoryFilter = selected.getFilterPredicate(); - cellEditor.stopCellEditing(); + if (contribTable.getCellEditor() != null) { + contribTable.getCellEditor().stopCellEditing(); + } contribModel.updateIndexFilter(categoryFilter, filters); } } diff --git a/app/src/cc/arduino/contributions/ui/listeners/AbstractKeyListener.java b/app/src/cc/arduino/contributions/ui/listeners/AbstractKeyListener.java new file mode 100644 index 000000000..9a2607cf5 --- /dev/null +++ b/app/src/cc/arduino/contributions/ui/listeners/AbstractKeyListener.java @@ -0,0 +1,23 @@ +package cc.arduino.contributions.ui.listeners; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public abstract class AbstractKeyListener implements KeyListener { + + @Override + public void keyTyped(KeyEvent keyEvent) { + + } + + @Override + public void keyPressed(KeyEvent keyEvent) { + + } + + @Override + public void keyReleased(KeyEvent keyEvent) { + + } + +} diff --git a/app/src/cc/arduino/contributions/ui/listeners/DelegatingKeyListener.java b/app/src/cc/arduino/contributions/ui/listeners/DelegatingKeyListener.java new file mode 100644 index 000000000..9b7dc9b1d --- /dev/null +++ b/app/src/cc/arduino/contributions/ui/listeners/DelegatingKeyListener.java @@ -0,0 +1,47 @@ +package cc.arduino.contributions.ui.listeners; + +import java.awt.*; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class DelegatingKeyListener implements KeyListener { + + private final Component delegate; + + public DelegatingKeyListener(Component delegate) { + this.delegate = delegate; + } + + @Override + public void keyTyped(final KeyEvent keyEvent) { + if (delegate.getKeyListeners() == null) { + return; + } + + for (KeyListener listener : delegate.getKeyListeners()) { + listener.keyTyped(keyEvent); + } + } + + @Override + public void keyPressed(KeyEvent keyEvent) { + if (delegate.getKeyListeners() == null) { + return; + } + + for (KeyListener listener : delegate.getKeyListeners()) { + listener.keyPressed(keyEvent); + } + } + + @Override + public void keyReleased(KeyEvent keyEvent) { + if (delegate.getKeyListeners() == null) { + return; + } + + for (KeyListener listener : delegate.getKeyListeners()) { + listener.keyReleased(keyEvent); + } + } +}