MusicCollection.java
package ui.musiccollection;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import jakarta.persistence.PersistenceException;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.stage.DirectoryChooser;
import javafx.stage.WindowEvent;
import jsidplay2.Photos;
import libsidplay.sidtune.SidTune;
import libsidplay.sidtune.SidTuneError;
import libsidplay.sidtune.SidTuneInfo;
import libsidutils.IOUtils;
import libsidutils.psid64.Psid64;
import libsidutils.siddatabase.SidDatabase;
import libsidutils.stil.STIL;
import net.java.truevfs.access.TFile;
import net.java.truevfs.access.TVFS;
import net.java.truevfs.kernel.spec.FsSyncException;
import sidplay.Player;
import sidplay.audio.Audio;
import ui.common.C64VBox;
import ui.common.C64Window;
import ui.common.TypeTextField;
import ui.common.UIPart;
import ui.common.converter.EnumToStringConverter;
import ui.common.download.DownloadThread;
import ui.common.download.MultiDownload;
import ui.common.download.ProgressListener;
import ui.common.filefilter.TuneFileFilter;
import ui.common.util.DesktopUtil;
import ui.common.util.SidTuneInfoDetails;
import ui.entities.DatabaseType;
import ui.entities.PersistenceProperties;
import ui.entities.collection.HVSCEntry;
import ui.entities.collection.service.VersionService;
import ui.entities.config.AudioSection;
import ui.entities.config.Configuration;
import ui.entities.config.FavoritesSection;
import ui.entities.config.SidPlay2Section;
import ui.musiccollection.search.SearchInIndexThread;
import ui.musiccollection.search.SearchIndexCreator;
import ui.musiccollection.search.SearchIndexerThread;
import ui.musiccollection.search.SearchThread;
import ui.stilview.STILView;
import java.beans.PropertyChangeListener;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static libsidutils.IOUtils.ioWrap;
/**
* Common view base for HVSC and CGSC collections. Loosely based on Rhythmbox, which is probably based on iTunes.
* Display is divided into 2 vertical panels of identical widths
* <p>
* - 1st shows file meta-info in the table, and composer's photo, scaled to 100% of width. These take the whole vertical
* space.
* <p>
* - 2nd column shows the search bar, which is used to match song and artist name
* <p>
* - 2nd column displays a list of artists.
* <p>
* - 2nd column displays a list of songs matching search criteria and selected artist. - currently playing symbol -
* artist name - song name - total song length (?)
*
* @author Ken Händel
* @author Antti Lankila
*/
public class MusicCollection extends C64VBox implements UIPart {
@FXML
private CheckBox autoConfiguration;
@FXML
private TableView<TuneInfo> tuneInfoTable;
@FXML
private TableColumn<TuneInfo, String> nameColumn, valueColumn;
@FXML
private TitledPane photographPane;
@FXML
private ImageView photograph;
@FXML
private TreeView<File> fileBrowser;
@FXML
private ComboBox<SearchCriteria<?, ?>> searchCriteria;
@FXML
private ComboBox<SearchScope> searchScope;
@FXML
private ComboBox<SearchResult> searchResult;
@FXML
private Button startSearch, stopSearch, resetSearch, createSearchIndex;
@FXML
private TextField collectionDir;
@FXML
private TypeTextField stringTextField, integerTextField, longTextField, shortTextField, localDateTextField;
@FXML
private ComboBox<Enum<?>> combo;
@FXML
private ContextMenu contextMenu;
@FXML
private MenuItem showStil, convertToPSID64, soasc6581R2, soasc6581R3, soasc6581R4, soasc8580R5;
@FXML
private Menu addToFavoritesMenu;
private final FileFilter tuneFilter = new TuneFileFilter();
private ObjectProperty<MusicCollectionType> type;
private ObservableList<TuneInfo> tuneInfos;
private ObservableList<Enum<?>> comboItems;
private ObjectProperty<List<TreeItem<File>>> currentlyPlayedTreeItemsProperty;
private EntityManager em;
private VersionService versionService;
private SearchThread searchThread;
private Object savedState, searchForValue, recentlySearchedForValue;
private SearchCriteria<?, ?> recentlySearchedCriteria;
private boolean searchOptionsChanged;
private String hvscName;
private int selectedSong;
private FavoritesSection favoritesToAddSearchResult;
private PropertyChangeListener tuneMatcherListener;
public MusicCollection() {
super();
}
public MusicCollection(C64Window window, Player player) {
super(window, player);
}
public MusicCollectionType getType() {
return type.get();
}
public void setType(MusicCollectionType type) {
this.type.set(type);
}
@FXML
@Override
protected void initialize() {
tuneMatcherListener = event -> Platform.runLater(
() -> showCurrentTune(fileBrowser.getRoot() != null ? fileBrowser.getRoot().getValue() : null));
ChangeListener<? super TreeItem<File>> tuneInfoListener = (observable, oldValue, newValue) -> {
if (newValue != null && newValue.getValue().isFile()) {
File tuneFile = newValue.getValue();
try {
SidTune sidTune = SidTune.load(tuneFile);
if (getType() == MusicCollectionType.HVSC) {
enableSOASC(sidTune.getInfo(), tuneFile);
showPhoto(sidTune.getInfo(), tuneFile);
}
showTuneInfos(tuneFile, sidTune);
} catch (IOException | SidTuneError e) {
openErrorDialog(String.format(util.getBundle().getString("ERR_IO_ERROR"), e.getMessage()),
getType());
}
}
};
EventHandler<WindowEvent> contextMenuEvent = event -> {
final TreeItem<File> selectedItem = fileBrowser.getSelectionModel().getSelectedItem();
showStil.setDisable(selectedItem == null || !((MusicCollectionTreeItem) selectedItem).hasSTIL());
convertToPSID64.setDisable(selectedItem == null);
SidPlay2Section sidplay2Section = util.getConfig().getSidplay2Section();
List<FavoritesSection> favorites = util.getConfig().getFavorites();
addToFavoritesMenu.getItems().clear();
for (final FavoritesSection section : favorites) {
MenuItem item = new MenuItem(section.getName());
item.setOnAction(event2 -> addFavorites(sidplay2Section, section, Collections.singletonList(selectedItem.getValue())));
addToFavoritesMenu.getItems().add(item);
}
addToFavoritesMenu.setDisable(addToFavoritesMenu.getItems().isEmpty());
};
util.getPlayer().stateProperty().addListener(tuneMatcherListener);
tuneInfos = FXCollections.observableArrayList();
SortedList<TuneInfo> sortedList = new SortedList<>(tuneInfos);
sortedList.comparatorProperty().bind(tuneInfoTable.comparatorProperty());
tuneInfoTable.setRowFactory(new TuneInfoRowFactory());
tuneInfoTable.setItems(sortedList);
tuneInfoTable.setPrefHeight(Double.MAX_VALUE);
photographPane.setPrefHeight(Double.MAX_VALUE);
nameColumn.prefWidthProperty().bind(tuneInfoTable.widthProperty().multiply(0.4));
valueColumn.prefWidthProperty().bind(tuneInfoTable.widthProperty().multiply(0.6));
searchScope.setConverter(new EnumToStringConverter<>(util.getBundle()));
searchScope.setItems(FXCollections.observableArrayList(SearchScope.values()));
searchScope.getSelectionModel().select(SearchScope.FORWARD);
searchResult.setConverter(new EnumToStringConverter<>(util.getBundle()));
searchResult.setItems(FXCollections.observableArrayList(SearchResult.values()));
searchResult.getSelectionModel().select(SearchResult.SHOW_NEXT_MATCH);
searchCriteria.setConverter(new SearchCriteriaToString(util.getBundle()));
searchCriteria.setItems(
FXCollections.observableArrayList(SearchCriteria.getSearchableAttributes()));
searchCriteria.getSelectionModel().select(0);
comboItems = FXCollections.observableArrayList();
combo.setItems(comboItems);
currentlyPlayedTreeItemsProperty = new SimpleObjectProperty<>(Collections.emptyList());
contextMenu.setOnShown(contextMenuEvent);
fileBrowser.setCellFactory(treeView -> new FileTreeCell());
fileBrowser.getSelectionModel().selectedItemProperty().addListener(tuneInfoListener);
fileBrowser.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
playSelected();
}
});
fileBrowser.setOnMousePressed(event -> {
if (event.isPrimaryButtonDown() && event.getClickCount() > 1) {
playSelected();
}
});
type = new SimpleObjectProperty<>();
type.addListener((observable, oldValue, newValue) -> Platform.runLater(() -> {
File initialRoot;
switch (getType()) {
case HVSC:
initialRoot = util.getConfig().getSidplay2Section().getHvsc();
break;
case CGSC:
initialRoot = util.getConfig().getSidplay2Section().getCgsc();
break;
default:
throw new RuntimeException("Illegal music collection type: " + type);
}
if (initialRoot != null) {
setRoot(initialRoot);
}
}));
}
private void showCurrentTune(File initialRoot) {
if (util.getPlayer().getTune() != SidTune.RESET && initialRoot != null) {
// auto-expand current selected tune
SidTune tune = util.getPlayer().getTune();
String collectionName = util.getPlayer().getSidDatabaseInfo(db -> db.getPath(tune), "");
showNextHit(new TFile(initialRoot, collectionName));
}
}
@Override
public void doClose() {
util.getPlayer().stateProperty().removeListener(tuneMatcherListener);
closeDatabase();
}
private void closeDatabase() {
if (fileBrowser.getRoot() != null && fileBrowser.getRoot().getValue() instanceof TFile) {
TFile tf = (TFile) fileBrowser.getRoot().getValue();
try {
TVFS.umount(tf);
} catch (FsSyncException e) {
e.printStackTrace();
}
}
if (em != null && em.isOpen()) {
em.getEntityManagerFactory().close();
}
}
@FXML
private void showSTIL() {
MusicCollectionTreeItem selectedItem = (MusicCollectionTreeItem) fileBrowser.getSelectionModel()
.getSelectedItem();
if (selectedItem != null && selectedItem.hasSTIL()) {
STILView stilInfo = new STILView(util.getPlayer());
stilInfo.setEntry(selectedItem.getStilEntry());
stilInfo.open();
}
}
@FXML
private void convertToPSID64() {
SidPlay2Section sidPlay2Section = util.getConfig().getSidplay2Section();
TreeItem<File> selectedItem = fileBrowser.getSelectionModel().getSelectedItem();
DirectoryChooser fileDialog = new DirectoryChooser();
fileDialog.setInitialDirectory(sidPlay2Section.getLastDirectory());
final File directory = fileDialog.showDialog(fileBrowser.getScene().getWindow());
if (directory != null) {
util.getConfig().getSidplay2Section().setLastDirectory(directory);
Psid64 c = new Psid64();
c.setTmpDir(util.getConfig().getSidplay2Section().getTmpDir());
c.setVerbose(true);
try {
c.convertFiles(util.getPlayer(), new File[]{selectedItem.getValue()}, directory,
sidPlay2Section.getHvsc());
} catch (IOException | SidTuneError e) {
openErrorDialog(String.format(util.getBundle().getString("ERR_IO_ERROR"), e.getMessage()), getType());
}
}
}
@FXML
private void startDownload6581R2() {
final String url = util.getConfig().getOnlineSection().getSoasc6581R2();
downloadStart(url, 49);
}
@FXML
private void startDownload6581R3() {
final String url = util.getConfig().getOnlineSection().getSoasc6581R3();
downloadStart(url, 49);
}
@FXML
private void startDownload6581R4() {
final String url = util.getConfig().getOnlineSection().getSoasc6581R4();
downloadStart(url, 49);
}
@FXML
private void startDownload8580R5() {
final String url = util.getConfig().getOnlineSection().getSoasc8580R5();
downloadStart(url, 49);
}
@FXML
private void doAutoConfiguration() {
String url, urlSearchIndex, urlSearchDataIndex, urlSearchIndexProperties;
switch (getType()) {
case HVSC:
url = util.getConfig().getOnlineSection().getHvscUrl();
urlSearchIndex = util.getConfig().getOnlineSection().getHvscSearchIndexUrl();
urlSearchDataIndex = util.getConfig().getOnlineSection().getHvscSearchIndexDataUrl();
urlSearchIndexProperties = util.getConfig().getOnlineSection().getHvscSearchIndexPropertiesUrl();
break;
case CGSC:
url = util.getConfig().getOnlineSection().getCgscUrl();
urlSearchIndex = util.getConfig().getOnlineSection().getCgscSearchIndexUrl();
urlSearchDataIndex = util.getConfig().getOnlineSection().getCgscSearchIndexDataUrl();
urlSearchIndexProperties = util.getConfig().getOnlineSection().getCgscSearchIndexPropertiesUrl();
break;
default:
throw new RuntimeException("Illegal music collection type: " + type);
}
if (autoConfiguration.isSelected()) {
autoConfiguration.setDisable(true);
closeDatabase();
MultiDownload multiDownload = new MultiDownload(util, fileBrowser.getScene(), downloadedFile -> Platform.runLater(() -> {
autoConfiguration.setDisable(false);
if (downloadedFile != null) {
setRoot(downloadedFile);
}
}));
multiDownload.download(Arrays.asList(urlSearchIndex, urlSearchDataIndex, urlSearchIndexProperties, url));
}
}
@FXML
private void searchCategory() {
if (searchCriteria.getSelectionModel().getSelectedItem() != recentlySearchedCriteria) {
searchOptionsChanged = true;
recentlySearchedCriteria = searchCriteria.getSelectionModel().getSelectedItem();
}
setSearchEditorVisible();
}
@FXML
private void doStartSearch() {
startSearch(false);
}
@FXML
private void doStopSearch() {
if (searchThread != null && searchThread.isAlive()) {
searchThread.setAborted(true);
}
}
@FXML
private void doResetSearch() {
savedState = null;
}
@FXML
private void doCreateSearchIndex() {
String msg = String.format(util.getBundle().getString("RECREATE_DATABASE"), type.get().toString());
Alert alert = new Alert(AlertType.CONFIRMATION, msg);
alert.setTitle(util.getBundle().getString("CREATE_SEARCH_DATABASE"));
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
startSearch(true);
}
}
@FXML
private void doBrowse() {
final DirectoryChooser fileDialog = new DirectoryChooser();
fileDialog.setInitialDirectory(util.getConfig().getSidplay2Section().getLastDirectory());
File directory = fileDialog.showDialog(autoConfiguration.getScene().getWindow());
if (directory != null) {
util.getConfig().getSidplay2Section().setLastDirectory(directory);
setRoot(directory);
}
}
@FXML
private void gotoURL() {
DesktopUtil.browse(type.get().getUrl());
}
@FXML
private void doSetValue() {
setSearchValue();
if (!Objects.equals(searchForValue, recentlySearchedForValue)) {
searchOptionsChanged = true;
recentlySearchedForValue = searchForValue;
}
startSearch(false);
}
private void setSearchEditorVisible() {
for (Node node : Arrays.asList(stringTextField, integerTextField, localDateTextField, longTextField,
shortTextField, combo)) {
node.setVisible(false);
}
SearchCriteria<?, ?> selectedItem = searchCriteria.getSelectionModel().getSelectedItem();
Class<?> type = selectedItem.getAttribute().getJavaType();
if (type == Long.class) {
longTextField.setVisible(true);
} else if (type == Integer.class) {
integerTextField.setVisible(true);
} else if (type == LocalDateTime.class) {
localDateTextField.setVisible(true);
} else if (type == Short.class) {
shortTextField.setVisible(true);
} else if (type == String.class) {
stringTextField.setVisible(true);
} else if (Enum.class.isAssignableFrom(type)) {
comboItems.clear();
@SuppressWarnings("unchecked")
Class<? extends Enum<?>> en = (Class<? extends Enum<?>>) type;
comboItems.addAll(Arrays.asList(en.getEnumConstants()));
combo.setVisible(true);
combo.getSelectionModel().select(0);
}
}
private void setSearchValue() {
SearchCriteria<?, ?> selectedItem = searchCriteria.getSelectionModel().getSelectedItem();
Class<?> type = selectedItem.getAttribute().getJavaType();
if (type == Integer.class) {
searchForValue = integerTextField.getValue();
} else if (type == Long.class) {
searchForValue = longTextField.getValue();
} else if (type == Short.class) {
searchForValue = shortTextField.getValue();
} else if (type == String.class) {
searchForValue = stringTextField.getValue();
} else if (Enum.class.isAssignableFrom(type)) {
searchForValue = combo.getSelectionModel().getSelectedItem();
} else if (type == LocalDateTime.class) {
searchForValue = localDateTextField.getValue();
}
}
private void setRoot(final File rootFile) {
Task<Void> task = new Task<Void>() {
@Override
public Void call() {
try {
SidPlay2Section sidPlay2Section = util.getConfig().getSidplay2Section();
final File theRootFile = new TFile(rootFile);
if (getType() == MusicCollectionType.HVSC) {
util.getPlayer().setSidDatabase(new SidDatabase(theRootFile));
util.getPlayer().setSTIL(new STIL(theRootFile));
sidPlay2Section.setHvsc(theRootFile);
setViewRoot(sidPlay2Section.getHvsc());
} else if (getType() == MusicCollectionType.CGSC) {
sidPlay2Section.setCgsc(theRootFile);
setViewRoot(sidPlay2Section.getCgsc());
}
closeDatabase();
File dbFilename = new File(theRootFile.getParentFile(), type.get().toString());
PersistenceProperties pp = new PersistenceProperties(DatabaseType.HSQL_FILE, "", "",
dbFilename.getAbsolutePath());
EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(type.get().getDataSource(),
pp);
em = emFactory.createEntityManager();
versionService = new VersionService(em);
Platform.runLater(() -> showCurrentTune(theRootFile));
} catch (FileNotFoundException e) {
openErrorDialog(String.format(util.getBundle().getString("ERR_FILE_NOT_FOUND"), e.getMessage()),
getType());
} catch (IOException | NoSuchFieldException | IllegalAccessException | PersistenceException
| IllegalStateException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
openErrorDialog(sw.toString(), getType());
}
return null;
}
};
new Thread(task).start();
}
private void openErrorDialog(String msg, MusicCollectionType type) {
msg = String.format(util.getBundle().getString("ERR_CANNOT_CONFIGURE"), type) + msg;
Alert alert = new Alert(AlertType.ERROR, msg);
alert.setTitle(util.getBundle().getString("ALERT_TITLE"));
alert.showAndWait();
}
private void setViewRoot(final File theRootFile) {
Platform.runLater(() -> {
MusicCollectionCellFactory cellFactory = new MusicCollectionCellFactory();
cellFactory.setCurrentlyPlayedTreeItems(currentlyPlayedTreeItemsProperty);
fileBrowser.setRoot(new MusicCollectionTreeItem(util.getPlayer(), theRootFile));
fileBrowser.setCellFactory(cellFactory);
collectionDir.setText(theRootFile.getAbsolutePath());
doResetSearch();
});
}
private void startSearch(boolean forceRecreate) {
if (searchThread != null && searchThread.isAlive()) {
return;
}
if (!new File(collectionDir.getText()).exists()) {
return;
}
/*
* validate a database: a version is inserted only after successful create
* completes.
*/
if (!forceRecreate && !versionService.isExpectedVersion()) {
forceRecreate = true;
}
Consumer<Void> searchStart;
Consumer<File> searchHit;
Consumer<Boolean> searchStop;
if (forceRecreate) {
if (!em.isOpen()) {
openErrorDialog(String.format(util.getBundle().getString("ERR_DATABASE")), getType());
return;
}
SearchIndexCreator searchIndexCreator = new SearchIndexCreator(fileBrowser.getRoot().getValue(),
util.getPlayer(), em);
searchStart = x -> {
Platform.runLater(() -> {
disableSearch();
util.progressProperty(fileBrowser.getScene()).set(ProgressIndicator.INDETERMINATE_PROGRESS);
});
searchIndexCreator.getSearchStart().accept(x);
};
searchHit = searchIndexCreator.getSearchHit();
searchStop = cancelled -> {
Platform.runLater(() -> {
enableSearch();
util.progressProperty(fileBrowser.getScene()).set(0);
});
searchIndexCreator.getSearchStop().accept(cancelled);
};
searchThread = new SearchIndexerThread(fileBrowser.getRoot().getValue(), searchStart, searchHit,
searchStop);
searchThread.start();
} else {
SidPlay2Section sidplay2Section = util.getConfig().getSidplay2Section();
switch (searchResult.getSelectionModel().getSelectedItem()) {
case ADD_TO_A_NEW_PLAYLIST:
searchStart = file -> Platform.runLater(() -> {
disableSearch();
createNewFavoritesTab();
});
searchHit = file -> {
while (favoritesToAddSearchResult == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
addFavorite(sidplay2Section, favoritesToAddSearchResult, file);
};
searchStop = cancelled -> Platform.runLater(this::enableSearch);
break;
case SHOW_NEXT_MATCH:
default:
searchStart = x -> Platform.runLater(this::disableSearch);
searchHit = file -> {
// ignore directories
if (file.isFile()) {
searchThread.setAborted(true);
Platform.runLater(() -> {
enableSearch();
showNextHit(file);
});
}
};
searchStop = cancelled -> Platform.runLater(this::enableSearch);
break;
}
setSearchValue();
final SearchInIndexThread t = new SearchInIndexThread(em,
searchScope.getSelectionModel().getSelectedItem() == SearchScope.FORWARD, searchStart, searchHit,
searchStop) {
@Override
public List<File> getFiles(String filePath) {
return IOUtils.getFiles(filePath, fileBrowser.getRoot().getValue(), tuneFilter);
}
};
t.setField(searchCriteria.getSelectionModel().getSelectedItem().getAttribute());
t.setFieldValue(searchForValue);
t.setCaseSensitive(false);
if (searchOptionsChanged) {
doResetSearch();
searchOptionsChanged = false;
}
searchThread = t;
searchThread.setSearchState(savedState);
searchThread.start();
}
}
private void disableSearch() {
startSearch.setDisable(true);
stopSearch.setDisable(false);
resetSearch.setDisable(true);
createSearchIndex.setDisable(true);
}
private void enableSearch() {
// remember the search state
savedState = searchThread.getSearchState();
startSearch.setDisable(false);
stopSearch.setDisable(true);
resetSearch.setDisable(false);
createSearchIndex.setDisable(false);
}
private void showNextHit(final File matchFile) {
if (!matchFile.isFile()) {
// ignore directories
return;
}
TreeItem<File> rootItem = fileBrowser.getRoot();
if (rootItem == null || matchFile.getName().toLowerCase(Locale.ENGLISH).endsWith(".mp3")) {
return;
}
List<TreeItem<File>> pathSegs = new ArrayList<>();
pathSegs.add(rootItem);
File rootFile = rootItem.getValue();
String filePath = matchFile.getPath();
TreeItem<File> curItem = rootItem;
for (File file : IOUtils.getFiles(filePath, rootFile, tuneFilter)) {
for (TreeItem<File> childItem : curItem.getChildren()) {
if (file.equals(childItem.getValue())) {
pathSegs.add(curItem = childItem);
childItem.setExpanded(true);
break;
}
}
}
if (!pathSegs.isEmpty()) {
currentlyPlayedTreeItemsProperty.set(pathSegs);
TreeItem<File> selectedItem = fileBrowser.getSelectionModel().getSelectedItem();
TreeItem<File> treeItem = pathSegs.get(pathSegs.size() - 1);
if (selectedItem == null || !treeItem.getValue().equals(selectedItem.getValue())) {
fileBrowser.getSelectionModel().select(treeItem);
fileBrowser.scrollTo(fileBrowser.getRow(treeItem));
}
}
}
private void createNewFavoritesTab() {
FavoritesSection newFavorites = new FavoritesSection();
newFavorites.setName(util.getBundle().getString("NEW_TAB"));
favoritesToAddSearchResult = newFavorites;
util.getConfig().getFavorites().add(newFavorites);
}
private void downloadStart(final String url, final int hvscVersion) {
String realUrl = MessageFormat.format(url, hvscVersion, hvscName, selectedSong).trim();
System.out.println("Download URL: <" + realUrl + ">");
try {
new DownloadThread(util.getConfig(), new ProgressListener(util, fileBrowser.getScene()) {
@Override
public void downloaded(final File downloadedFile) {
if ((downloadedFile == null || downloadedFile.length() < 1000) && hvscVersion < 100) {
// skip errors, try to guess HVSC version
if (downloadedFile != null) {
ioWrap(downloadedFile::delete);
}
downloadStart(url, hvscVersion + 1);
} else if (downloadedFile != null) {
downloadedFile.deleteOnExit();
Platform.runLater(() -> {
Configuration config = util.getConfig();
SidPlay2Section sidplay2Section = config.getSidplay2Section();
AudioSection audioSection = config.getAudioSection();
audioSection.setMp3(downloadedFile);
audioSection.setPlayOriginal(true);
audioSection.setAudio(Audio.COMPARE_MP3);
playTune(IOUtils.getFile(hvscName + ".sid", sidplay2Section.getHvsc(), null));
});
}
}
}, new URI(realUrl).toURL(), false).start();
} catch (MalformedURLException | URISyntaxException e) {
e.printStackTrace();
}
}
private void enableSOASC(SidTuneInfo tuneInfo, File tuneFile) {
soasc6581R2.setDisable(true);
soasc6581R3.setDisable(true);
soasc6581R4.setDisable(true);
soasc8580R5.setDisable(true);
File hvscFile = util.getConfig().getSidplay2Section().getHvsc();
hvscName = IOUtils.getCollectionName(hvscFile, tuneFile);
if (hvscName != null) {
hvscName = hvscName.replace(".sid", "");
selectedSong = tuneInfo.getSelectedSong();
soasc6581R2.setDisable(false);
soasc6581R3.setDisable(false);
soasc6581R4.setDisable(false);
soasc8580R5.setDisable(false);
}
}
private void showPhoto(SidTuneInfo info, File tuneFile) {
File hvscRoot = fileBrowser.getRoot().getValue();
String collectionName = null;
if (tuneFile.getParentFile() != null) {
collectionName = IOUtils.getCollectionName(hvscRoot, tuneFile.getParentFile());
}
String author = null;
if (info.getInfoString().size() > 1) {
Iterator<String> iterator = info.getInfoString().iterator();
/* title = */
iterator.next();
author = iterator.next();
}
photograph.setImage(new Image(new ByteArrayInputStream(Photos.getPhoto(collectionName, author))));
}
private void showTuneInfos(File tuneFile, SidTune tune) {
String collectionName = IOUtils.getCollectionName(fileBrowser.getRoot().getValue(), tuneFile);
HVSCEntry entry = new HVSCEntry(() -> util.getPlayer().getSidDatabaseInfo(db -> db.getTuneLength(tune), 0.),
tuneFile.getName(), collectionName, tuneFile.length(), tuneFile.lastModified(), tune,
String.join(",", tune.identify()));
ResourceBundle detailsBundle = ResourceBundle.getBundle(SidTuneInfoDetails.BUNDLE);
SidTuneInfoDetails details = SidTuneInfoDetails.forFile(tune, tuneFile,
util.getConfig().getSidplay2Section().isEnableDatabase()
? song -> util.getPlayer().getSidDatabaseInfo(db -> db.getSongLength(song), 0.) : null,
util.getConfig().getSidplay2Section().getDefaultPlayLength(), detailsBundle::getString);
tuneInfos.setAll(SearchCriteria
.getAttributeValues(entry, field -> searchCriteria.getConverter().toString(field),
details, name -> SidTuneInfoDetails.localizeName(name, detailsBundle::getString)).stream()
.map(info -> new TuneInfo(info.getKey(), info.getValue())).collect(Collectors.toList()));
}
private void addFavorites(SidPlay2Section sidplay2Section, FavoritesSection section, List<File> files) {
for (File file : files) {
final File[] listFiles = file.listFiles();
if (file.isDirectory() && listFiles != null) {
addFavorites(sidplay2Section, section, Arrays.asList(listFiles));
} else if (file.isFile() && tuneFilter.accept(file)) {
addFavorite(sidplay2Section, section, file);
}
}
}
private void addFavorite(SidPlay2Section sidPlay2Section, FavoritesSection section, File file) {
try {
SidTune tune = SidTune.load(file);
String collectionName;
if (getType() == MusicCollectionType.HVSC) {
collectionName = IOUtils.getCollectionName(sidPlay2Section.getHvsc(), file);
} else {
collectionName = IOUtils.getCollectionName(sidPlay2Section.getCgsc(), file);
}
HVSCEntry entry = new HVSCEntry(() -> util.getPlayer().getSidDatabaseInfo(db -> db.getTuneLength(tune), 0.),
file.getName(), collectionName, file.length(), file.lastModified(), tune,
String.join(",", tune.identify()));
section.getFavorites().add(entry);
} catch (IOException | SidTuneError e) {
openErrorDialog(String.format(util.getBundle().getString("ERR_IO_ERROR"), e.getMessage()), getType());
}
}
private void playSelected() {
final TreeItem<File> selectedItem = fileBrowser.getSelectionModel().getSelectedItem();
if (selectedItem != null && !selectedItem.equals(fileBrowser.getRoot()) && selectedItem.getValue().isFile()) {
playTune(selectedItem.getValue());
}
}
private void playTune(final File file) {
util.setPlayingTab(this, currentlyPlayedTreeItemsProperty);
try {
util.getPlayer().play(SidTune.load(file));
} catch (IOException | SidTuneError e) {
openErrorDialog(String.format(util.getBundle().getString("ERR_IO_ERROR"), e.getMessage()), getType());
}
}
}