|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) |
| 5 | + * |
| 6 | + * Arduino is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + * |
| 20 | + * As a special exception, you may use this file as part of a free software |
| 21 | + * library without restriction. Specifically, if other files instantiate |
| 22 | + * templates or use macros or inline functions from this file, or you compile |
| 23 | + * this file and link it with other files to produce an executable, this |
| 24 | + * file does not by itself cause the resulting executable to be covered by |
| 25 | + * the GNU General Public License. This exception does not however |
| 26 | + * invalidate any other reasons why the executable file might be covered by |
| 27 | + * the GNU General Public License. |
| 28 | + */ |
| 29 | + |
| 30 | +package cc.arduino.contributions.libraries.ui; |
| 31 | + |
| 32 | +import static processing.app.I18n.tr; |
| 33 | + |
| 34 | +import java.awt.Color; |
| 35 | +import java.awt.Component; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.LinkedList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +import javax.swing.JComboBox; |
| 42 | +import javax.swing.JTable; |
| 43 | + |
| 44 | +import cc.arduino.contributions.DownloadableContributionVersionComparator; |
| 45 | +import cc.arduino.contributions.VersionComparator; |
| 46 | +import cc.arduino.contributions.filters.BuiltInPredicate; |
| 47 | +import cc.arduino.contributions.filters.InstalledPredicate; |
| 48 | +import cc.arduino.contributions.libraries.ContributedLibrary; |
| 49 | +import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate; |
| 50 | +import cc.arduino.contributions.ui.InstallerTableCell; |
| 51 | +import cc.arduino.utils.ReverseComparator; |
| 52 | + |
| 53 | +@SuppressWarnings("serial") |
| 54 | +public class ContributedLibraryTableCellEditor extends InstallerTableCell { |
| 55 | + |
| 56 | + private ContributedLibraryReleases editorValue; |
| 57 | + private ContributedLibraryTableCellJPanel editorCell; |
| 58 | + |
| 59 | + @Override |
| 60 | + public Object getCellEditorValue() { |
| 61 | + return editorValue; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Component getTableCellEditorComponent(JTable table, Object value, |
| 66 | + boolean isSelected, int row, |
| 67 | + int column) { |
| 68 | + editorValue = (ContributedLibraryReleases) value; |
| 69 | + |
| 70 | + editorCell = new ContributedLibraryTableCellJPanel(table, value, true); |
| 71 | + editorCell.installButton |
| 72 | + .addActionListener(e -> onInstall(editorValue.getSelected(), |
| 73 | + editorValue.getInstalled())); |
| 74 | + editorCell.downgradeButton.addActionListener(e -> { |
| 75 | + JComboBox chooser = editorCell.downgradeChooser; |
| 76 | + ContributedLibrary lib = (ContributedLibrary) chooser.getSelectedItem(); |
| 77 | + onInstall(lib, editorValue.getInstalled()); |
| 78 | + }); |
| 79 | + editorCell.versionToInstallChooser.addItemListener(e -> editorValue |
| 80 | + .select((ContributedLibrary) editorCell.versionToInstallChooser |
| 81 | + .getSelectedItem())); |
| 82 | + |
| 83 | + setEnabled(true); |
| 84 | + |
| 85 | + final ContributedLibrary installed = editorValue.getInstalled(); |
| 86 | + |
| 87 | + List<ContributedLibrary> releases = editorValue.getReleases().stream() |
| 88 | + .filter(new OnlyUpstreamReleasePredicate()) |
| 89 | + .collect(Collectors.toList()); |
| 90 | + List<ContributedLibrary> uninstalledReleases = releases.stream() |
| 91 | + .filter(new InstalledPredicate().negate()).collect(Collectors.toList()); |
| 92 | + |
| 93 | + List<ContributedLibrary> installedBuiltIn = releases.stream() |
| 94 | + .filter(new InstalledPredicate()).filter(new BuiltInPredicate()) |
| 95 | + .collect(Collectors.toList()); |
| 96 | + |
| 97 | + if (installed != null && !installedBuiltIn.contains(installed)) { |
| 98 | + uninstalledReleases.addAll(installedBuiltIn); |
| 99 | + } |
| 100 | + |
| 101 | + Collections.sort(uninstalledReleases, new ReverseComparator<>( |
| 102 | + new DownloadableContributionVersionComparator())); |
| 103 | + |
| 104 | + editorCell.downgradeChooser.removeAllItems(); |
| 105 | + editorCell.downgradeChooser.addItem(tr("Select version")); |
| 106 | + |
| 107 | + final List<ContributedLibrary> uninstalledPreviousReleases = new LinkedList<>(); |
| 108 | + final List<ContributedLibrary> uninstalledNewerReleases = new LinkedList<>(); |
| 109 | + |
| 110 | + final VersionComparator versionComparator = new VersionComparator(); |
| 111 | + uninstalledReleases.stream().forEach(input -> { |
| 112 | + if (installed == null |
| 113 | + || versionComparator.greaterThan(installed.getParsedVersion(), |
| 114 | + input.getParsedVersion())) { |
| 115 | + uninstalledPreviousReleases.add(input); |
| 116 | + } else { |
| 117 | + uninstalledNewerReleases.add(input); |
| 118 | + } |
| 119 | + }); |
| 120 | + uninstalledNewerReleases.forEach(editorCell.downgradeChooser::addItem); |
| 121 | + uninstalledPreviousReleases.forEach(editorCell.downgradeChooser::addItem); |
| 122 | + |
| 123 | + editorCell.downgradeChooser |
| 124 | + .setVisible(installed != null |
| 125 | + && (!uninstalledPreviousReleases.isEmpty() |
| 126 | + || uninstalledNewerReleases.size() > 1)); |
| 127 | + editorCell.downgradeButton |
| 128 | + .setVisible(installed != null |
| 129 | + && (!uninstalledPreviousReleases.isEmpty() |
| 130 | + || uninstalledNewerReleases.size() > 1)); |
| 131 | + |
| 132 | + editorCell.versionToInstallChooser.removeAllItems(); |
| 133 | + uninstalledReleases.forEach(editorCell.versionToInstallChooser::addItem); |
| 134 | + editorCell.versionToInstallChooser |
| 135 | + .setVisible(installed == null && uninstalledReleases.size() > 1); |
| 136 | + |
| 137 | + editorCell.setBackground(new Color(218, 227, 227)); // #dae3e3 |
| 138 | + return editorCell; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void setEnabled(boolean enabled) { |
| 143 | + editorCell.setButtonsVisible(enabled); |
| 144 | + } |
| 145 | + |
| 146 | + public void setStatus(String status) { |
| 147 | + editorCell.statusLabel.setText(status); |
| 148 | + } |
| 149 | + |
| 150 | + protected void onRemove(ContributedLibrary selected) { |
| 151 | + // Empty |
| 152 | + } |
| 153 | + |
| 154 | + protected void onInstall(ContributedLibrary selected, |
| 155 | + ContributedLibrary installed) { |
| 156 | + // Empty |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments