diff --git a/src/main/java/com/CDPrintable/ConfigManager.java b/src/main/java/com/CDPrintable/ConfigManager.java index aa7ccd9..464ad3b 100644 --- a/src/main/java/com/CDPrintable/ConfigManager.java +++ b/src/main/java/com/CDPrintable/ConfigManager.java @@ -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); @@ -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. @@ -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. */ diff --git a/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java b/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java index d35fd0c..9ed90ad 100644 --- a/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java +++ b/src/main/java/com/CDPrintable/MusicBrainzResources/MusicBrainzLabelGenerator.java @@ -10,6 +10,8 @@ package com.CDPrintable.MusicBrainzResources; +import com.CDPrintable.ConfigManager; + import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; @@ -18,37 +20,30 @@ public class MusicBrainzLabelGenerator implements Printable { private final ArrayList 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]; @@ -56,22 +51,12 @@ public MusicBrainzLabelGenerator() { 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 @@ -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(); @@ -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()) { @@ -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; + } } \ No newline at end of file diff --git a/src/main/java/com/CDPrintable/ProgramWindow.java b/src/main/java/com/CDPrintable/ProgramWindow.java index b8dd5cd..4c85ffe 100644 --- a/src/main/java/com/CDPrintable/ProgramWindow.java +++ b/src/main/java/com/CDPrintable/ProgramWindow.java @@ -23,10 +23,10 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Locale; +import java.util.function.Consumer; public class ProgramWindow { private final UserAgent userAgent; - private JLabel fullUserAgentLabel = new JLabel(); private final JPanel cdSearchPanel = new JPanel(); private final JLabel searchStatusLabel = new JLabel("Status: Nothing's going on."); private static final ArrayList idList = new ArrayList<>(); @@ -263,7 +263,7 @@ private DefaultTableModel createTableModel(String[] columnNames, String[][] data * @return A JPanel with the settings window. */ private JPanel settingsPanel() { - JPanel panel = new JPanel(new GridLayout(1, 2)); + JPanel panel = new JPanel(new BorderLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5, 5, 5, 5); gbc.fill = GridBagConstraints.HORIZONTAL; @@ -276,28 +276,79 @@ private JPanel settingsPanel() { JLabel userAgentLabel = new JLabel("User Agent (this doesn't save):"); JTextField userAgentField = new JTextField(15); userAgentField.setText(userAgent.getUserAgent()); - userAgentField.addActionListener(_ -> userAgent.setUserAgent(userAgentField.getText(), fullUserAgentLabel)); + userAgentField.addActionListener(_ -> userAgent.setUserAgent(userAgentField.getText(), true)); // Set up the user agent field with labels and document listener. JLabel userAgentWebAddressLabel = new JLabel("User Agent Web Address:"); JTextField userAgentWebAddressField = new JTextField(15); userAgentWebAddressField.setText(userAgent.getUserAgentWebAddress()); userAgentWebAddressField.addActionListener(_ -> { - userAgent.setUserAgentWebAddress(userAgentWebAddressField.getText(), fullUserAgentLabel); + userAgent.setUserAgentWebAddress(userAgentWebAddressField.getText(), true); ConfigManager.setProperty("userAgentWebAddress", userAgentWebAddressField.getText()); }); - fullUserAgentLabel = new JLabel(userAgent.toString()); - // Font settings - JPanel fontPanel = new JPanel(new GridBagLayout()); - fontPanel.setBorder(BorderFactory.createTitledBorder("Font")); + JPanel printerPanel = new JPanel(new GridBagLayout()); + printerPanel.setBorder(BorderFactory.createTitledBorder("Printer Settings (Units are in inches)")); JLabel fontLabel = new JLabel("Font:"); JTextField fontField = new JTextField(30); + fontField.setText(ConfigManager.getProperty("font")); + fontField.addActionListener(_ -> { + ConfigManager.setProperty("font", fontField.getText()); + labelGenerator.setFontName(fontField.getText()); + }); JLabel fontSizeLabel = new JLabel("Font Size:"); JTextField fontSizeField = new JTextField(30); + fontSizeField.setText(ConfigManager.getProperty("fontSize")); + fontSizeField.addActionListener(_ -> validateAndSetDoubleField( + fontSizeField, + labelGenerator::setFontSize, + "fontSize", + "Font Size" + )); + + JLabel paperWidthLabel = new JLabel("Paper Width:"); + JTextField paperWidthField = new JTextField(30); + paperWidthField.setText(ConfigManager.getProperty("paperWidth")); + paperWidthField.addActionListener(_ -> validateAndSetDoubleField( + paperWidthField, + labelGenerator::setPageWidth, + "paperWidth", + "Paper Width" + )); + + + JLabel paperHeightLabel = new JLabel("Paper Height:"); + JTextField paperHeightField = new JTextField(30); + paperHeightField.setText(ConfigManager.getProperty("paperHeight")); + paperHeightField.addActionListener(_ -> validateAndSetDoubleField( + paperHeightField, + labelGenerator::setPageHeight, + "paperHeight", + "Paper Height" + )); + + JLabel labelWidthLabel = new JLabel("Label Width:"); + JTextField labelWidthField = new JTextField(30); + labelWidthField.setText(ConfigManager.getProperty("labelWidth")); + labelWidthField.addActionListener(_ -> validateAndSetDoubleField( + labelWidthField, + labelGenerator::setLabelWidth, + "labelWidth", + "Label Width" + )); + + JLabel labelMaxHeightLabel = new JLabel("Label Max Height:"); + JTextField labelMaxHeightField = new JTextField(30); + labelMaxHeightField.setText(ConfigManager.getProperty("labelMaxHeight")); + labelMaxHeightField.addActionListener(_ -> validateAndSetDoubleField( + labelMaxHeightField, + labelGenerator::setLabelMaxHeight, + "labelHeight", + "Label Max Height" + )); JPanel userAgentInputPanel = new JPanel(new GridBagLayout()); JPanel fullAgentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); @@ -306,32 +357,45 @@ private JPanel settingsPanel() { gbc.gridx = 0; gbc.gridy = 0; userAgentInputPanel.add(userAgentLabel, gbc); - fontPanel.add(fontLabel, gbc); - + printerPanel.add(fontLabel, gbc); gbc.gridx = 1; userAgentInputPanel.add(userAgentField, gbc); - fontPanel.add(fontField, gbc); - + printerPanel.add(fontField, gbc); gbc.gridx = 0; gbc.gridy = 1; userAgentInputPanel.add(userAgentWebAddressLabel, gbc); - fontPanel.add(fontSizeLabel, gbc); - + printerPanel.add(fontSizeLabel, gbc); gbc.gridx = 1; userAgentInputPanel.add(userAgentWebAddressField, gbc); - fontPanel.add(fontSizeField, gbc); - - userAgentInputPanel.add(fullUserAgentLabel, gbc); - fullAgentPanel.add(fullUserAgentLabel); + printerPanel.add(fontSizeField, gbc); + gbc.gridx = 0; + gbc.gridy = 2; + printerPanel.add(paperWidthLabel, gbc); + gbc.gridx = 1; + printerPanel.add(paperWidthField, gbc); + gbc.gridx = 0; + gbc.gridy = 3; + printerPanel.add(paperHeightLabel, gbc); + gbc.gridx = 1; + printerPanel.add(paperHeightField, gbc); + gbc.gridx = 0; + gbc.gridy = 4; + printerPanel.add(labelWidthLabel, gbc); + gbc.gridx = 1; + printerPanel.add(labelWidthField, gbc); + gbc.gridx = 0; + gbc.gridy = 5; + printerPanel.add(labelMaxHeightLabel, gbc); + gbc.gridx = 1; + printerPanel.add(labelMaxHeightField, gbc); // Add panels to the UA main panel userAgentPanel.add(fullAgentPanel, BorderLayout.NORTH); userAgentPanel.add(userAgentInputPanel, BorderLayout.CENTER); - userAgentPanel.add(new JLabel("Press enter to save settings anywhere."), BorderLayout.SOUTH); - // Add subpanels to the main panel - panel.add(userAgentPanel); - panel.add(fontPanel); + panel.add(new JLabel("Press enter to save settings anywhere."), BorderLayout.NORTH); + panel.add(userAgentPanel, BorderLayout.WEST); + panel.add(printerPanel, BorderLayout.EAST); return panel; } @@ -493,4 +557,37 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole return cell; } } + + /** + * Helper method to make sure that an int field is valid. + * @param field The field. + * @param setter The method in MusicBrainzLabelGenerator that takes an int. + * @param configKey the JSON config key. + * @param fieldName The field name. + */ + private void validateAndSetDoubleField(JTextField field, Consumer setter, String configKey, String fieldName) { + String input = field.getText().trim(); + + if (input.isEmpty()) { + showError(fieldName + " cannot be empty."); + return; + } + + try { + double value = Double.parseDouble(input); + if (value <= 0) { + showError(fieldName + " must be a positive number."); + return; + } + setter.accept(value); + ConfigManager.setDoubleProperty(configKey, value); + } catch (NumberFormatException e) { + showError("Please enter a valid whole number for " + fieldName + "."); + } + } + + private void showError(String message) { + JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); + } + } \ No newline at end of file diff --git a/src/main/java/com/CDPrintable/UserAgent.java b/src/main/java/com/CDPrintable/UserAgent.java index df4c52f..e73c2b4 100644 --- a/src/main/java/com/CDPrintable/UserAgent.java +++ b/src/main/java/com/CDPrintable/UserAgent.java @@ -10,7 +10,7 @@ package com.CDPrintable; -import javax.swing.JLabel; +import javax.swing.*; public class UserAgent { private String userAgent; @@ -70,6 +70,16 @@ public void setUserAgent(String userAgent, JLabel fullUserAgentLabel) { fullUserAgentLabel.setText(toString()); } + /** + * Another random method for setting the user agent. + */ + public void setUserAgent(String userAgent, boolean b) { + this.userAgent = userAgent; + if (b) { + JOptionPane.showMessageDialog(null, "Here's your new agent: " + this, "New User Agent", JOptionPane.INFORMATION_MESSAGE); + } + } + /** * Sets the user agent web address and does some tomfoolery IDK man. * @param address The user agent web address to set. @@ -78,4 +88,14 @@ public void setUserAgentWebAddress(String address, JLabel fullUserAgentLabel) { this.userAgentWebAddress = address; fullUserAgentLabel.setText(toString()); } -} + + /** + * Another random method for setting user agent web address. + */ + public void setUserAgentWebAddress(String address, boolean b) { + this.userAgentWebAddress = address; + if (b) { + JOptionPane.showMessageDialog(null, "Here's your new agent: " + this, "New User Agent", JOptionPane.INFORMATION_MESSAGE); + } + } +} \ No newline at end of file