1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.util.prefs.Preferences;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
public class MonApplicationPreferences {
private static final String NOM_DE_TON_APPLICATION = "Mon Application MachinChouette"; // le nom de ton application (pour reconnaitre que les préférences sont celles de ton application)
private static final String PREF_EXECUTABLE_PATH = "executablePath"; // un nom de propriété pour stocker la préférence
private String executablePath;
public MonApplicationPreferences() {
Preferences prefs = Preferences.userRoot().node(NOM_DE_TON_APPLICATION);
executablePath = prefs.get(PREF_EXECUTABLE_PATH, null); // lit la valeur actuelle de la préférence
if ( executablePath==null ) { // s'il n'y a pas de chemin dans les préférences on en demande un
executablePath = configExecutablePath();
if ( executablePath!=null ) {
prefs.put(PREF_EXECUTABLE_PATH, executablePath); // sauvegarde la préférence
}
}
}
public void editExecutablePath() {
executablePath = configExecutablePath();
if ( executablePath!=null ) {
Preferences prefs = Preferences.userRoot().node(NOM_DE_TON_APPLICATION);
prefs.put(PREF_EXECUTABLE_PATH, executablePath); // sauvegarde la préférence
}
}
private String configExecutablePath() {
ExecutablePathPanel dialog = new ExecutablePathPanel(executablePath);
if ( JOptionPane.showConfirmDialog(null, dialog, "Sélectionner le chemin de gatodecoder.exe", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION ) {
String path = dialog.getPath();
File file = new File(path);
if ( file!=null ) {
if ( !file.exists() ) {
JOptionPane.showMessageDialog(null, "Fichier inexistant");
return null;
}
}
return path;
}
else {
return null;
}
}
public String getExecutablePath() {
return executablePath;
}
private static class ExecutablePathPanel extends JPanel {
private JTextField textField;
public ExecutablePathPanel(String file) {
super(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(3,3,3,3);
gbc.weightx=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
textField = new JTextField(file==null?"":file);
add(textField, gbc);
gbc.weightx=0;
gbc.gridx=1;
gbc.fill=GridBagConstraints.NONE;
JButton buttonBrowser = new JButton("Parcourir...");
add(buttonBrowser, gbc);
textField.setPreferredSize(new Dimension(300, (int)buttonBrowser.getPreferredSize().getHeight()));
buttonBrowser.addActionListener(e->chooseFile());
}
private void chooseFile() {
JFileChooser chooser = new JFileChooser();
if ( !textField.getText().isEmpty() ) {
chooser.setSelectedFile(new File(textField.getText()));
}
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "Exécutables (.exe)";
}
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".exe");
}
});
if ( chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
File file = chooser.getSelectedFile();
if ( file!=null ) {
textField.setText(file.toString());
}
}
}
public String getPath() {
String path = textField.getText().trim();
if ( path.isEmpty() ) {
return null;
}
return path;
}
}
public static void main(String[] args) {
MonApplicationPreferences prefs = new MonApplicationPreferences();
System.out.println("Executable path : " + prefs.getExecutablePath());
JOptionPane.showMessageDialog(null, "Le chemin de l'exécutable dans les préférences est : " + prefs.getExecutablePath());
}
} |
Partager