suggs: better event handling, better string handling

This commit is contained in:
Downforce Agent 2024-07-21 12:56:43 -05:00
parent 13f82e3866
commit f46ca4c066

View File

@ -19,6 +19,8 @@
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
import java.awt.*; import java.awt.*;
@ -86,6 +88,23 @@ public class Suggs implements ActionListener, ListSelectionListener {
private File sptrack; private File sptrack;
private File mptrack; private File mptrack;
DocumentListener id3TagEditorHandler = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent documentEvent) {
updateSelectionToMatchTextFields();
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
updateSelectionToMatchTextFields();
}
@Override
public void changedUpdate(DocumentEvent documentEvent) {
updateSelectionToMatchTextFields();
}
};
public Suggs(JFrame parent) { public Suggs(JFrame parent) {
this.parent = parent; this.parent = parent;
parent.setEnabled(false); parent.setEnabled(false);
@ -118,11 +137,12 @@ public class Suggs implements ActionListener, ListSelectionListener {
@Override @Override
public void windowClosing(WindowEvent e) public void windowClosing(WindowEvent e)
{ {
int result = JOptionPane.showConfirmDialog(frame, "Are you sure?\nAll unsaved changes will be lost.", "Soundtrack Mod Generator", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (!tracklist.isEmpty() || sptrack != null || mptrack != null) {
if (result == JOptionPane.YES_OPTION) { int result = JOptionPane.showConfirmDialog(frame, "Are you sure?\nAll unsaved changes will be lost.", "Soundtrack Mod Generator", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
parent.setEnabled(true); if (result == JOptionPane.NO_OPTION) {return;}
e.getWindow().dispose();
} }
parent.setEnabled(true);
e.getWindow().dispose();
} }
}); });
@ -133,6 +153,9 @@ public class Suggs implements ActionListener, ListSelectionListener {
fTitle.setEnabled(false); fTitle.setEnabled(false);
fArtist.setEnabled(false); fArtist.setEnabled(false);
fTitle.getDocument().addDocumentListener(id3TagEditorHandler);
fArtist.getDocument().addDocumentListener(id3TagEditorHandler);
frame.setVisible(true); frame.setVisible(true);
} }
@ -143,17 +166,12 @@ public class Suggs implements ActionListener, ListSelectionListener {
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource() == cancelBtn) { if (actionEvent.getSource() == cancelBtn) {
int result = JOptionPane.showConfirmDialog(frame, "Are you sure?\nAll unsaved changes will be lost.", "Soundtrack Mod Generator", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (!tracklist.isEmpty() || sptrack != null || mptrack != null) {
if (result == JOptionPane.YES_OPTION) { int result = JOptionPane.showConfirmDialog(frame, "Are you sure?\nAll unsaved changes will be lost.", "Soundtrack Mod Generator", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
parent.setEnabled(true); if (result == JOptionPane.NO_OPTION) {return;}
frame.dispose();
} }
} else parent.setEnabled(true);
if (actionEvent.getSource() == fTitle || actionEvent.getSource() == fArtist) { frame.dispose();
tracklist.get(curIndex).title = fTitle.getText();
tracklist.get(curIndex).artist = fArtist.getText();
InitializeSongListInGUI();
dSongList.setSelectedIndex(curIndex);
} else } else
if (actionEvent.getSource() == addSongBtn) {addSong();} else if (actionEvent.getSource() == addSongBtn) {addSong();} else
if (actionEvent.getSource() == deleteSongBtn) {remove(curIndex);} else if (actionEvent.getSource() == deleteSongBtn) {remove(curIndex);} else
@ -189,6 +207,8 @@ public class Suggs implements ActionListener, ListSelectionListener {
@Override @Override
public void valueChanged(ListSelectionEvent listSelectionEvent) { public void valueChanged(ListSelectionEvent listSelectionEvent) {
fTitle.getDocument().removeDocumentListener(id3TagEditorHandler);
fArtist.getDocument().removeDocumentListener(id3TagEditorHandler);
curIndex = dSongList.getSelectedIndex(); curIndex = dSongList.getSelectedIndex();
if (curIndex >= 0) { if (curIndex >= 0) {
fTitle.setEnabled(true); fTitle.setEnabled(true);
@ -204,6 +224,8 @@ public class Suggs implements ActionListener, ListSelectionListener {
if (at.size > 1048575) { if (at.size > 1048575) {
dFileSize.setText((at.size / 1048576) + " MB"); dFileSize.setText((at.size / 1048576) + " MB");
} }
fTitle.getDocument().addDocumentListener(id3TagEditorHandler);
fArtist.getDocument().addDocumentListener(id3TagEditorHandler);
} else { } else {
fTitle.setEnabled(false); fTitle.setEnabled(false);
fArtist.setEnabled(false); fArtist.setEnabled(false);
@ -211,8 +233,17 @@ public class Suggs implements ActionListener, ListSelectionListener {
fArtist.setText(""); fArtist.setText("");
dTrackNo.setText("\u200E"); dTrackNo.setText("\u200E");
dFileSize.setText("\u200E"); dFileSize.setText("\u200E");
fTitle.getDocument().addDocumentListener(id3TagEditorHandler);
fArtist.getDocument().addDocumentListener(id3TagEditorHandler);
} }
} }
private void updateSelectionToMatchTextFields() {
tracklist.get(curIndex).title = fTitle.getText();
tracklist.get(curIndex).artist = fArtist.getText();
InitializeSongListInGUI();
dSongList.setSelectedIndex(curIndex);
}
private void remove(int index) { private void remove(int index) {
if (index >= 0) { if (index >= 0) {
@ -236,6 +267,7 @@ public class Suggs implements ActionListener, ListSelectionListener {
} }
private void InitializeSongListInGUI() { private void InitializeSongListInGUI() {
dSongList.removeListSelectionListener(this); // prevent weird bullshit
dSongList.clearSelection(); dSongList.clearSelection();
dSongList.removeAll(); dSongList.removeAll();
dSongList.setVisibleRowCount(tracklist.size()); dSongList.setVisibleRowCount(tracklist.size());
@ -245,16 +277,19 @@ public class Suggs implements ActionListener, ListSelectionListener {
int i = 0; int i = 0;
String[] contents = new String[tracklist.size()]; String[] contents = new String[tracklist.size()];
while (i < tracklist.size()) { while (i < tracklist.size()) {
String dTitleInList; // avoid editing the actual list
String dArtistInList; // otherwise we cause weird JTextField behavior
if (tracklist.get(i).title == null || tracklist.get(i).title.isEmpty()) if (tracklist.get(i).title == null || tracklist.get(i).title.isEmpty())
{tracklist.get(i).title = tracklist.get(i).path.getName();} {dTitleInList = tracklist.get(i).path.getName();} else {dTitleInList = tracklist.get(i).title;}
if (tracklist.get(i).artist == null || tracklist.get(i).artist.isEmpty()) if (tracklist.get(i).artist == null || tracklist.get(i).artist.isEmpty())
{tracklist.get(i).artist = "???";} {dArtistInList = "???";} else {dArtistInList = tracklist.get(i).artist;}
contents[i] = tracklist.get(i).artist + " - " + tracklist.get(i).title; contents[i] = dArtistInList + " - " + dTitleInList;
i++; i++;
} }
dSongList.setListData(contents); dSongList.setListData(contents);
dSongList.setSelectedIndex(curIndex); dSongList.setSelectedIndex(curIndex);
dSongList.addListSelectionListener(this);
} }
private void setSPMusic() { private void setSPMusic() {