|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Copyright 2014 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 | +package cc.arduino.packages.contributions.ui; |
| 30 | + |
| 31 | +import static processing.app.I18n._; |
| 32 | +import static processing.app.I18n.format; |
| 33 | + |
| 34 | +import java.awt.Color; |
| 35 | +import java.awt.Component; |
| 36 | +import java.awt.Dimension; |
| 37 | +import java.awt.Insets; |
| 38 | +import java.awt.event.ActionEvent; |
| 39 | +import java.awt.event.ActionListener; |
| 40 | + |
| 41 | +import javax.swing.AbstractCellEditor; |
| 42 | +import javax.swing.Box; |
| 43 | +import javax.swing.BoxLayout; |
| 44 | +import javax.swing.JButton; |
| 45 | +import javax.swing.JPanel; |
| 46 | +import javax.swing.JTable; |
| 47 | +import javax.swing.JTextPane; |
| 48 | +import javax.swing.border.EmptyBorder; |
| 49 | +import javax.swing.event.HyperlinkEvent; |
| 50 | +import javax.swing.event.HyperlinkListener; |
| 51 | +import javax.swing.table.TableCellEditor; |
| 52 | +import javax.swing.table.TableCellRenderer; |
| 53 | +import javax.swing.text.Document; |
| 54 | +import javax.swing.text.html.HTMLDocument; |
| 55 | +import javax.swing.text.html.StyleSheet; |
| 56 | + |
| 57 | +import processing.app.Base; |
| 58 | +import cc.arduino.packages.contributions.ContributedBoard; |
| 59 | +import cc.arduino.packages.contributions.ContributedPlatform; |
| 60 | +import cc.arduino.packages.contributions.ui.ContributionIndexTableModel.ContributedPlatformReleases; |
| 61 | + |
| 62 | +@SuppressWarnings("serial") |
| 63 | +public class ContributedPlatformTableCell extends AbstractCellEditor implements |
| 64 | + TableCellEditor, TableCellRenderer { |
| 65 | + |
| 66 | + private JPanel panel; |
| 67 | + private JTextPane description; |
| 68 | + private JButton installButton; |
| 69 | + private JButton removeButton; |
| 70 | + private Component removeButtonPlaceholder; |
| 71 | + |
| 72 | + public ContributedPlatformTableCell() { |
| 73 | + description = new JTextPane(); |
| 74 | + description.setInheritsPopupMenu(true); |
| 75 | + Insets margin = description.getMargin(); |
| 76 | + margin.bottom = 0; |
| 77 | + description.setMargin(margin); |
| 78 | + description.setContentType("text/html"); |
| 79 | + Document doc = description.getDocument(); |
| 80 | + if (doc instanceof HTMLDocument) { |
| 81 | + HTMLDocument html = (HTMLDocument) doc; |
| 82 | + StyleSheet stylesheet = html.getStyleSheet(); |
| 83 | + stylesheet.addRule("body { margin: 0; padding: 0;" |
| 84 | + + "font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" |
| 85 | + + "font-size: 100%;" + "font-size: 0.95em; }"); |
| 86 | + } |
| 87 | + description.setOpaque(false); |
| 88 | + description.setBorder(new EmptyBorder(4, 7, 7, 7)); |
| 89 | + description.setHighlighter(null); |
| 90 | + description.setEditable(false); |
| 91 | + description.addHyperlinkListener(new HyperlinkListener() { |
| 92 | + @Override |
| 93 | + public void hyperlinkUpdate(HyperlinkEvent e) { |
| 94 | + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { |
| 95 | + Base.openURL(e.getDescription()); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + installButton = new JButton(_("Install")); |
| 101 | + installButton.addActionListener(new ActionListener() { |
| 102 | + @Override |
| 103 | + public void actionPerformed(ActionEvent e) { |
| 104 | + onInstall(editorValue.getSelected()); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + removeButton = new JButton(_("Remove")); |
| 109 | + removeButton.addActionListener(new ActionListener() { |
| 110 | + @Override |
| 111 | + public void actionPerformed(ActionEvent e) { |
| 112 | + onRemove(editorValue.getInstalled()); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + int width = removeButton.getPreferredSize().width; |
| 117 | + removeButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1)); |
| 118 | + |
| 119 | + panel = new JPanel(); |
| 120 | + panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); |
| 121 | + |
| 122 | + panel.add(description); |
| 123 | + panel.add(Box.createHorizontalStrut(5)); |
| 124 | + panel.add(installButton); |
| 125 | + panel.add(Box.createHorizontalStrut(5)); |
| 126 | + panel.add(removeButton); |
| 127 | + panel.add(removeButtonPlaceholder); |
| 128 | + panel.add(Box.createHorizontalStrut(5)); |
| 129 | + } |
| 130 | + |
| 131 | + protected void onRemove(ContributedPlatform contributedPlatform) { |
| 132 | + // Empty |
| 133 | + } |
| 134 | + |
| 135 | + protected void onInstall(ContributedPlatform contributedPlatform) { |
| 136 | + // Empty |
| 137 | + } |
| 138 | + |
| 139 | + public Component getTableCellRendererComponent(JTable table, Object value, |
| 140 | + boolean isSelected, |
| 141 | + boolean hasFocus, int row, |
| 142 | + int column) { |
| 143 | + parentTable = table; |
| 144 | + Component panel = getUpdatedCellComponent(value, isSelected, row); |
| 145 | + return panel; |
| 146 | + } |
| 147 | + |
| 148 | + private ContributedPlatformReleases editorValue; |
| 149 | + private JTable parentTable; |
| 150 | + |
| 151 | + @Override |
| 152 | + public Object getCellEditorValue() { |
| 153 | + return editorValue; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Component getTableCellEditorComponent(JTable table, Object value, |
| 158 | + boolean isSelected, int row, |
| 159 | + int column) { |
| 160 | + parentTable = table; |
| 161 | + editorValue = (ContributedPlatformReleases) value; |
| 162 | + return getUpdatedCellComponent(value, true, row); |
| 163 | + } |
| 164 | + |
| 165 | + private Component getUpdatedCellComponent(Object value, boolean isSelected, |
| 166 | + int row) { |
| 167 | + ContributedPlatformReleases releases = (ContributedPlatformReleases) value; |
| 168 | + ContributedPlatform selectedPlatform = releases.getSelected(); |
| 169 | + ContributedPlatform installedPlatform = releases.getInstalled(); |
| 170 | + |
| 171 | + boolean removable, installable, upgradable; |
| 172 | + if (installedPlatform == null) { |
| 173 | + installable = true; |
| 174 | + removable = false; |
| 175 | + upgradable = false; |
| 176 | + } else { |
| 177 | + installable = false; |
| 178 | + removable = true; |
| 179 | + upgradable = (selectedPlatform != installedPlatform); |
| 180 | + } |
| 181 | + |
| 182 | + if (installable) |
| 183 | + installButton.setText(_("Install")); |
| 184 | + if (upgradable) |
| 185 | + installButton.setText(_("Upgrade")); |
| 186 | + installButton.setVisible(installable || upgradable); |
| 187 | + |
| 188 | + removeButton.setVisible(removable); |
| 189 | + removeButtonPlaceholder.setVisible(!removable); |
| 190 | + |
| 191 | + String desc = "<html><body>"; |
| 192 | + desc += "<b>" + selectedPlatform.getName() + "</b>"; |
| 193 | + String author = selectedPlatform.getParentPackage().getMaintainer(); |
| 194 | + String url = selectedPlatform.getParentPackage().getWebsiteURL(); |
| 195 | + if (author != null && !author.isEmpty()) { |
| 196 | + desc += format(" by <a href=\"{0}\">{1}</a>", url, author); |
| 197 | + } |
| 198 | + desc += "<br />"; |
| 199 | + desc += "<br />"; |
| 200 | + |
| 201 | + desc += _("Boards contributed in this package:") + "<br />"; |
| 202 | + for (ContributedBoard board : selectedPlatform.getBoards()) |
| 203 | + desc += format("- {0}<br />", board.getName()); |
| 204 | + desc += "<br />"; |
| 205 | + desc += format(_("Available version: <b>{0}</b>"), |
| 206 | + selectedPlatform.getVersion()); |
| 207 | + desc += "<br />"; |
| 208 | + if (removable) { |
| 209 | + desc += format(_("Installed version: <b>{0}</b>"), |
| 210 | + installedPlatform.getVersion()); |
| 211 | + } |
| 212 | + desc += "<br />"; |
| 213 | + desc += "</body></html>"; |
| 214 | + description.setText(desc); |
| 215 | + description.setBackground(Color.WHITE); |
| 216 | + |
| 217 | + if (isSelected) { |
| 218 | + panel.setBackground(parentTable.getSelectionBackground()); |
| 219 | + panel.setForeground(parentTable.getSelectionForeground()); |
| 220 | + } else { |
| 221 | + panel.setBackground(parentTable.getBackground()); |
| 222 | + panel.setForeground(parentTable.getForeground()); |
| 223 | + } |
| 224 | + |
| 225 | + return panel; |
| 226 | + } |
| 227 | + |
| 228 | + void setEnabled(boolean enabled) { |
| 229 | + installButton.setEnabled(enabled); |
| 230 | + removeButton.setEnabled(enabled); |
| 231 | + } |
| 232 | + |
| 233 | + public void invalidate() { |
| 234 | + panel.invalidate(); |
| 235 | + } |
| 236 | + |
| 237 | +} |
0 commit comments