feat: New menu option to edit primary joy port for multiple games

This commit is contained in:
lantzelot-swe 2023-10-01 08:28:38 +02:00
parent febe34ce49
commit 5024f60ddd
5 changed files with 206 additions and 7 deletions

View File

@ -333,7 +333,7 @@ public class DbConnector
Integer.toString(rs.getInt("rowid")),
rs.getInt("Favorite"),
viewTag != null && viewTag.contains("GIS:"));
//For filtering
data.setComposer(rs.getString("Composer"));
data.setAuthor(rs.getString("Author"));
@ -469,13 +469,12 @@ public class DbConnector
{
ExceptionHandler.handleException(e, "Could not fetch title by id");
}
String oldViewName = viewNumber > 1 ? originalViewName + "/" + viewNumber : originalViewName;
//Always use "0" as for first view (sorting is a bit random...)
String firstGameLetter = viewNumber == 1 ? "0" : firstGameTitle.substring(0, 1).toUpperCase();
String newViewName = originalViewName + "/" + firstGameLetter + "-" +
lastGameTitle.substring(0, 1).toUpperCase();
String newViewName = originalViewName + "/" + firstGameLetter + "-" + lastGameTitle.substring(0, 1).toUpperCase();
sqlBuilder = new StringBuilder();
sqlBuilder.append("");
@ -1474,12 +1473,12 @@ public class DbConnector
ExceptionHandler.handleException(e, "Could not delete games in db.");
}
}
public void deleteGames(List<GameListData> selectedGameListData)
{
List<String> idList = selectedGameListData.stream().map(data -> data.getGameId()).collect(Collectors.toList());
String idsString = String.join(",", idList);
String sql = "DELETE FROM gameinfo where rowId IN (" + idsString + ");";
logger.debug("Generated DELETE String:\n{}", sql);
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql))
@ -1687,6 +1686,41 @@ public class DbConnector
}
}
public void updatePrimaryJoystickPort(List<String> idList, boolean port1)
{
String joy1Default = "J:1*:";
String joy1NotDefault = "J:1:";
String joy2Default = "J:2*:";
String joy2NotDefault = "J:2:";
String idListString = String.join(",", idList);
StringBuilder sqlBuilder = new StringBuilder();
String expression = String
.format("UPDATE gameinfo SET Joy1Config = REPLACE(Joy1Config, '%s', '%s'),Joy2Config = REPLACE(Joy2Config, '%s', '%s') WHERE rowId IN (",
port1 ? joy1NotDefault : joy1Default,
port1 ? joy1Default : joy1NotDefault,
port1 ? joy2Default : joy2NotDefault,
port1 ? joy2NotDefault : joy2Default
);
sqlBuilder.append(expression);
sqlBuilder.append(idListString);
sqlBuilder.append(");");
logger.debug("Generated SQL for primary joy ports:\n{}", sqlBuilder.toString());
try (Connection conn = this.connect(); PreparedStatement joy1tmt = conn.prepareStatement(sqlBuilder.toString());)
{
int value = joy1tmt.executeUpdate();
logger.debug("Executed successfully, value = {}", value);
}
catch (SQLException e)
{
ExceptionHandler.handleException(e, "Could not update primary joystick port");
}
}
public void setAccurateDiskForView(GameView view, boolean accurateDisk)
{

View File

@ -11,6 +11,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.swing.JEditorPane;
import javax.swing.JMenu;
@ -111,6 +112,7 @@ public class MenuManager
private JMenuItem clearFavorites10Item;
private JMenuItem editViewTagItem;
private JMenuItem editPrimaryJoystickItem;
private JMenuItem backupDbItem;
private JMenuItem restoreDbItem;
@ -336,6 +338,7 @@ public class MenuManager
}
editMenu.addSeparator();
editMenu.add(getEditViewTagItem());
editMenu.add(getPrimaryJoystickItem());
return editMenu;
}
@ -822,6 +825,43 @@ public class MenuManager
return editViewTagItem;
}
private JMenuItem getPrimaryJoystickItem()
{
editPrimaryJoystickItem = new JMenuItem("Edit primary Joystick...");
KeyStroke keyStrokeToEditJoy = KeyStroke.getKeyStroke(KeyEvent.VK_J, InputEvent.CTRL_DOWN_MASK);
editPrimaryJoystickItem.setAccelerator(keyStrokeToEditJoy);
editPrimaryJoystickItem.addActionListener(e -> {
if (!mainWindow.getMainPanel().isNoGameSelected())
{
PrimaryJoystickDialog dialog = new PrimaryJoystickDialog(MainWindow.getInstance());
dialog.pack();
dialog.setLocationRelativeTo(this.mainWindow);
if (dialog.showDialog())
{
mainWindow.setWaitCursor(true);
List<String> selectedGameIds = mainWindow.getMainPanel().getListPanel().getSelectedGameListData().stream()
.map(data -> data.getGameId()).collect(Collectors.toList());
uiModel.updatePrimaryJoystickPort(selectedGameIds, dialog.isPort1Primary());
mainWindow.setWaitCursor(false);
JOptionPane.showMessageDialog(MainWindow.getInstance().getMainPanel(),
"Primary joystick port updated for the selected games.",
"Primary port",
JOptionPane.INFORMATION_MESSAGE);
if (selectedGameIds.size() == 1)
{
MainWindow.getInstance().reloadCurrentGameView();
}
}
}
});
return editPrimaryJoystickItem;
}
private JMenuItem getBackupDbItem()
{
backupDbItem = new JMenuItem("Backup database");

View File

@ -0,0 +1,36 @@
package se.lantz.gui;
import java.awt.Dimension;
import java.awt.Frame;
public class PrimaryJoystickDialog extends BaseDialog
{
private Dimension dialogSize = new Dimension(350, 170);
private PrimaryJoystickSelectionPanel portPanel;
public PrimaryJoystickDialog(Frame owner)
{
super(owner);
//Create a separate model so that changes can be cancelled
setTitle("Edit primary port");
addContent(getPrimaryPortPanel());
this.setPreferredSize(dialogSize);
this.setResizable(false);
}
private PrimaryJoystickSelectionPanel getPrimaryPortPanel()
{
if (portPanel == null)
{
portPanel = new PrimaryJoystickSelectionPanel();
}
return portPanel;
}
public boolean isPort1Primary()
{
return getPrimaryPortPanel().isPort1Primary();
}
}

View File

@ -0,0 +1,76 @@
package se.lantz.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class PrimaryJoystickSelectionPanel extends JPanel
{
private JLabel infoLabel;
private JRadioButton port1RadioButton;
private JRadioButton port2RadioButton;
private final ButtonGroup buttonGroup = new ButtonGroup();
public PrimaryJoystickSelectionPanel()
{
GridBagLayout gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
GridBagConstraints gbc_infoLabel = new GridBagConstraints();
gbc_infoLabel.weightx = 1.0;
gbc_infoLabel.insets = new Insets(15, 5, 10, 0);
gbc_infoLabel.gridx = 0;
gbc_infoLabel.gridy = 0;
add(getInfoLabel(), gbc_infoLabel);
GridBagConstraints gbc_port1RadioButton = new GridBagConstraints();
gbc_port1RadioButton.gridx = 0;
gbc_port1RadioButton.gridy = 1;
add(getPort1RadioButton(), gbc_port1RadioButton);
GridBagConstraints gbc_port2RadioButton = new GridBagConstraints();
gbc_port2RadioButton.anchor = GridBagConstraints.NORTH;
gbc_port2RadioButton.weighty = 1.0;
gbc_port2RadioButton.gridx = 0;
gbc_port2RadioButton.gridy = 2;
add(getPort2RadioButton(), gbc_port2RadioButton);
}
private JLabel getInfoLabel()
{
if (infoLabel == null)
{
infoLabel = new JLabel("Select which port to set as primary for the selected games:");
}
return infoLabel;
}
private JRadioButton getPort1RadioButton()
{
if (port1RadioButton == null)
{
port1RadioButton = new JRadioButton("Port 1");
buttonGroup.add(port1RadioButton);
}
return port1RadioButton;
}
private JRadioButton getPort2RadioButton()
{
if (port2RadioButton == null)
{
port2RadioButton = new JRadioButton("Port 2");
port2RadioButton.setSelected(true);
buttonGroup.add(port2RadioButton);
}
return port2RadioButton;
}
public boolean isPort1Primary()
{
return getPort1RadioButton().isSelected();
}
}

View File

@ -991,6 +991,14 @@ public class MainViewModel extends AbstractModel
gameListModel.notifyChange();
}
}
public void setPrimaryJoystick(boolean port1)
{
//Enough to toggle on joy 1 model?
getJoy1Model().setPrimary(port1);
//Save the current game
saveData();
}
private int toggleFavorite(GameListData data, int favoritesNumber, int favoritesCount, GameView favoritesView)
{
@ -1037,6 +1045,11 @@ public class MainViewModel extends AbstractModel
{
dbConnector.resetJoystickConfigsForView(getSelectedGameView());
}
public void updatePrimaryJoystickPort(List<String> gameIdList, boolean port1)
{
dbConnector.updatePrimaryJoystickPort(gameIdList, port1);
}
public void enableAccurateDiskForAllGamesInCurrentView()
{