Skip to content

Commit 11baa07

Browse files
Merge pull request #75 from EatSleepProgramRepeat/74-make-settings-fields-work
make settings fields work and add a bunch to ConfigManager
2 parents ed90b26 + 3e273e0 commit 11baa07

File tree

4 files changed

+294
-58
lines changed

4 files changed

+294
-58
lines changed

src/main/java/com/CDPrintable/ConfigManager.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ public class ConfigManager {
3535
try {
3636
if (file.createNewFile()) {
3737
JOptionPane.showMessageDialog(null, "Config file created. Welcome to CDPrintable!", "Welcome", JOptionPane.INFORMATION_MESSAGE);
38-
Files.writeString(file.toPath(), "{\"userAgentWebAddress\": \"https://github.com/EatSleepProgramRepeat/CDPrintable\"}");
38+
Files.writeString(file.toPath(), "{}");
39+
setProperty("userAgentWebAddress", "https://github.com/EatSleepProgramRepeat/CDPrintable");
40+
setProperty("font", "Arial");
41+
setIntProperty("fontSize", 10);
42+
setDoubleProperty("paperWidth", 8.5);
43+
setDoubleProperty("paperHeight", 11);
44+
setDoubleProperty("labelWidth", 4);
45+
setDoubleProperty("labelMaxHeight", 2);
3946
}
4047
} catch (IOException e) {
4148
JOptionPane.showMessageDialog(null, "oopsie poopsies", "Error", JOptionPane.ERROR_MESSAGE);
@@ -58,6 +65,60 @@ public static String getProperty(String key) {
5865
}
5966
}
6067

68+
/**
69+
* Reads a property from the config file.
70+
* @param key The key to read.
71+
* @param defaultValue The default to return if it doesn't exist.
72+
* @return The requested property.
73+
*/
74+
public static String getProperty(String key, String defaultValue) {
75+
readConfigFile();
76+
try {
77+
JsonElement jsonElement = JsonParser.parseString(json);
78+
JsonObject jsonObject = jsonElement.getAsJsonObject();
79+
return jsonObject.has(key) ? jsonObject.get(key).getAsString() : defaultValue;
80+
} catch (Exception e) {
81+
JOptionPane.showMessageDialog(null, "Error reading property from config file!", "Error", JOptionPane.ERROR_MESSAGE);
82+
return defaultValue;
83+
}
84+
}
85+
86+
/**
87+
* Reads an integer property from the config.
88+
* @param key The key to read.
89+
* @param defaultValue The default to return if it doesn't exist.
90+
* @return The requested int.
91+
*/
92+
public static int getIntProperty(String key, int defaultValue) {
93+
readConfigFile();
94+
try {
95+
JsonElement jsonElement = JsonParser.parseString(json);
96+
JsonObject jsonObject = jsonElement.getAsJsonObject();
97+
return jsonObject.has(key) ? jsonObject.get(key).getAsInt() : defaultValue;
98+
} catch (Exception e) {
99+
JOptionPane.showMessageDialog(null, "Error reading property from config file!", "Error", JOptionPane.ERROR_MESSAGE);
100+
return defaultValue;
101+
}
102+
}
103+
104+
/**
105+
* Reads a double property from the config.
106+
* @param key The key to read.
107+
* @param defaultValue The default to return if it doesn't exist.
108+
* @return The requested double.
109+
*/
110+
public static double getDoubleProperty(String key, double defaultValue) {
111+
readConfigFile();
112+
try {
113+
JsonElement jsonElement = JsonParser.parseString(json);
114+
JsonObject jsonObject = jsonElement.getAsJsonObject();
115+
return jsonObject.has(key) ? jsonObject.get(key).getAsDouble() : defaultValue;
116+
} catch (Exception e) {
117+
JOptionPane.showMessageDialog(null, "Error reading property from config file!", "Error", JOptionPane.ERROR_MESSAGE);
118+
return defaultValue;
119+
}
120+
}
121+
61122
/**
62123
* Sets a property in the config file.
63124
* @param key The key to set.
@@ -71,6 +132,32 @@ public static void setProperty(String key, String value) {
71132
writeConfigFile(jsonObject);
72133
}
73134

135+
/**
136+
* Sets an int in the config file.
137+
* @param key The key to set.
138+
* @param value The value to set.
139+
*/
140+
public static void setIntProperty(String key, int value) {
141+
readConfigFile();
142+
JsonElement jsonElement = JsonParser.parseString(json);
143+
JsonObject jsonObject = jsonElement.getAsJsonObject();
144+
jsonObject.addProperty(key, value);
145+
writeConfigFile(jsonObject);
146+
}
147+
148+
/**
149+
* Sets a double in the config file.
150+
* @param key The key to set.
151+
* @param value The value to set.
152+
*/
153+
public static void setDoubleProperty(String key, double value) {
154+
readConfigFile();
155+
JsonElement jsonElement = JsonParser.parseString(json);
156+
JsonObject jsonObject = jsonElement.getAsJsonObject();
157+
jsonObject.addProperty(key, value);
158+
writeConfigFile(jsonObject);
159+
}
160+
74161
/**
75162
* Helper method to read a JSON file.
76163
*/

src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
package com.CDPrintable.MusicBrainzResources;
1212

13+
import com.CDPrintable.ConfigManager;
14+
1315
import javax.swing.*;
1416
import java.awt.*;
1517
import java.awt.image.BufferedImage;
@@ -18,60 +20,43 @@
1820

1921
public class MusicBrainzLabelGenerator implements Printable {
2022
private final ArrayList<MusicBrainzFinalizedRelease> finalizedReleaseList;
21-
public int LABEL_WIDTH;
22-
public int LABEL_MAX_HEIGHT;
23+
public double labelWidth;
24+
public double labelMaxHeight;
2325
public int dpiX;
2426
public int dpiY;
2527
public int marginTop;
2628
public int marginBottom;
2729
public int marginLeft;
2830
public int marginRight;
29-
private int fontSize = 10;
30-
public double pageWidth = 8.5;
31-
public double pageHeight = 11;
32-
33-
public int getFontSize() {
34-
return fontSize;
35-
}
36-
37-
public void setFontSize(int fontSize) {
38-
this.fontSize = fontSize;
39-
}
31+
private double fontSize;
32+
public double pageWidth;
33+
public double pageHeight;
34+
public String fontName;
4035

4136
public MusicBrainzLabelGenerator() {
4237
double[] dpi = getDPI();
4338
this.dpiX = (int) dpi[0];
4439
this.dpiY = (int) dpi[1];
4540

46-
this.LABEL_WIDTH = 4 * dpiX; // Example: 1 inch width
47-
this.LABEL_MAX_HEIGHT = 2 * dpiY; // Example: 1 inch height
41+
this.labelWidth = ConfigManager.getDoubleProperty("labelWidth", 4) * dpiX;
42+
this.labelMaxHeight = ConfigManager.getDoubleProperty("labelMaxHeight", 2) * dpiY;
4843

4944
finalizedReleaseList = new ArrayList<>();
5045
System.out.println("DPI: dpiX=" + dpiX + ", dpiY=" + dpiY);
51-
System.out.println("Label dimensions: " + LABEL_WIDTH + "x" + LABEL_MAX_HEIGHT);
46+
System.out.println("Label dimensions: " + labelWidth + "x" + labelMaxHeight);
5247

5348
double[] margins = getMargins();
5449
this.marginTop = (int) margins[0];
5550
this.marginBottom = (int) margins[1];
5651
this.marginLeft = (int) margins[2];
5752
this.marginRight = (int) margins[3];
5853
System.out.println("Margins: " + margins[0] + "x" + margins[1] + "x" + margins[2] + "x" + margins[3]);
59-
}
6054

61-
public int getLabelWidth() {
62-
return LABEL_WIDTH;
63-
}
55+
this.fontSize = ConfigManager.getDoubleProperty("fontSize", 10);
56+
this.pageWidth = ConfigManager.getDoubleProperty("pageWidth", 8.5);
57+
this.pageHeight = ConfigManager.getDoubleProperty("pageHeight", 11);
58+
this.fontName = ConfigManager.getProperty("fontName", "Arial");
6459

65-
public void setLabelWidth(int labelWidth) {
66-
LABEL_WIDTH = labelWidth * dpiX;
67-
}
68-
69-
public int getLabelMaxHeight() {
70-
return LABEL_MAX_HEIGHT;
71-
}
72-
73-
public void setLabelMaxHeight(int labelMaxHeight) {
74-
LABEL_MAX_HEIGHT = labelMaxHeight * dpiY;
7560
}
7661

7762
@Override
@@ -80,7 +65,7 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
8065
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
8166
g2d.setColor(Color.BLACK);
8267

83-
Font font = new Font("Arial", Font.PLAIN, fontSize);
68+
Font font = new Font(fontName, Font.PLAIN, (int) fontSize);
8469
g2d.setFont(font);
8570
FontMetrics fontMetrics = g2d.getFontMetrics();
8671

@@ -98,11 +83,11 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
9883
for (MusicBrainzTrack track : release.getTracks()) {
9984
String line = track.getTrackNumber() + ". " + track.getTitle() + " ";
10085
// Split stuff up IF the line gets too long
101-
if (fontMetrics.stringWidth(lineBuilder + line) > LABEL_WIDTH) {
86+
if (fontMetrics.stringWidth(lineBuilder + line) > labelWidth) {
10287
releaseLines.add(lineBuilder.toString());
10388
lineBuilder.delete(0, lineBuilder.length());
10489
}
105-
if (releaseLines.size() * fontMetrics.getHeight() >= LABEL_MAX_HEIGHT) {break;}
90+
if (releaseLines.size() * fontMetrics.getHeight() >= labelMaxHeight) {break;}
10691
lineBuilder.append(line);
10792
}
10893
if (!lineBuilder.isEmpty()) {
@@ -259,4 +244,51 @@ public void displayPagesAsImages() {
259244
}
260245
}
261246

247+
public double getLabelWidth() {
248+
return labelWidth;
249+
}
250+
251+
public void setLabelWidth(double labelWidth) {
252+
this.labelWidth = labelWidth;
253+
}
254+
255+
public double getLabelMaxHeight() {
256+
return labelMaxHeight;
257+
}
258+
259+
public void setLabelMaxHeight(double labelMaxHeight) {
260+
this.labelMaxHeight = labelMaxHeight;
261+
}
262+
263+
public double getFontSize() {
264+
return fontSize;
265+
}
266+
267+
public void setFontSize(double fontSize) {
268+
this.fontSize = fontSize;
269+
}
270+
271+
public double getPageWidth() {
272+
return pageWidth;
273+
}
274+
275+
public void setPageWidth(double pageWidth) {
276+
this.pageWidth = pageWidth;
277+
}
278+
279+
public double getPageHeight() {
280+
return pageHeight;
281+
}
282+
283+
public void setPageHeight(double pageHeight) {
284+
this.pageHeight = pageHeight;
285+
}
286+
287+
public String getFontName() {
288+
return fontName;
289+
}
290+
291+
public void setFontName(String fontName) {
292+
this.fontName = fontName;
293+
}
262294
}

0 commit comments

Comments
 (0)