Skip to content

Commit afcbd85

Browse files
author
werwolf2303
committed
Bug fixes and improvements
Fixed some nullpointers in right click menus Fixed table layout not working correctly in browse tab Hopefully translated everything now Auto select on right click
1 parent a08dcac commit afcbd85

File tree

10 files changed

+107
-59
lines changed

10 files changed

+107
-59
lines changed

src/main/java/com/spotifyxp/ctxmenu/ContextMenu.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import javax.swing.*;
2424
import java.awt.*;
25+
import java.awt.event.ActionEvent;
26+
import java.awt.event.ActionListener;
2527
import java.awt.event.MouseAdapter;
2628
import java.awt.event.MouseEvent;
2729
import java.util.ArrayList;
@@ -89,9 +91,31 @@ public ContextMenu() {
8991
public ContextMenu(JComponent component, @Nullable ArrayList<String> uris, Class<?> containingClass) {
9092
component.addMouseListener(new AsyncMouseListener(new MouseAdapter() {
9193
@Override
92-
public void mouseClicked(MouseEvent e) {
93-
super.mouseClicked(e);
94+
public void mousePressed(MouseEvent e) {
95+
super.mousePressed(e);
96+
showPopup(e);
97+
}
98+
99+
@Override
100+
public void mouseReleased(MouseEvent e) {
101+
super.mouseReleased(e);
102+
showPopup(e);
103+
}
104+
105+
private void showPopup(MouseEvent e) {
106+
if(!e.isPopupTrigger()) return;
94107
if (SwingUtilities.isRightMouseButton(e)) {
108+
if(component instanceof JTable) {
109+
JTable table = (JTable) component;
110+
int rowToSelect = table.rowAtPoint(e.getPoint());
111+
table.clearSelection();
112+
table.setRowSelectionInterval(rowToSelect, rowToSelect);
113+
}else if(component instanceof JList) {
114+
JList<?> list = (JList<?>) component;
115+
int rowToSelect = list.locationToIndex(e.getPoint());
116+
list.clearSelection();
117+
list.setSelectionInterval(rowToSelect, rowToSelect);
118+
}
95119
if(uris == null) {} else if(uris.isEmpty()) return;
96120
holder.show(component, e.getX(), e.getY(), uris);
97121
}

src/main/java/com/spotifyxp/guielements/SpotifyBrowseSection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public SpotifyBrowseSection(List<ArrayList<String>> entries) {
4545
new Object[][]{
4646
},
4747
new String[]{
48-
"Name", "Description", ""
48+
PublicValues.language.translate("ui.general.name"), PublicValues.language.translate("ui.general.description"), ""
4949
}
5050
));
5151
table.setForeground(PublicValues.globalFontColor);
@@ -114,7 +114,7 @@ public SpotifyBrowseSection(ArrayList<UnofficialSpotifyAPI.SpotifyBrowseEntry> e
114114
new Object[][]{
115115
},
116116
new String[]{
117-
"Name", ""
117+
PublicValues.language.translate("ui.general.name"), ""
118118
}
119119
));
120120
table.setForeground(PublicValues.globalFontColor);

src/main/java/com/spotifyxp/lib/libLanguage.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,19 +327,21 @@ public String translate(String key) {
327327
try {
328328
jsoncache = new Gson().fromJson(removeComment(new Resources().readToString(clazz, lf + "/" + languageCode + ".json")), JsonObject.class);
329329
} catch (Exception e) {
330+
// Can be too early in init. So no ConsoleLogging.Throwable
330331
ConsoleLogging.error("Failed to get translation for: " + key);
331-
ConsoleLogging.Throwable(e);
332-
return ret[0];
332+
e.printStackTrace();
333+
return key;
333334
}
334335
}
335336
try {
336337
if(!jsoncache.has(key)) {
337338
ConsoleLogging.error("Failed to get translation for: " + key);
338-
return ret[0];
339+
return key;
339340
}
340341
ret[0] = jsoncache.get(key).getAsString();
341342
} catch (Exception e) {
342-
ConsoleLogging.Throwable(e);
343+
// Can be too early in init. So no ConsoleLogging.Throwable
344+
e.printStackTrace();
343345
}
344346
return ret[0].replaceAll("%APPNAME%", ApplicationUtils.getName());
345347
}

src/main/java/com/spotifyxp/panels/ArtistPanel.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public class ArtistPanel extends JScrollPane implements View {
6868
public static ArrayList<String> relatedArtistsUriCache = new ArrayList<>();
6969
public static ArrayList<String> discoveredOnUriCache = new ArrayList<>();
7070

71-
private boolean trackPanelOpen = false;
72-
7371
public ArtistPanel() {
7472
contentPanel = new JPanel();
7573
contentPanel.setLayout(null);
@@ -99,7 +97,7 @@ public ArtistPanel() {
9997
backButton.addActionListener(new AsyncActionListener(e -> ContentPanel.switchView(ContentPanel.lastView)));
10098
contentPanel.add(backButton);
10199

102-
artistPopularLabel = new JLabel("Popular"); //ToDo: Translate
100+
artistPopularLabel = new JLabel(PublicValues.language.translate("ui.artist.popular"));
103101
artistPopularLabel.setBounds(5, 291, 137, 27);
104102
artistPopularLabel.setForeground(PublicValues.globalFontColor);
105103
contentPanel.add(artistPopularLabel);
@@ -171,12 +169,12 @@ public void mouseClicked(MouseEvent e) {
171169
artistAlbumScrollPane.setViewportView(artistAlbumTable);
172170
contentPanel.add(artistAlbumScrollPane);
173171

174-
artistAlbumLabel = new JLabel("Albums"); //ToDo: Translate
172+
artistAlbumLabel = new JLabel(PublicValues.language.translate("ui.artist.album"));
175173
artistAlbumLabel.setBounds(5, 642, 102, 14);
176174
artistAlbumLabel.setForeground(PublicValues.globalFontColor);
177175
contentPanel.add(artistAlbumLabel);
178176

179-
relatedArtistsLabel = new JLabel("Related Artists");
177+
relatedArtistsLabel = new JLabel(PublicValues.language.translate("ui.artist.relatedartists"));
180178
relatedArtistsLabel.setBounds(5, 1372, 102, 14);
181179
relatedArtistsLabel.setForeground(PublicValues.globalFontColor);
182180
contentPanel.add(relatedArtistsLabel);
@@ -188,7 +186,7 @@ public void mouseClicked(MouseEvent e) {
188186
new Object[][]{
189187
},
190188
new String[]{
191-
"Artist"
189+
PublicValues.language.translate("ui.general.artist")
192190
}
193191
));
194192
relatedArtistsTable.addMouseListener(new AsyncMouseListener(new MouseAdapter() {
@@ -210,7 +208,7 @@ public void mouseClicked(MouseEvent e) {
210208
relatedArtistsScrollPane.setBounds(5, 1397, 760, 295);
211209
contentPanel.add(relatedArtistsScrollPane);
212210

213-
discoveredOnLabel = new JLabel("Discovered On");
211+
discoveredOnLabel = new JLabel(PublicValues.language.translate("ui.artist.discoveredon"));
214212
discoveredOnLabel.setForeground(PublicValues.globalFontColor);
215213
discoveredOnLabel.setBounds(5, 1007, 102, 14);
216214
contentPanel.add(discoveredOnLabel);
@@ -222,7 +220,7 @@ public void mouseClicked(MouseEvent e) {
222220
new Object[][]{
223221
},
224222
new String[]{
225-
"Name", "Description"
223+
PublicValues.language.translate("ui.general.name"), PublicValues.language.translate("ui.general.description")
226224
}
227225
));
228226
discoveredOnTable.addMouseListener(new AsyncMouseListener(new MouseAdapter() {
@@ -256,7 +254,6 @@ public void run() {
256254
setVisible(true);
257255
ContentPanel.tabPanel.revalidate();
258256
ContentPanel.tabPanel.repaint();
259-
trackPanelOpen = false;
260257
}
261258
});
262259
}
@@ -297,7 +294,7 @@ public void fillWith(Artist artist) throws IOException {
297294
artistTitle.getStyledDocument().getLength(),
298295
artist.getName() + "\n",
299296
style);
300-
artistTitle.getStyledDocument().insertString(artistTitle.getStyledDocument().getLength(), artist.getFollowers().getTotal().toString() + " Followers", null);
297+
artistTitle.getStyledDocument().insertString(artistTitle.getStyledDocument().getLength(), artist.getFollowers().getTotal().toString() + " " + PublicValues.language.translate("ui.artist.followers"), null);
301298
} catch (BadLocationException e) {
302299
ConsoleLogging.Throwable(e);
303300
}

src/main/java/com/spotifyxp/panels/BrowsePanel.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public void mouseClicked(MouseEvent e) {
8383
thread.start();
8484

8585
popupMenu = new JPopupMenu();
86-
metroLayout = new JCheckBoxMenuItem("Metro layout"); //ToDo: Translate
87-
tableLayout = new JCheckBoxMenuItem("Table layout"); //ToDo: Translate
86+
metroLayout = new JCheckBoxMenuItem(PublicValues.language.translate("ui.browse.ctxmenu.metro"));
87+
tableLayout = new JCheckBoxMenuItem(PublicValues.language.translate("ui.browse.ctxmenu.table"));
8888
metroLayout.setSelected(PublicValues.config.getInt(ConfigValues.browse_view_style.name) == 0);
8989
tableLayout.setSelected(!metroLayout.isSelected());
9090
metroLayout.addActionListener(e -> {
@@ -131,7 +131,7 @@ void displayBrowseTable() throws NoSuchElementException {
131131
new Object[][]{
132132
},
133133
new String[]{
134-
"Name"
134+
PublicValues.language.translate("ui.general.name")
135135
}
136136
));
137137
table.setForeground(PublicValues.globalFontColor);
@@ -144,13 +144,17 @@ public void mouseClicked(MouseEvent e) {
144144
return;
145145
}
146146
if(e.getClickCount() == 2) {
147-
idRunnable.run(genreIds.get(table.getSelectedRow()).split(":")[2]);
147+
try {
148+
idRunnable.run(genreIds.get(table.getSelectedRow()).split(":")[2]);
149+
}catch (ArrayIndexOutOfBoundsException ex) {
150+
idRunnable.run(genreIds.get(table.getSelectedRow()));
151+
}
148152
}
149153
}
150154
});
151155

152156
tableScrollPane = new JScrollPane(table);
153-
tableScrollPane.setBounds(10, 10, getWidth() - 30, getHeight() - 40);
157+
tableScrollPane.setBounds(10, 10, 774, 461);
154158

155159
for(UnofficialSpotifyAPI.SpotifyBrowseEntry entry : spotifyBrowse.getBody()) {
156160
if(entry.getMetadata().isPresent()) {
@@ -160,8 +164,9 @@ public void mouseClicked(MouseEvent e) {
160164
&& entry.getCustom().isPresent()
161165
&& entry.getCustom().get().getBackgroundColor().isPresent()) {
162166
table.addModifyAction(() -> ((DefaultTableModel) table.getModel()).addRow(new Object[] {entry.getText().getTitle()}));
163-
if(!entry.getEvents().isPresent()
164-
|| entry.getEvents().get().getEvents().get(0).getData_uri().isPresent()
167+
if (!entry.getImages().isPresent()
168+
|| !entry.getEvents().isPresent()
169+
|| !entry.getEvents().get().getEvents().get(0).getData_uri().isPresent()
165170
) throw new NoSuchElementException();
166171
genreIds.add(entry.getEvents().get().getEvents().get(0).getData_uri().get().getUri());
167172
}
@@ -170,6 +175,8 @@ public void mouseClicked(MouseEvent e) {
170175

171176
contentPanel.add(tableScrollPane);
172177
contentPanel.setPreferredSize(new Dimension(782, 405));
178+
revalidate();
179+
repaint();
173180
}
174181

175182
void displayBrowseMetro() throws NoSuchElementException {
@@ -309,7 +316,7 @@ private SpotifySectionPanel.ViewDescriptorBuilder concertsToViewDescriptor(Conce
309316
eventsListTableScrollPane.setVisible(false);
310317

311318
eventsTable.setModel(new DefaultTableModel(new Object[][]{}, new Object[]{
312-
"Artist",
319+
PublicValues.language.translate("ui.general.artist"),
313320
""
314321
}));
315322

@@ -374,8 +381,8 @@ public void run() {
374381
});
375382

376383
eventsListTable.setModel(new DefaultTableModel(new Object[][]{}, new Object[]{
377-
"Location",
378-
"Date"
384+
PublicValues.language.translate("ui.general.location"),
385+
PublicValues.language.translate("ui.general.date")
379386
}));
380387

381388
eventsListTable.setForeground(PublicValues.globalFontColor);
@@ -416,7 +423,7 @@ public void mouseClicked(MouseEvent e) {
416423
public void run() {
417424
((DefaultTableModel) eventsTable.getModel()).addRow(new Object[]{
418425
artist.getName(),
419-
artist.getArtistConcerts().getConcertsList().size() + " Events"
426+
artist.getArtistConcerts().getConcertsList().size() + " " + PublicValues.language.translate("ui.browse.events.events")
420427
});
421428
}
422429
});

src/main/java/com/spotifyxp/panels/ContentPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metr
323323
case 2:
324324
currentView = Views.LIBRARY;
325325
if (Library.libraryTracks.librarySongList.getModel().getRowCount() == 0) {
326-
Library.libraryTracks.libraryThread.start();
326+
Library.libraryTracks.loadLibrary();
327327
}
328328
preventBuglegacySwitch();
329329
legacySwitch.setComponentAt(legacySwitch.getSelectedIndex(), tabPanel);

src/main/java/com/spotifyxp/panels/HomePanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void addModule(UnofficialSpotifyAPI.HomeTabSection section, int titleHeig
153153
new Object[][]{
154154
},
155155
new String[]{
156-
"Name", "Artist"
156+
PublicValues.language.translate( "ui.general.name"), PublicValues.language.translate("ui.general.artist")
157157
}
158158
));
159159

src/main/java/com/spotifyxp/panels/LibraryTracks.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,7 @@ public class LibraryTracks extends JScrollPane implements View {
3939
private static boolean libraryLoadingInProgress = false;
4040
public static ContextMenu contextMenu;
4141
public static int totalTracks = 0;
42-
public static final Thread libraryThread = new Thread(new Runnable() {
43-
public void run() {
44-
try {
45-
libraryLoadingInProgress = true;
46-
int limit = 50;
47-
Paging<SavedTrack> libraryTracks = InstanceManager.getSpotifyApi().getUsersSavedTracks().limit(limit).build().execute();
48-
totalTracks = libraryTracks.getTotal();
49-
for(SavedTrack track : libraryTracks.getItems()) {
50-
libraryUriCache.add(track.getTrack().getUri());
51-
String a = TrackUtils.getArtists(track.getTrack().getArtists());
52-
librarySongList.addModifyAction(() -> ((DefaultTableModel) librarySongList.getModel()).addRow(new Object[]{track.getTrack().getName() + " - " + a, TrackUtils.calculateFileSizeKb(track.getTrack()), TrackUtils.getBitrate(), TrackUtils.getHHMMSSOfTrack(track.getTrack().getDurationMs())}));
53-
}
54-
libraryLoadingInProgress = false;
55-
} catch (Exception e) {
56-
ConsoleLogging.error("Error loading users library! Library now locked");
57-
libraryLoadingInProgress = true;
58-
throw new RuntimeException(e);
59-
}
60-
}
61-
}, "Library thread");
42+
public static Thread libraryThread;
6243

6344
public LibraryTracks() {
6445
setVisible(false);
@@ -104,12 +85,36 @@ public void mouseClicked(MouseEvent e) {
10485
createcontextMenu();
10586
}
10687

88+
public void loadLibrary() {
89+
libraryThread = new Thread(new Runnable() {
90+
public void run() {
91+
try {
92+
libraryLoadingInProgress = true;
93+
int limit = 50;
94+
Paging<SavedTrack> libraryTracks = InstanceManager.getSpotifyApi().getUsersSavedTracks().limit(limit).build().execute();
95+
totalTracks = libraryTracks.getTotal();
96+
for(SavedTrack track : libraryTracks.getItems()) {
97+
libraryUriCache.add(track.getTrack().getUri());
98+
String a = TrackUtils.getArtists(track.getTrack().getArtists());
99+
librarySongList.addModifyAction(() -> ((DefaultTableModel) librarySongList.getModel()).addRow(new Object[]{track.getTrack().getName() + " - " + a, TrackUtils.calculateFileSizeKb(track.getTrack()), TrackUtils.getBitrate(), TrackUtils.getHHMMSSOfTrack(track.getTrack().getDurationMs())}));
100+
}
101+
libraryLoadingInProgress = false;
102+
} catch (Exception e) {
103+
ConsoleLogging.error("Error loading users library! Library now locked");
104+
libraryLoadingInProgress = true;
105+
throw new RuntimeException(e);
106+
}
107+
}
108+
}, "Library thread");
109+
libraryThread.start();
110+
}
111+
107112
void createcontextMenu() {
108113
contextMenu = new ContextMenu(librarySongList, libraryUriCache, getClass());
109114
contextMenu.addItem(PublicValues.language.translate("ui.general.refresh"), () -> {
110115
libraryUriCache.clear();
111-
librarySongList.removeAll();
112-
libraryThread.start();
116+
((DefaultTableModel) librarySongList.getModel()).setRowCount(0);
117+
loadLibrary();
113118
});
114119
contextMenu.addItem("Add to queue", () -> {
115120
if(librarySongList.getSelectedRow() == -1) return;

src/main/java/com/spotifyxp/panels/Queue.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.spotifyxp.utils.TrackUtils;
2828

2929
import javax.swing.*;
30+
import java.awt.*;
3031
import java.awt.event.MouseAdapter;
3132
import java.awt.event.MouseEvent;
3233
import java.io.IOException;
@@ -39,7 +40,6 @@ public class Queue extends JScrollPane implements View {
3940
public static ContextMenu contextMenu;
4041

4142

42-
//ToDo: Implement the reordering of the queue after dragging
4343
public Queue() throws IOException {
4444
setVisible(false);
4545

@@ -92,7 +92,7 @@ public void mouseClicked(MouseEvent e) {
9292
String a = TrackUtils.getArtists(track.getArtists());
9393
queueListModel.addElement(track.getName() + " - " + a);
9494
} catch (ArrayIndexOutOfBoundsException e) {
95-
// This happens when (psst... i dont know)
95+
// This happens when (psst... I don't know)
9696
} catch (Exception e) {
9797
throw new RuntimeException("Failed to list tracks in queue");
9898
}
@@ -111,7 +111,7 @@ public void mouseClicked(MouseEvent e) {
111111
queueListModel.addElement(track.getName() + " - " + a);
112112
}
113113
} catch (ArrayIndexOutOfBoundsException e) {
114-
// This happens when (psst... i dont know)
114+
// This happens when (psst... I don't know)
115115
} catch (Exception e) {
116116
throw new RuntimeException("Failed to list tracks in queue", e);
117117
}
@@ -138,7 +138,7 @@ public void mouseClicked(MouseEvent e) {
138138
queueListModel.addElement(track.getName() + " - " + a);
139139
}
140140
} catch (ArrayIndexOutOfBoundsException e) {
141-
// This happens when (psst... i dont know)
141+
// This happens when (psst... I don't know)
142142
} catch (Exception e) {
143143
throw new RuntimeException("Failed to list tracks in queue");
144144
}
@@ -164,7 +164,7 @@ public void mouseClicked(MouseEvent e) {
164164
queueListModel.addElement(track.getName() + " - " + a);
165165
}
166166
} catch (ArrayIndexOutOfBoundsException e) {
167-
// This happens when (psst... i dont know)
167+
// This happens when (psst... I don't know)
168168
} catch (Exception e) {
169169
throw new RuntimeException("Failed to list tracks in queue");
170170
}

0 commit comments

Comments
 (0)