Skip to content

Commit

Permalink
preférences sauvegardées dans un fichier dans le dossier user.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dim145 committed Oct 8, 2020
1 parent 608c616 commit 6f159a5
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 24 deletions.
116 changes: 116 additions & 0 deletions src/renameFiles/Controleur.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("<font color=\"red\">Error, cannot create the app file in user directory</font>");

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("<font color=\"red\">Error, cannot create the preferences file</font>");
}

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("<font color=\"red\">Error in file writting");
e.printStackTrace();
}
}

public void renameFile( String path, String patern )
Expand Down
17 changes: 13 additions & 4 deletions src/renameFiles/ihm/IHMGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class IHMGUI extends JFrame

private final Picker picker;

private ArrayList<JPanel> allJPanel;
private final ArrayList<JPanel> allJPanel;

public IHMGUI(Controleur ctrl)
{
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
}
44 changes: 24 additions & 20 deletions src/renameFiles/ihm/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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)
{
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -107,8 +115,4 @@ protected void paintComponent(Graphics g)
g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
}

public Color getCurrentColor()
{
return this.currentColor;
}
}

0 comments on commit 6f159a5

Please sign in to comment.