From 7a9fee0d49daa73dbb583652f7519553cd107d75 Mon Sep 17 00:00:00 2001 From: "mikael.lantz" Date: Fri, 18 Dec 2020 16:50:26 +0100 Subject: [PATCH] Adds abut dialog, version reading and build adjustments --- pom.xml | 97 +++++----- src/main/java/se/lantz/gui/AboutDialog.java | 182 ++++++++++++++++++ src/main/java/se/lantz/gui/ListPanel.java | 20 +- src/main/java/se/lantz/gui/MainWindow.java | 15 +- src/main/java/se/lantz/gui/MenuManager.java | 26 +-- .../java/se/lantz/gui/ScreenshotsPanel.java | 18 +- src/main/java/se/lantz/gui/SystemPanel.java | 4 +- .../java/se/lantz/model/AbstractModel.java | 11 +- .../java/se/lantz/model/MainViewModel.java | 27 ++- src/main/java/se/lantz/util/FileManager.java | 40 ++++ 10 files changed, 361 insertions(+), 79 deletions(-) create mode 100644 src/main/java/se/lantz/gui/AboutDialog.java diff --git a/pom.xml b/pom.xml index b05fdbb..260ae39 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 se.lantz PCUGameManager - 0.0.9 + 0.9.0 PCUGameManager @@ -70,6 +70,11 @@ se.lantz.PCUGameManager + + + ${project.version} + + jar-with-dependencies @@ -79,50 +84,52 @@ - com.akathist.maven.plugins.launch4j - launch4j-maven-plugin - 3.12 - - - l4j-gui - package - launch4j - - gui - target/PcuGameManager.exe - target/PCUGameManager-${project.version}-jar-with-dependencies.jar - PcuGameManager - . - - PcuGameManager - - - se.lantz.PCUGameManager - - - - 1.8 - ./JRE - - - target/classes/PCUGameManager.bmp - 30 - - target/classes/Icon.ico - - 0.0.0.1 - txt file version? - a description - my copyright - 0.0.0.1 - txt product version - PcuGameManager - pcu - PcuGameManager.exe - - - - + com.akathist.maven.plugins.launch4j + launch4j-maven-plugin + 1.7.25 + + + l4j-gui + package + + launch4j + + + gui + target/PcuGameManager.exe + target/PCUGameManager-${project.version}-jar-with-dependencies.jar + PcuGameManager + . + + PcuGameManager + + + se.lantz.PCUGameManager + + + + 1.8 + ./JRE + + + target/classes/PCUGameManager.bmp + 30 + + target/classes/Icon.ico + + ${project.version}.1 + ${project.version}} + PCU Game Manager + Lantzelot + ${project.version}.1 + ${project.version} + PCU Game Manager + pcu + PcuGameManager.exe + + + + diff --git a/src/main/java/se/lantz/gui/AboutDialog.java b/src/main/java/se/lantz/gui/AboutDialog.java new file mode 100644 index 0000000..7b87717 --- /dev/null +++ b/src/main/java/se/lantz/gui/AboutDialog.java @@ -0,0 +1,182 @@ +package se.lantz.gui; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.KeyStroke; + +import se.lantz.util.FileManager; +import java.awt.Font; + +public class AboutDialog extends JDialog +{ + private JLabel imageLabel; + private JLabel titleLabel; + private JLabel versionLabel; + private JLabel programmedLabel; + private JLabel thanksTo1Label; + private JLabel thanks2Label; + + KeyStroke escKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); + + Action escAction = new AbstractAction() + { + private static final long serialVersionUID = 1756396251349970052L; + + @Override + public void actionPerformed(ActionEvent e) + { + setVisible(false); + } + }; + private JButton closeButton; + private JLabel jreVersionLabel; + + public AboutDialog() + { + super(MainWindow.getInstance(), "About", true); + GridBagLayout gridBagLayout = new GridBagLayout(); + getContentPane().setLayout(gridBagLayout); + GridBagConstraints gbc_imageLabel = new GridBagConstraints(); + gbc_imageLabel.weightx = 1.0; + gbc_imageLabel.insets = new Insets(0, 0, 5, 0); + gbc_imageLabel.gridx = 0; + gbc_imageLabel.gridy = 0; + getContentPane().add(getImageLabel(), gbc_imageLabel); + GridBagConstraints gbc_titleLabel = new GridBagConstraints(); + gbc_titleLabel.insets = new Insets(0, 0, 5, 0); + gbc_titleLabel.gridx = 0; + gbc_titleLabel.gridy = 1; + getContentPane().add(getTitleLabel(), gbc_titleLabel); + GridBagConstraints gbc_versionLabel = new GridBagConstraints(); + gbc_versionLabel.insets = new Insets(0, 0, 5, 0); + gbc_versionLabel.gridx = 0; + gbc_versionLabel.gridy = 2; + getContentPane().add(getVersionLabel(), gbc_versionLabel); + GridBagConstraints gbc_jreVersionLabel = new GridBagConstraints(); + gbc_jreVersionLabel.insets = new Insets(0, 0, 5, 0); + gbc_jreVersionLabel.gridx = 0; + gbc_jreVersionLabel.gridy = 3; + getContentPane().add(getJreVersionLabel(), gbc_jreVersionLabel); + GridBagConstraints gbc_programmedLabel = new GridBagConstraints(); + gbc_programmedLabel.insets = new Insets(0, 0, 5, 0); + gbc_programmedLabel.gridx = 0; + gbc_programmedLabel.gridy = 4; + getContentPane().add(getProgrammedLabel(), gbc_programmedLabel); + GridBagConstraints gbc_thanksTo1Label = new GridBagConstraints(); + gbc_thanksTo1Label.insets = new Insets(10, 0, 5, 0); + gbc_thanksTo1Label.gridx = 0; + gbc_thanksTo1Label.gridy = 5; + getContentPane().add(getThanksTo1Label(), gbc_thanksTo1Label); + GridBagConstraints gbc_closeButton = new GridBagConstraints(); + gbc_closeButton.anchor = GridBagConstraints.SOUTH; + gbc_closeButton.insets = new Insets(5, 5, 10, 5); + gbc_closeButton.gridx = 0; + gbc_closeButton.gridy = 7; + getContentPane().add(getCloseButton(), gbc_closeButton); + GridBagConstraints gbc_thanks2Label = new GridBagConstraints(); + gbc_thanks2Label.insets = new Insets(0, 0, 5, 0); + gbc_thanks2Label.anchor = GridBagConstraints.NORTH; + gbc_thanks2Label.weighty = 1.0; + gbc_thanks2Label.gridx = 0; + gbc_thanks2Label.gridy = 6; + getContentPane().add(getThanks2Label(), gbc_thanks2Label); + + this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + //Register esc as closing the window + getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escKeyStroke, "closeDialog"); + getRootPane().getActionMap().put("closeDialog", escAction); + this.setResizable(false); + } + + private JLabel getImageLabel() + { + if (imageLabel == null) + { + imageLabel = new JLabel(""); + imageLabel.setIcon(new ImageIcon(getClass().getResource("/se/lantz/PCUGameManager.png"))); + } + return imageLabel; + } + + private JLabel getTitleLabel() + { + if (titleLabel == null) + { + titleLabel = new JLabel("Project Carousel USB Game Manager"); + } + return titleLabel; + } + + private JLabel getVersionLabel() + { + if (versionLabel == null) + { + versionLabel = new JLabel("Version: " + FileManager.getPcuVersionFromManifest()); + versionLabel.setFont(new Font("Tahoma", Font.BOLD, 11)); + + } + return versionLabel; + } + + private JLabel getProgrammedLabel() + { + if (programmedLabel == null) + { + programmedLabel = new JLabel("Coded by Lantzelot"); + } + return programmedLabel; + } + + private JLabel getThanksTo1Label() + { + if (thanksTo1Label == null) + { + thanksTo1Label = + new JLabel("Thanks to Spannernick for the Project Carousel USB initiative and idea for this application."); + } + return thanksTo1Label; + } + + private JLabel getThanks2Label() + { + if (thanks2Label == null) + { + thanks2Label = new JLabel(""); + } + return thanks2Label; + } + + private JButton getCloseButton() + { + if (closeButton == null) + { + closeButton = new JButton("Close"); + closeButton.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + setVisible(false); + } + }); + } + return closeButton; + } + private JLabel getJreVersionLabel() { + if (jreVersionLabel == null) { + jreVersionLabel = new JLabel("JRE version:" + System.getProperty("java.runtime.version")); + } + return jreVersionLabel; + } +} diff --git a/src/main/java/se/lantz/gui/ListPanel.java b/src/main/java/se/lantz/gui/ListPanel.java index e66276f..8bf5397 100644 --- a/src/main/java/se/lantz/gui/ListPanel.java +++ b/src/main/java/se/lantz/gui/ListPanel.java @@ -145,13 +145,16 @@ public class ListPanel extends JPanel { public void actionPerformed(ActionEvent e) { - uiModel.setSelectedGameView((GameView) listViewComboBox.getSelectedItem()); - //TODO: keep track of selected index for the view and select it once data is updated - updateViewInfoLabel(); - SwingUtilities.invokeLater(() -> { - getList().setSelectedIndex(0); - getList().ensureIndexIsVisible(0); - }); + if (!uiModel.isDisableChangeNotifcation()) + { + uiModel.setSelectedGameView((GameView) listViewComboBox.getSelectedItem()); + //TODO: keep track of selected index for the view and select it once data is updated + updateViewInfoLabel(); + SwingUtilities.invokeLater(() -> { + getList().setSelectedIndex(0); + getList().ensureIndexIsVisible(0); + }); + } } }); listViewComboBox.setModel(uiModel.getGameViewModel()); @@ -290,7 +293,7 @@ public class ListPanel extends JPanel list.addListSelectionListener(e -> { if (!e.getValueIsAdjusting() || pageButtonPressed) { - if (!delayDetailsUpdate) + if (!delayDetailsUpdate && !uiModel.isDisableChangeNotifcation()) { updateSelectedGame(); } @@ -308,6 +311,7 @@ public class ListPanel extends JPanel private void updateSelectedGame() { + System.out.println("Update selected Game!!!"); SwingUtilities .invokeLater(() -> mainPanel.getGameDetailsBackgroundPanel().updateSelectedGame(list.getSelectedValue())); } diff --git a/src/main/java/se/lantz/gui/MainWindow.java b/src/main/java/se/lantz/gui/MainWindow.java index 7e641fc..c55d1e7 100644 --- a/src/main/java/se/lantz/gui/MainWindow.java +++ b/src/main/java/se/lantz/gui/MainWindow.java @@ -6,10 +6,13 @@ import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; +import javax.swing.SwingUtilities; +import javax.swing.WindowConstants; import se.lantz.model.MainViewModel; +import se.lantz.util.FileManager; -public class MainWindow extends JFrame +public final class MainWindow extends JFrame { /** * @@ -43,7 +46,7 @@ public class MainWindow extends JFrame this.setTitle("PCU Game Manager"); uiModel = new MainViewModel(); menuManager = new MenuManager(uiModel, this); - this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); this.addWindowListener(new java.awt.event.WindowAdapter() { @@ -56,11 +59,19 @@ public class MainWindow extends JFrame this.setJMenuBar(getMainMenuBar()); + + //Update title with version if available + String versionValue = FileManager.getPcuVersionFromManifest(); + if (!versionValue.isEmpty()) + { + setTitle("PCU Game Manager v." + versionValue); + } } public void initialize() { getMainPanel().initialize(); + menuManager.intialize(); } MainPanel getMainPanel() diff --git a/src/main/java/se/lantz/gui/MenuManager.java b/src/main/java/se/lantz/gui/MenuManager.java index 0fdf6e8..5bd6a8f 100644 --- a/src/main/java/se/lantz/gui/MenuManager.java +++ b/src/main/java/se/lantz/gui/MenuManager.java @@ -94,22 +94,11 @@ public class MenuManager helpMenu = new JMenu("Help"); helpMenu.add(getHelpItem()); helpMenu.add(getAboutItem()); - - uiModel.addSaveChangeListener(e -> { - addGameItem.setEnabled(!uiModel.isDataChanged()); - }); - // KeyStroke keyStrokeToAddGame = KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_DOWN_MASK); - // - // mainWindow.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStrokeToAddGame, "AddGame"); - // mainWindow.getRootPane().getActionMap().put("AddGame", new AbstractAction() - // { - // @Override - // public void actionPerformed(ActionEvent arg0) - // { - // mainWindow.getMainPanel().addNewGame(); - // } - // }); - + } + + public void intialize() + { + uiModel.addSaveChangeListener(e -> addGameItem.setEnabled(!uiModel.isDataChanged())); } public List getMenues() @@ -224,7 +213,10 @@ public class MenuManager { aboutItem = new JMenuItem("About..."); aboutItem.addActionListener(e -> { - //TODO + AboutDialog dialog = new AboutDialog(); + dialog.pack(); + dialog.setLocationRelativeTo(this.mainWindow); + dialog.setVisible(true); }); return aboutItem; } diff --git a/src/main/java/se/lantz/gui/ScreenshotsPanel.java b/src/main/java/se/lantz/gui/ScreenshotsPanel.java index 3b4150b..3182b5d 100644 --- a/src/main/java/se/lantz/gui/ScreenshotsPanel.java +++ b/src/main/java/se/lantz/gui/ScreenshotsPanel.java @@ -125,19 +125,31 @@ public class ScreenshotsPanel extends JPanel private void reloadScreens() { String modelCoverFile = model.getCoverFile(); - if (!modelCoverFile.equals(currentCoverFile)) + if (modelCoverFile.isEmpty()) + { + getCoverImageLabel().setIcon(getMissingCoverImageIcon()); + } + else if (!modelCoverFile.equals(currentCoverFile)) { loadCover(modelCoverFile); currentCoverFile = modelCoverFile; } String modelScreen1File = model.getScreens1File(); - if (!model.getScreens1File().equals(currentScreen1File)) + if (modelScreen1File.isEmpty()) + { + getScreen1ImageLabel().setIcon(getMissingScreenshotImageIcon()); + } + else if (!model.getScreens1File().equals(currentScreen1File)) { loadScreen(modelScreen1File, getScreen1ImageLabel()); currentScreen1File = modelScreen1File; } String modelScreen2File = model.getScreens2File(); - if (!modelScreen2File.equals(currentScreen2File)) + if (modelScreen2File.isEmpty()) + { + getScreen2ImageLabel().setIcon(getMissingScreenshotImageIcon()); + } + else if (!modelScreen2File.equals(currentScreen2File)) { loadScreen(modelScreen2File, getScreen2ImageLabel()); currentScreen2File = modelScreen2File; diff --git a/src/main/java/se/lantz/gui/SystemPanel.java b/src/main/java/se/lantz/gui/SystemPanel.java index f4c169f..028e808 100644 --- a/src/main/java/se/lantz/gui/SystemPanel.java +++ b/src/main/java/se/lantz/gui/SystemPanel.java @@ -122,7 +122,7 @@ public class SystemPanel extends JPanel add(getConfigPanel(), gbc_configPanel); if (!Beans.isDesignTime()) { - model.addPropertyChangeListener((e) -> modelChanged()); + model.addPropertyChangeListener(e -> modelChanged()); } } @@ -181,6 +181,7 @@ public class SystemPanel extends JPanel if (c64Button == null) { c64Button = new JRadioButton("Commodore 64"); + c64Button.setSelected(true); c64Button.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) @@ -192,7 +193,6 @@ public class SystemPanel extends JPanel } } }); - c64Button.setSelected(true); c64Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) diff --git a/src/main/java/se/lantz/model/AbstractModel.java b/src/main/java/se/lantz/model/AbstractModel.java index 901e55f..e1ae41d 100644 --- a/src/main/java/se/lantz/model/AbstractModel.java +++ b/src/main/java/se/lantz/model/AbstractModel.java @@ -47,10 +47,19 @@ public abstract class AbstractModel dataChanged = false; propertyChangeSupport.firePropertyChange("notify", null, ""); } + + public void resetDataChangedAfterInit() + { + dataChanged = false; + } public void disableChangeNotification(boolean disable) { this.disable = disable; - + } + + public boolean isDisableChangeNotifcation() + { + return disable; } } diff --git a/src/main/java/se/lantz/model/MainViewModel.java b/src/main/java/se/lantz/model/MainViewModel.java index e20d08c..36a11a8 100644 --- a/src/main/java/se/lantz/model/MainViewModel.java +++ b/src/main/java/se/lantz/model/MainViewModel.java @@ -57,6 +57,8 @@ public class MainViewModel extends AbstractModel joy2Model.setPrimaryChangeListener(e -> joy1Model .setPrimaryWithoutListenerNotification(!Boolean.valueOf(e.getActionCommand()))); + + resetDataChangedAfterInit(); } private void setupGameViews() @@ -74,9 +76,10 @@ public class MainViewModel extends AbstractModel public void reloadGameViews() { + this.disableChangeNotification(true); gameViewModel.removeAllElements(); setupGameViews(); - reloadCurrentGameView(); + this.disableChangeNotification(false); } public ListModel getGameListModel() @@ -175,6 +178,7 @@ public class MainViewModel extends AbstractModel public void reloadCurrentGameView() { setSelectedGameView(getSelectedGameView()); + resetDataChanged(); } public void setSelectedGameView(GameView gameView) @@ -182,6 +186,7 @@ public class MainViewModel extends AbstractModel this.selectedGameView = gameView; if (gameView != null) { + this.disableChangeNotification(true); logger.debug("Fetching games for view {}...", gameView); gameListModel.clear(); List gamesList = dbConnector.fetchGamesByView(gameView); @@ -194,6 +199,7 @@ public class MainViewModel extends AbstractModel { this.allGamesCount = gamesList.size(); } + this.disableChangeNotification(false); logger.debug("...done."); } } @@ -242,6 +248,24 @@ public class MainViewModel extends AbstractModel return infoModel.isDataChanged() || joy1Model.isDataChanged() || joy2Model.isDataChanged() || systemModel.isDataChanged(); } + + @Override + public void resetDataChanged() + { + infoModel.resetDataChanged(); + joy1Model.resetDataChanged(); + joy2Model.resetDataChanged(); + systemModel.resetDataChanged(); + } + + @Override + public void resetDataChangedAfterInit() + { + infoModel.resetDataChangedAfterInit(); + joy1Model.resetDataChangedAfterInit(); + joy2Model.resetDataChangedAfterInit(); + systemModel.resetDataChangedAfterInit(); + } /** * @@ -376,6 +400,7 @@ public class MainViewModel extends AbstractModel if (gameListModel.get(gameListModel.getSize() - 1).getGameId().isEmpty()) { gameListModel.remove(gameListModel.getSize() - 1); + resetDataChanged(); } } } diff --git a/src/main/java/se/lantz/util/FileManager.java b/src/main/java/se/lantz/util/FileManager.java index d0dbdc7..4a40edb 100644 --- a/src/main/java/se/lantz/util/FileManager.java +++ b/src/main/java/se/lantz/util/FileManager.java @@ -10,6 +10,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -17,6 +18,8 @@ import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.Properties; +import java.util.jar.Attributes; +import java.util.jar.Manifest; import java.util.stream.Collectors; import java.util.zip.GZIPOutputStream; @@ -26,6 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import se.lantz.db.DbConnector; +import se.lantz.gui.MainWindow; import se.lantz.model.InfoModel; import se.lantz.model.data.GameDetails; @@ -523,6 +527,17 @@ public class FileManager { List returnList = new ArrayList<>(); File backupFolder = new File(BACKUP); + if (!backupFolder.exists()) + { + try + { + Files.createDirectory(backupFolder.toPath()); + } + catch (IOException e) + { + ExceptionHandler.handleException(e, "Could not create backup folder"); + } + } for (File file : backupFolder.listFiles()) { if (file.isDirectory()) @@ -532,4 +547,29 @@ public class FileManager } return returnList; } + + public static String getPcuVersionFromManifest() + { + String returnValue = ""; + Class clazz = FileManager.class; + String className = clazz.getSimpleName() + ".class"; + String classPath = clazz.getResource(className).toString(); + if (classPath.startsWith("jar")) + { + String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; + Manifest manifest; + try + { + manifest = new Manifest(new URL(manifestPath).openStream()); + Attributes attr = manifest.getMainAttributes(); + returnValue = attr.getValue("BuildVersion"); + } + catch (IOException e1) + { + // TODO Auto-generated catch block + ExceptionHandler.handleException(e1, "Could not read manifest"); + } + } + return returnValue; + } }