From 6f159a5c483d4f9c1bc08bec89ca142e6b8cf183 Mon Sep 17 00:00:00 2001
From: Dim145 <50747004+Dim145@users.noreply.github.com>
Date: Thu, 8 Oct 2020 22:53:54 +0200
Subject: [PATCH] =?UTF-8?q?pref=C3=A9rences=20sauvegard=C3=A9es=20dans=20u?=
=?UTF-8?q?n=20fichier=20dans=20le=20dossier=20user.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/renameFiles/Controleur.java | 116 +++++++++++++++++++++++++++++++
src/renameFiles/ihm/IHMGUI.java | 17 +++--
src/renameFiles/ihm/MenuBar.java | 44 ++++++------
3 files changed, 153 insertions(+), 24 deletions(-)
diff --git a/src/renameFiles/Controleur.java b/src/renameFiles/Controleur.java
index 99e722c..2785605 100644
--- a/src/renameFiles/Controleur.java
+++ b/src/renameFiles/Controleur.java
@@ -5,16 +5,132 @@
import javax.swing.*;
import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Scanner;
public class Controleur
{
private final Metier metier;
private final IHMGUI ihm;
+ private File prefFile;
+
public Controleur()
{
this.metier = new Metier(this);
this.ihm = new IHMGUI(this);
+
+ File appDirectorie = new File(System.getProperty("user.home") + "/.FileRenamer");
+
+ if( !appDirectorie.exists() )
+ if( appDirectorie.mkdir() ) this.ihm.printInConsole("File \".FileRenamer\" created.");
+ else this.ihm.printInConsole("Error, cannot create the app file in user directory");
+
+ this.prefFile = new File(appDirectorie.getAbsolutePath() + "/.preferences.conf");
+
+ if( this.prefFile.exists())
+ {
+ try(Scanner scanner = new Scanner(this.prefFile))
+ {
+ int cpt = 0;
+ while( scanner.hasNext() )
+ {
+ String line = scanner.nextLine();
+ String[] tab = line.split("=");
+
+ if( tab.length < 2 ) continue;
+
+ switch (cpt)
+ {
+ case 0 ->
+ {
+ try
+ {
+ if(Boolean.parseBoolean(tab[1]))
+ this.ihm.changeBlockParam();
+ }
+ catch (Exception ignored) { }
+ }
+
+ case 1 ->
+ {
+ try
+ {
+ boolean darkTheme = Boolean.parseBoolean(tab[1]);
+
+ if( darkTheme ) this.ihm.changeTheme();
+ }
+ catch ( Exception ignored) { }
+ }
+ }
+
+ cpt++;
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ else
+ {
+ try
+ {
+ if( this.prefFile.createNewFile() )
+ {
+ this.ihm.printInConsole("File preferences created");
+ }
+ else
+ {
+ this.ihm.printInConsole("Error, cannot create the preferences file");
+ }
+
+ if( this.prefFile.exists() )
+ {
+ this.saveBooleanPreferences("ignoreRenameProtection", false, true);
+ this.saveBooleanPreferences("darkMode", false, false);
+ }
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ this.ihm.setVisible(true);
+ }
+
+ private String readAllFile( File file )
+ {
+ StringBuilder res = new StringBuilder();
+
+ try(Scanner scanner = new Scanner(file))
+ {
+ scanner.reset();
+
+ while( scanner.hasNext() )
+ res.append(scanner.nextLine()).append("\n");
+ }
+ catch (Exception e) { e.printStackTrace(); }
+
+ return res.toString();
+ }
+
+ public void saveBooleanPreferences( String name, boolean value, boolean clearFile )
+ {
+ String fileValue = "";
+ if( !clearFile ) fileValue = this.readAllFile(this.prefFile);
+
+ try(FileWriter writer = new FileWriter(this.prefFile))
+ {
+ writer.append(fileValue).append(name).append("=").append(String.valueOf(value)).append("\n");
+ }
+ catch (Exception e)
+ {
+ this.ihm.printInConsole("Error in file writting");
+ e.printStackTrace();
+ }
}
public void renameFile( String path, String patern )
diff --git a/src/renameFiles/ihm/IHMGUI.java b/src/renameFiles/ihm/IHMGUI.java
index 9443331..fd029d9 100644
--- a/src/renameFiles/ihm/IHMGUI.java
+++ b/src/renameFiles/ihm/IHMGUI.java
@@ -25,7 +25,7 @@ public class IHMGUI extends JFrame
private final Picker picker;
- private ArrayList allJPanel;
+ private final ArrayList allJPanel;
public IHMGUI(Controleur ctrl)
{
@@ -153,7 +153,6 @@ public void mouseExited(MouseEvent e)
this.setSize(this.getWidth() + 200, this.getHeight());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
- this.setVisible(true);
this.extensions.grabFocus();
}
@@ -244,8 +243,18 @@ private static Color couleurPlusClair(Color baseColor, boolean plusFoncer)
return new Color(baseColor.getRed() + ECART_COLOR, baseColor.getGreen() + ECART_COLOR, baseColor.getBlue() + ECART_COLOR);
}
- public Color getCurrentColorTheme()
+ public void changeTheme()
{
- return ((MenuBar) this.getJMenuBar()).getCurrentColor();
+ ((MenuBar) this.getJMenuBar()).changeTheme();
+ }
+
+ public void changeBlockParam()
+ {
+ ((MenuBar) this.getJMenuBar()).changeBlockParam();
+ }
+
+ public void saveBooleanPreferences( String name, boolean value, boolean clearFile )
+ {
+ this.ctrl.saveBooleanPreferences(name, value, clearFile);
}
}
diff --git a/src/renameFiles/ihm/MenuBar.java b/src/renameFiles/ihm/MenuBar.java
index bc3052f..833686a 100644
--- a/src/renameFiles/ihm/MenuBar.java
+++ b/src/renameFiles/ihm/MenuBar.java
@@ -8,14 +8,15 @@ public class MenuBar extends JMenuBar
private final IHMGUI ihm;
private final JMenuItem itemBlockIfNotMatchNumber;
private final JMenuItem aide;
+ private final JMenuItem darkTheme;
- private boolean darkTheme;
+ private boolean bDarkTheme;
private Color currentColor;
public MenuBar( IHMGUI ihm )
{
this.ihm = ihm;
- this.darkTheme = false;
+ this.bDarkTheme = false;
JMenu optionMenu = new JMenu("option");
JMenu aideMenu = new JMenu("aide");
@@ -25,7 +26,7 @@ public MenuBar( IHMGUI ihm )
this.aide = new JMenuItem("aide");
JMenuItem aPropos = new JMenuItem("a propos");
- JMenuItem darkTheme = new JMenuItem("X dark thème");
+ this.darkTheme = new JMenuItem("X dark thème");
optionMenu.add(itemBlockIfNotMatchNumber);
optionMenu.add(darkTheme);
@@ -36,14 +37,7 @@ public MenuBar( IHMGUI ihm )
this.itemBlockIfNotMatchNumber.addActionListener(e -> changeBlockParam());
aPropos.addActionListener(e -> new APropos(this.currentColor));
- darkTheme.addActionListener(e ->
- {
- this.changeTheme();
-
- char first = darkTheme.getText().charAt(0);
-
- darkTheme.setText( (first == '✓' ? 'X' : '✓') + darkTheme.getText().substring(1) );
- });
+ darkTheme.addActionListener(e -> this.changeTheme());
this.add(optionMenu);
this.add(aideMenu);
@@ -53,11 +47,13 @@ public MenuBar( IHMGUI ihm )
this.currentColor = Color.WHITE;
}
- private void changeTheme()
+ public void changeTheme()
{
- this.darkTheme = !this.darkTheme;
+ this.bDarkTheme = !this.bDarkTheme;
- Color baseColor = this.darkTheme ? new Color(50, 50, 50) : Color.WHITE;
+ this.darkTheme.setText( (this.bDarkTheme ? '✓' : 'X' ) + darkTheme.getText().substring(1) );
+
+ Color baseColor = this.bDarkTheme ? new Color(50, 50, 50) : Color.WHITE;
this.currentColor = baseColor;
@@ -66,12 +62,14 @@ private void changeTheme()
for (int i = 0; i < this.getComponentCount(); i++)
this.setRecursiveColor(baseColor, this.getComponent(i));
+
+ this.reWritePrefParam();
}
private void setRecursiveColor( Color color, Component component)
{
component.setBackground(color);
- component.setForeground(this.darkTheme ? Color.WHITE : Color.BLACK);
+ component.setForeground(this.bDarkTheme ? Color.WHITE : Color.BLACK);
if( component instanceof Container)
{
@@ -80,10 +78,12 @@ private void setRecursiveColor( Color color, Component component)
}
}
- private void changeBlockParam()
+ public void changeBlockParam()
{
char first = this.itemBlockIfNotMatchNumber.getText().charAt(0);
+ System.out.println("Param block changed");
+
if( '✓' == first )
{
this.itemBlockIfNotMatchNumber.setBackground(Color.RED);
@@ -96,6 +96,14 @@ else if( 'X' == first )
this.itemBlockIfNotMatchNumber.setText('✓' + this.itemBlockIfNotMatchNumber.getText().substring(1));
this.ihm.setBlockIfNotMathPatern(true);
}
+
+ this.reWritePrefParam();
+ }
+
+ private void reWritePrefParam()
+ {
+ this.ihm.saveBooleanPreferences("ignoreRenameProtection", this.itemBlockIfNotMatchNumber.getText().charAt(0) == 'X', true);
+ this.ihm.saveBooleanPreferences("darkMode", this.bDarkTheme, false);
}
@Override
@@ -107,8 +115,4 @@ protected void paintComponent(Graphics g)
g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
}
- public Color getCurrentColor()
- {
- return this.currentColor;
- }
}