Skip to content

Instantly share code, notes, and snippets.

@Skyost
Last active July 9, 2017 17:15
Show Gist options
  • Save Skyost/92a01a9cf67d344cb7bc3875a13ce4a4 to your computer and use it in GitHub Desktop.
Save Skyost/92a01a9cf67d344cb7bc3875a13ce4a4 to your computer and use it in GitHub Desktop.
Petit programme sympa pour Neiox.
public class Neiox extends JFrame {
private static final long serialVersionUID = 1L;
public static final String TITLE = "%d nombre(s) présent(s)";
public static final int MIN = 0;
public static final int MAX = 9999;
private List<String> data;
public Neiox() throws IOException, URISyntaxException {
data = readFile();
refreshTitle();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridBagLayout());
final JSpinner numberSpinner = new JSpinner(new SpinnerNumberModel(MIN, MIN, MAX, 1));
final JFormattedTextField field = ((NumberEditor)numberSpinner.getEditor()).getTextField();
((NumberFormatter)field.getFormatter()).setAllowsInvalid(false);
final GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 10, 10);
constraints.fill = GridBagConstraints.HORIZONTAL;
this.add(numberSpinner, constraints);
final JButton addButton = new JButton("Ajouter");
addButton.addActionListener(new ActionListener() {
@Override
public final void actionPerformed(final ActionEvent event) {
try {
final String line = String.valueOf((Integer)numberSpinner.getValue());
if(data.contains(line)) {
JOptionPane.showMessageDialog(Neiox.this, "Ce nombre a déjà été ajouté !", "Erreur !", JOptionPane.ERROR_MESSAGE);
return;
}
data.add(line);
saveFile();
numberSpinner.setValue(0);
refreshTitle();
}
catch(final Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(Neiox.this, ex.getClass().getName(), "Erreur !", JOptionPane.ERROR_MESSAGE);
}
}
});
constraints.gridy++;
constraints.ipadx = 200;
this.add(addButton, constraints);
final JButton valuesButton = new JButton("Valeurs...");
valuesButton.addActionListener(new ActionListener() {
@Override
public final void actionPerformed(final ActionEvent event) {
new NumbersDialog().setVisible(true);
}
});
constraints.gridy++;
this.add(valuesButton, constraints);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public static void main(final String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new Neiox().setVisible(true);
}
catch(final Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, ex.getClass().getName(), "Erreur !", JOptionPane.ERROR_MESSAGE);
}
}
public final void refreshTitle() {
this.setTitle(String.format(TITLE, data.size()));
}
public final File getFile() throws IOException, URISyntaxException {
final File parentFolder = new File(Neiox.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile();
final File file = new File(parentFolder, "data");
if(!file.exists() || !file.isFile()) {
file.createNewFile();
}
return file;
}
public List<String> readFile() throws IOException, URISyntaxException {
final List<String> result = new ArrayList<String>();
for(final String line : Files.readAllLines(getFile().toPath(), StandardCharsets.UTF_8)) {
try {
final int nombre = Integer.parseInt(line);
if(nombre < MIN || nombre > MAX) {
throw new NumberFormatException();
}
if(!result.contains(line)) {
result.add(line);
}
}
catch(final NumberFormatException ex) {
JOptionPane.showMessageDialog(null, line + " n'est pas un entier valide !", "Erreur !", JOptionPane.ERROR_MESSAGE);
}
}
return result;
}
public final void saveFile() throws IOException, URISyntaxException {
if(data == null) {
return;
}
Files.write(getFile().toPath(), data, StandardCharsets.UTF_8);
}
public class NumbersDialog extends JDialog {
private static final long serialVersionUID = 1L;
public NumbersDialog() {
this.setTitle("Liste des nombres");
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
final DefaultTableModel model = new DefaultTableModel() {
private static final long serialVersionUID = 1L;
@Override
public final boolean isCellEditable(final int row, final int column) {
return false;
}
};
model.setColumnIdentifiers(new String[]{"Nombre", "Présent ?"});
for(int i = MIN; i <= MAX; i++) {
final String line = String.valueOf(i);
model.addRow(new String[]{line, data.contains(line) ? "Oui" : "Non"});
}
final JTable table = new JTable(model);
table.setAutoCreateRowSorter(true);
this.add(new JScrollPane(table));
this.pack();
this.setLocationRelativeTo(Neiox.this);
this.setModal(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment