implemented fonts and documented settings class.

This commit is contained in:
Josh Ashton
2024-03-21 14:12:14 -06:00
parent a053c98a7b
commit ac1464a9fb
6 changed files with 228 additions and 61 deletions
+2
View File
@@ -2,6 +2,8 @@ import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.UIManager; import javax.swing.UIManager;
import gui.Settings;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Dimension; import java.awt.Dimension;
+4 -1
View File
@@ -8,6 +8,7 @@ import javax.swing.JButton;
import gui.ComboBox; import gui.ComboBox;
import gui.FileChooser; import gui.FileChooser;
import gui.Settings;
/** /**
* The Nav class represents the Navigation bar at the top of the JFrame window. * The Nav class represents the Navigation bar at the top of the JFrame window.
* *
@@ -87,7 +88,9 @@ public class Nav extends JPanel {
*/ */
private void createGUI() { private void createGUI() {
JLabel elapsedTime = new JLabel("Elapsed Time: 00:00:00"); JLabel elapsedTime = new JLabel("Elapsed Time: 00:00:00");
elapsedTime.setFont(s.getFont()); // TODO: Make custom Label class.
JButton solve = new JButton("Solve"); JButton solve = new JButton("Solve");
solve.setFont(s.getFont()); // TODO: Make custom Button class.
add(createFileOptions()); add(createFileOptions());
add(elapsedTime); add(elapsedTime);
@@ -100,7 +103,7 @@ public class Nav extends JPanel {
* @return ComboBox<String> * @return ComboBox<String>
*/ */
private ComboBox<String> createFileOptions() { private ComboBox<String> createFileOptions() {
ComboBox<String> fileOptions = new ComboBox<>(); ComboBox<String> fileOptions = new ComboBox<>(s);
fileOptions.addItem("New"); fileOptions.addItem("New");
fileOptions.addItem("Open"); fileOptions.addItem("Open");
fileOptions.addItem("Save"); fileOptions.addItem("Save");
-56
View File
@@ -1,56 +0,0 @@
import java.util.Scanner;
import java.io.File;
/**
* A class solely to store user settings relating to default behaviors,
* difficulty, filepaths, and more.
*
* These user settings are stored in a {TBD} file located at {TBD}.
*/
public class Settings {
private int newFileProperties;
private String defaultDirectory;
/**
* Create a new Settings object, initializing the default settings
* or reading in the settings from a file if it exists.
*/
public Settings() {
// TODO: Read in a file from the master default directory and populate
// settings, effectively toggles. If the file does not exist,
// populate it with the following values (possibly in .json).
newFileProperties = 0;
defaultDirectory = "C:\\Users\\jashton\\AppData\\Local\\Sudoku"; // TODO: This is a Windows default. Need OS detection.
}
/**
* New File Properties is a configurable setting, where the following
* values are allowable:
* 0 - Load the last opened .sdku file, default behavior
* 1 - Create a new, blank .sdku file
* 2 - Create a new, random .sdku file
*
* @return int
*/
public int getNewFileProperties() {
return newFileProperties;
}
/**
* The Default Directory is a configurable setting, where the user can
* specify a default directory to store .sdku files. This is useful for
* eliminating the need for user input for file operations.
*
* By default, the value will be "~/.config/sudoku/" on MacOS and Linux.
* On Windows, the value will be "~/AppData/Local/sudoku/".
*
* NOTE: This does not change the location that the settings file may
* be located in. This must be located at {TBD}.
*
* @return String
*/
public String getDefaultDirectory() {
return defaultDirectory;
}
}
+7 -1
View File
@@ -6,12 +6,18 @@ import javax.swing.JComboBox;
* A custom JComboBox with the appropriate styling for the Sudoku app. * A custom JComboBox with the appropriate styling for the Sudoku app.
*/ */
public class ComboBox<E> extends JComboBox<E> { public class ComboBox<E> extends JComboBox<E> {
private Settings s;
/** /**
* Create a new ComboBox with the default styling and without initial items. * Create a new ComboBox with the default styling and without initial items.
*/ */
public ComboBox() { public ComboBox() {
super(); super();
}
public ComboBox(Settings s) {
super();
this.s = s;
style(); style();
} }
@@ -20,6 +26,6 @@ public class ComboBox<E> extends JComboBox<E> {
*/ */
private void style() { private void style() {
// TODO: Style the ComboBox. // TODO: Style the ComboBox.
System.out.println("TODO: Style the ComboBox."); setFont(s.getFont());
} }
} }
+23 -3
View File
@@ -9,10 +9,11 @@ import javax.swing.JFileChooser;
* Additionally, this FileChooser only allows .sdku files to be selected. * Additionally, this FileChooser only allows .sdku files to be selected.
*/ */
public class FileChooser extends JFileChooser { public class FileChooser extends JFileChooser {
private Settings s;
/** /**
* Create a new FileChooser with the default directory set to the user's * Create a new FileChooser with the default directory set to the user's
* Sudoku directory. * Sudoku directory and no special styling.
* *
* @param defaultDirectory * @param defaultDirectory
*/ */
@@ -21,6 +22,26 @@ public class FileChooser extends JFileChooser {
for (File f: defaultDirectory.listFiles()) { for (File f: defaultDirectory.listFiles()) {
ensureFileIsVisible(f); ensureFileIsVisible(f);
} }
style();
setupSettings();
}
/**
* Create a new FileChooser with the default directory set to the user's
* Sudoku directory.
*
* @param defaultDirectory
* @param s
*/
public FileChooser(File defaultDirectory, Settings s) {
super(defaultDirectory);
this.s = s;
for (File f: defaultDirectory.listFiles()) {
ensureFileIsVisible(f);
}
style(); style();
setupSettings(); setupSettings();
} }
@@ -30,7 +51,7 @@ public class FileChooser extends JFileChooser {
*/ */
private void style() { private void style() {
// TODO: Style the FileChooser. // TODO: Style the FileChooser.
System.out.println("TODO: Style the FileChooser."); setFont(s.getFont());
} }
/** /**
@@ -39,7 +60,6 @@ public class FileChooser extends JFileChooser {
private void setupSettings() { private void setupSettings() {
setFileHidingEnabled(false); setFileHidingEnabled(false);
setFileSelectionMode(JFileChooser.FILES_ONLY); setFileSelectionMode(JFileChooser.FILES_ONLY);
rescanCurrentDirectory();
setFileFilter(new Filter()); setFileFilter(new Filter());
} }
+192
View File
@@ -0,0 +1,192 @@
package gui;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.awt.GraphicsEnvironment;
import java.awt.Font;
import java.awt.FontFormatException;
/**
* A class solely to store user settings relating to default behaviors,
* difficulty, filepaths, and more.
*
* These user settings are stored in a {TBD} file located at {TBD}.
*/
public class Settings {
private final String os;
private int newFileProperties;
private String defaultDirectory;
private Font font;
/**
* Create a new Settings object, initializing the default settings
* or reading in the settings from settings file if it exists.
*
* While the defaultDirectory can be modified, it will not change where
* the settings file is located. The settings file is always located in
* either "~/.config/sudoku/" or "%APPDATA%/Local/Sudoku/", depending
* on the user's operating system.
*/
public Settings() {
os = System.getProperty("os.name").toLowerCase();
if(os.startsWith("windows"))
defaultDirectory = System.getProperty("user.home") + "\\AppData\\Local\\Sudoku\\";
else
defaultDirectory = System.getProperty("user.home") + "/.config/sudoku/";
File dir = new File(defaultDirectory);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
defaultSettings();
}
readSettings();
}
/**
* New File Properties is a configurable setting, where the following
* values are allowable:
* 0 - Load the last opened .sdku file, default behavior
* 1 - Create a new, blank .sdku file
* 2 - Create a new, random .sdku file
*
* @return int
*/
public int getNewFileProperties() {
return newFileProperties;
}
/**
* Set the new file properties and write them to the settings file.
*
* @param newFileProperties
*/
public void setNewFileProperties(int newFileProperties) {
this.newFileProperties = newFileProperties;
updateSettingsFile();
}
/**
* The Default Directory is a configurable setting, where the user can
* specify a default directory to store .sdku files. This is useful for
* eliminating the need for user input for file operations.
*
* NOTE: This does not change the location that the settings file may
* be located in. At this time, the settings file is always located in
* either "~/.config/sudoku/" or "%APPDATA%/Local/Sudoku/", depending
* on the user's operating system.
*
* @return String
*/
public String getDefaultDirectory() {
return defaultDirectory;
}
/**
* Set the default directory and write it to the settings file.
*
* @param defaultDirectory
*/
public void setDefaultDirectory(String defaultDirectory) {
this.defaultDirectory = defaultDirectory;
updateSettingsFile();
}
/**
* The Font is a configurable setting, where the user can specify a
* font to be used throughout the application.
*
* Fonts are loaded from the system directory. In Windows, the font
* is loaded from C:\Windows\Fonts\. In MacOS, the font is loaded
* from /Library/Fonts/. In Linux, the font is loaded from
* /usr/share/fonts/.
*
* @return Font
*/
public Font getFont() {
return font;
}
/**
* Set the font and write it to the settings file.
*
* @param font
*/
public void setFont(Font font) {
this.font = font;
updateSettingsFile();
}
/**
* Open and write the settings to the settings file.
*
* This method is called whenever a setting is updated.
*/
private void updateSettingsFile() {
// TODO: Write the settings to the settings file.
}
/**
* Set the default settings for the application and write them to the
* settings file.
*/
private void defaultSettings() {
newFileProperties = 0;
// Load the font from the system directory.
if(loadFont("HackNerdFont-Regular") == 0)
font = new Font("Hack Nerd Font", Font.PLAIN, 12);
// If the font does not exist, use Arial as the default font.
else
font = new Font("Arial", Font.PLAIN, 12);
updateSettingsFile();
}
/**
* Read the settings from the settings file.
*
* If the settings file does not exist, the default settings will be used
* to populate the Settings file.
*/
private void readSettings() {
File settingsFile = new File(defaultDirectory + "/settings");
if (!settingsFile.exists()) {
defaultSettings();
return;
}
// TODO: Read the settings from the settings file.
}
/**
* Load the font from the resources folder in the user's system directory.
*
* @return int 0 if successful, 1 if the file does not exist.
*/
private int loadFont(String fontName) {
// Load the font from the system directory.
String fontFilepath;
if(os.startsWith("windows"))
fontFilepath = System.getProperty("user.home") + "\\AppData\\Local\\Microsoft\\Windows\\Fonts\\" + fontName + ".ttf";
else if(os.startsWith("mac"))
fontFilepath = "/Library/Fonts/" + fontName + ".ttf";
else
fontFilepath = "/usr/share/fonts/" + fontName + ".ttf";
// Register the font with the GraphicsEnvironment.
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontFilepath)));
return 0;
} catch(IOException | FontFormatException e) {
System.out.println("Filepath of font not found: " + fontFilepath + " does not exist.");
return 1;
}
}
}