Skip to content

Commit 7e0b553

Browse files
author
Joe Wegner
committed
Move some repeated code into methods.
1 parent 24584e2 commit 7e0b553

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellJPanel.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ public class ContributedLibraryTableCellJPanel extends JPanel {
4242
final JPanel buttonsPanel;
4343
final JPanel inactiveButtonsPanel;
4444
final JLabel statusLabel;
45+
private final String moreInfoLbl = tr("More info");
4546

4647
public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
4748
boolean isSelected) {
4849
super();
4950
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
5051

51-
moreInfoButton = new JButton(tr("More info"));
52+
moreInfoButton = new JButton(moreInfoLbl);
5253
moreInfoButton.setVisible(false);
5354
installButton = new JButton(tr("Install"));
5455
int width = installButton.getPreferredSize().width;
@@ -197,16 +198,7 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
197198
desc += "<br />";
198199
}
199200
if (author != null && !author.isEmpty()) {
200-
boolean accessibleIDE = PreferencesData.getBoolean("ide.accessible");
201-
if (accessibleIDE) {
202-
moreInfoButton.setVisible(true);
203-
moreInfoButton.addActionListener(e -> {
204-
Base.openURL(website);
205-
});
206-
}
207-
else {
208-
desc += format("<a href=\"{0}\">More info</a>", website);
209-
}
201+
desc = setButtonOrLink(moreInfoButton, desc, moreInfoLbl, website);
210202
}
211203

212204
desc += "</body></html>";
@@ -233,6 +225,25 @@ public ContributedLibraryTableCellJPanel(JTable parentTable, Object value,
233225
}
234226
}
235227

228+
// same function as in ContributedPlatformTableCellJPanel - is there a utils file this can move to?
229+
private String setButtonOrLink(JButton button, String desc, String label, String url) {
230+
boolean accessibleIDE = PreferencesData.getBoolean("ide.accessible");
231+
String retString = desc;
232+
233+
if (accessibleIDE) {
234+
button.setVisible(true);
235+
button.addActionListener(e -> {
236+
Base.openURL(url);
237+
});
238+
}
239+
else {
240+
// if not accessible IDE, keep link the same EXCEPT that now the link text is translated!
241+
retString += format("<a href=\"{0}\">{1}</a>", url, label);
242+
}
243+
244+
return retString;
245+
}
246+
236247
// TODO Make this a method of Theme
237248
private JTextPane makeNewDescription() {
238249
if (getComponentCount() > 0) {

app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellJPanel.java

+25-22
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ public class ContributedPlatformTableCellJPanel extends JPanel {
7575
final JPanel buttonsPanel;
7676
final JPanel inactiveButtonsPanel;
7777
final JLabel statusLabel;
78+
private final String moreInfoLbl = tr("More Info");
79+
private final String onlineHelpLbl = tr("Online Help");
7880

7981
public ContributedPlatformTableCellJPanel() {
8082
super();
8183
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
8284

8385
{
8486
installButton = new JButton(tr("Install"));
85-
moreInfoButton = new JButton(tr("More Info"));
87+
moreInfoButton = new JButton(moreInfoLbl);
8688
moreInfoButton.setVisible(false);
87-
onlineHelpButton = new JButton(tr("Online Help"));
89+
onlineHelpButton = new JButton(onlineHelpLbl);
8890
onlineHelpButton.setVisible(false);
8991
int width = installButton.getPreferredSize().width;
9092
installButtonPlaceholder = Box.createRigidArea(new Dimension(width, 1));
@@ -163,6 +165,25 @@ public ContributedPlatformTableCellJPanel() {
163165
add(Box.createVerticalStrut(15));
164166
}
165167

168+
// same function as in ContributedLibraryTableCellJPanel - is there a utils file this can move to?
169+
private String setButtonOrLink(JButton button, String desc, String label, String url) {
170+
boolean accessibleIDE = PreferencesData.getBoolean("ide.accessible");
171+
String retString = desc;
172+
173+
if (accessibleIDE) {
174+
button.setVisible(true);
175+
button.addActionListener(e -> {
176+
Base.openURL(url);
177+
});
178+
}
179+
else {
180+
// if not accessible IDE, keep link the same EXCEPT that now the link text is translated!
181+
retString += " " + format("<a href=\"{0}\">{1}</a>", url, label);
182+
}
183+
184+
return retString;
185+
}
186+
166187
void update(JTable parentTable, Object value, boolean isSelected,
167188
boolean hasBuiltInRelease) {
168189
ContributedPlatformReleases releases = (ContributedPlatformReleases) value;
@@ -231,34 +252,16 @@ void update(JTable parentTable, Object value, boolean isSelected,
231252
help = selected.getParentPackage().getHelp();
232253
}
233254

234-
boolean accessibleIDE = PreferencesData.getBoolean("ide.accessible");
235-
236255
if (help != null) {
237256
String url = help.getOnline();
238257
if (url != null && !url.isEmpty()) {
239-
if (accessibleIDE) {
240-
onlineHelpButton.setVisible(true);
241-
onlineHelpButton.addActionListener(e -> {
242-
Base.openURL(url);
243-
});
244-
}
245-
else {
246-
desc += " " + format("<a href=\"{0}\">Online help</a><br/>", url);
247-
}
258+
desc = setButtonOrLink(onlineHelpButton, desc, onlineHelpLbl, url);
248259
}
249260
}
250261

251262
String url = selected.getParentPackage().getWebsiteURL();
252263
if (url != null && !url.isEmpty()) {
253-
if (accessibleIDE) {
254-
moreInfoButton.setVisible(true);
255-
moreInfoButton.addActionListener(e -> {
256-
Base.openURL(url);
257-
});
258-
}
259-
else {
260-
desc += " " + format("<a href=\"{0}\">More info</a>", url);
261-
}
264+
desc = setButtonOrLink(moreInfoButton, desc, moreInfoLbl, url);
262265
}
263266

264267
desc += "</body></html>";

0 commit comments

Comments
 (0)