refactor gui code

This commit is contained in:
moehreag 2024-06-03 13:20:08 +02:00
parent e076c98531
commit 6ff44aac48
3 changed files with 50 additions and 84 deletions

View file

@ -1,9 +1,12 @@
package dev.frogmc.frogloader.impl.gui; package dev.frogmc.frogloader.impl.gui;
import javax.swing.*; import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.net.URI; import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import dev.frogmc.frogloader.impl.LoaderImpl; import dev.frogmc.frogloader.impl.LoaderImpl;
@ -63,15 +66,15 @@ public class LoaderGui {
pane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); pane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
pane.add(title, BorderLayout.NORTH); pane.add(title, BorderLayout.NORTH);
JPanel list = new JPanel();
for (ModDependencyResolver.UnfulfilledDependencyException.Entry e : ex.getDependencies()) { ex.getDependencies().forEach(e -> {
String descriptionText; String description;
if (e.presentVersion() != null){ if (e.presentVersion() != null){
descriptionText = String.format("Mod %s (%s) depends on a Mod with id %s with a version matching %s, but a different version is present or provided: %s", e.source().id(), e.source().name(), e.dependency(), printVersionRange(e.range()), e.presentVersion()); description = String.format("Mod %s (%s) depends on a Mod with id %s with a version matching %s, but a different version is present or provided: %s", e.source().id(), e.source().name(), e.dependency(), printVersionRange(e.range()), e.presentVersion());
} else { } else {
descriptionText = String.format("Mod %s (%s) depends on a Mod with id %s with a version matching %s. \nNo version is currently available.", e.source().id(), e.source().name(), e.dependency(), printVersionRange(e.range())); description = String.format("Mod %s (%s) depends on a Mod with id %s with a version matching %s. \nNo version is currently available.", e.source().id(), e.source().name(), e.dependency(), printVersionRange(e.range()));
} }
SwingWidget text = new SwingWidget(descriptionText); List<JButton> actions = new ArrayList<>();
if (e.link() != null) { if (e.link() != null) {
boolean install = e.link().endsWith(LoaderImpl.MOD_FILE_EXTENSION); boolean install = e.link().endsWith(LoaderImpl.MOD_FILE_EXTENSION);
String name = install ? "Install" : "Open mod page"; String name = install ? "Install" : "Open mod page";
@ -85,14 +88,14 @@ public class LoaderGui {
} }
} }
}); });
text.add(urlButton); // TODO ??? fix this bs actions.add(urlButton);
} }
pane.add(text); JPanel entry = getEntry(description, list.getBackground(), actions);
} entry.setSize(list.getWidth(), entry.getHeight());
pane.setSize(pane.getHeight(), frame.getWidth()); list.add(entry);
JScrollPane scroll = new JScrollPane(pane); });
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); pane.add(new JScrollPane(list));
frame.add(scroll); frame.add(pane);
}); });
public static ContentType<ModDependencyResolver.BreakingModException> INFO_BREAKING_DEP = new ContentType<>((frame, ex) -> { public static ContentType<ModDependencyResolver.BreakingModException> INFO_BREAKING_DEP = new ContentType<>((frame, ex) -> {
JPanel pane = new JPanel(new BorderLayout()); JPanel pane = new JPanel(new BorderLayout());
@ -105,15 +108,17 @@ public class LoaderGui {
pane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); pane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
pane.add(title, BorderLayout.NORTH); pane.add(title, BorderLayout.NORTH);
for (ModDependencyResolver.BreakingModException.Entry e : ex.getBreaks()) { JPanel list = new JPanel();
String descriptionText = String.format("Mod %s (%s) breaks with mod %s (%s) for versions matching %s \n(present: %s)", e.source().id(), e.source().name(), e.broken().id(), e.broken().name(), printVersionRange(e.range()), e.broken().version()); ex.getBreaks().forEach(e -> {
SwingWidget text = new SwingWidget(descriptionText); String description = String.format("Mod %s (%s) breaks with mod %s (%s) for versions matching %s \n(present: %s)", e.source().id(), e.source().name(), e.broken().id(), e.broken().name(), printVersionRange(e.range()), e.broken().version());
pane.add(text); List<JButton> actions = new ArrayList<>();
}
pane.setSize(pane.getHeight(), frame.getWidth()); JPanel entry = getEntry(description, list.getBackground(), actions);
JScrollPane scroll = new JScrollPane(pane); entry.setSize(list.getWidth(), entry.getHeight());
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); list.add(entry);
frame.add(scroll); });
pane.add(new JScrollPane(list));
frame.add(pane);
}); });
private final BiConsumer<JFrame, T> contentSetter; private final BiConsumer<JFrame, T> contentSetter;
@ -122,4 +127,19 @@ public class LoaderGui {
private static String printVersionRange(ModDependencyResolver.VersionRange range){ private static String printVersionRange(ModDependencyResolver.VersionRange range){
return "range: \n"+ range.toString().replace("||", "or")+" \n("+range+")"; return "range: \n"+ range.toString().replace("||", "or")+" \n("+range+")";
} }
private static JPanel getEntry(String description, Color background, List<JButton> actions){
JPanel entry = new JPanel(new BorderLayout());
entry.setBorder(BasicBorders.getInternalFrameBorder());
JTextPane text = new JTextPane();
text.setContentType("text/html");
text.setEditable(false);
text.setBackground(background);
text.setText("<html>" + description.replace("\n", "<br>") + "</html>");
JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
actions.forEach(actionPanel::add);
entry.add(text);
entry.add(actionPanel, BorderLayout.SOUTH);
return entry;
}
} }

View file

@ -1,60 +0,0 @@
package dev.frogmc.frogloader.impl.gui;
import javax.swing.*;
import java.awt.*;
public class SwingWidget extends JComponent {
private final String description;
public SwingWidget(String description) {
this.description = description;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets = getParent().getInsets();
Graphics graphics = g.create();
graphics.setColor(Color.LIGHT_GRAY);
int width = getWidth()-insets.left-insets.right;
graphics.fillRoundRect(getX(), getY(), width, 75, 32, 32);
graphics.setColor(Color.BLACK);
drawWrapped(graphics, description, getX()+5, getY()+20, width-20);
paintChildren(g);
}
private int drawWrapped(Graphics graphics, String text, int x, int y, int width){
int lineHeight = graphics.getFontMetrics().getHeight();
int lineY = y;
int lineX = x;
StringBuilder builder = new StringBuilder();
String[] split = text.split(" ");
for (int i = 0, splitLength = split.length; i < splitLength; i++) {
String s = split[i];
int rt = s.indexOf('\n');
if (rt != -1){
builder.append(s, 0, rt);
graphics.drawString(builder.toString(), lineX, lineY);
lineY+=lineHeight;
lineX= x+20;
builder.delete(0, builder.length());
builder.append(s.substring(rt)).append(" ");
continue;
}
builder.append(s).append(" ");
String next = i<splitLength-1 ? split[i + 1] : "";
if (graphics.getFontMetrics().stringWidth(builder+next) >= width) {
graphics.drawString(builder.toString(), lineX, lineY);
lineY += lineHeight;
lineX= x+20;
builder.delete(0, builder.length());
}
}
if (!builder.isEmpty()) {
graphics.drawString(builder.toString(), lineX, lineY);
}
return lineY+lineHeight;
}
}

View file

@ -13,9 +13,15 @@ public class URLUtil {
public static void open(URI url){ public static void open(URI url){
LOGGER.info("Opening: {}", url);
try { try {
Desktop.getDesktop().browse(url); if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
} catch (IOException e) { Desktop.getDesktop().browse(url);
} else {
throw new UnsupportedOperationException("Browse action is not supported");
}
} catch (IOException | UnsupportedOperationException e) {
ProcessBuilder builder = new ProcessBuilder("xdg-open", url.toString()); ProcessBuilder builder = new ProcessBuilder("xdg-open", url.toString());
try { try {
builder.start(); builder.start();