From 926a8c9101348cc6a26c09f47b4ded3c9237e64a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 24 Dec 2015 16:57:17 +0100 Subject: [PATCH 01/16] Correct implementation of ContributedLibraryTableCell getTableCellRendererComponent() interface requires to return independent Component objects used for "stamping" the table element. --- .../ui/ContributedLibraryTableCell.java | 443 ++++++++++-------- 1 file changed, 240 insertions(+), 203 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index 54cd56718e6..9a3ab82a1ec 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -29,6 +29,35 @@ package cc.arduino.contributions.libraries.ui; +import static processing.app.I18n.format; +import static processing.app.I18n.tr; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTable; +import javax.swing.JTextPane; +import javax.swing.Timer; +import javax.swing.border.EmptyBorder; +import javax.swing.event.HyperlinkEvent; +import javax.swing.text.Document; +import javax.swing.text.html.HTMLDocument; +import javax.swing.text.html.StyleSheet; + import cc.arduino.contributions.DownloadableContributionVersionComparator; import cc.arduino.contributions.VersionComparator; import cc.arduino.contributions.filters.BuiltInPredicate; @@ -36,74 +65,60 @@ import cc.arduino.contributions.libraries.ContributedLibrary; 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 processing.app.Base; -import javax.swing.*; -import javax.swing.border.EmptyBorder; -import javax.swing.event.HyperlinkEvent; -import javax.swing.text.Document; -import javax.swing.text.html.HTMLDocument; -import javax.swing.text.html.StyleSheet; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.stream.Collectors; - -import static processing.app.I18n.format; -import static processing.app.I18n.tr; - @SuppressWarnings("serial") public class ContributedLibraryTableCell extends InstallerTableCell { - private final JPanel panel; - private final JButton installButton; - private final Component installButtonPlaceholder; - private JComboBox downgradeChooser; - private final JComboBox versionToInstallChooser; - private final JButton downgradeButton; - private final JPanel buttonsPanel; - private final JPanel inactiveButtonsPanel; - private final JLabel statusLabel; - - public ContributedLibraryTableCell() { - { + class Cell { + private final JPanel panel; + private final JButton installButton; + private final Component installButtonPlaceholder; + private JComboBox downgradeChooser; + private final JComboBox versionToInstallChooser; + private final JButton downgradeButton; + private final JPanel buttonsPanel; + private final JPanel inactiveButtonsPanel; + private final JLabel statusLabel; + + public Cell() { installButton = new JButton(tr("Install")); - installButton.addActionListener(e -> onInstall(editorValue.getSelected(), editorValue.getInstalled())); + installButton + .addActionListener(e -> onInstall(editorValue.getSelected(), + editorValue.getInstalled())); int width = installButton.getPreferredSize().width; installButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); - } - downgradeButton = new JButton(tr("Install")); - downgradeButton.addActionListener(e -> { - ContributedLibrary selected = (ContributedLibrary) downgradeChooser.getSelectedItem(); - onInstall(selected, editorValue.getInstalled()); - }); + downgradeButton = new JButton(tr("Install")); + downgradeButton.addActionListener(e -> { + ContributedLibrary selected = (ContributedLibrary) downgradeChooser + .getSelectedItem(); + onInstall(selected, editorValue.getInstalled()); + }); + + downgradeChooser = new JComboBox(); + downgradeChooser.addItem("-"); + downgradeChooser.setMaximumSize(downgradeChooser.getPreferredSize()); + downgradeChooser.addItemListener(e -> { + Object selectVersionItem = downgradeChooser.getItemAt(0); + boolean disableDowngrade = (e.getItem() == selectVersionItem); + downgradeButton.setEnabled(!disableDowngrade); + }); + + versionToInstallChooser = new JComboBox(); + versionToInstallChooser.addItem("-"); + versionToInstallChooser + .setMaximumSize(versionToInstallChooser.getPreferredSize()); + versionToInstallChooser.addItemListener(e -> editorValue + .select((ContributedLibrary) versionToInstallChooser + .getSelectedItem())); + + panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + + makeNewDescription(panel); - downgradeChooser = new JComboBox(); - downgradeChooser.addItem("-"); - downgradeChooser.setMaximumSize(downgradeChooser.getPreferredSize()); - downgradeChooser.addItemListener(e -> { - Object selectVersionItem = downgradeChooser.getItemAt(0); - boolean disableDowngrade = (e.getItem() == selectVersionItem); - downgradeButton.setEnabled(!disableDowngrade); - }); - - versionToInstallChooser = new JComboBox(); - versionToInstallChooser.addItem("-"); - versionToInstallChooser.setMaximumSize(versionToInstallChooser.getPreferredSize()); - versionToInstallChooser.addItemListener(e -> editorValue.select((ContributedLibrary) versionToInstallChooser.getSelectedItem())); - - panel = new JPanel(); - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - - makeNewDescription(panel); - - { buttonsPanel = new JPanel(); buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); buttonsPanel.setOpaque(false); @@ -122,11 +137,10 @@ public ContributedLibraryTableCell() { buttonsPanel.add(Box.createHorizontalStrut(15)); panel.add(buttonsPanel); - } - { inactiveButtonsPanel = new JPanel(); - inactiveButtonsPanel.setLayout(new BoxLayout(inactiveButtonsPanel, BoxLayout.X_AXIS)); + inactiveButtonsPanel + .setLayout(new BoxLayout(inactiveButtonsPanel, BoxLayout.X_AXIS)); inactiveButtonsPanel.setOpaque(false); int height = installButton.getMinimumSize().height; @@ -138,12 +152,117 @@ public ContributedLibraryTableCell() { inactiveButtonsPanel.add(Box.createHorizontalStrut(15)); panel.add(inactiveButtonsPanel); + + panel.add(Box.createVerticalStrut(15)); } - panel.add(Box.createVerticalStrut(15)); + private void update(Object value, boolean isSelected, int row, + boolean hasBuiltInRelease) { + ContributedLibraryReleases releases = (ContributedLibraryReleases) value; + + JTextPane description = makeNewDescription(panel); + + // FIXME: happens on macosx, don't know why + if (releases == null) + return; + + ContributedLibrary selected = releases.getSelected(); + ContributedLibrary installed = releases.getInstalled(); + + boolean installable, upgradable; + if (installed == null) { + installable = true; + upgradable = false; + } else { + installable = false; + upgradable = new DownloadableContributionVersionComparator() + .compare(selected, installed) > 0; + } + if (installable) { + installButton.setText(tr("Install")); + } + if (upgradable) { + installButton.setText(tr("Update")); + } + installButton.setVisible(installable || upgradable); + installButtonPlaceholder.setVisible(!(installable || upgradable)); + + String name = selected.getName(); + String author = selected.getAuthor(); + // String maintainer = selectedLib.getMaintainer(); + String website = selected.getWebsite(); + String sentence = selected.getSentence(); + String paragraph = selected.getParagraph(); + // String availableVer = selectedLib.getVersion(); + // String url = selected.getUrl(); + + String midcolor = isSelected ? "#000000" : "#888888"; + + String desc = ""; + + // Library name... + desc += format("{0}", name); + if (installed != null && installed.isReadOnly()) { + desc += " Built-In "; + } + + // ...author... + desc += format("", midcolor); + if (author != null && !author.isEmpty()) { + desc += format(" by {0}", author); + } + + // ...version. + if (installed != null) { + String installedVer = installed.getParsedVersion(); + if (installedVer == null) { + desc += " " + tr("Version unknown"); + } else { + desc += " " + format(tr("Version {0}"), installedVer); + } + } + desc += ""; + + if (installed != null) { + desc += " INSTALLED"; + } + + desc += "
"; + + // Description + if (sentence != null) { + desc += format("{0} ", sentence); + if (paragraph != null && !paragraph.isEmpty()) + desc += format("{0}", paragraph); + desc += "
"; + } + if (author != null && !author.isEmpty()) { + desc += format("More info", website); + } + + desc += ""; + description.setText(desc); + description.setBackground(Color.WHITE); + + // for modelToView to work, the text area has to be sized. It doesn't + // matter if it's visible or not. + + // See: + // http://stackoverflow.com/questions/3081210/how-to-set-jtextarea-to-have-height-that-matches-the-size-of-a-text-it-contains + int width = parentTable.getBounds().width; + setJTextPaneDimensionToFitContainedText(description, width); + + if (isSelected) { + panel.setBackground(parentTable.getSelectionBackground()); + panel.setForeground(parentTable.getSelectionForeground()); + } else { + panel.setBackground(parentTable.getBackground()); + panel.setForeground(parentTable.getForeground()); + } + } } - private JTextPane makeNewDescription(JPanel panel) { + private static JTextPane makeNewDescription(JPanel panel) { if (panel.getComponentCount() > 0) { panel.remove(0); } @@ -158,8 +277,8 @@ private JTextPane makeNewDescription(JPanel panel) { HTMLDocument html = (HTMLDocument) doc; StyleSheet stylesheet = html.getStyleSheet(); stylesheet.addRule("body { margin: 0; padding: 0;" - + "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" - + "font-size: 100%;" + "font-size: 0.95em; }"); + + "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" + + "font-size: 100%;" + "font-size: 0.95em; }"); } description.setOpaque(false); description.setBorder(new EmptyBorder(4, 7, 7, 7)); @@ -170,7 +289,7 @@ private JTextPane makeNewDescription(JPanel panel) { Base.openURL(e.getDescription()); } }); - description.addKeyListener(new DelegatingKeyListener(parentTable)); + // description.addKeyListener(new DelegatingKeyListener(parentTable)); panel.add(description, 0); return description; } @@ -179,7 +298,8 @@ protected void onRemove(ContributedLibrary selected) { // Empty } - protected void onInstall(ContributedLibrary selected, ContributedLibrary installed) { + protected void onInstall(ContributedLibrary selected, + ContributedLibrary installed) { // Empty } @@ -188,24 +308,30 @@ public Component getTableCellRendererComponent(JTable table, Object value, boolean hasFocus, int row, int column) { parentTable = table; - setEnabled(false); - Component component = getUpdatedCellComponent(value, isSelected, row, false); + + Cell cell = new Cell(); + cell.installButton.setEnabled(false); + cell.buttonsPanel.setVisible(false); + cell.inactiveButtonsPanel.setVisible(true); + cell.update(value, isSelected, row, false); if (row % 2 == 0) { - component.setBackground(new Color(236, 241, 241)); //#ecf1f1 + cell.panel.setBackground(new Color(236, 241, 241)); // #ecf1f1 } else { - component.setBackground(new Color(255, 255, 255)); + cell.panel.setBackground(new Color(255, 255, 255)); } - int height = new Double(component.getPreferredSize().getHeight()).intValue(); + int height = new Double(cell.panel.getPreferredSize().getHeight()) + .intValue(); if (table.getRowHeight(row) < height) { table.setRowHeight(row, height); } - return component; + return cell.panel; } private ContributedLibraryReleases editorValue; private JTable parentTable; + private Cell editorCell; @Override public Object getCellEditorValue() { @@ -218,154 +344,65 @@ public Component getTableCellEditorComponent(JTable table, Object value, int column) { parentTable = table; editorValue = (ContributedLibraryReleases) value; + editorCell = new Cell(); + setEnabled(true); final ContributedLibrary installed = editorValue.getInstalled(); - List releases = editorValue.getReleases().stream().filter(new OnlyUpstreamReleasePredicate()).collect(Collectors.toList()); - List uninstalledReleases = releases.stream().filter(new InstalledPredicate().negate()).collect(Collectors.toList()); + List releases = editorValue.getReleases().stream() + .filter(new OnlyUpstreamReleasePredicate()) + .collect(Collectors.toList()); + List uninstalledReleases = releases.stream() + .filter(new InstalledPredicate().negate()).collect(Collectors.toList()); - List installedBuiltIn = releases.stream().filter(new InstalledPredicate()).filter(new BuiltInPredicate()).collect(Collectors.toList()); + List installedBuiltIn = releases.stream() + .filter(new InstalledPredicate()).filter(new BuiltInPredicate()) + .collect(Collectors.toList()); if (installed != null && !installedBuiltIn.contains(installed)) { uninstalledReleases.addAll(installedBuiltIn); } - Collections.sort(uninstalledReleases, new ReverseComparator<>(new DownloadableContributionVersionComparator())); + Collections.sort(uninstalledReleases, new ReverseComparator<>( + new DownloadableContributionVersionComparator())); - downgradeChooser.removeAllItems(); - downgradeChooser.addItem(tr("Select version")); + editorCell.downgradeChooser.removeAllItems(); + editorCell.downgradeChooser.addItem(tr("Select version")); final List uninstalledPreviousReleases = new LinkedList<>(); final List uninstalledNewerReleases = new LinkedList<>(); final VersionComparator versionComparator = new VersionComparator(); uninstalledReleases.stream().forEach(input -> { - if (installed == null || versionComparator.greaterThan(installed.getParsedVersion(), input.getParsedVersion())) { + if (installed == null + || versionComparator.greaterThan(installed.getParsedVersion(), + input.getParsedVersion())) { uninstalledPreviousReleases.add(input); } else { uninstalledNewerReleases.add(input); } }); - uninstalledNewerReleases.forEach(downgradeChooser::addItem); - uninstalledPreviousReleases.forEach(downgradeChooser::addItem); - - downgradeChooser.setVisible(installed != null && (!uninstalledPreviousReleases.isEmpty() || uninstalledNewerReleases.size() > 1)); - downgradeButton.setVisible(installed != null && (!uninstalledPreviousReleases.isEmpty() || uninstalledNewerReleases.size() > 1)); - - versionToInstallChooser.removeAllItems(); - uninstalledReleases.forEach(versionToInstallChooser::addItem); - versionToInstallChooser.setVisible(installed == null && uninstalledReleases.size() > 1); - - Component component = getUpdatedCellComponent(value, true, row, !installedBuiltIn.isEmpty()); - component.setBackground(new Color(218, 227, 227)); //#dae3e3 - return component; - } - - private Component getUpdatedCellComponent(Object value, boolean isSelected, int row, boolean hasBuiltInRelease) { - ContributedLibraryReleases releases = (ContributedLibraryReleases) value; - - JTextPane description = makeNewDescription(panel); - - //FIXME: happens on macosx, don't know why - if (releases == null) { - return panel; - } - - ContributedLibrary selected = releases.getSelected(); - ContributedLibrary installed = releases.getInstalled(); - - boolean installable, upgradable; - if (installed == null) { - installable = true; - upgradable = false; - } else { - installable = false; - upgradable = new DownloadableContributionVersionComparator().compare(selected, installed) > 0; - } - if (installable) { - installButton.setText(tr("Install")); - } - if (upgradable) { - installButton.setText(tr("Update")); - } - installButton.setVisible(installable || upgradable); - installButtonPlaceholder.setVisible(!(installable || upgradable)); - - String name = selected.getName(); - String author = selected.getAuthor(); - // String maintainer = selectedLib.getMaintainer(); - String website = selected.getWebsite(); - String sentence = selected.getSentence(); - String paragraph = selected.getParagraph(); - // String availableVer = selectedLib.getVersion(); - // String url = selected.getUrl(); - - String midcolor = isSelected ? "#000000" : "#888888"; - - String desc = ""; - - // Library name... - desc += format("{0}", name); - if (installed != null && installed.isReadOnly()) { - desc += " Built-In "; - } - - // ...author... - desc += format("", midcolor); - if (author != null && !author.isEmpty()) { - desc += format(" by {0}", author); - } - - // ...version. - if (installed != null) { - String installedVer = installed.getParsedVersion(); - if (installedVer == null) { - desc += " " + tr("Version unknown"); - } else { - desc += " " + format(tr("Version {0}"), installedVer); - } - } - desc += ""; - - if (installed != null) { - desc += " INSTALLED"; - } - - desc += "
"; - - // Description - if (sentence != null) { - desc += format("{0} ", sentence); - if (paragraph != null && !paragraph.isEmpty()) - desc += format("{0}", paragraph); - desc += "
"; - } - if (author != null && !author.isEmpty()) { - desc += format("More info", website); - } - - desc += ""; - description.setText(desc); - description.setBackground(Color.WHITE); - - // for modelToView to work, the text area has to be sized. It doesn't - // matter if it's visible or not. - - // See: - // http://stackoverflow.com/questions/3081210/how-to-set-jtextarea-to-have-height-that-matches-the-size-of-a-text-it-contains - int width = parentTable.getBounds().width; - setJTextPaneDimensionToFitContainedText(description, width); - - if (isSelected) { - panel.setBackground(parentTable.getSelectionBackground()); - panel.setForeground(parentTable.getSelectionForeground()); - } else { - panel.setBackground(parentTable.getBackground()); - panel.setForeground(parentTable.getForeground()); - } - - return panel; + uninstalledNewerReleases.forEach(editorCell.downgradeChooser::addItem); + uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); + + editorCell.downgradeChooser + .setVisible(installed != null + && (!uninstalledPreviousReleases.isEmpty() + || uninstalledNewerReleases.size() > 1)); + editorCell.downgradeButton + .setVisible(installed != null + && (!uninstalledPreviousReleases.isEmpty() + || uninstalledNewerReleases.size() > 1)); + + editorCell.versionToInstallChooser.removeAllItems(); + uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); + editorCell.versionToInstallChooser + .setVisible(installed == null && uninstalledReleases.size() > 1); + + editorCell.update(value, true, row, !installedBuiltIn.isEmpty()); + editorCell.panel.setBackground(new Color(218, 227, 227)); // #dae3e3 + return editorCell.panel; } private final Timer enabler = new Timer(100, new ActionListener() { @@ -384,20 +421,20 @@ public void setEnabled(boolean enabled) { } else { enabler.stop(); } - buttonsPanel.setVisible(enabled); - inactiveButtonsPanel.setVisible(!enabled); + editorCell.buttonsPanel.setVisible(enabled); + editorCell.inactiveButtonsPanel.setVisible(!enabled); } public void enable(boolean enabled) { - installButton.setEnabled(enabled); + editorCell.installButton.setEnabled(enabled); } public void setStatus(String status) { - statusLabel.setText(status); + editorCell.statusLabel.setText(status); } public void invalidate() { - panel.invalidate(); + editorCell.panel.invalidate(); } } From 268ae81759c03760c4e8473f388b32a08282e812 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 24 Dec 2015 17:04:02 +0100 Subject: [PATCH 02/16] Removed useless parentTable field in ContributedLibraryTableCell --- .../libraries/ui/ContributedLibraryTableCell.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index 9a3ab82a1ec..c9db9ac51cc 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -156,8 +156,8 @@ public Cell() { panel.add(Box.createVerticalStrut(15)); } - private void update(Object value, boolean isSelected, int row, - boolean hasBuiltInRelease) { + private void update(JTable parentTable, Object value, boolean isSelected, + int row, boolean hasBuiltInRelease) { ContributedLibraryReleases releases = (ContributedLibraryReleases) value; JTextPane description = makeNewDescription(panel); @@ -307,13 +307,12 @@ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - parentTable = table; Cell cell = new Cell(); cell.installButton.setEnabled(false); cell.buttonsPanel.setVisible(false); cell.inactiveButtonsPanel.setVisible(true); - cell.update(value, isSelected, row, false); + cell.update(table, value, isSelected, row, false); if (row % 2 == 0) { cell.panel.setBackground(new Color(236, 241, 241)); // #ecf1f1 } else { @@ -330,7 +329,6 @@ public Component getTableCellRendererComponent(JTable table, Object value, } private ContributedLibraryReleases editorValue; - private JTable parentTable; private Cell editorCell; @Override @@ -342,7 +340,6 @@ public Object getCellEditorValue() { public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { - parentTable = table; editorValue = (ContributedLibraryReleases) value; editorCell = new Cell(); @@ -400,7 +397,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, editorCell.versionToInstallChooser .setVisible(installed == null && uninstalledReleases.size() > 1); - editorCell.update(value, true, row, !installedBuiltIn.isEmpty()); + editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); editorCell.panel.setBackground(new Color(218, 227, 227)); // #dae3e3 return editorCell.panel; } From ed30cd7b582439adede06e8da0ac7ebeeea52226 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 24 Dec 2015 17:17:51 +0100 Subject: [PATCH 03/16] Removed ContributedLibraryTableCell.Cell dependency from upper editorValue field --- .../ui/ContributedLibraryTableCell.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index c9db9ac51cc..b7358ed730e 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -71,11 +71,11 @@ @SuppressWarnings("serial") public class ContributedLibraryTableCell extends InstallerTableCell { - class Cell { + private class Cell { private final JPanel panel; private final JButton installButton; private final Component installButtonPlaceholder; - private JComboBox downgradeChooser; + private final JComboBox downgradeChooser; private final JComboBox versionToInstallChooser; private final JButton downgradeButton; private final JPanel buttonsPanel; @@ -84,18 +84,10 @@ class Cell { public Cell() { installButton = new JButton(tr("Install")); - installButton - .addActionListener(e -> onInstall(editorValue.getSelected(), - editorValue.getInstalled())); int width = installButton.getPreferredSize().width; installButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); downgradeButton = new JButton(tr("Install")); - downgradeButton.addActionListener(e -> { - ContributedLibrary selected = (ContributedLibrary) downgradeChooser - .getSelectedItem(); - onInstall(selected, editorValue.getInstalled()); - }); downgradeChooser = new JComboBox(); downgradeChooser.addItem("-"); @@ -110,9 +102,6 @@ public Cell() { versionToInstallChooser.addItem("-"); versionToInstallChooser .setMaximumSize(versionToInstallChooser.getPreferredSize()); - versionToInstallChooser.addItemListener(e -> editorValue - .select((ContributedLibrary) versionToInstallChooser - .getSelectedItem())); panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); @@ -341,7 +330,19 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { editorValue = (ContributedLibraryReleases) value; + editorCell = new Cell(); + editorCell.installButton + .addActionListener(e -> onInstall(editorValue.getSelected(), + editorValue.getInstalled())); + editorCell.downgradeButton.addActionListener(e -> { + JComboBox chooser = editorCell.downgradeChooser; + ContributedLibrary lib = (ContributedLibrary) chooser.getSelectedItem(); + onInstall(lib, editorValue.getInstalled()); + }); + editorCell.versionToInstallChooser.addItemListener(e -> editorValue + .select((ContributedLibrary) editorCell.versionToInstallChooser + .getSelectedItem())); setEnabled(true); From fd04767269ad579f679c3217a524ec19dbc38563 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 24 Dec 2015 19:12:54 +0100 Subject: [PATCH 04/16] Renamed ContributedLibraryTableCell to ContributedLibraryTableCellRenderer This is in preparation for the next refactoring. --- ...ableCell.java => ContributedLibraryTableCellRenderer.java} | 2 +- .../arduino/contributions/libraries/ui/LibraryManagerUI.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename app/src/cc/arduino/contributions/libraries/ui/{ContributedLibraryTableCell.java => ContributedLibraryTableCellRenderer.java} (99%) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java similarity index 99% rename from app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java rename to app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java index b7358ed730e..ae3b1f31c14 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java @@ -69,7 +69,7 @@ import processing.app.Base; @SuppressWarnings("serial") -public class ContributedLibraryTableCell extends InstallerTableCell { +public class ContributedLibraryTableCellRenderer extends InstallerTableCell { private class Cell { private final JPanel panel; diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java index 988ffa0ab24..64210818d4b 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java @@ -66,12 +66,12 @@ private LibrariesIndexTableModel getContribModel() { @Override protected InstallerTableCell createCellRenderer() { - return new ContributedLibraryTableCell(); + return new ContributedLibraryTableCellRenderer(); } @Override protected InstallerTableCell createCellEditor() { - return new ContributedLibraryTableCell() { + return new ContributedLibraryTableCellRenderer() { @Override protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) { if (selectedLibrary.isReadOnly()) { From 6370a74632b6ff23e94cedea6049d1a51443695b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 24 Dec 2015 19:47:57 +0100 Subject: [PATCH 05/16] Split TableCellRenderes from TableCellEditors This rationalization helps to better follow the swing abstractions of table models and increase separation of concerns. (WIP: ContributedPlatforms needs a similar refactoring that will be done in the next commits) --- .../ui/ContributedLibraryTableCell.java | 243 +++++++++++ .../ui/ContributedLibraryTableCellEditor.java | 187 +++++++++ .../ContributedLibraryTableCellRenderer.java | 380 +----------------- .../libraries/ui/LibraryManagerUI.java | 35 +- .../contributions/ui/InstallerJDialog.java | 45 ++- .../contributions/ui/InstallerTableCell.java | 18 +- 6 files changed, 501 insertions(+), 407 deletions(-) create mode 100644 app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java create mode 100644 app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java new file mode 100644 index 00000000000..b6f18a604b2 --- /dev/null +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -0,0 +1,243 @@ +package cc.arduino.contributions.libraries.ui; + +import static processing.app.I18n.format; +import static processing.app.I18n.tr; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Insets; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTable; +import javax.swing.JTextPane; +import javax.swing.border.EmptyBorder; +import javax.swing.event.HyperlinkEvent; +import javax.swing.text.Document; +import javax.swing.text.html.HTMLDocument; +import javax.swing.text.html.StyleSheet; + +import cc.arduino.contributions.DownloadableContributionVersionComparator; +import cc.arduino.contributions.libraries.ContributedLibrary; +import cc.arduino.contributions.ui.InstallerTableCell; +import processing.app.Base; + +public class ContributedLibraryTableCell { + + protected final JPanel panel; + protected final JButton installButton; + protected final Component installButtonPlaceholder; + protected final JComboBox downgradeChooser; + protected final JComboBox versionToInstallChooser; + protected final JButton downgradeButton; + protected final JPanel buttonsPanel; + protected final JPanel inactiveButtonsPanel; + protected final JLabel statusLabel; + + public ContributedLibraryTableCell() { + installButton = new JButton(tr("Install")); + int width = installButton.getPreferredSize().width; + installButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); + + downgradeButton = new JButton(tr("Install")); + + downgradeChooser = new JComboBox(); + downgradeChooser.addItem("-"); + downgradeChooser.setMaximumSize(downgradeChooser.getPreferredSize()); + downgradeChooser.addItemListener(e -> { + Object selectVersionItem = downgradeChooser.getItemAt(0); + boolean disableDowngrade = (e.getItem() == selectVersionItem); + downgradeButton.setEnabled(!disableDowngrade); + }); + + versionToInstallChooser = new JComboBox(); + versionToInstallChooser.addItem("-"); + versionToInstallChooser + .setMaximumSize(versionToInstallChooser.getPreferredSize()); + + panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + + makeNewDescription(panel); + + buttonsPanel = new JPanel(); + buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); + buttonsPanel.setOpaque(false); + + buttonsPanel.add(Box.createHorizontalStrut(7)); + buttonsPanel.add(downgradeChooser); + buttonsPanel.add(Box.createHorizontalStrut(5)); + buttonsPanel.add(downgradeButton); + + buttonsPanel.add(Box.createHorizontalGlue()); + + buttonsPanel.add(versionToInstallChooser); + buttonsPanel.add(Box.createHorizontalStrut(5)); + buttonsPanel.add(installButton); + buttonsPanel.add(Box.createHorizontalStrut(5)); + buttonsPanel.add(Box.createHorizontalStrut(15)); + + panel.add(buttonsPanel); + + inactiveButtonsPanel = new JPanel(); + inactiveButtonsPanel + .setLayout(new BoxLayout(inactiveButtonsPanel, BoxLayout.X_AXIS)); + inactiveButtonsPanel.setOpaque(false); + + int height = installButton.getMinimumSize().height; + inactiveButtonsPanel.add(Box.createVerticalStrut(height)); + inactiveButtonsPanel.add(Box.createGlue()); + + statusLabel = new JLabel(" "); + inactiveButtonsPanel.add(statusLabel); + inactiveButtonsPanel.add(Box.createHorizontalStrut(15)); + + panel.add(inactiveButtonsPanel); + + panel.add(Box.createVerticalStrut(15)); + } + + void update(JTable parentTable, Object value, boolean isSelected, + int row, boolean hasBuiltInRelease) { + ContributedLibraryReleases releases = (ContributedLibraryReleases) value; + + JTextPane description = makeNewDescription(panel); + + // FIXME: happens on macosx, don't know why + if (releases == null) + return; + + ContributedLibrary selected = releases.getSelected(); + ContributedLibrary installed = releases.getInstalled(); + + boolean installable, upgradable; + if (installed == null) { + installable = true; + upgradable = false; + } else { + installable = false; + upgradable = new DownloadableContributionVersionComparator() + .compare(selected, installed) > 0; + } + if (installable) { + installButton.setText(tr("Install")); + } + if (upgradable) { + installButton.setText(tr("Update")); + } + installButton.setVisible(installable || upgradable); + installButtonPlaceholder.setVisible(!(installable || upgradable)); + + String name = selected.getName(); + String author = selected.getAuthor(); + // String maintainer = selectedLib.getMaintainer(); + String website = selected.getWebsite(); + String sentence = selected.getSentence(); + String paragraph = selected.getParagraph(); + // String availableVer = selectedLib.getVersion(); + // String url = selected.getUrl(); + + String midcolor = isSelected ? "#000000" : "#888888"; + + String desc = ""; + + // Library name... + desc += format("{0}", name); + if (installed != null && installed.isReadOnly()) { + desc += " Built-In "; + } + + // ...author... + desc += format("", midcolor); + if (author != null && !author.isEmpty()) { + desc += format(" by {0}", author); + } + + // ...version. + if (installed != null) { + String installedVer = installed.getParsedVersion(); + if (installedVer == null) { + desc += " " + tr("Version unknown"); + } else { + desc += " " + format(tr("Version {0}"), installedVer); + } + } + desc += ""; + + if (installed != null) { + desc += " INSTALLED"; + } + + desc += "
"; + + // Description + if (sentence != null) { + desc += format("{0} ", sentence); + if (paragraph != null && !paragraph.isEmpty()) + desc += format("{0}", paragraph); + desc += "
"; + } + if (author != null && !author.isEmpty()) { + desc += format("More info", website); + } + + desc += ""; + description.setText(desc); + description.setBackground(Color.WHITE); + + // for modelToView to work, the text area has to be sized. It doesn't + // matter if it's visible or not. + + // See: + // http://stackoverflow.com/questions/3081210/how-to-set-jtextarea-to-have-height-that-matches-the-size-of-a-text-it-contains + int width = parentTable.getBounds().width; + InstallerTableCell.setJTextPaneDimensionToFitContainedText(description, width); + + if (isSelected) { + panel.setBackground(parentTable.getSelectionBackground()); + panel.setForeground(parentTable.getSelectionForeground()); + } else { + panel.setBackground(parentTable.getBackground()); + panel.setForeground(parentTable.getForeground()); + } + } + + private static JTextPane makeNewDescription(JPanel panel) { + if (panel.getComponentCount() > 0) { + panel.remove(0); + } + JTextPane description = new JTextPane(); + description.setInheritsPopupMenu(true); + Insets margin = description.getMargin(); + margin.bottom = 0; + description.setMargin(margin); + description.setContentType("text/html"); + Document doc = description.getDocument(); + if (doc instanceof HTMLDocument) { + HTMLDocument html = (HTMLDocument) doc; + StyleSheet stylesheet = html.getStyleSheet(); + stylesheet.addRule("body { margin: 0; padding: 0;" + + "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" + + "font-size: 100%;" + "font-size: 0.95em; }"); + } + description.setOpaque(false); + description.setBorder(new EmptyBorder(4, 7, 7, 7)); + description.setHighlighter(null); + description.setEditable(false); + description.addHyperlinkListener(e -> { + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { + Base.openURL(e.getDescription()); + } + }); + // description.addKeyListener(new DelegatingKeyListener(parentTable)); + panel.add(description, 0); + return description; + } + + +} diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java new file mode 100644 index 00000000000..bb838c5136b --- /dev/null +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -0,0 +1,187 @@ +/* + * This file is part of Arduino. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + * + * Arduino 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package cc.arduino.contributions.libraries.ui; + +import static processing.app.I18n.tr; + +import java.awt.Color; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import javax.swing.JComboBox; +import javax.swing.JTable; +import javax.swing.Timer; + +import cc.arduino.contributions.DownloadableContributionVersionComparator; +import cc.arduino.contributions.VersionComparator; +import cc.arduino.contributions.filters.BuiltInPredicate; +import cc.arduino.contributions.filters.InstalledPredicate; +import cc.arduino.contributions.libraries.ContributedLibrary; +import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate; +import cc.arduino.contributions.ui.InstallerTableCell; +import cc.arduino.utils.ReverseComparator; + +@SuppressWarnings("serial") +public class ContributedLibraryTableCellEditor extends InstallerTableCell { + + private ContributedLibraryReleases editorValue; + private ContributedLibraryTableCell editorCell; + + @Override + public Object getCellEditorValue() { + return editorValue; + } + + @Override + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, int row, + int column) { + editorValue = (ContributedLibraryReleases) value; + + editorCell = new ContributedLibraryTableCell(); + editorCell.installButton + .addActionListener(e -> onInstall(editorValue.getSelected(), + editorValue.getInstalled())); + editorCell.downgradeButton.addActionListener(e -> { + JComboBox chooser = editorCell.downgradeChooser; + ContributedLibrary lib = (ContributedLibrary) chooser.getSelectedItem(); + onInstall(lib, editorValue.getInstalled()); + }); + editorCell.versionToInstallChooser.addItemListener(e -> editorValue + .select((ContributedLibrary) editorCell.versionToInstallChooser + .getSelectedItem())); + + setEnabled(true); + + final ContributedLibrary installed = editorValue.getInstalled(); + + List releases = editorValue.getReleases().stream() + .filter(new OnlyUpstreamReleasePredicate()) + .collect(Collectors.toList()); + List uninstalledReleases = releases.stream() + .filter(new InstalledPredicate().negate()).collect(Collectors.toList()); + + List installedBuiltIn = releases.stream() + .filter(new InstalledPredicate()).filter(new BuiltInPredicate()) + .collect(Collectors.toList()); + + if (installed != null && !installedBuiltIn.contains(installed)) { + uninstalledReleases.addAll(installedBuiltIn); + } + + Collections.sort(uninstalledReleases, new ReverseComparator<>( + new DownloadableContributionVersionComparator())); + + editorCell.downgradeChooser.removeAllItems(); + editorCell.downgradeChooser.addItem(tr("Select version")); + + final List uninstalledPreviousReleases = new LinkedList<>(); + final List uninstalledNewerReleases = new LinkedList<>(); + + final VersionComparator versionComparator = new VersionComparator(); + uninstalledReleases.stream().forEach(input -> { + if (installed == null + || versionComparator.greaterThan(installed.getParsedVersion(), + input.getParsedVersion())) { + uninstalledPreviousReleases.add(input); + } else { + uninstalledNewerReleases.add(input); + } + }); + uninstalledNewerReleases.forEach(editorCell.downgradeChooser::addItem); + uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); + + editorCell.downgradeChooser + .setVisible(installed != null + && (!uninstalledPreviousReleases.isEmpty() + || uninstalledNewerReleases.size() > 1)); + editorCell.downgradeButton + .setVisible(installed != null + && (!uninstalledPreviousReleases.isEmpty() + || uninstalledNewerReleases.size() > 1)); + + editorCell.versionToInstallChooser.removeAllItems(); + uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); + editorCell.versionToInstallChooser + .setVisible(installed == null && uninstalledReleases.size() > 1); + + editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); + editorCell.panel.setBackground(new Color(218, 227, 227)); // #dae3e3 + return editorCell.panel; + } + + private final Timer enabler = new Timer(100, new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + enable(true); + enabler.stop(); + } + }); + + @Override + public void setEnabled(boolean enabled) { + enable(false); + if (enabled) { + enabler.start(); + } else { + enabler.stop(); + } + editorCell.buttonsPanel.setVisible(enabled); + editorCell.inactiveButtonsPanel.setVisible(!enabled); + } + + public void enable(boolean enabled) { + editorCell.installButton.setEnabled(enabled); + } + + public void setStatus(String status) { + editorCell.statusLabel.setText(status); + } + + public void invalidate() { + editorCell.panel.invalidate(); + } + + + protected void onRemove(ContributedLibrary selected) { + // Empty + } + + protected void onInstall(ContributedLibrary selected, + ContributedLibrary installed) { + // Empty + } + +} diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java index ae3b1f31c14..5fae977de8a 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java @@ -29,279 +29,27 @@ package cc.arduino.contributions.libraries.ui; -import static processing.app.I18n.format; -import static processing.app.I18n.tr; - import java.awt.Color; import java.awt.Component; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.stream.Collectors; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JLabel; -import javax.swing.JPanel; import javax.swing.JTable; -import javax.swing.JTextPane; -import javax.swing.Timer; -import javax.swing.border.EmptyBorder; -import javax.swing.event.HyperlinkEvent; -import javax.swing.text.Document; -import javax.swing.text.html.HTMLDocument; -import javax.swing.text.html.StyleSheet; - -import cc.arduino.contributions.DownloadableContributionVersionComparator; -import cc.arduino.contributions.VersionComparator; -import cc.arduino.contributions.filters.BuiltInPredicate; -import cc.arduino.contributions.filters.InstalledPredicate; -import cc.arduino.contributions.libraries.ContributedLibrary; -import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate; -import cc.arduino.contributions.ui.InstallerTableCell; -import cc.arduino.utils.ReverseComparator; -import processing.app.Base; +import javax.swing.table.TableCellRenderer; @SuppressWarnings("serial") -public class ContributedLibraryTableCellRenderer extends InstallerTableCell { - - private class Cell { - private final JPanel panel; - private final JButton installButton; - private final Component installButtonPlaceholder; - private final JComboBox downgradeChooser; - private final JComboBox versionToInstallChooser; - private final JButton downgradeButton; - private final JPanel buttonsPanel; - private final JPanel inactiveButtonsPanel; - private final JLabel statusLabel; - - public Cell() { - installButton = new JButton(tr("Install")); - int width = installButton.getPreferredSize().width; - installButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); - - downgradeButton = new JButton(tr("Install")); - - downgradeChooser = new JComboBox(); - downgradeChooser.addItem("-"); - downgradeChooser.setMaximumSize(downgradeChooser.getPreferredSize()); - downgradeChooser.addItemListener(e -> { - Object selectVersionItem = downgradeChooser.getItemAt(0); - boolean disableDowngrade = (e.getItem() == selectVersionItem); - downgradeButton.setEnabled(!disableDowngrade); - }); - - versionToInstallChooser = new JComboBox(); - versionToInstallChooser.addItem("-"); - versionToInstallChooser - .setMaximumSize(versionToInstallChooser.getPreferredSize()); - - panel = new JPanel(); - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - - makeNewDescription(panel); - - buttonsPanel = new JPanel(); - buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); - buttonsPanel.setOpaque(false); - - buttonsPanel.add(Box.createHorizontalStrut(7)); - buttonsPanel.add(downgradeChooser); - buttonsPanel.add(Box.createHorizontalStrut(5)); - buttonsPanel.add(downgradeButton); - - buttonsPanel.add(Box.createHorizontalGlue()); - - buttonsPanel.add(versionToInstallChooser); - buttonsPanel.add(Box.createHorizontalStrut(5)); - buttonsPanel.add(installButton); - buttonsPanel.add(Box.createHorizontalStrut(5)); - buttonsPanel.add(Box.createHorizontalStrut(15)); - - panel.add(buttonsPanel); - - inactiveButtonsPanel = new JPanel(); - inactiveButtonsPanel - .setLayout(new BoxLayout(inactiveButtonsPanel, BoxLayout.X_AXIS)); - inactiveButtonsPanel.setOpaque(false); - - int height = installButton.getMinimumSize().height; - inactiveButtonsPanel.add(Box.createVerticalStrut(height)); - inactiveButtonsPanel.add(Box.createGlue()); - - statusLabel = new JLabel(" "); - inactiveButtonsPanel.add(statusLabel); - inactiveButtonsPanel.add(Box.createHorizontalStrut(15)); - - panel.add(inactiveButtonsPanel); - - panel.add(Box.createVerticalStrut(15)); - } - - private void update(JTable parentTable, Object value, boolean isSelected, - int row, boolean hasBuiltInRelease) { - ContributedLibraryReleases releases = (ContributedLibraryReleases) value; - - JTextPane description = makeNewDescription(panel); - - // FIXME: happens on macosx, don't know why - if (releases == null) - return; - - ContributedLibrary selected = releases.getSelected(); - ContributedLibrary installed = releases.getInstalled(); - - boolean installable, upgradable; - if (installed == null) { - installable = true; - upgradable = false; - } else { - installable = false; - upgradable = new DownloadableContributionVersionComparator() - .compare(selected, installed) > 0; - } - if (installable) { - installButton.setText(tr("Install")); - } - if (upgradable) { - installButton.setText(tr("Update")); - } - installButton.setVisible(installable || upgradable); - installButtonPlaceholder.setVisible(!(installable || upgradable)); - - String name = selected.getName(); - String author = selected.getAuthor(); - // String maintainer = selectedLib.getMaintainer(); - String website = selected.getWebsite(); - String sentence = selected.getSentence(); - String paragraph = selected.getParagraph(); - // String availableVer = selectedLib.getVersion(); - // String url = selected.getUrl(); - - String midcolor = isSelected ? "#000000" : "#888888"; - - String desc = ""; - - // Library name... - desc += format("{0}", name); - if (installed != null && installed.isReadOnly()) { - desc += " Built-In "; - } - - // ...author... - desc += format("", midcolor); - if (author != null && !author.isEmpty()) { - desc += format(" by {0}", author); - } - - // ...version. - if (installed != null) { - String installedVer = installed.getParsedVersion(); - if (installedVer == null) { - desc += " " + tr("Version unknown"); - } else { - desc += " " + format(tr("Version {0}"), installedVer); - } - } - desc += ""; - - if (installed != null) { - desc += " INSTALLED"; - } - - desc += "
"; - - // Description - if (sentence != null) { - desc += format("{0} ", sentence); - if (paragraph != null && !paragraph.isEmpty()) - desc += format("{0}", paragraph); - desc += "
"; - } - if (author != null && !author.isEmpty()) { - desc += format("More info", website); - } - - desc += ""; - description.setText(desc); - description.setBackground(Color.WHITE); - - // for modelToView to work, the text area has to be sized. It doesn't - // matter if it's visible or not. - - // See: - // http://stackoverflow.com/questions/3081210/how-to-set-jtextarea-to-have-height-that-matches-the-size-of-a-text-it-contains - int width = parentTable.getBounds().width; - setJTextPaneDimensionToFitContainedText(description, width); - - if (isSelected) { - panel.setBackground(parentTable.getSelectionBackground()); - panel.setForeground(parentTable.getSelectionForeground()); - } else { - panel.setBackground(parentTable.getBackground()); - panel.setForeground(parentTable.getForeground()); - } - } - } - - private static JTextPane makeNewDescription(JPanel panel) { - if (panel.getComponentCount() > 0) { - panel.remove(0); - } - JTextPane description = new JTextPane(); - description.setInheritsPopupMenu(true); - Insets margin = description.getMargin(); - margin.bottom = 0; - description.setMargin(margin); - description.setContentType("text/html"); - Document doc = description.getDocument(); - if (doc instanceof HTMLDocument) { - HTMLDocument html = (HTMLDocument) doc; - StyleSheet stylesheet = html.getStyleSheet(); - stylesheet.addRule("body { margin: 0; padding: 0;" - + "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" - + "font-size: 100%;" + "font-size: 0.95em; }"); - } - description.setOpaque(false); - description.setBorder(new EmptyBorder(4, 7, 7, 7)); - description.setHighlighter(null); - description.setEditable(false); - description.addHyperlinkListener(e -> { - if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { - Base.openURL(e.getDescription()); - } - }); - // description.addKeyListener(new DelegatingKeyListener(parentTable)); - panel.add(description, 0); - return description; - } - - protected void onRemove(ContributedLibrary selected) { - // Empty - } - - protected void onInstall(ContributedLibrary selected, - ContributedLibrary installed) { - // Empty - } +public class ContributedLibraryTableCellRenderer implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - Cell cell = new Cell(); + ContributedLibraryTableCell cell = new ContributedLibraryTableCell(); cell.installButton.setEnabled(false); cell.buttonsPanel.setVisible(false); cell.inactiveButtonsPanel.setVisible(true); + cell.update(table, value, isSelected, row, false); + if (row % 2 == 0) { cell.panel.setBackground(new Color(236, 241, 241)); // #ecf1f1 } else { @@ -317,122 +65,4 @@ public Component getTableCellRendererComponent(JTable table, Object value, return cell.panel; } - private ContributedLibraryReleases editorValue; - private Cell editorCell; - - @Override - public Object getCellEditorValue() { - return editorValue; - } - - @Override - public Component getTableCellEditorComponent(JTable table, Object value, - boolean isSelected, int row, - int column) { - editorValue = (ContributedLibraryReleases) value; - - editorCell = new Cell(); - editorCell.installButton - .addActionListener(e -> onInstall(editorValue.getSelected(), - editorValue.getInstalled())); - editorCell.downgradeButton.addActionListener(e -> { - JComboBox chooser = editorCell.downgradeChooser; - ContributedLibrary lib = (ContributedLibrary) chooser.getSelectedItem(); - onInstall(lib, editorValue.getInstalled()); - }); - editorCell.versionToInstallChooser.addItemListener(e -> editorValue - .select((ContributedLibrary) editorCell.versionToInstallChooser - .getSelectedItem())); - - setEnabled(true); - - final ContributedLibrary installed = editorValue.getInstalled(); - - List releases = editorValue.getReleases().stream() - .filter(new OnlyUpstreamReleasePredicate()) - .collect(Collectors.toList()); - List uninstalledReleases = releases.stream() - .filter(new InstalledPredicate().negate()).collect(Collectors.toList()); - - List installedBuiltIn = releases.stream() - .filter(new InstalledPredicate()).filter(new BuiltInPredicate()) - .collect(Collectors.toList()); - - if (installed != null && !installedBuiltIn.contains(installed)) { - uninstalledReleases.addAll(installedBuiltIn); - } - - Collections.sort(uninstalledReleases, new ReverseComparator<>( - new DownloadableContributionVersionComparator())); - - editorCell.downgradeChooser.removeAllItems(); - editorCell.downgradeChooser.addItem(tr("Select version")); - - final List uninstalledPreviousReleases = new LinkedList<>(); - final List uninstalledNewerReleases = new LinkedList<>(); - - final VersionComparator versionComparator = new VersionComparator(); - uninstalledReleases.stream().forEach(input -> { - if (installed == null - || versionComparator.greaterThan(installed.getParsedVersion(), - input.getParsedVersion())) { - uninstalledPreviousReleases.add(input); - } else { - uninstalledNewerReleases.add(input); - } - }); - uninstalledNewerReleases.forEach(editorCell.downgradeChooser::addItem); - uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); - - editorCell.downgradeChooser - .setVisible(installed != null - && (!uninstalledPreviousReleases.isEmpty() - || uninstalledNewerReleases.size() > 1)); - editorCell.downgradeButton - .setVisible(installed != null - && (!uninstalledPreviousReleases.isEmpty() - || uninstalledNewerReleases.size() > 1)); - - editorCell.versionToInstallChooser.removeAllItems(); - uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); - editorCell.versionToInstallChooser - .setVisible(installed == null && uninstalledReleases.size() > 1); - - editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); - editorCell.panel.setBackground(new Color(218, 227, 227)); // #dae3e3 - return editorCell.panel; - } - - private final Timer enabler = new Timer(100, new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - enable(true); - enabler.stop(); - } - }); - - @Override - public void setEnabled(boolean enabled) { - enable(false); - if (enabled) { - enabler.start(); - } else { - enabler.stop(); - } - editorCell.buttonsPanel.setVisible(enabled); - editorCell.inactiveButtonsPanel.setVisible(!enabled); - } - - public void enable(boolean enabled) { - editorCell.installButton.setEnabled(enabled); - } - - public void setStatus(String status) { - editorCell.statusLabel.setText(status); - } - - public void invalidate() { - editorCell.panel.invalidate(); - } - } diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java index 64210818d4b..943c21fdd7e 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java @@ -29,16 +29,10 @@ package cc.arduino.contributions.libraries.ui; -import cc.arduino.contributions.DownloadableContribution; -import cc.arduino.contributions.libraries.ContributedLibrary; -import cc.arduino.contributions.libraries.LibraryInstaller; -import cc.arduino.contributions.libraries.LibraryTypeComparator; -import cc.arduino.contributions.ui.*; -import cc.arduino.utils.Progress; -import processing.app.BaseNoGui; +import static processing.app.I18n.tr; -import javax.swing.*; -import java.awt.*; +import java.awt.Dialog; +import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Collection; @@ -46,7 +40,24 @@ import java.util.LinkedList; import java.util.function.Predicate; -import static processing.app.I18n.tr; +import javax.swing.Box; +import javax.swing.JComboBox; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.table.TableCellRenderer; + +import cc.arduino.contributions.DownloadableContribution; +import cc.arduino.contributions.libraries.ContributedLibrary; +import cc.arduino.contributions.libraries.LibraryInstaller; +import cc.arduino.contributions.libraries.LibraryTypeComparator; +import cc.arduino.contributions.ui.DropdownAllItem; +import cc.arduino.contributions.ui.DropdownItem; +import cc.arduino.contributions.ui.FilteredAbstractTableModel; +import cc.arduino.contributions.ui.InstallerJDialog; +import cc.arduino.contributions.ui.InstallerJDialogUncaughtExceptionHandler; +import cc.arduino.contributions.ui.InstallerTableCell; +import cc.arduino.utils.Progress; +import processing.app.BaseNoGui; @SuppressWarnings("serial") public class LibraryManagerUI extends InstallerJDialog { @@ -65,13 +76,13 @@ private LibrariesIndexTableModel getContribModel() { } @Override - protected InstallerTableCell createCellRenderer() { + protected TableCellRenderer createCellRenderer() { return new ContributedLibraryTableCellRenderer(); } @Override protected InstallerTableCell createCellEditor() { - return new ContributedLibraryTableCellRenderer() { + return new ContributedLibraryTableCellEditor() { @Override protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) { if (selectedLibrary.isReadOnly()) { diff --git a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java index 255d6f40747..14c53b02500 100644 --- a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java +++ b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java @@ -29,21 +29,44 @@ package cc.arduino.contributions.ui; -import cc.arduino.contributions.ui.listeners.AbstractKeyListener; -import processing.app.Base; -import processing.app.Theme; +import static cc.arduino.contributions.packages.ui.ContributionIndexTableModel.DESCRIPTION_COL; +import static processing.app.I18n.tr; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.awt.event.KeyEvent; +import java.awt.event.WindowEvent; +import java.util.function.Predicate; +import java.util.stream.Stream; -import javax.swing.*; +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; +import javax.swing.SwingUtilities; +import javax.swing.WindowConstants; import javax.swing.border.EmptyBorder; +import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; -import java.awt.*; -import java.awt.event.*; -import java.util.function.Predicate; -import java.util.stream.Stream; -import static cc.arduino.contributions.packages.ui.ContributionIndexTableModel.DESCRIPTION_COL; -import static processing.app.I18n.tr; +import cc.arduino.contributions.ui.listeners.AbstractKeyListener; +import processing.app.Base; +import processing.app.Theme; public abstract class InstallerJDialog extends JDialog { @@ -67,7 +90,7 @@ public abstract class InstallerJDialog extends JDialog { abstract protected FilteredAbstractTableModel createContribModel(); - abstract protected InstallerTableCell createCellRenderer(); + abstract protected TableCellRenderer createCellRenderer(); abstract protected InstallerTableCell createCellEditor(); diff --git a/app/src/cc/arduino/contributions/ui/InstallerTableCell.java b/app/src/cc/arduino/contributions/ui/InstallerTableCell.java index e45655d2f96..bbf62ebad70 100644 --- a/app/src/cc/arduino/contributions/ui/InstallerTableCell.java +++ b/app/src/cc/arduino/contributions/ui/InstallerTableCell.java @@ -29,21 +29,21 @@ package cc.arduino.contributions.ui; -import javax.swing.*; +import java.awt.Dimension; +import java.awt.Rectangle; + +import javax.swing.AbstractCellEditor; +import javax.swing.JTextPane; import javax.swing.table.TableCellEditor; -import javax.swing.table.TableCellRenderer; import javax.swing.text.BadLocationException; -import java.awt.*; -public abstract class InstallerTableCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer { +public abstract class InstallerTableCell extends AbstractCellEditor implements TableCellEditor { - public void setEnabled(boolean b) { - } + abstract public void setEnabled(boolean b); - public void setStatus(String s) { - } + abstract public void setStatus(String s); - protected void setJTextPaneDimensionToFitContainedText(JTextPane jTextPane, int width) { + public static void setJTextPaneDimensionToFitContainedText(JTextPane jTextPane, int width) { Dimension minimumDimension = new Dimension(width, 10); jTextPane.setPreferredSize(minimumDimension); jTextPane.setSize(minimumDimension); From 4725584a49f2c33b9cbffa8d59807bab9e4aa47c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 24 Dec 2015 20:08:08 +0100 Subject: [PATCH 06/16] ContributedPlatformCell* now follows swing cell model abstraction This commit completes the refactoring --- .../ui/ContributedPlatformTableCell.java | 330 ++++++------------ .../ContributedPlatformTableCellEditor.java | 184 ++++++++++ .../ContributedPlatformTableCellRenderer.java | 67 ++++ .../packages/ui/ContributionManagerUI.java | 46 ++- 4 files changed, 386 insertions(+), 241 deletions(-) create mode 100644 app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java create mode 100644 app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java index 04dc4d8f2d0..00dd772e9f0 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java @@ -29,69 +29,64 @@ package cc.arduino.contributions.packages.ui; -import cc.arduino.contributions.DownloadableContributionVersionComparator; -import cc.arduino.contributions.VersionComparator; -import cc.arduino.contributions.filters.BuiltInPredicate; -import cc.arduino.contributions.filters.InstalledPredicate; -import cc.arduino.contributions.packages.ContributedBoard; -import cc.arduino.contributions.packages.ContributedHelp; -import cc.arduino.contributions.packages.ContributedPlatform; -import cc.arduino.contributions.ui.InstallerTableCell; -import cc.arduino.contributions.ui.listeners.DelegatingKeyListener; -import cc.arduino.utils.ReverseComparator; -import processing.app.Base; +import static processing.app.I18n.format; +import static processing.app.I18n.tr; -import javax.swing.*; +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Insets; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTable; +import javax.swing.JTextPane; import javax.swing.border.EmptyBorder; import javax.swing.event.HyperlinkEvent; import javax.swing.text.Document; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.StyleSheet; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Collections; -import java.util.LinkedList; -import java.util.stream.Collectors; -import static processing.app.I18n.format; -import static processing.app.I18n.tr; +import cc.arduino.contributions.DownloadableContributionVersionComparator; +import cc.arduino.contributions.packages.ContributedBoard; +import cc.arduino.contributions.packages.ContributedHelp; +import cc.arduino.contributions.packages.ContributedPlatform; +import cc.arduino.contributions.ui.InstallerTableCell; +import processing.app.Base; @SuppressWarnings("serial") -public class ContributedPlatformTableCell extends InstallerTableCell { - - private final JPanel panel; - private final JButton installButton; - private final JButton removeButton; - private final Component removeButtonPlaceholder; - private final Component installButtonPlaceholder; - private JComboBox downgradeChooser; - private final JComboBox versionToInstallChooser; - private final JButton downgradeButton; - private final JPanel buttonsPanel; - private final JPanel inactiveButtonsPanel; - private final JLabel statusLabel; +public class ContributedPlatformTableCell { + + final JPanel panel; + final JButton installButton; + final JButton removeButton; + final Component removeButtonPlaceholder; + final Component installButtonPlaceholder; + JComboBox downgradeChooser; + final JComboBox versionToInstallChooser; + final JButton downgradeButton; + final JPanel buttonsPanel; + final JPanel inactiveButtonsPanel; + final JLabel statusLabel; public ContributedPlatformTableCell() { { installButton = new JButton(tr("Install")); - installButton.addActionListener(e -> onInstall(editorValue.getSelected(), editorValue.getInstalled())); int width = installButton.getPreferredSize().width; installButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); } { removeButton = new JButton(tr("Remove")); - removeButton.addActionListener(e -> onRemove(editorValue.getInstalled())); int width = removeButton.getPreferredSize().width; removeButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); } downgradeButton = new JButton(tr("Install")); - downgradeButton.addActionListener(e -> { - ContributedPlatform selected = (ContributedPlatform) downgradeChooser.getSelectedItem(); - onInstall(selected, editorValue.getInstalled()); - }); downgradeChooser = new JComboBox(); downgradeChooser.addItem("-"); @@ -104,183 +99,62 @@ public ContributedPlatformTableCell() { versionToInstallChooser = new JComboBox(); versionToInstallChooser.addItem("-"); - versionToInstallChooser.setMaximumSize(versionToInstallChooser.getPreferredSize()); - versionToInstallChooser.addItemListener(e -> editorValue.select((ContributedPlatform) versionToInstallChooser.getSelectedItem())); + versionToInstallChooser + .setMaximumSize(versionToInstallChooser.getPreferredSize()); panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); makeNewDescription(panel); - { - buttonsPanel = new JPanel(); - buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); - buttonsPanel.setOpaque(false); - - buttonsPanel.add(Box.createHorizontalStrut(7)); - buttonsPanel.add(downgradeChooser); - buttonsPanel.add(Box.createHorizontalStrut(5)); - buttonsPanel.add(downgradeButton); - - buttonsPanel.add(Box.createHorizontalGlue()); - - buttonsPanel.add(versionToInstallChooser); - buttonsPanel.add(Box.createHorizontalStrut(5)); - buttonsPanel.add(installButton); - buttonsPanel.add(Box.createHorizontalStrut(5)); - buttonsPanel.add(removeButton); - buttonsPanel.add(Box.createHorizontalStrut(5)); - buttonsPanel.add(Box.createHorizontalStrut(15)); - - panel.add(buttonsPanel); - } - - { - inactiveButtonsPanel = new JPanel(); - inactiveButtonsPanel.setLayout(new BoxLayout(inactiveButtonsPanel, BoxLayout.X_AXIS)); - inactiveButtonsPanel.setOpaque(false); - - int height = installButton.getMinimumSize().height; - inactiveButtonsPanel.add(Box.createVerticalStrut(height)); - inactiveButtonsPanel.add(Box.createGlue()); - - statusLabel = new JLabel(" "); - inactiveButtonsPanel.add(statusLabel); - inactiveButtonsPanel.add(Box.createHorizontalStrut(15)); - - panel.add(inactiveButtonsPanel); - } - - panel.add(Box.createVerticalStrut(15)); - } - - private JTextPane makeNewDescription(JPanel panel) { - if (panel.getComponentCount() > 0) { - panel.remove(0); - } - JTextPane description = new JTextPane(); - description.setInheritsPopupMenu(true); - Insets margin = description.getMargin(); - margin.bottom = 0; - description.setMargin(margin); - description.setContentType("text/html"); - Document doc = description.getDocument(); - if (doc instanceof HTMLDocument) { - HTMLDocument html = (HTMLDocument) doc; - StyleSheet stylesheet = html.getStyleSheet(); - stylesheet.addRule("body { margin: 0; padding: 0;" - + "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" - + "font-size: 100%;" + "font-size: 0.95em; }"); - } - description.setOpaque(false); - description.setBorder(new EmptyBorder(4, 7, 7, 7)); - description.setHighlighter(null); - description.setEditable(false); - description.addHyperlinkListener(e -> { - if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { - Base.openURL(e.getDescription()); - } - }); - description.addKeyListener(new DelegatingKeyListener(parentTable)); - panel.add(description, 0); - return description; - } - - protected void onRemove(ContributedPlatform contributedPlatform) { - // Empty - } - - protected void onInstall(ContributedPlatform contributedPlatform, ContributedPlatform installed) { - // Empty - } - - public Component getTableCellRendererComponent(JTable table, Object value, - boolean isSelected, - boolean hasFocus, int row, - int column) { - parentTable = table; - setEnabled(false); - Component component = getUpdatedCellComponent(value, isSelected, row, false); - if (row % 2 == 0) { - component.setBackground(new Color(236, 241, 241)); //#ecf1f1 - } else { - component.setBackground(new Color(255, 255, 255)); - } - - int height = new Double(component.getPreferredSize().getHeight()).intValue(); - if (table.getRowHeight(row) < height) { - table.setRowHeight(row, height); - } - - return component; - } - - private ContributionIndexTableModel.ContributedPlatformReleases editorValue; - private JTable parentTable; - - @Override - public Object getCellEditorValue() { - return editorValue; - } - - @Override - public Component getTableCellEditorComponent(JTable table, Object value, - boolean isSelected, int row, - int column) { - parentTable = table; - editorValue = (ContributionIndexTableModel.ContributedPlatformReleases) value; - setEnabled(true); + buttonsPanel = new JPanel(); + buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); + buttonsPanel.setOpaque(false); - final ContributedPlatform installed = editorValue.getInstalled(); + buttonsPanel.add(Box.createHorizontalStrut(7)); + buttonsPanel.add(downgradeChooser); + buttonsPanel.add(Box.createHorizontalStrut(5)); + buttonsPanel.add(downgradeButton); - java.util.List releases = new LinkedList<>(editorValue.releases); - java.util.List uninstalledReleases = releases.stream().filter(new InstalledPredicate().negate()).collect(Collectors.toList()); + buttonsPanel.add(Box.createHorizontalGlue()); - java.util.List installedBuiltIn = releases.stream().filter(new InstalledPredicate()).filter(new BuiltInPredicate()).collect(Collectors.toList()); - - if (installed != null && !installedBuiltIn.contains(installed)) { - uninstalledReleases.addAll(installedBuiltIn); - } + buttonsPanel.add(versionToInstallChooser); + buttonsPanel.add(Box.createHorizontalStrut(5)); + buttonsPanel.add(installButton); + buttonsPanel.add(Box.createHorizontalStrut(5)); + buttonsPanel.add(removeButton); + buttonsPanel.add(Box.createHorizontalStrut(5)); + buttonsPanel.add(Box.createHorizontalStrut(15)); - Collections.sort(uninstalledReleases, new ReverseComparator<>(new DownloadableContributionVersionComparator())); + panel.add(buttonsPanel); - downgradeChooser.removeAllItems(); - downgradeChooser.addItem(tr("Select version")); + inactiveButtonsPanel = new JPanel(); + inactiveButtonsPanel + .setLayout(new BoxLayout(inactiveButtonsPanel, BoxLayout.X_AXIS)); + inactiveButtonsPanel.setOpaque(false); - final java.util.List uninstalledPreviousReleases = new LinkedList<>(); - final java.util.List uninstalledNewerReleases = new LinkedList<>(); - - final VersionComparator versionComparator = new VersionComparator(); - uninstalledReleases.stream().forEach(input -> { - if (installed == null || versionComparator.greaterThan(installed.getParsedVersion(), input.getParsedVersion())) { - uninstalledPreviousReleases.add(input); - } else { - uninstalledNewerReleases.add(input); - } - }); - uninstalledNewerReleases.forEach(downgradeChooser::addItem); - uninstalledPreviousReleases.forEach(downgradeChooser::addItem); + int height = installButton.getMinimumSize().height; + inactiveButtonsPanel.add(Box.createVerticalStrut(height)); + inactiveButtonsPanel.add(Box.createGlue()); - downgradeChooser.setVisible(installed != null && (!uninstalledPreviousReleases.isEmpty() || uninstalledNewerReleases.size() > 1)); - downgradeButton.setVisible(installed != null && (!uninstalledPreviousReleases.isEmpty() || uninstalledNewerReleases.size() > 1)); + statusLabel = new JLabel(" "); + inactiveButtonsPanel.add(statusLabel); + inactiveButtonsPanel.add(Box.createHorizontalStrut(15)); - versionToInstallChooser.removeAllItems(); - uninstalledReleases.forEach(versionToInstallChooser::addItem); - versionToInstallChooser.setVisible(installed == null && uninstalledReleases.size() > 1); + panel.add(inactiveButtonsPanel); - Component component = getUpdatedCellComponent(value, true, row, !installedBuiltIn.isEmpty()); - component.setBackground(new Color(218, 227, 227)); //#dae3e3 - return component; + panel.add(Box.createVerticalStrut(15)); } - private Component getUpdatedCellComponent(Object value, boolean isSelected, int row, boolean hasBuiltInRelease) { + void update(JTable parentTable, Object value, boolean isSelected, int row, + boolean hasBuiltInRelease) { ContributionIndexTableModel.ContributedPlatformReleases releases = (ContributionIndexTableModel.ContributedPlatformReleases) value; JTextPane description = makeNewDescription(panel); - //FIXME: happens on macosx, don't know why + // FIXME: happens on macosx, don't know why if (releases == null) { - return panel; + return; } ContributedPlatform selected = releases.getSelected(); @@ -294,7 +168,8 @@ private Component getUpdatedCellComponent(Object value, boolean isSelected, int } else { installable = false; removable = !installed.isReadOnly() && !hasBuiltInRelease; - upgradable = new DownloadableContributionVersionComparator().compare(selected, installed) > 0; + upgradable = new DownloadableContributionVersionComparator() + .compare(selected, installed) > 0; } if (installable) { installButton.setText(tr("Install")); @@ -318,7 +193,9 @@ private Component getUpdatedCellComponent(Object value, boolean isSelected, int desc += " " + format("by {0}", author); } if (installed != null) { - desc += " " + format(tr("version {0}"), installed.getParsedVersion()) + " INSTALLED"; + desc += " " + + format(tr("version {0}"), installed.getParsedVersion()) + + " INSTALLED"; } desc += "
"; @@ -358,7 +235,8 @@ private Component getUpdatedCellComponent(Object value, boolean isSelected, int // See: // http://stackoverflow.com/questions/3081210/how-to-set-jtextarea-to-have-height-that-matches-the-size-of-a-text-it-contains int width = parentTable.getBounds().width; - setJTextPaneDimensionToFitContainedText(description, width); + InstallerTableCell.setJTextPaneDimensionToFitContainedText(description, + width); if (isSelected) { panel.setBackground(parentTable.getSelectionBackground()); @@ -367,41 +245,37 @@ private Component getUpdatedCellComponent(Object value, boolean isSelected, int panel.setBackground(parentTable.getBackground()); panel.setForeground(parentTable.getForeground()); } - - return panel; } - private final Timer enabler = new Timer(100, new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - enable(true); - enabler.stop(); + private static JTextPane makeNewDescription(JPanel panel) { + if (panel.getComponentCount() > 0) { + panel.remove(0); } - }); - - @Override - public void setEnabled(boolean enabled) { - enable(false); - if (enabled) { - enabler.start(); - } else { - enabler.stop(); + JTextPane description = new JTextPane(); + description.setInheritsPopupMenu(true); + Insets margin = description.getMargin(); + margin.bottom = 0; + description.setMargin(margin); + description.setContentType("text/html"); + Document doc = description.getDocument(); + if (doc instanceof HTMLDocument) { + HTMLDocument html = (HTMLDocument) doc; + StyleSheet stylesheet = html.getStyleSheet(); + stylesheet.addRule("body { margin: 0; padding: 0;" + + "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" + + "font-size: 100%;" + "font-size: 0.95em; }"); } - buttonsPanel.setVisible(enabled); - inactiveButtonsPanel.setVisible(!enabled); - } - - public void enable(boolean enabled) { - installButton.setEnabled(enabled); - removeButton.setEnabled(enabled); - } - - public void setStatus(String status) { - statusLabel.setText(status); - } - - public void invalidate() { - panel.invalidate(); + description.setOpaque(false); + description.setBorder(new EmptyBorder(4, 7, 7, 7)); + description.setHighlighter(null); + description.setEditable(false); + description.addHyperlinkListener(e -> { + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { + Base.openURL(e.getDescription()); + } + }); + panel.add(description, 0); + return description; } } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java new file mode 100644 index 00000000000..4c245c7d8f5 --- /dev/null +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -0,0 +1,184 @@ +/* + * This file is part of Arduino. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + * + * Arduino 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package cc.arduino.contributions.packages.ui; + +import static processing.app.I18n.tr; + +import java.awt.Color; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Collections; +import java.util.LinkedList; +import java.util.stream.Collectors; + +import javax.swing.JTable; +import javax.swing.Timer; + +import cc.arduino.contributions.DownloadableContributionVersionComparator; +import cc.arduino.contributions.VersionComparator; +import cc.arduino.contributions.filters.BuiltInPredicate; +import cc.arduino.contributions.filters.InstalledPredicate; +import cc.arduino.contributions.packages.ContributedPlatform; +import cc.arduino.contributions.ui.InstallerTableCell; +import cc.arduino.utils.ReverseComparator; + +@SuppressWarnings("serial") +public class ContributedPlatformTableCellEditor extends InstallerTableCell { + + private ContributedPlatformTableCell editorCell; + private ContributionIndexTableModel.ContributedPlatformReleases editorValue; + + @Override + public Object getCellEditorValue() { + return editorValue; + } + + @Override + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, int row, + int column) { + editorCell = new ContributedPlatformTableCell(); + editorCell.installButton + .addActionListener(e -> onInstall(editorValue.getSelected(), + editorValue.getInstalled())); + editorCell.removeButton + .addActionListener(e -> onRemove(editorValue.getInstalled())); + editorCell.downgradeButton.addActionListener(e -> { + ContributedPlatform selected = (ContributedPlatform) editorCell.downgradeChooser + .getSelectedItem(); + onInstall(selected, editorValue.getInstalled()); + }); + editorCell.versionToInstallChooser.addItemListener(e -> editorValue + .select((ContributedPlatform) editorCell.versionToInstallChooser + .getSelectedItem())); + + editorValue = (ContributionIndexTableModel.ContributedPlatformReleases) value; + setEnabled(true); + + final ContributedPlatform installed = editorValue.getInstalled(); + + java.util.List releases = new LinkedList<>( + editorValue.releases); + java.util.List uninstalledReleases = releases.stream() + .filter(new InstalledPredicate().negate()).collect(Collectors.toList()); + + java.util.List installedBuiltIn = releases.stream() + .filter(new InstalledPredicate()).filter(new BuiltInPredicate()) + .collect(Collectors.toList()); + + if (installed != null && !installedBuiltIn.contains(installed)) { + uninstalledReleases.addAll(installedBuiltIn); + } + + Collections.sort(uninstalledReleases, new ReverseComparator<>( + new DownloadableContributionVersionComparator())); + + editorCell.downgradeChooser.removeAllItems(); + editorCell.downgradeChooser.addItem(tr("Select version")); + + final java.util.List uninstalledPreviousReleases = new LinkedList<>(); + final java.util.List uninstalledNewerReleases = new LinkedList<>(); + + final VersionComparator versionComparator = new VersionComparator(); + uninstalledReleases.stream().forEach(input -> { + if (installed == null + || versionComparator.greaterThan(installed.getParsedVersion(), + input.getParsedVersion())) { + uninstalledPreviousReleases.add(input); + } else { + uninstalledNewerReleases.add(input); + } + }); + uninstalledNewerReleases.forEach(editorCell.downgradeChooser::addItem); + uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); + + editorCell.downgradeChooser + .setVisible(installed != null + && (!uninstalledPreviousReleases.isEmpty() + || uninstalledNewerReleases.size() > 1)); + editorCell.downgradeButton + .setVisible(installed != null + && (!uninstalledPreviousReleases.isEmpty() + || uninstalledNewerReleases.size() > 1)); + + editorCell.versionToInstallChooser.removeAllItems(); + uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); + editorCell.versionToInstallChooser + .setVisible(installed == null && uninstalledReleases.size() > 1); + + editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); + editorCell.panel.setBackground(new Color(218, 227, 227)); // #dae3e3 + return editorCell.panel; + } + + private final Timer enabler = new Timer(100, new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + enable(true); + enabler.stop(); + } + }); + + @Override + public void setEnabled(boolean enabled) { + enable(false); + if (enabled) { + enabler.start(); + } else { + enabler.stop(); + } + editorCell.buttonsPanel.setVisible(enabled); + editorCell.inactiveButtonsPanel.setVisible(!enabled); + } + + public void enable(boolean enabled) { + editorCell.installButton.setEnabled(enabled); + editorCell.removeButton.setEnabled(enabled); + } + + public void setStatus(String status) { + editorCell.statusLabel.setText(status); + } + + public void invalidate() { + editorCell.panel.invalidate(); + } + + protected void onRemove(ContributedPlatform contributedPlatform) { + // Empty + } + + protected void onInstall(ContributedPlatform contributedPlatform, + ContributedPlatform installed) { + // Empty + } + +} diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java new file mode 100644 index 00000000000..85339adfe63 --- /dev/null +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java @@ -0,0 +1,67 @@ +/* + * This file is part of Arduino. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + * + * Arduino 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package cc.arduino.contributions.packages.ui; + +import java.awt.Color; +import java.awt.Component; + +import javax.swing.JTable; +import javax.swing.table.TableCellRenderer; + +@SuppressWarnings("serial") +public class ContributedPlatformTableCellRenderer implements TableCellRenderer { + + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, + boolean hasFocus, int row, + int column) { + ContributedPlatformTableCell cell = new ContributedPlatformTableCell(); + cell.installButton.setEnabled(false); + cell.removeButton.setEnabled(false); + cell.buttonsPanel.setVisible(false); + cell.inactiveButtonsPanel.setVisible(true); + + cell.update(table, value, isSelected, row, false); + if (row % 2 == 0) { + cell.panel.setBackground(new Color(236, 241, 241)); // #ecf1f1 + } else { + cell.panel.setBackground(new Color(255, 255, 255)); + } + + int height = new Double(cell.panel.getPreferredSize().getHeight()) + .intValue(); + if (table.getRowHeight(row) < height) { + table.setRowHeight(row, height); + } + + return cell.panel; + } + +} diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java b/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java index 307c02cf63d..7e1389637ce 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java @@ -38,6 +38,8 @@ import processing.app.I18n; import javax.swing.*; +import javax.swing.table.TableCellRenderer; + import java.awt.*; import java.util.Collection; import java.util.LinkedList; @@ -60,15 +62,16 @@ private ContributionIndexTableModel getContribModel() { } @Override - protected InstallerTableCell createCellRenderer() { - return new ContributedPlatformTableCell(); + protected TableCellRenderer createCellRenderer() { + return new ContributedPlatformTableCellRenderer(); } @Override protected InstallerTableCell createCellEditor() { - return new ContributedPlatformTableCell() { + return new ContributedPlatformTableCellEditor() { @Override - protected void onInstall(ContributedPlatform selected, ContributedPlatform installed) { + protected void onInstall(ContributedPlatform selected, + ContributedPlatform installed) { if (selected.isReadOnly()) { onRemovePressed(installed, false); } else { @@ -84,12 +87,14 @@ protected void onRemove(ContributedPlatform installedPlatform) { } public ContributionManagerUI(Frame parent, ContributionInstaller installer) { - super(parent, tr("Boards Manager"), Dialog.ModalityType.APPLICATION_MODAL, tr("Unable to reach Arduino.cc due to possible network issues.")); + super(parent, tr("Boards Manager"), Dialog.ModalityType.APPLICATION_MODAL, + tr("Unable to reach Arduino.cc due to possible network issues.")); this.installer = installer; } public void updateUI() { - DropdownItem previouslySelectedCategory = (DropdownItem) categoryChooser.getSelectedItem(); + DropdownItem previouslySelectedCategory = (DropdownItem) categoryChooser + .getSelectedItem(); categoryChooser.removeActionListener(categoryChooserActionListener); @@ -138,7 +143,8 @@ public void onUpdatePressed() { installerThread = new Thread(() -> { try { setProgressVisible(true, ""); - List downloadedPackageIndexFiles = installer.updateIndex(this::setProgress); + List downloadedPackageIndexFiles = installer + .updateIndex(this::setProgress); installer.deleteUnknownFiles(downloadedPackageIndexFiles); onIndexesUpdated(); } catch (Exception e) { @@ -148,11 +154,14 @@ public void onUpdatePressed() { } }); installerThread.setName("ContributionManager Update Thread"); - installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage)); + installerThread + .setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler( + this, noConnectionErrorMessage)); installerThread.start(); } - public void onInstallPressed(final ContributedPlatform platformToInstall, final ContributedPlatform platformToRemove) { + public void onInstallPressed(final ContributedPlatform platformToInstall, + final ContributedPlatform platformToRemove) { clearErrorMessage(); installerThread = new Thread(() -> { List errors = new LinkedList<>(); @@ -173,15 +182,24 @@ public void onInstallPressed(final ContributedPlatform platformToInstall, final } }); installerThread.setName("ContributionManager Install Thread"); - installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage)); + installerThread + .setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler( + this, noConnectionErrorMessage)); installerThread.start(); } - public void onRemovePressed(final ContributedPlatform platform, boolean showWarning) { + public void onRemovePressed(final ContributedPlatform platform, + boolean showWarning) { clearErrorMessage(); if (showWarning) { - int chosenOption = JOptionPane.showConfirmDialog(this, I18n.format(tr("Do you want to remove {0}?\nIf you do so you won't be able to use {0} any more."), platform.getName()), tr("Please confirm boards deletion"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + int chosenOption = JOptionPane + .showConfirmDialog(this, + I18n.format(tr("Do you want to remove {0}?\nIf you do so you won't be able to use {0} any more."), + platform.getName()), + tr("Please confirm boards deletion"), + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE); if (chosenOption != JOptionPane.YES_OPTION) { return; } @@ -199,7 +217,9 @@ public void onRemovePressed(final ContributedPlatform platform, boolean showWarn } }); installerThread.setName("ContributionManager Remove Thread"); - installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage)); + installerThread + .setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler( + this, noConnectionErrorMessage)); installerThread.start(); } From c1387ed316d0f1107b57a31ab311cf160ab4ea99 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 28 Dec 2015 12:29:55 +0100 Subject: [PATCH 07/16] Removed weird hack for refreshing table cells --- .../ui/ContributedLibraryTableCellEditor.java | 27 +----------------- .../ContributedPlatformTableCellEditor.java | 28 ++----------------- 2 files changed, 3 insertions(+), 52 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java index bb838c5136b..b2f00c42357 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -33,8 +33,6 @@ import java.awt.Color; import java.awt.Component; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -42,7 +40,6 @@ import javax.swing.JComboBox; import javax.swing.JTable; -import javax.swing.Timer; import cc.arduino.contributions.DownloadableContributionVersionComparator; import cc.arduino.contributions.VersionComparator; @@ -142,39 +139,17 @@ public Component getTableCellEditorComponent(JTable table, Object value, return editorCell.panel; } - private final Timer enabler = new Timer(100, new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - enable(true); - enabler.stop(); - } - }); - @Override public void setEnabled(boolean enabled) { - enable(false); - if (enabled) { - enabler.start(); - } else { - enabler.stop(); - } + editorCell.installButton.setEnabled(enabled); editorCell.buttonsPanel.setVisible(enabled); editorCell.inactiveButtonsPanel.setVisible(!enabled); } - public void enable(boolean enabled) { - editorCell.installButton.setEnabled(enabled); - } - public void setStatus(String status) { editorCell.statusLabel.setText(status); } - public void invalidate() { - editorCell.panel.invalidate(); - } - - protected void onRemove(ContributedLibrary selected) { // Empty } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java index 4c245c7d8f5..04739980644 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -33,14 +33,11 @@ import java.awt.Color; import java.awt.Component; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.util.Collections; import java.util.LinkedList; import java.util.stream.Collectors; import javax.swing.JTable; -import javax.swing.Timer; import cc.arduino.contributions.DownloadableContributionVersionComparator; import cc.arduino.contributions.VersionComparator; @@ -139,39 +136,18 @@ public Component getTableCellEditorComponent(JTable table, Object value, return editorCell.panel; } - private final Timer enabler = new Timer(100, new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - enable(true); - enabler.stop(); - } - }); - @Override public void setEnabled(boolean enabled) { - enable(false); - if (enabled) { - enabler.start(); - } else { - enabler.stop(); - } - editorCell.buttonsPanel.setVisible(enabled); - editorCell.inactiveButtonsPanel.setVisible(!enabled); - } - - public void enable(boolean enabled) { editorCell.installButton.setEnabled(enabled); editorCell.removeButton.setEnabled(enabled); + editorCell.buttonsPanel.setVisible(enabled); + editorCell.inactiveButtonsPanel.setVisible(!enabled); } public void setStatus(String status) { editorCell.statusLabel.setText(status); } - public void invalidate() { - editorCell.panel.invalidate(); - } - protected void onRemove(ContributedPlatform contributedPlatform) { // Empty } From 015f658d1ec888b838b379dd682416c5562466c2 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 28 Dec 2015 13:24:03 +0100 Subject: [PATCH 08/16] Factored out TableCell setEnable() code --- .../libraries/ui/ContributedLibraryTableCell.java | 6 +++++- .../libraries/ui/ContributedLibraryTableCellEditor.java | 4 +--- .../libraries/ui/ContributedLibraryTableCellRenderer.java | 5 +---- .../packages/ui/ContributedPlatformTableCell.java | 7 +++++++ .../packages/ui/ContributedPlatformTableCellEditor.java | 5 +---- .../packages/ui/ContributedPlatformTableCellRenderer.java | 5 +---- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index b6f18a604b2..e3a9c6969f9 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -239,5 +239,9 @@ private static JTextPane makeNewDescription(JPanel panel) { return description; } - + public void setButtonsVisible(boolean enabled) { + installButton.setEnabled(enabled); + buttonsPanel.setVisible(enabled); + inactiveButtonsPanel.setVisible(!enabled); + } } diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java index b2f00c42357..b199be91058 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -141,9 +141,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, @Override public void setEnabled(boolean enabled) { - editorCell.installButton.setEnabled(enabled); - editorCell.buttonsPanel.setVisible(enabled); - editorCell.inactiveButtonsPanel.setVisible(!enabled); + editorCell.setButtonsVisible(enabled); } public void setStatus(String status) { diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java index 5fae977de8a..785467584c1 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java @@ -44,10 +44,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, int column) { ContributedLibraryTableCell cell = new ContributedLibraryTableCell(); - cell.installButton.setEnabled(false); - cell.buttonsPanel.setVisible(false); - cell.inactiveButtonsPanel.setVisible(true); - + cell.setButtonsVisible(false); cell.update(table, value, isSelected, row, false); if (row % 2 == 0) { diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java index 00dd772e9f0..4ab7c2a5ff5 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java @@ -278,4 +278,11 @@ private static JTextPane makeNewDescription(JPanel panel) { return description; } + public void setButtonsVisible(boolean enabled) { + installButton.setEnabled(enabled); + removeButton.setEnabled(enabled); + buttonsPanel.setVisible(enabled); + inactiveButtonsPanel.setVisible(!enabled); + } + } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java index 04739980644..b5b2f8f5cf4 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -138,10 +138,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, @Override public void setEnabled(boolean enabled) { - editorCell.installButton.setEnabled(enabled); - editorCell.removeButton.setEnabled(enabled); - editorCell.buttonsPanel.setVisible(enabled); - editorCell.inactiveButtonsPanel.setVisible(!enabled); + editorCell.setButtonsVisible(enabled); } public void setStatus(String status) { diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java index 85339adfe63..23585dab059 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java @@ -43,10 +43,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boolean hasFocus, int row, int column) { ContributedPlatformTableCell cell = new ContributedPlatformTableCell(); - cell.installButton.setEnabled(false); - cell.removeButton.setEnabled(false); - cell.buttonsPanel.setVisible(false); - cell.inactiveButtonsPanel.setVisible(true); + cell.setButtonsVisible(false); cell.update(table, value, isSelected, row, false); if (row % 2 == 0) { From 76068c92b4121651f55b6fc4e51bac4847989f1e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 28 Dec 2015 15:31:52 +0100 Subject: [PATCH 09/16] *TableCellEditor now extends JPanel --- .../ui/ContributedLibraryTableCell.java | 42 +++++++++---------- .../ui/ContributedLibraryTableCellEditor.java | 4 +- .../ContributedLibraryTableCellRenderer.java | 8 ++-- .../ui/ContributedPlatformTableCell.java | 35 ++++++++-------- .../ContributedPlatformTableCellEditor.java | 4 +- .../ContributedPlatformTableCellRenderer.java | 8 ++-- 6 files changed, 50 insertions(+), 51 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index e3a9c6969f9..ad4144f634e 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -27,9 +27,8 @@ import cc.arduino.contributions.ui.InstallerTableCell; import processing.app.Base; -public class ContributedLibraryTableCell { +public class ContributedLibraryTableCell extends JPanel { - protected final JPanel panel; protected final JButton installButton; protected final Component installButtonPlaceholder; protected final JComboBox downgradeChooser; @@ -40,6 +39,9 @@ public class ContributedLibraryTableCell { protected final JLabel statusLabel; public ContributedLibraryTableCell() { + super(); + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + installButton = new JButton(tr("Install")); int width = installButton.getPreferredSize().width; installButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); @@ -60,10 +62,7 @@ public ContributedLibraryTableCell() { versionToInstallChooser .setMaximumSize(versionToInstallChooser.getPreferredSize()); - panel = new JPanel(); - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - - makeNewDescription(panel); + makeNewDescription(); buttonsPanel = new JPanel(); buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); @@ -82,7 +81,7 @@ public ContributedLibraryTableCell() { buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(Box.createHorizontalStrut(15)); - panel.add(buttonsPanel); + add(buttonsPanel); inactiveButtonsPanel = new JPanel(); inactiveButtonsPanel @@ -97,16 +96,16 @@ public ContributedLibraryTableCell() { inactiveButtonsPanel.add(statusLabel); inactiveButtonsPanel.add(Box.createHorizontalStrut(15)); - panel.add(inactiveButtonsPanel); + add(inactiveButtonsPanel); - panel.add(Box.createVerticalStrut(15)); + add(Box.createVerticalStrut(15)); } - void update(JTable parentTable, Object value, boolean isSelected, - int row, boolean hasBuiltInRelease) { + void update(JTable parentTable, Object value, boolean isSelected, int row, + boolean hasBuiltInRelease) { ContributedLibraryReleases releases = (ContributedLibraryReleases) value; - JTextPane description = makeNewDescription(panel); + JTextPane description = makeNewDescription(); // FIXME: happens on macosx, don't know why if (releases == null) @@ -196,20 +195,21 @@ void update(JTable parentTable, Object value, boolean isSelected, // See: // http://stackoverflow.com/questions/3081210/how-to-set-jtextarea-to-have-height-that-matches-the-size-of-a-text-it-contains int width = parentTable.getBounds().width; - InstallerTableCell.setJTextPaneDimensionToFitContainedText(description, width); + InstallerTableCell.setJTextPaneDimensionToFitContainedText(description, + width); if (isSelected) { - panel.setBackground(parentTable.getSelectionBackground()); - panel.setForeground(parentTable.getSelectionForeground()); + setBackground(parentTable.getSelectionBackground()); + setForeground(parentTable.getSelectionForeground()); } else { - panel.setBackground(parentTable.getBackground()); - panel.setForeground(parentTable.getForeground()); + setBackground(parentTable.getBackground()); + setForeground(parentTable.getForeground()); } } - private static JTextPane makeNewDescription(JPanel panel) { - if (panel.getComponentCount() > 0) { - panel.remove(0); + private JTextPane makeNewDescription() { + if (getComponentCount() > 0) { + remove(0); } JTextPane description = new JTextPane(); description.setInheritsPopupMenu(true); @@ -235,7 +235,7 @@ private static JTextPane makeNewDescription(JPanel panel) { } }); // description.addKeyListener(new DelegatingKeyListener(parentTable)); - panel.add(description, 0); + add(description, 0); return description; } diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java index b199be91058..08bfdb854ad 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -135,8 +135,8 @@ public Component getTableCellEditorComponent(JTable table, Object value, .setVisible(installed == null && uninstalledReleases.size() > 1); editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); - editorCell.panel.setBackground(new Color(218, 227, 227)); // #dae3e3 - return editorCell.panel; + editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 + return editorCell; } @Override diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java index 785467584c1..a472029a3d0 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java @@ -48,18 +48,18 @@ public Component getTableCellRendererComponent(JTable table, Object value, cell.update(table, value, isSelected, row, false); if (row % 2 == 0) { - cell.panel.setBackground(new Color(236, 241, 241)); // #ecf1f1 + cell.setBackground(new Color(236, 241, 241)); // #ecf1f1 } else { - cell.panel.setBackground(new Color(255, 255, 255)); + cell.setBackground(new Color(255, 255, 255)); } - int height = new Double(cell.panel.getPreferredSize().getHeight()) + int height = new Double(cell.getPreferredSize().getHeight()) .intValue(); if (table.getRowHeight(row) < height) { table.setRowHeight(row, height); } - return cell.panel; + return cell; } } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java index 4ab7c2a5ff5..34ac5ca0ec1 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java @@ -59,9 +59,8 @@ import processing.app.Base; @SuppressWarnings("serial") -public class ContributedPlatformTableCell { +public class ContributedPlatformTableCell extends JPanel { - final JPanel panel; final JButton installButton; final JButton removeButton; final Component removeButtonPlaceholder; @@ -74,6 +73,9 @@ public class ContributedPlatformTableCell { final JLabel statusLabel; public ContributedPlatformTableCell() { + super(); + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + { installButton = new JButton(tr("Install")); int width = installButton.getPreferredSize().width; @@ -102,10 +104,7 @@ public ContributedPlatformTableCell() { versionToInstallChooser .setMaximumSize(versionToInstallChooser.getPreferredSize()); - panel = new JPanel(); - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - - makeNewDescription(panel); + makeNewDescription(); buttonsPanel = new JPanel(); buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); @@ -126,7 +125,7 @@ public ContributedPlatformTableCell() { buttonsPanel.add(Box.createHorizontalStrut(5)); buttonsPanel.add(Box.createHorizontalStrut(15)); - panel.add(buttonsPanel); + add(buttonsPanel); inactiveButtonsPanel = new JPanel(); inactiveButtonsPanel @@ -141,16 +140,16 @@ public ContributedPlatformTableCell() { inactiveButtonsPanel.add(statusLabel); inactiveButtonsPanel.add(Box.createHorizontalStrut(15)); - panel.add(inactiveButtonsPanel); + add(inactiveButtonsPanel); - panel.add(Box.createVerticalStrut(15)); + add(Box.createVerticalStrut(15)); } void update(JTable parentTable, Object value, boolean isSelected, int row, boolean hasBuiltInRelease) { ContributionIndexTableModel.ContributedPlatformReleases releases = (ContributionIndexTableModel.ContributedPlatformReleases) value; - JTextPane description = makeNewDescription(panel); + JTextPane description = makeNewDescription(); // FIXME: happens on macosx, don't know why if (releases == null) { @@ -239,17 +238,17 @@ void update(JTable parentTable, Object value, boolean isSelected, int row, width); if (isSelected) { - panel.setBackground(parentTable.getSelectionBackground()); - panel.setForeground(parentTable.getSelectionForeground()); + setBackground(parentTable.getSelectionBackground()); + setForeground(parentTable.getSelectionForeground()); } else { - panel.setBackground(parentTable.getBackground()); - panel.setForeground(parentTable.getForeground()); + setBackground(parentTable.getBackground()); + setForeground(parentTable.getForeground()); } } - private static JTextPane makeNewDescription(JPanel panel) { - if (panel.getComponentCount() > 0) { - panel.remove(0); + private JTextPane makeNewDescription() { + if (getComponentCount() > 0) { + remove(0); } JTextPane description = new JTextPane(); description.setInheritsPopupMenu(true); @@ -274,7 +273,7 @@ private static JTextPane makeNewDescription(JPanel panel) { Base.openURL(e.getDescription()); } }); - panel.add(description, 0); + add(description, 0); return description; } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java index b5b2f8f5cf4..20e8170b86f 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -132,8 +132,8 @@ public Component getTableCellEditorComponent(JTable table, Object value, .setVisible(installed == null && uninstalledReleases.size() > 1); editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); - editorCell.panel.setBackground(new Color(218, 227, 227)); // #dae3e3 - return editorCell.panel; + editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 + return editorCell; } @Override diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java index 23585dab059..fa0230eb0d1 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java @@ -47,18 +47,18 @@ public Component getTableCellRendererComponent(JTable table, Object value, cell.update(table, value, isSelected, row, false); if (row % 2 == 0) { - cell.panel.setBackground(new Color(236, 241, 241)); // #ecf1f1 + cell.setBackground(new Color(236, 241, 241)); // #ecf1f1 } else { - cell.panel.setBackground(new Color(255, 255, 255)); + cell.setBackground(new Color(255, 255, 255)); } - int height = new Double(cell.panel.getPreferredSize().getHeight()) + int height = new Double(cell.getPreferredSize().getHeight()) .intValue(); if (table.getRowHeight(row) < height) { table.setRowHeight(row, height); } - return cell.panel; + return cell; } } From 47fcf318b113ec97a53bb736c8392d68fe65dc00 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Dec 2015 10:48:11 +0100 Subject: [PATCH 10/16] Removed unused parameters in *TableCell.update(..) method --- .../libraries/ui/ContributedLibraryTableCell.java | 3 +-- .../libraries/ui/ContributedLibraryTableCellEditor.java | 2 +- .../libraries/ui/ContributedLibraryTableCellRenderer.java | 2 +- .../packages/ui/ContributedPlatformTableCell.java | 2 +- .../packages/ui/ContributedPlatformTableCellEditor.java | 2 +- .../packages/ui/ContributedPlatformTableCellRenderer.java | 7 +++---- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index ad4144f634e..3645149f887 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -101,8 +101,7 @@ public ContributedLibraryTableCell() { add(Box.createVerticalStrut(15)); } - void update(JTable parentTable, Object value, boolean isSelected, int row, - boolean hasBuiltInRelease) { + void update(JTable parentTable, Object value, boolean isSelected) { ContributedLibraryReleases releases = (ContributedLibraryReleases) value; JTextPane description = makeNewDescription(); diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java index 08bfdb854ad..f825210b685 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -134,7 +134,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, editorCell.versionToInstallChooser .setVisible(installed == null && uninstalledReleases.size() > 1); - editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); + editorCell.update(table, value, true); editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 return editorCell; } diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java index a472029a3d0..1613d7aafa1 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java @@ -45,7 +45,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, ContributedLibraryTableCell cell = new ContributedLibraryTableCell(); cell.setButtonsVisible(false); - cell.update(table, value, isSelected, row, false); + cell.update(table, value, isSelected); if (row % 2 == 0) { cell.setBackground(new Color(236, 241, 241)); // #ecf1f1 diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java index 34ac5ca0ec1..d0cdba0b9dd 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java @@ -145,7 +145,7 @@ public ContributedPlatformTableCell() { add(Box.createVerticalStrut(15)); } - void update(JTable parentTable, Object value, boolean isSelected, int row, + void update(JTable parentTable, Object value, boolean isSelected, boolean hasBuiltInRelease) { ContributionIndexTableModel.ContributedPlatformReleases releases = (ContributionIndexTableModel.ContributedPlatformReleases) value; diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java index 20e8170b86f..cee5108264b 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -131,7 +131,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, editorCell.versionToInstallChooser .setVisible(installed == null && uninstalledReleases.size() > 1); - editorCell.update(table, value, true, row, !installedBuiltIn.isEmpty()); + editorCell.update(table, value, true, !installedBuiltIn.isEmpty()); editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 return editorCell; } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java index fa0230eb0d1..b3a45bc0b11 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java @@ -44,16 +44,15 @@ public Component getTableCellRendererComponent(JTable table, Object value, int column) { ContributedPlatformTableCell cell = new ContributedPlatformTableCell(); cell.setButtonsVisible(false); - - cell.update(table, value, isSelected, row, false); + cell.update(table, value, isSelected, false); + if (row % 2 == 0) { cell.setBackground(new Color(236, 241, 241)); // #ecf1f1 } else { cell.setBackground(new Color(255, 255, 255)); } - int height = new Double(cell.getPreferredSize().getHeight()) - .intValue(); + int height = new Double(cell.getPreferredSize().getHeight()).intValue(); if (table.getRowHeight(row) < height) { table.setRowHeight(row, height); } From 71eb3c3ab21a6c93ace3e527abeefaaaaa4b556e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Dec 2015 14:09:29 +0100 Subject: [PATCH 11/16] Removed useless DESCRIPTION_COL constant --- .../libraries/ui/LibrariesIndexTableModel.java | 12 +++--------- .../packages/ui/ContributionIndexTableModel.java | 13 +++---------- .../arduino/contributions/ui/InstallerJDialog.java | 3 +-- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java index 79917e0b14b..63d51571144 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java @@ -43,7 +43,6 @@ @SuppressWarnings("serial") public class LibrariesIndexTableModel extends FilteredAbstractTableModel { - public final static int DESCRIPTION_COL = 0; private final List contributions = new ArrayList<>(); @@ -120,9 +119,7 @@ public Class getColumnClass(int colum) { @Override public void setValueAt(Object value, int row, int col) { - if (col == DESCRIPTION_COL) { - fireTableCellUpdated(row, col); - } + fireTableCellUpdated(row, col); } @Override @@ -131,15 +128,12 @@ public Object getValueAt(int row, int col) { return null; } ContributedLibraryReleases contribution = contributions.get(row); - if (col == DESCRIPTION_COL) { - return contribution;// .getSelected(); - } - return null; + return contribution;// .getSelected(); } @Override public boolean isCellEditable(int row, int col) { - return col == DESCRIPTION_COL; + return true; } public ContributedLibraryReleases getReleases(int row) { diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java b/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java index 212c8617deb..cf8fe86d78b 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java @@ -48,8 +48,6 @@ @SuppressWarnings("serial") public class ContributionIndexTableModel extends FilteredAbstractTableModel { - public final static int DESCRIPTION_COL = 0; - public static class ContributedPlatformReleases { public final ContributedPackage packager; public final String arch; @@ -184,9 +182,7 @@ public Class getColumnClass(int colum) { @Override public void setValueAt(Object value, int row, int col) { - if (col == DESCRIPTION_COL) { - fireTableCellUpdated(row, col); - } + fireTableCellUpdated(row, col); } @Override @@ -195,15 +191,12 @@ public Object getValueAt(int row, int col) { return null; } ContributedPlatformReleases contribution = contributions.get(row); - if (col == DESCRIPTION_COL) { - return contribution;// .getSelected(); - } - return null; + return contribution;// .getSelected(); } @Override public boolean isCellEditable(int row, int col) { - return col == DESCRIPTION_COL; + return true; } public ContributedPlatformReleases getReleases(int row) { diff --git a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java index 14c53b02500..9de0dc136cd 100644 --- a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java +++ b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java @@ -29,7 +29,6 @@ package cc.arduino.contributions.ui; -import static cc.arduino.contributions.packages.ui.ContributionIndexTableModel.DESCRIPTION_COL; import static processing.app.I18n.tr; import java.awt.BorderLayout; @@ -165,7 +164,7 @@ public void keyReleased(KeyEvent keyEvent) { { TableColumnModel tcm = contribTable.getColumnModel(); - TableColumn col = tcm.getColumn(DESCRIPTION_COL); + TableColumn col = tcm.getColumn(0); col.setCellRenderer(createCellRenderer()); col.setCellEditor(createCellEditor()); col.setResizable(true); From 91f18dff243b246afce7a501301739e50f19239b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Dec 2015 14:14:52 +0100 Subject: [PATCH 12/16] Factored out ContributedLibraryTableCell.update(...) method --- .../ui/ContributedLibraryTableCell.java | 30 +++++++++---------- .../ui/ContributedLibraryTableCellEditor.java | 3 +- .../ContributedLibraryTableCellRenderer.java | 8 ++--- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java index 3645149f887..671489a9765 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java @@ -29,16 +29,17 @@ public class ContributedLibraryTableCell extends JPanel { - protected final JButton installButton; - protected final Component installButtonPlaceholder; - protected final JComboBox downgradeChooser; - protected final JComboBox versionToInstallChooser; - protected final JButton downgradeButton; - protected final JPanel buttonsPanel; - protected final JPanel inactiveButtonsPanel; - protected final JLabel statusLabel; - - public ContributedLibraryTableCell() { + final JButton installButton; + final Component installButtonPlaceholder; + final JComboBox downgradeChooser; + final JComboBox versionToInstallChooser; + final JButton downgradeButton; + final JPanel buttonsPanel; + final JPanel inactiveButtonsPanel; + final JLabel statusLabel; + + public ContributedLibraryTableCell(JTable parentTable, Object value, + boolean isSelected) { super(); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); @@ -99,11 +100,8 @@ public ContributedLibraryTableCell() { add(inactiveButtonsPanel); add(Box.createVerticalStrut(15)); - } - void update(JTable parentTable, Object value, boolean isSelected) { ContributedLibraryReleases releases = (ContributedLibraryReleases) value; - JTextPane description = makeNewDescription(); // FIXME: happens on macosx, don't know why @@ -193,9 +191,9 @@ void update(JTable parentTable, Object value, boolean isSelected) { // See: // http://stackoverflow.com/questions/3081210/how-to-set-jtextarea-to-have-height-that-matches-the-size-of-a-text-it-contains - int width = parentTable.getBounds().width; - InstallerTableCell.setJTextPaneDimensionToFitContainedText(description, - width); + InstallerTableCell + .setJTextPaneDimensionToFitContainedText(description, + parentTable.getBounds().width); if (isSelected) { setBackground(parentTable.getSelectionBackground()); diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java index f825210b685..4ab9d4b5f14 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -67,7 +67,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, int column) { editorValue = (ContributedLibraryReleases) value; - editorCell = new ContributedLibraryTableCell(); + editorCell = new ContributedLibraryTableCell(table, value, true); editorCell.installButton .addActionListener(e -> onInstall(editorValue.getSelected(), editorValue.getInstalled())); @@ -134,7 +134,6 @@ public Component getTableCellEditorComponent(JTable table, Object value, editorCell.versionToInstallChooser .setVisible(installed == null && uninstalledReleases.size() > 1); - editorCell.update(table, value, true); editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 return editorCell; } diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java index 1613d7aafa1..ffee3b24ec8 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java @@ -42,10 +42,9 @@ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - - ContributedLibraryTableCell cell = new ContributedLibraryTableCell(); + ContributedLibraryTableCell cell = new ContributedLibraryTableCell(table, + value, isSelected); cell.setButtonsVisible(false); - cell.update(table, value, isSelected); if (row % 2 == 0) { cell.setBackground(new Color(236, 241, 241)); // #ecf1f1 @@ -53,8 +52,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, cell.setBackground(new Color(255, 255, 255)); } - int height = new Double(cell.getPreferredSize().getHeight()) - .intValue(); + int height = new Double(cell.getPreferredSize().getHeight()).intValue(); if (table.getRowHeight(row) < height) { table.setRowHeight(row, height); } From d598f0cfa7165ec139e331a699b591b66a3794aa Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Dec 2015 15:53:02 +0100 Subject: [PATCH 13/16] Removed unused LibraryManagerUI.getContribModel() method --- .../arduino/contributions/libraries/ui/LibraryManagerUI.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java index 943c21fdd7e..4c94700197f 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java @@ -71,10 +71,6 @@ protected FilteredAbstractTableModel createContribModel() { return new LibrariesIndexTableModel(); } - private LibrariesIndexTableModel getContribModel() { - return (LibrariesIndexTableModel) contribModel; - } - @Override protected TableCellRenderer createCellRenderer() { return new ContributedLibraryTableCellRenderer(); From 2b9483b6e6c9ea384e2fccfee7977786c3d11798 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Dec 2015 15:58:27 +0100 Subject: [PATCH 14/16] Extract ContributionIndexTableModel as outer class --- .../ui/ContributedPlatformReleases.java | 103 ++++++++++++++++++ .../ui/ContributedPlatformTableCell.java | 2 +- .../ContributedPlatformTableCellEditor.java | 4 +- .../ui/ContributionIndexTableModel.java | 64 +---------- 4 files changed, 108 insertions(+), 65 deletions(-) create mode 100644 app/src/cc/arduino/contributions/packages/ui/ContributedPlatformReleases.java diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformReleases.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformReleases.java new file mode 100644 index 00000000000..3c356749e7b --- /dev/null +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformReleases.java @@ -0,0 +1,103 @@ +/* + * This file is part of Arduino. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + * + * Arduino 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package cc.arduino.contributions.packages.ui; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; +import cc.arduino.contributions.filters.InstalledPredicate; +import cc.arduino.contributions.packages.ContributedPackage; +import cc.arduino.contributions.packages.ContributedPlatform; + +public class ContributedPlatformReleases { + + public final ContributedPackage packager; + public final String arch; + public final List releases; + public final List versions; + public ContributedPlatform selected = null; + + public ContributedPlatformReleases(ContributedPlatform platform) { + packager = platform.getParentPackage(); + arch = platform.getArchitecture(); + releases = new LinkedList<>(); + versions = new LinkedList<>(); + add(platform); + } + + public boolean shouldContain(ContributedPlatform platform) { + if (platform.getParentPackage() != packager) + return false; + return platform.getArchitecture().equals(arch); + } + + public void add(ContributedPlatform platform) { + releases.add(platform); + String version = platform.getParsedVersion(); + if (version != null) { + versions.add(version); + } + selected = getLatest(); + } + + public ContributedPlatform getInstalled() { + List installedReleases = releases.stream() + .filter(new InstalledPredicate()).collect(Collectors.toList()); + Collections + .sort(installedReleases, + new DownloadableContributionBuiltInAtTheBottomComparator()); + + if (installedReleases.isEmpty()) { + return null; + } + + return installedReleases.get(0); + } + + public ContributedPlatform getLatest() { + return ContributionIndexTableModel.getLatestOf(releases); + } + + public ContributedPlatform getSelected() { + return selected; + } + + public void select(ContributedPlatform value) { + for (ContributedPlatform plat : releases) { + if (plat == value) { + selected = plat; + return; + } + } + } +} \ No newline at end of file diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java index d0cdba0b9dd..3fb0c2e808c 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java @@ -147,7 +147,7 @@ public ContributedPlatformTableCell() { void update(JTable parentTable, Object value, boolean isSelected, boolean hasBuiltInRelease) { - ContributionIndexTableModel.ContributedPlatformReleases releases = (ContributionIndexTableModel.ContributedPlatformReleases) value; + ContributedPlatformReleases releases = (ContributedPlatformReleases) value; JTextPane description = makeNewDescription(); diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java index cee5108264b..f24352ff371 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -51,7 +51,7 @@ public class ContributedPlatformTableCellEditor extends InstallerTableCell { private ContributedPlatformTableCell editorCell; - private ContributionIndexTableModel.ContributedPlatformReleases editorValue; + private ContributedPlatformReleases editorValue; @Override public Object getCellEditorValue() { @@ -77,7 +77,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, .select((ContributedPlatform) editorCell.versionToInstallChooser .getSelectedItem())); - editorValue = (ContributionIndexTableModel.ContributedPlatformReleases) value; + editorValue = (ContributedPlatformReleases) value; setEnabled(true); final ContributedPlatform installed = editorValue.getInstalled(); diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java b/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java index cf8fe86d78b..e186fc95afe 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java @@ -38,74 +38,14 @@ import processing.app.BaseNoGui; import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @SuppressWarnings("serial") -public class ContributionIndexTableModel extends FilteredAbstractTableModel { - - public static class ContributedPlatformReleases { - public final ContributedPackage packager; - public final String arch; - public final List releases; - public final List versions; - public ContributedPlatform selected = null; - - public ContributedPlatformReleases(ContributedPlatform platform) { - this.packager = platform.getParentPackage(); - this.arch = platform.getArchitecture(); - this.releases = new LinkedList<>(); - this.versions = new LinkedList<>(); - add(platform); - } - - public boolean shouldContain(ContributedPlatform platform) { - if (platform.getParentPackage() != packager) - return false; - return platform.getArchitecture().equals(arch); - } - - public void add(ContributedPlatform platform) { - releases.add(platform); - String version = platform.getParsedVersion(); - if (version != null) { - versions.add(version); - } - selected = getLatest(); - } - - public ContributedPlatform getInstalled() { - List installedReleases = releases.stream().filter(new InstalledPredicate()).collect(Collectors.toList()); - Collections.sort(installedReleases, new DownloadableContributionBuiltInAtTheBottomComparator()); - - if (installedReleases.isEmpty()) { - return null; - } - - return installedReleases.get(0); - } - - public ContributedPlatform getLatest() { - return getLatestOf(releases); - } - - public ContributedPlatform getSelected() { - return selected; - } - - public void select(ContributedPlatform value) { - for (ContributedPlatform plat : releases) { - if (plat == value) { - selected = plat; - return; - } - } - } - } +public class ContributionIndexTableModel + extends FilteredAbstractTableModel { private final List contributions = new ArrayList<>(); From a7e22e1958bd675a427f9802725b39732feeb0ac Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Dec 2015 15:58:49 +0100 Subject: [PATCH 15/16] Some minor cosmetics --- .../ui/LibrariesIndexTableModel.java | 25 +++--- .../ui/ContributedPlatformTableCell.java | 2 +- .../ContributedPlatformTableCellEditor.java | 86 +++++++++---------- .../ui/ContributionIndexTableModel.java | 21 ++--- 4 files changed, 68 insertions(+), 66 deletions(-) diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java index 63d51571144..40bd655ad8e 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java @@ -41,19 +41,20 @@ import java.util.stream.Stream; @SuppressWarnings("serial") -public class LibrariesIndexTableModel extends FilteredAbstractTableModel { - +public class LibrariesIndexTableModel + extends FilteredAbstractTableModel { private final List contributions = new ArrayList<>(); - private final String[] columnNames = {"Description"}; + private final String[] columnNames = { "Description" }; - private final Class[] columnTypes = {ContributedPlatform.class}; + private final Class[] columnTypes = { ContributedPlatform.class }; Predicate selectedCategoryFilter = null; String selectedFilters[] = null; - public void updateIndexFilter(String filters[], Stream> additionalFilters) { + public void updateIndexFilter(String filters[], + Stream> additionalFilters) { selectedCategoryFilter = additionalFilters.reduce(Predicate::and).get(); selectedFilters = filters; update(); @@ -66,7 +67,7 @@ public void updateIndexFilter(String filters[], Streamtrue if all the strings in set are contained in - * string. + * string. */ private boolean stringContainsAll(String string, String filters[]) { if (string == null) { @@ -154,7 +155,8 @@ private void applyFilterToLibrary(ContributedLibrary lib) { return; } - String compoundTargetSearchText = lib.getName() + "\n" + lib.getParagraph() + "\n" + lib.getSentence(); + String compoundTargetSearchText = lib.getName() + "\n" + lib.getParagraph() + + "\n" + lib.getSentence(); if (!stringContainsAll(compoundTargetSearchText, selectedFilters)) { return; } @@ -190,9 +192,12 @@ public void updateLibrary(ContributedLibrary lib) { private void updateContributions() { contributions.clear(); - BaseNoGui.librariesIndexer.getIndex().getLibraries().forEach(this::applyFilterToLibrary); - BaseNoGui.librariesIndexer.getInstalledLibraries().forEach(this::applyFilterToLibrary); - Collections.sort(contributions, new ContributedLibraryReleasesComparator("Arduino")); + BaseNoGui.librariesIndexer.getIndex().getLibraries() + .forEach(this::applyFilterToLibrary); + BaseNoGui.librariesIndexer.getInstalledLibraries() + .forEach(this::applyFilterToLibrary); + Collections.sort(contributions, + new ContributedLibraryReleasesComparator("Arduino")); } } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java index 3fb0c2e808c..95d61e36fe7 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java @@ -65,7 +65,7 @@ public class ContributedPlatformTableCell extends JPanel { final JButton removeButton; final Component removeButtonPlaceholder; final Component installButtonPlaceholder; - JComboBox downgradeChooser; + final JComboBox downgradeChooser; final JComboBox versionToInstallChooser; final JButton downgradeButton; final JPanel buttonsPanel; diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java index f24352ff371..4e066ffa4c6 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -35,6 +35,7 @@ import java.awt.Component; import java.util.Collections; import java.util.LinkedList; +import java.util.List; import java.util.stream.Collectors; import javax.swing.JTable; @@ -50,44 +51,42 @@ @SuppressWarnings("serial") public class ContributedPlatformTableCellEditor extends InstallerTableCell { - private ContributedPlatformTableCell editorCell; - private ContributedPlatformReleases editorValue; + private ContributedPlatformTableCell cell; + private ContributedPlatformReleases value; @Override public Object getCellEditorValue() { - return editorValue; + return value; } @Override - public Component getTableCellEditorComponent(JTable table, Object value, + public Component getTableCellEditorComponent(JTable table, Object _value, boolean isSelected, int row, int column) { - editorCell = new ContributedPlatformTableCell(); - editorCell.installButton - .addActionListener(e -> onInstall(editorValue.getSelected(), - editorValue.getInstalled())); - editorCell.removeButton - .addActionListener(e -> onRemove(editorValue.getInstalled())); - editorCell.downgradeButton.addActionListener(e -> { - ContributedPlatform selected = (ContributedPlatform) editorCell.downgradeChooser + value = (ContributedPlatformReleases) _value; + + cell = new ContributedPlatformTableCell(); + cell.installButton.addActionListener(e -> onInstall(value.getSelected(), + value.getInstalled())); + cell.removeButton.addActionListener(e -> onRemove(value.getInstalled())); + cell.downgradeButton.addActionListener(e -> { + ContributedPlatform selected = (ContributedPlatform) cell.downgradeChooser .getSelectedItem(); - onInstall(selected, editorValue.getInstalled()); + onInstall(selected, value.getInstalled()); }); - editorCell.versionToInstallChooser.addItemListener(e -> editorValue - .select((ContributedPlatform) editorCell.versionToInstallChooser + cell.versionToInstallChooser.addItemListener(e -> value + .select((ContributedPlatform) cell.versionToInstallChooser .getSelectedItem())); - editorValue = (ContributedPlatformReleases) value; setEnabled(true); - final ContributedPlatform installed = editorValue.getInstalled(); + final ContributedPlatform installed = value.getInstalled(); - java.util.List releases = new LinkedList<>( - editorValue.releases); - java.util.List uninstalledReleases = releases.stream() + List releases = new LinkedList<>(value.releases); + List uninstalledReleases = releases.stream() .filter(new InstalledPredicate().negate()).collect(Collectors.toList()); - java.util.List installedBuiltIn = releases.stream() + List installedBuiltIn = releases.stream() .filter(new InstalledPredicate()).filter(new BuiltInPredicate()) .collect(Collectors.toList()); @@ -98,11 +97,11 @@ public Component getTableCellEditorComponent(JTable table, Object value, Collections.sort(uninstalledReleases, new ReverseComparator<>( new DownloadableContributionVersionComparator())); - editorCell.downgradeChooser.removeAllItems(); - editorCell.downgradeChooser.addItem(tr("Select version")); + cell.downgradeChooser.removeAllItems(); + cell.downgradeChooser.addItem(tr("Select version")); - final java.util.List uninstalledPreviousReleases = new LinkedList<>(); - final java.util.List uninstalledNewerReleases = new LinkedList<>(); + final List uninstalledPreviousReleases = new LinkedList<>(); + final List uninstalledNewerReleases = new LinkedList<>(); final VersionComparator versionComparator = new VersionComparator(); uninstalledReleases.stream().forEach(input -> { @@ -114,35 +113,32 @@ public Component getTableCellEditorComponent(JTable table, Object value, uninstalledNewerReleases.add(input); } }); - uninstalledNewerReleases.forEach(editorCell.downgradeChooser::addItem); - uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); - - editorCell.downgradeChooser - .setVisible(installed != null - && (!uninstalledPreviousReleases.isEmpty() - || uninstalledNewerReleases.size() > 1)); - editorCell.downgradeButton - .setVisible(installed != null - && (!uninstalledPreviousReleases.isEmpty() - || uninstalledNewerReleases.size() > 1)); - - editorCell.versionToInstallChooser.removeAllItems(); - uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); - editorCell.versionToInstallChooser + uninstalledNewerReleases.forEach(cell.downgradeChooser::addItem); + uninstalledPreviousReleases.forEach(cell.downgradeChooser::addItem); + + boolean downgradeVisible = installed != null + && (!uninstalledPreviousReleases.isEmpty() + || uninstalledNewerReleases.size() > 1); + cell.downgradeChooser.setVisible(downgradeVisible); + cell.downgradeButton.setVisible(downgradeVisible); + + cell.versionToInstallChooser.removeAllItems(); + uninstalledReleases.forEach(cell.versionToInstallChooser::addItem); + cell.versionToInstallChooser .setVisible(installed == null && uninstalledReleases.size() > 1); - editorCell.update(table, value, true, !installedBuiltIn.isEmpty()); - editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 - return editorCell; + cell.update(table, _value, true, !installedBuiltIn.isEmpty()); + cell.setBackground(new Color(218, 227, 227)); // #dae3e3 + return cell; } @Override public void setEnabled(boolean enabled) { - editorCell.setButtonsVisible(enabled); + cell.setButtonsVisible(enabled); } public void setStatus(String status) { - editorCell.statusLabel.setText(status); + cell.statusLabel.setText(status); } protected void onRemove(ContributedPlatform contributedPlatform) { diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java b/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java index e186fc95afe..283df1067c2 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributionIndexTableModel.java @@ -29,8 +29,6 @@ package cc.arduino.contributions.packages.ui; -import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator; -import cc.arduino.contributions.filters.InstalledPredicate; import cc.arduino.contributions.packages.ContributedBoard; import cc.arduino.contributions.packages.ContributedPackage; import cc.arduino.contributions.packages.ContributedPlatform; @@ -48,17 +46,20 @@ public class ContributionIndexTableModel extends FilteredAbstractTableModel { private final List contributions = new ArrayList<>(); + private final String[] columnNames = { "Description" }; + private final Class[] columnTypes = { ContributedPlatform.class }; - private final String[] columnNames = {"Description"}; - - private final Class[] columnTypes = {ContributedPlatform.class}; - - public void updateIndexFilter(String[] filters, Stream> additionalFilters) { + public void updateIndexFilter(String[] filters, + Stream> additionalFilters) { contributions.clear(); - Predicate filter = additionalFilters.reduce(Predicate::and).get(); + Predicate filter = additionalFilters + .reduce(Predicate::and).get(); for (ContributedPackage pack : BaseNoGui.indexer.getPackages()) { for (ContributedPlatform platform : pack.getPlatforms()) { - String compoundTargetSearchText = platform.getName() + "\n" + platform.getBoards().stream().map(ContributedBoard::getName).collect(Collectors.joining(" ")); + String compoundTargetSearchText = platform.getName() + "\n" + + platform.getBoards().stream() + .map(ContributedBoard::getName) + .collect(Collectors.joining(" ")); if (!filter.test(platform)) { continue; } @@ -77,7 +78,7 @@ public void updateIndexFilter(String[] filters, Streamtrue if all the strings in set are contained in - * string. + * string. */ private boolean stringContainsAll(String string, String set[]) { if (set == null) From 16c470180218b00eff842d1204abb2c6e0816ece Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Dec 2015 16:01:07 +0100 Subject: [PATCH 16/16] Rename Contributed*TableCell to Contributed*TableCellJPanel --- .../libraries/ui/ContributedLibraryTableCellEditor.java | 4 ++-- ...yTableCell.java => ContributedLibraryTableCellJPanel.java} | 4 ++-- .../libraries/ui/ContributedLibraryTableCellRenderer.java | 2 +- .../packages/ui/ContributedPlatformTableCellEditor.java | 4 ++-- ...TableCell.java => ContributedPlatformTableCellJPanel.java} | 4 ++-- .../packages/ui/ContributedPlatformTableCellRenderer.java | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) rename app/src/cc/arduino/contributions/libraries/ui/{ContributedLibraryTableCell.java => ContributedLibraryTableCellJPanel.java} (98%) rename app/src/cc/arduino/contributions/packages/ui/{ContributedPlatformTableCell.java => ContributedPlatformTableCellJPanel.java} (98%) diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java index 4ab9d4b5f14..f2299ac52b5 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java @@ -54,7 +54,7 @@ public class ContributedLibraryTableCellEditor extends InstallerTableCell { private ContributedLibraryReleases editorValue; - private ContributedLibraryTableCell editorCell; + private ContributedLibraryTableCellJPanel editorCell; @Override public Object getCellEditorValue() { @@ -67,7 +67,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, int column) { editorValue = (ContributedLibraryReleases) value; - editorCell = new ContributedLibraryTableCell(table, value, true); + editorCell = new ContributedLibraryTableCellJPanel(table, value, true); editorCell.installButton .addActionListener(e -> onInstall(editorValue.getSelected(), editorValue.getInstalled())); diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java similarity index 98% rename from app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java rename to app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java index 671489a9765..59087b6b001 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCell.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java @@ -27,7 +27,7 @@ import cc.arduino.contributions.ui.InstallerTableCell; import processing.app.Base; -public class ContributedLibraryTableCell extends JPanel { +public class ContributedLibraryTableCellJPanel extends JPanel { final JButton installButton; final Component installButtonPlaceholder; @@ -38,7 +38,7 @@ public class ContributedLibraryTableCell extends JPanel { final JPanel inactiveButtonsPanel; final JLabel statusLabel; - public ContributedLibraryTableCell(JTable parentTable, Object value, + public ContributedLibraryTableCellJPanel(JTable parentTable, Object value, boolean isSelected) { super(); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); diff --git a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java index ffee3b24ec8..bc4b3ffd940 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellRenderer.java @@ -42,7 +42,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - ContributedLibraryTableCell cell = new ContributedLibraryTableCell(table, + ContributedLibraryTableCellJPanel cell = new ContributedLibraryTableCellJPanel(table, value, isSelected); cell.setButtonsVisible(false); diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java index 4e066ffa4c6..cc3cfbb63f5 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java @@ -51,7 +51,7 @@ @SuppressWarnings("serial") public class ContributedPlatformTableCellEditor extends InstallerTableCell { - private ContributedPlatformTableCell cell; + private ContributedPlatformTableCellJPanel cell; private ContributedPlatformReleases value; @Override @@ -65,7 +65,7 @@ public Component getTableCellEditorComponent(JTable table, Object _value, int column) { value = (ContributedPlatformReleases) _value; - cell = new ContributedPlatformTableCell(); + cell = new ContributedPlatformTableCellJPanel(); cell.installButton.addActionListener(e -> onInstall(value.getSelected(), value.getInstalled())); cell.removeButton.addActionListener(e -> onRemove(value.getInstalled())); diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellJPanel.java similarity index 98% rename from app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java rename to app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellJPanel.java index 95d61e36fe7..7ed3b4480f3 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCell.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellJPanel.java @@ -59,7 +59,7 @@ import processing.app.Base; @SuppressWarnings("serial") -public class ContributedPlatformTableCell extends JPanel { +public class ContributedPlatformTableCellJPanel extends JPanel { final JButton installButton; final JButton removeButton; @@ -72,7 +72,7 @@ public class ContributedPlatformTableCell extends JPanel { final JPanel inactiveButtonsPanel; final JLabel statusLabel; - public ContributedPlatformTableCell() { + public ContributedPlatformTableCellJPanel() { super(); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java index b3a45bc0b11..cc4b1d0c186 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellRenderer.java @@ -42,7 +42,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - ContributedPlatformTableCell cell = new ContributedPlatformTableCell(); + ContributedPlatformTableCellJPanel cell = new ContributedPlatformTableCellJPanel(); cell.setButtonsVisible(false); cell.update(table, value, isSelected, false);