|
1 | | -package GUI;public class ViewUserInterface { |
| 1 | +package GUI; |
| 2 | + |
| 3 | +import History.ControllerBackup; |
| 4 | +import Replace.ControllerReplace; |
| 5 | +import Search.ControllerSearch; |
| 6 | + |
| 7 | +import javax.swing.*; |
| 8 | +import java.awt.*; |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.net.URI; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.concurrent.ExecutorService; |
| 16 | +import java.util.concurrent.Executors; |
| 17 | + |
| 18 | +public class ViewUserInterface extends JFrame { |
| 19 | + |
| 20 | + private JPanel panelMain; |
| 21 | + private JLabel labelRequiredAction; |
| 22 | + private JButton buttonDLSSWebsite; |
| 23 | + private JButton buttonAddGameDirectory; |
| 24 | + private JLabel labelRestoreBackup; |
| 25 | + private JButton buttonRestoreBackup; |
| 26 | + private JLabel labelGitHubLink; |
| 27 | + private JButton buttonReplaceDLSS; |
| 28 | + private JLabel labelProgress; |
| 29 | + private JLabel labelProgressUpdate; |
| 30 | + |
| 31 | + private ExecutorService controllerService; |
| 32 | + public static boolean isControllerRunning; |
| 33 | + |
| 34 | + public ViewUserInterface() { |
| 35 | + |
| 36 | + SwingUtilities.invokeLater(this::initUserInterface); |
| 37 | + SwingUtilities.invokeLater(this::initNonUserInterface); |
| 38 | + |
| 39 | + buttonDLSSWebsite.addActionListener(e -> { |
| 40 | + |
| 41 | + try { |
| 42 | + Desktop desktop = Desktop.getDesktop(); |
| 43 | + desktop.browse(URI.create("https://www.techpowerup.com/download/nvidia-dlss-dll/")); |
| 44 | + } catch (IOException ex) { throw new RuntimeException(ex); } |
| 45 | + |
| 46 | + }); |
| 47 | + |
| 48 | + buttonAddGameDirectory.addActionListener(e -> { |
| 49 | + |
| 50 | + if (isControllerRunning) return; |
| 51 | + |
| 52 | + File gameRootDir = null; |
| 53 | + |
| 54 | + JFileChooser dirChooser = new JFileChooser(); |
| 55 | + dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| 56 | + dirChooser.setDialogTitle("Select Game Root Directory"); |
| 57 | + |
| 58 | + int dirChoice = dirChooser.showDialog(this, "Select"); |
| 59 | + |
| 60 | + if (dirChoice == JFileChooser.APPROVE_OPTION) gameRootDir = dirChooser.getSelectedFile(); |
| 61 | + |
| 62 | + if (gameRootDir != null) responseButtonAddGameDirectory(gameRootDir); |
| 63 | + |
| 64 | + }); |
| 65 | + |
| 66 | + buttonReplaceDLSS.addActionListener(e -> { |
| 67 | + |
| 68 | + if (isControllerRunning) return; |
| 69 | + |
| 70 | + File[] DLSSDir = new File("DLSS").listFiles(); |
| 71 | + |
| 72 | + if (DLSSDir == null || DLSSDir.length == 0) { |
| 73 | + |
| 74 | + showMessage("DLSS directory is empty!"); |
| 75 | + return; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + ControllerBackup controllerBackup = new ControllerBackup(this); |
| 80 | + ControllerReplace controllerReplace = new ControllerReplace(this, false); |
| 81 | + this.controllerService.submit(controllerBackup); |
| 82 | + this.controllerService.submit(controllerReplace); |
| 83 | + |
| 84 | + }); |
| 85 | + |
| 86 | + buttonRestoreBackup.addActionListener(e -> { |
| 87 | + |
| 88 | + if (isControllerRunning) return; |
| 89 | + |
| 90 | + File[] backupDir = new File("Backup").listFiles(); |
| 91 | + |
| 92 | + if (backupDir == null || backupDir.length == 0) { |
| 93 | + |
| 94 | + showMessage("Backup directory is empty!"); |
| 95 | + return; |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + ControllerReplace controllerReplace = new ControllerReplace(this, true); |
| 100 | + this.controllerService.submit(controllerReplace); |
| 101 | + |
| 102 | + }); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + private void responseButtonAddGameDirectory(File gameRootDir) { |
| 107 | + |
| 108 | + ControllerSearch controllerSearch = new ControllerSearch(this, gameRootDir); |
| 109 | + this.controllerService.submit(controllerSearch); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + private void showMessage(String text) { |
| 114 | + |
| 115 | + SwingUtilities.invokeLater(() -> |
| 116 | + JOptionPane.showMessageDialog(this, text, "Warning", JOptionPane.WARNING_MESSAGE)); |
| 117 | + } |
| 118 | + |
| 119 | + public void updateLabelProgressUpdate(String progressText) { |
| 120 | + |
| 121 | + SwingUtilities.invokeLater(() -> labelProgressUpdate.setText(progressText)); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + private void initUserInterface() { |
| 126 | + |
| 127 | + this.labelRequiredAction.setText("Required User Action"); |
| 128 | + this.buttonDLSSWebsite.setText("Download DLSS"); |
| 129 | + this.buttonAddGameDirectory.setText("Add Game Directory"); |
| 130 | + |
| 131 | + this.labelRestoreBackup.setText("Restore"); |
| 132 | + this.buttonRestoreBackup.setText("Restore Backup"); |
| 133 | + |
| 134 | + this.labelGitHubLink.setText("https://github.com/YorkshireDev"); |
| 135 | + this.buttonReplaceDLSS.setText("Replace DLSS"); |
| 136 | + this.labelProgress.setText("Progress"); |
| 137 | + this.labelProgressUpdate.setText("N/A"); |
| 138 | + |
| 139 | + this.setContentPane(panelMain); |
| 140 | + this.setTitle("Supersede"); |
| 141 | + this.setPreferredSize(new Dimension(480, 240)); |
| 142 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 143 | + this.setLocationRelativeTo(null); |
| 144 | + this.pack(); |
| 145 | + this.setVisible(true); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + private void initNonUserInterface() { |
| 150 | + |
| 151 | + Path dlssFolderPath = Path.of("DLSS"); |
| 152 | + Path dlssValidGameFilePath = Path.of("Game_List.TXT"); |
| 153 | + |
| 154 | + try { |
| 155 | + |
| 156 | + if (! Files.exists(dlssFolderPath)) Files.createDirectory(dlssFolderPath); |
| 157 | + if (! Files.exists(dlssValidGameFilePath)) Files.createFile(dlssValidGameFilePath); |
| 158 | + |
| 159 | + } catch (IOException e) { throw new RuntimeException(e); } |
| 160 | + |
| 161 | + this.controllerService = Executors.newSingleThreadExecutor(); |
| 162 | + isControllerRunning = false; |
| 163 | + |
| 164 | + } |
| 165 | + |
2 | 166 | } |
0 commit comments