Skip to content

Commit

Permalink
Change settings classes to records
Browse files Browse the repository at this point in the history
GSON doesn't like it, but there is a workaround at
google/gson#1794
  • Loading branch information
aroelke committed Apr 30, 2021
1 parent e53193d commit 17bc5a3
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 339 deletions.
34 changes: 17 additions & 17 deletions src/main/java/editor/gui/MainFrame.java
Expand Up @@ -436,8 +436,8 @@ public MainFrame(List<File> files)
JOptionPane.showMessageDialog(this, "Bad file URL: " + SettingsDialog.settings().inventory.url() + ".zip", "Warning", JOptionPane.WARNING_MESSAGE);
}
inventoryFile = new File(SettingsDialog.settings().inventory.path());
recentCount = SettingsDialog.settings().editor.recents.count;
newestVersion = SettingsDialog.settings().inventory.version;
recentCount = SettingsDialog.settings().editor.recents.count();
newestVersion = SettingsDialog.settings().inventory.version();

setTitle("MTG Workstation");
setIconImages(IntStream.rangeClosed(4, 8).mapToObj((i) -> new ImageIcon(MainFrame.class.getResource("/icon/" + (1 << i) + ".png")).getImage()).collect(Collectors.toList()));
Expand Down Expand Up @@ -501,7 +501,7 @@ public MainFrame(List<File> files)
// Recent files menu
recentsMenu = new JMenu("Open Recent");
recentsMenu.setEnabled(false);
for (String fname : SettingsDialog.settings().editor.recents.files)
for (String fname : SettingsDialog.settings().editor.recents.files())
updateRecents(new File(fname));
fileMenu.add(recentsMenu);

Expand Down Expand Up @@ -1331,7 +1331,7 @@ public void actionPerformed(ActionEvent e)

// Panel showing the image of the currently-selected card
cardPane.addTab("Image", imagePanel = new CardImagePanel());
setImageBackground(SettingsDialog.settings().inventory.background);
setImageBackground(SettingsDialog.settings().inventory.background());

// Pane displaying the Oracle text
oracleTextPane = new JTextPane();
Expand Down Expand Up @@ -1429,7 +1429,7 @@ public void actionPerformed(ActionEvent e)
inventoryTable.setDefaultRenderer(Integer.class, new InventoryTableCellRenderer());
inventoryTable.setDefaultRenderer(Rarity.class, new InventoryTableCellRenderer());
inventoryTable.setDefaultRenderer(List.class, new InventoryTableCellRenderer());
inventoryTable.setStripeColor(SettingsDialog.settings().inventory.stripe);
inventoryTable.setStripeColor(SettingsDialog.settings().inventory.stripe());
inventoryTable.addMouseListener(MouseListenerFactory.createClickListener((e) -> selectedFrame.ifPresent((f) -> {
if (e.getClickCount() % 2 == 0)
f.addCards(EditorFrame.MAIN_DECK, getSelectedCards(), 1);
Expand Down Expand Up @@ -1559,7 +1559,7 @@ public void windowClosing(WindowEvent e)
@Override
public void windowOpened(WindowEvent e)
{
if (checkForUpdate(SettingsDialog.settings().inventory.update) == UPDATE_NEEDED && updateInventory())
if (checkForUpdate(SettingsDialog.settings().inventory.update()) == UPDATE_NEEDED && updateInventory())
SettingsDialog.setInventoryVersion(newestVersion);
loadInventory();
TableSelectionListener listener = new TableSelectionListener(MainFrame.this, inventoryTable, inventory);
Expand All @@ -1568,7 +1568,7 @@ public void windowOpened(WindowEvent e)

if (!inventory.isEmpty())
{
for (CategorySpec spec : SettingsDialog.settings().editor.categories.presets)
for (CategorySpec spec : SettingsDialog.settings().editor.categories.presets())
{
JMenuItem categoryItem = new JMenuItem(spec.getName());
categoryItem.addActionListener((v) -> selectedFrame.ifPresent((f) -> f.addCategory(spec)));
Expand Down Expand Up @@ -1626,20 +1626,20 @@ public void applySettings()
JOptionPane.showMessageDialog(this, "Bad file URL: " + SettingsDialog.settings().inventory.url() + ".zip", "Warning", JOptionPane.WARNING_MESSAGE);
}
inventoryFile = new File(SettingsDialog.settings().inventory.path());
recentCount = SettingsDialog.settings().editor.recents.count;
inventoryModel.setColumns(SettingsDialog.settings().inventory.columns);
inventoryTable.setStripeColor(SettingsDialog.settings().inventory.stripe);
recentCount = SettingsDialog.settings().editor.recents.count();
inventoryModel.setColumns(SettingsDialog.settings().inventory.columns());
inventoryTable.setStripeColor(SettingsDialog.settings().inventory.stripe());
for (EditorFrame frame : editors)
frame.applySettings();
presetMenu.removeAll();
for (CategorySpec spec : SettingsDialog.settings().editor.categories.presets)
for (CategorySpec spec : SettingsDialog.settings().editor.categories.presets())
{
JMenuItem categoryItem = new JMenuItem(spec.getName());
categoryItem.addActionListener((e) -> selectedFrame.ifPresent((f) -> f.addCategory(spec)));
presetMenu.add(categoryItem);
}
setImageBackground(SettingsDialog.settings().inventory.background);
setHandBackground(SettingsDialog.settings().editor.hand.background);
setImageBackground(SettingsDialog.settings().inventory.background());
setHandBackground(SettingsDialog.settings().editor.hand.background());

revalidate();
repaint();
Expand Down Expand Up @@ -1668,19 +1668,19 @@ public int checkForUpdate(UpdateFrequency freq)
}
return UPDATE_NEEDED;
}
else if (SettingsDialog.settings().inventory.update != UpdateFrequency.NEVER)
else if (SettingsDialog.settings().inventory.update() != UpdateFrequency.NEVER)
{
try (BufferedReader in = new BufferedReader(new InputStreamReader(versionSite.openStream())))
{
JsonObject data = new JsonParser().parse(in.lines().collect(Collectors.joining())).getAsJsonObject();
newestVersion = new DatabaseVersion((data.has("data") ? data.get("data").getAsJsonObject() : data).get("version").getAsString());
}
if (newestVersion.needsUpdate(SettingsDialog.settings().inventory.version, freq))
if (newestVersion.needsUpdate(SettingsDialog.settings().inventory.version(), freq))
{
int wantUpdate = JOptionPane.showConfirmDialog(
this,
"Inventory is out of date:\n" +
UnicodeSymbols.BULLET + " Current version: " + SettingsDialog.settings().inventory.version + "\n" +
UnicodeSymbols.BULLET + " Current version: " + SettingsDialog.settings().inventory.version() + "\n" +
UnicodeSymbols.BULLET + " Latest version: " + newestVersion + "\n" +
"\n" +
"Download update?",
Expand Down Expand Up @@ -1823,7 +1823,7 @@ public void loadInventory()
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
inventory = InventoryLoader.loadInventory(this, inventoryFile);
inventory.sort(CardAttribute.NAME.comparingCard());
inventoryModel = new CardTableModel(inventory, SettingsDialog.settings().inventory.columns);
inventoryModel = new CardTableModel(inventory, SettingsDialog.settings().inventory.columns());
inventoryTable.setModel(inventoryModel);
setCursor(Cursor.getDefaultCursor());
System.gc();
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/editor/gui/display/CardImagePanel.java
Expand Up @@ -208,16 +208,16 @@ protected Void doInBackground() throws Exception
System.out.println(e);
}
}
if (SettingsDialog.settings().inventory.imageLimitEnable)
if (SettingsDialog.settings().inventory.imageLimitEnable())
{
int count = 0;
do
{
var images = Paths.get(SettingsDialog.settings().inventory.scans).toFile().listFiles();
var images = Paths.get(SettingsDialog.settings().inventory.scans()).toFile().listFiles();
count = images.length;
if (count > SettingsDialog.settings().inventory.imageLimit)
if (count > SettingsDialog.settings().inventory.imageLimit())
Arrays.stream(images).min(Comparator.comparingLong(File::lastModified)).ifPresent(File::delete);
} while (count > SettingsDialog.settings().inventory.imageLimit);
} while (count > SettingsDialog.settings().inventory.imageLimit());
}
SwingUtilities.invokeLater(() -> {
if (req.source.card == req.card)
Expand Down Expand Up @@ -261,12 +261,12 @@ protected void process(List<Integer> chunks)
*/
private static List<File> getFiles(Card c)
{
switch (SettingsDialog.settings().inventory.imageSource)
switch (SettingsDialog.settings().inventory.imageSource())
{
case "Scryfall":
return IntStream.range(0, c.imageNames().size()).mapToObj((i) -> Paths.get(SettingsDialog.settings().inventory.scans, c.scryfallid().get(i) + ";" + i + ".jpg").toFile()).collect(Collectors.toList());
return IntStream.range(0, c.imageNames().size()).mapToObj((i) -> Paths.get(SettingsDialog.settings().inventory.scans(), c.scryfallid().get(i) + ";" + i + ".jpg").toFile()).collect(Collectors.toList());
case "Gatherer":
return IntStream.range(0, c.multiverseid().size()).mapToObj((i) -> Paths.get(SettingsDialog.settings().inventory.scans, c.multiverseid().get(i) + ";" + i + ".jpg").toFile()).collect(Collectors.toList());
return IntStream.range(0, c.multiverseid().size()).mapToObj((i) -> Paths.get(SettingsDialog.settings().inventory.scans(), c.multiverseid().get(i) + ";" + i + ".jpg").toFile()).collect(Collectors.toList());
default:
return Collections.emptyList();
}
Expand Down Expand Up @@ -306,7 +306,7 @@ public static JPanel createStatusBar()
private static List<Optional<URL>> getURLs(Card c) throws MalformedURLException
{
List<Optional<URL>> urls = new ArrayList<>();
switch (SettingsDialog.settings().inventory.imageSource)
switch (SettingsDialog.settings().inventory.imageSource())
{
case "Scryfall":
switch (c.layout())
Expand Down Expand Up @@ -562,7 +562,7 @@ public void setCard(Card c)
faceImages.clear();
try
{
Files.createDirectories(Path.of(SettingsDialog.settings().inventory.scans));
Files.createDirectories(Path.of(SettingsDialog.settings().inventory.scans()));
if (getFiles(card).stream().map(File::toPath).allMatch(Files::exists))
loadImages();
else
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/editor/gui/display/CardJList.java
Expand Up @@ -67,7 +67,7 @@ public int getSize()
return cards.size();
}
});
setVisibleRowCount(SettingsDialog.settings().editor.categories.explicits);
setVisibleRowCount(SettingsDialog.settings().editor.categories.explicits());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/editor/gui/editor/CalculateHandPanel.java
Expand Up @@ -130,7 +130,7 @@ public Object getValueAt(int rowIndex, int columnIndex)
if (columnIndex == CATEGORY)
yield category;
else if (columnIndex - (E_INFO_COLS - 1) < expectedCounts.get(category).size())
yield ROUND_MODE.get(SettingsDialog.settings().editor.hand.rounding).apply(expectedCounts.get(category).get(columnIndex - (E_INFO_COLS - 1)));
yield ROUND_MODE.get(SettingsDialog.settings().editor.hand.rounding()).apply(expectedCounts.get(category).get(columnIndex - (E_INFO_COLS - 1)));
else
yield "";
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/editor/gui/editor/CategoryPanel.java
Expand Up @@ -264,7 +264,7 @@ public CategoryPanel(Deck d, String n, EditorFrame editor)
name = n;
background = getBackground();
flashTimer = new FlashTimer();
tableRows = SettingsDialog.settings().editor.categories.rows;
tableRows = SettingsDialog.settings().editor.categories.rows();

// Each category is surrounded by a border with a title
setBorder(border = BorderFactory.createTitledBorder(name));
Expand Down Expand Up @@ -362,7 +362,7 @@ public void mouseDragged(MouseEvent e)
Point p = SwingUtilities.convertPoint((Component)e.getSource(), e.getPoint(), table);
setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
final int minRows = 1;
final int maxRows = Math.max(deck.getCategoryList(name).total(), SettingsDialog.settings().editor.categories.rows);
final int maxRows = Math.max(deck.getCategoryList(name).total(), SettingsDialog.settings().editor.categories.rows());
if (p.y <= base - table.getRowHeight()/2 && tableRows > minRows)
{
int n = Math.min(((base - p.y) + table.getRowHeight() - 1)/table.getRowHeight(), tableRows - minRows);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/editor/gui/editor/EditorFrame.java
Expand Up @@ -584,7 +584,7 @@ public EditorFrame(MainFrame p, int u, DeckSerializer manager)
unsaved = false;
undoBuffer = new Stack<>();
redoBuffer = new Stack<>();
startingHandSize = SettingsDialog.settings().editor.hand.size;
startingHandSize = SettingsDialog.settings().editor.hand.size();
if (manager.canSaveFile())
setFile(manager.file());
else
Expand Down Expand Up @@ -834,7 +834,7 @@ public EditorFrame(MainFrame p, int u, DeckSerializer manager)
imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.X_AXIS));
imagePane = new JScrollPane(imagePanel);
imagePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
setHandBackground(SettingsDialog.settings().editor.hand.background);
setHandBackground(SettingsDialog.settings().editor.hand.background());

// Control panel for manipulating the sample hand
JPanel handModPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
Expand All @@ -847,7 +847,7 @@ public EditorFrame(MainFrame p, int u, DeckSerializer manager)
{
CardImagePanel panel = new CardImagePanel();
panel.setCard(c);
panel.setBackground(SettingsDialog.settings().editor.hand.background);
panel.setBackground(SettingsDialog.settings().editor.hand.background());
imagePanel.add(panel);
imagePanel.add(Box.createHorizontalStrut(10));
}
Expand All @@ -865,7 +865,7 @@ public EditorFrame(MainFrame p, int u, DeckSerializer manager)
CardImagePanel panel = new CardImagePanel();
imagePanel.add(panel);
panel.setCard(c);
panel.setBackground(SettingsDialog.settings().editor.hand.background);
panel.setBackground(SettingsDialog.settings().editor.hand.background());
imagePanel.add(Box.createHorizontalStrut(10));
}
imagePanel.validate();
Expand All @@ -878,7 +878,7 @@ public EditorFrame(MainFrame p, int u, DeckSerializer manager)
{
hand.draw();
CardImagePanel panel = new CardImagePanel();
panel.setBackground(SettingsDialog.settings().editor.hand.background);
panel.setBackground(SettingsDialog.settings().editor.hand.background());
imagePanel.add(panel);
panel.setCard(hand.get(hand.size() - 1));
imagePanel.add(Box.createHorizontalStrut(10));
Expand Down Expand Up @@ -1153,7 +1153,7 @@ public void applySettings()
deck().table.getColumn(deck().model.getColumnName(i)).setCellEditor(CardTable.createCellEditor(this, deck().model.getColumnData(i)));
for (CategoryPanel category : categoryPanels)
category.applySettings(this);
startingHandSize = SettingsDialog.settings().editor.hand.size;
startingHandSize = SettingsDialog.settings().editor.hand.size();
updateStats();
update();
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/editor/gui/editor/LegalityPanel.java
Expand Up @@ -136,20 +136,20 @@ public void setSelectionInterval(int index0, int index1)

// Panel containing check box for enabling commander search
Box cmdrPanel = Box.createHorizontalBox();
JCheckBox cmdrCheck = new JCheckBox("", SettingsDialog.settings().editor.legality.searchForCommander);
JCheckBox cmdrCheck = new JCheckBox("", SettingsDialog.settings().editor.legality.searchForCommander());
cmdrCheck.setText(cmdrCheck.isSelected() ? "Search for commander in:" : "Search for commander");
cmdrPanel.add(cmdrCheck);
List<String> names = new ArrayList<>(List.of(MAIN_DECK, ALL_LISTS));
names.addAll(editor.getExtraNames());
var cmdrBox = new JComboBox<>(names.toArray(String[]::new));
cmdrBox.setVisible(SettingsDialog.settings().editor.legality.searchForCommander);
if (SettingsDialog.settings().editor.legality.main)
cmdrBox.setVisible(SettingsDialog.settings().editor.legality.searchForCommander());
if (SettingsDialog.settings().editor.legality.main())
cmdrBox.setSelectedIndex(names.indexOf(MAIN_DECK));
else if (SettingsDialog.settings().editor.legality.all)
else if (SettingsDialog.settings().editor.legality.all())
cmdrBox.setSelectedIndex(names.indexOf(ALL_LISTS));
else
{
String name = SettingsDialog.settings().editor.legality.list;
String name = SettingsDialog.settings().editor.legality.list();
cmdrBox.setSelectedIndex(names.contains(name) ? names.indexOf(name) : names.indexOf(MAIN_DECK));
}
cmdrBox.setMaximumSize(cmdrBox.getPreferredSize());
Expand All @@ -162,7 +162,7 @@ else if (SettingsDialog.settings().editor.legality.all)
final JComboBox<String> sideCombo;
if (!editor.getExtraNames().isEmpty())
{
String sb = SettingsDialog.settings().editor.legality.sideboard;
String sb = SettingsDialog.settings().editor.legality.sideboard();

add(Box.createVerticalStrut(2));
Box sideboardBox = Box.createHorizontalBox();
Expand Down
Expand Up @@ -41,7 +41,7 @@ public DefaultsFilterPanel()

categories = new HashMap<>();

var presets = SettingsDialog.settings().editor.categories.presets;
var presets = SettingsDialog.settings().editor.categories.presets();
String[] names = presets.stream().map(CategorySpec::getName).toArray(String[]::new);
for (int i = 0; i < presets.size(); i++)
categories.put(names[i], presets.get(i));
Expand Down

0 comments on commit 17bc5a3

Please sign in to comment.