Skip to content

Commit

Permalink
added config file for font
Browse files Browse the repository at this point in the history
  • Loading branch information
jcksndfrd committed Aug 6, 2021
1 parent ccea4af commit 726d3de
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.settings/
target/
*.class
config.yaml
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Student ID: 19023939
### *Student 2*
```css
Name: Jack Sandford;
Student ID: ???
Student ID: 19025303
```
---

Expand All @@ -30,4 +30,6 @@ Fletcher:
```

Jack:
```css
```
---
44 changes: 44 additions & 0 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nz.ac.massey.cs</groupId>
<artifactId>nz.ac.massey.cs.flackpad</artifactId>
<version>0.3.4-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>nz.ac.massey.cs.flackpad.RunApp</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.target>16</maven.compiler.target>
<javaVersion>16</javaVersion>
<maven.compiler.source>16</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>nz.ac.massey.cs</groupId>
<artifactId>nz.ac.massey.cs.flackpad</artifactId>
<version>0.3.4-SNAPSHOT</version>
<version>0.3.5</version>
<properties>
<javaVersion>16</javaVersion>
<maven.compiler.source>16</maven.compiler.source>
Expand All @@ -22,6 +22,11 @@
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.29</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/nz/ac/massey/cs/flackpad/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package nz.ac.massey.cs.flackpad;

import java.awt.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

class Config {

private Window window;
private Map<String, Object> defaults = new LinkedHashMap<String, Object>();
private Map<String, Object> config = new LinkedHashMap<String, Object>();

Config(Window window) {
this.window = window;

loadDefaults();
loadConfigFile();
}

private void loadDefaults() {
defaults.put("fontFamily", "Consolas");
defaults.put("fontStyle", Font.PLAIN);
defaults.put("fontSize", 12);
}

void loadConfigFile() {
// Read config file
Yaml yaml = new Yaml();

try {
InputStream inputStream = new FileInputStream("config.yaml");
config = yaml.load(inputStream);
inputStream.close();
} catch (FileNotFoundException e) {
Dialogs.error("Configuration file does not exist, using defaults", window);
} catch (IOException e) {
Dialogs.error("Something wen't wrong when loading configuration file", window);
}

// Set missing values with defaults
for (String key:defaults.keySet()) {
if (!config.containsKey(key)) config.put(key, defaults.get(key));
}

// Save config
saveConfigFile();
}

void saveConfigFile() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Yaml yaml = new Yaml(options);

try {
PrintWriter writer = new PrintWriter("config.yaml");
yaml.dump(config, writer);
writer.flush();
writer.close();
} catch (IOException e) {
Dialogs.error("Something wen't wrong when saving configuration", window);
}
}

Font getFont() {
try {
return new Font((String) config.get("fontFamily"), (int) config.get("fontStyle"), (int) config.get("fontSize"));
} catch (Exception e) {
Dialogs.error("Something wen't wrong when loading the configured font, using defaults", window);
}
setFont(new Font((String) defaults.get("fontFamily"), (int) defaults.get("fontStyle"), (int) defaults.get("fontSize")));
return getFont();
}

void setFont(Font font) {
config.put("fontFamily", font.getFamily());
config.put("fontStyle", font.getStyle());
config.put("fontSize", font.getSize());
}

}
6 changes: 6 additions & 0 deletions src/main/java/nz/ac/massey/cs/flackpad/TextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ void addTimeAndDate() {
}
}

void setFontWithZoom(Font font) {
fontSize = font.getSize();
setFont(font);
zoom();
}

//Zoom methods work by changing font size
void zoomIn() {
if (fontPercentage < 1000) {
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/nz/ac/massey/cs/flackpad/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Window extends JFrame {
private String name = "FlackPad";
private JFrame frame;
private WindowListener winListener;
private Config config;

private MenuBar menuBar;
private TextArea textArea;
Expand All @@ -32,14 +33,11 @@ class Window extends JFrame {
frame = new JFrame(fileName + " - " + name);

// Add icon
// ImageIcon icon = new ImageIcon("./flackpad.png");
// System.out.println(icon.getImageLoadStatus());
// frame.setIconImage(icon.getImage());
frame.setIconImages(List.of(
new ImageIcon("16x16.png").getImage(),
new ImageIcon("32x32.png").getImage(),
new ImageIcon("64x64.png").getImage(),
new ImageIcon("128x128.png").getImage()));
new ImageIcon("icons/16x16.png").getImage(),
new ImageIcon("icons/32x32.png").getImage(),
new ImageIcon("icons/64x64.png").getImage(),
new ImageIcon("icons/128x128.png").getImage()));

// Add window listener
winListener = new WinListener(this);
Expand All @@ -61,6 +59,12 @@ class Window extends JFrame {
frame.setSize(1000, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

// Get config
config = new Config(this);

// Set configuration
textArea.setFontWithZoom(config.getFont());
}

void newDoc() {
Expand All @@ -85,6 +89,7 @@ void exit() {
if (saveChoice == JOptionPane.YES_OPTION) saved = FileIO.save(this);

if (saved == FileIO.SAVED) {
config.saveConfigFile();
frame.dispose();
}
}
Expand Down Expand Up @@ -126,7 +131,6 @@ void setFile(File file) {
this.file = file;
fileName = file == null ? "Untitled" : file.getName();
fileMIME = FileIO.getFileMIME(file);
// System.out.println(fileMIME);
}

String getFileName() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fontFamily: Consolas
fontStyle: 0
fontSize: 12
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit 726d3de

Please sign in to comment.