Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion src/main/java/com/CDPrintable/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public class ConfigManager {
try {
if (file.createNewFile()) {
JOptionPane.showMessageDialog(null, "Config file created. Welcome to CDPrintable!", "Welcome", JOptionPane.INFORMATION_MESSAGE);
Files.writeString(file.toPath(), "{\"userAgentWebAddress\": \"https://github.com/EatSleepProgramRepeat/CDPrintable\"}");
Files.writeString(file.toPath(), "{}");
setProperty("userAgentWebAddress", "https://github.com/EatSleepProgramRepeat/CDPrintable");
setProperty("font", "Arial");
setIntProperty("fontSize", 10);
setDoubleProperty("paperWidth", 8.5);
setDoubleProperty("paperHeight", 11);
setDoubleProperty("labelWidth", 4);
setDoubleProperty("labelMaxHeight", 2);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "oopsie poopsies", "Error", JOptionPane.ERROR_MESSAGE);
Expand All @@ -58,6 +65,60 @@ public static String getProperty(String key) {
}
}

/**
* Reads a property from the config file.
* @param key The key to read.
* @param defaultValue The default to return if it doesn't exist.
* @return The requested property.
*/
public static String getProperty(String key, String defaultValue) {
readConfigFile();
try {
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonObject.has(key) ? jsonObject.get(key).getAsString() : defaultValue;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error reading property from config file!", "Error", JOptionPane.ERROR_MESSAGE);
return defaultValue;
}
}

/**
* Reads an integer property from the config.
* @param key The key to read.
* @param defaultValue The default to return if it doesn't exist.
* @return The requested int.
*/
public static int getIntProperty(String key, int defaultValue) {
readConfigFile();
try {
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonObject.has(key) ? jsonObject.get(key).getAsInt() : defaultValue;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error reading property from config file!", "Error", JOptionPane.ERROR_MESSAGE);
return defaultValue;
}
}

/**
* Reads a double property from the config.
* @param key The key to read.
* @param defaultValue The default to return if it doesn't exist.
* @return The requested double.
*/
public static double getDoubleProperty(String key, double defaultValue) {
readConfigFile();
try {
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonObject.has(key) ? jsonObject.get(key).getAsDouble() : defaultValue;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error reading property from config file!", "Error", JOptionPane.ERROR_MESSAGE);
return defaultValue;
}
}

/**
* Sets a property in the config file.
* @param key The key to set.
Expand All @@ -71,6 +132,32 @@ public static void setProperty(String key, String value) {
writeConfigFile(jsonObject);
}

/**
* Sets an int in the config file.
* @param key The key to set.
* @param value The value to set.
*/
public static void setIntProperty(String key, int value) {
readConfigFile();
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();
jsonObject.addProperty(key, value);
writeConfigFile(jsonObject);
}

/**
* Sets a double in the config file.
* @param key The key to set.
* @param value The value to set.
*/
public static void setDoubleProperty(String key, double value) {
readConfigFile();
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();
jsonObject.addProperty(key, value);
writeConfigFile(jsonObject);
}

/**
* Helper method to read a JSON file.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package com.CDPrintable.MusicBrainzResources;

import com.CDPrintable.ConfigManager;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
Expand All @@ -18,60 +20,43 @@

public class MusicBrainzLabelGenerator implements Printable {
private final ArrayList<MusicBrainzFinalizedRelease> finalizedReleaseList;
public int LABEL_WIDTH;
public int LABEL_MAX_HEIGHT;
public double labelWidth;
public double labelMaxHeight;
public int dpiX;
public int dpiY;
public int marginTop;
public int marginBottom;
public int marginLeft;
public int marginRight;
private int fontSize = 10;
public double pageWidth = 8.5;
public double pageHeight = 11;

public int getFontSize() {
return fontSize;
}

public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
private double fontSize;
public double pageWidth;
public double pageHeight;
public String fontName;

public MusicBrainzLabelGenerator() {
double[] dpi = getDPI();
this.dpiX = (int) dpi[0];
this.dpiY = (int) dpi[1];

this.LABEL_WIDTH = 4 * dpiX; // Example: 1 inch width
this.LABEL_MAX_HEIGHT = 2 * dpiY; // Example: 1 inch height
this.labelWidth = ConfigManager.getDoubleProperty("labelWidth", 4) * dpiX;
this.labelMaxHeight = ConfigManager.getDoubleProperty("labelMaxHeight", 2) * dpiY;

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

double[] margins = getMargins();
this.marginTop = (int) margins[0];
this.marginBottom = (int) margins[1];
this.marginLeft = (int) margins[2];
this.marginRight = (int) margins[3];
System.out.println("Margins: " + margins[0] + "x" + margins[1] + "x" + margins[2] + "x" + margins[3]);
}

public int getLabelWidth() {
return LABEL_WIDTH;
}
this.fontSize = ConfigManager.getDoubleProperty("fontSize", 10);
this.pageWidth = ConfigManager.getDoubleProperty("pageWidth", 8.5);
this.pageHeight = ConfigManager.getDoubleProperty("pageHeight", 11);
this.fontName = ConfigManager.getProperty("fontName", "Arial");

public void setLabelWidth(int labelWidth) {
LABEL_WIDTH = labelWidth * dpiX;
}

public int getLabelMaxHeight() {
return LABEL_MAX_HEIGHT;
}

public void setLabelMaxHeight(int labelMaxHeight) {
LABEL_MAX_HEIGHT = labelMaxHeight * dpiY;
}

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

Font font = new Font("Arial", Font.PLAIN, fontSize);
Font font = new Font(fontName, Font.PLAIN, (int) fontSize);
g2d.setFont(font);
FontMetrics fontMetrics = g2d.getFontMetrics();

Expand All @@ -98,11 +83,11 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
for (MusicBrainzTrack track : release.getTracks()) {
String line = track.getTrackNumber() + ". " + track.getTitle() + " ";
// Split stuff up IF the line gets too long
if (fontMetrics.stringWidth(lineBuilder + line) > LABEL_WIDTH) {
if (fontMetrics.stringWidth(lineBuilder + line) > labelWidth) {
releaseLines.add(lineBuilder.toString());
lineBuilder.delete(0, lineBuilder.length());
}
if (releaseLines.size() * fontMetrics.getHeight() >= LABEL_MAX_HEIGHT) {break;}
if (releaseLines.size() * fontMetrics.getHeight() >= labelMaxHeight) {break;}
lineBuilder.append(line);
}
if (!lineBuilder.isEmpty()) {
Expand Down Expand Up @@ -259,4 +244,51 @@ public void displayPagesAsImages() {
}
}

public double getLabelWidth() {
return labelWidth;
}

public void setLabelWidth(double labelWidth) {
this.labelWidth = labelWidth;
}

public double getLabelMaxHeight() {
return labelMaxHeight;
}

public void setLabelMaxHeight(double labelMaxHeight) {
this.labelMaxHeight = labelMaxHeight;
}

public double getFontSize() {
return fontSize;
}

public void setFontSize(double fontSize) {
this.fontSize = fontSize;
}

public double getPageWidth() {
return pageWidth;
}

public void setPageWidth(double pageWidth) {
this.pageWidth = pageWidth;
}

public double getPageHeight() {
return pageHeight;
}

public void setPageHeight(double pageHeight) {
this.pageHeight = pageHeight;
}

public String getFontName() {
return fontName;
}

public void setFontName(String fontName) {
this.fontName = fontName;
}
}
Loading