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;
import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import dev.frogmc.frogloader.impl.LoaderImpl;
@ -63,15 +66,15 @@ public class LoaderGui {
pane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
pane.add(title, BorderLayout.NORTH);
for (ModDependencyResolver.UnfulfilledDependencyException.Entry e : ex.getDependencies()) {
String descriptionText;
JPanel list = new JPanel();
ex.getDependencies().forEach(e -> {
String description;
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 {
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) {
boolean install = e.link().endsWith(LoaderImpl.MOD_FILE_EXTENSION);
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);
}
pane.setSize(pane.getHeight(), frame.getWidth());
JScrollPane scroll = new JScrollPane(pane);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scroll);
JPanel entry = getEntry(description, list.getBackground(), actions);
entry.setSize(list.getWidth(), entry.getHeight());
list.add(entry);
});
pane.add(new JScrollPane(list));
frame.add(pane);
});
public static ContentType<ModDependencyResolver.BreakingModException> INFO_BREAKING_DEP = new ContentType<>((frame, ex) -> {
JPanel pane = new JPanel(new BorderLayout());
@ -105,15 +108,17 @@ public class LoaderGui {
pane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
pane.add(title, BorderLayout.NORTH);
for (ModDependencyResolver.BreakingModException.Entry e : ex.getBreaks()) {
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());
SwingWidget text = new SwingWidget(descriptionText);
pane.add(text);
}
pane.setSize(pane.getHeight(), frame.getWidth());
JScrollPane scroll = new JScrollPane(pane);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scroll);
JPanel list = new JPanel();
ex.getBreaks().forEach(e -> {
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());
List<JButton> actions = new ArrayList<>();
JPanel entry = getEntry(description, list.getBackground(), actions);
entry.setSize(list.getWidth(), entry.getHeight());
list.add(entry);
});
pane.add(new JScrollPane(list));
frame.add(pane);
});
private final BiConsumer<JFrame, T> contentSetter;
@ -122,4 +127,19 @@ public class LoaderGui {
private static String printVersionRange(ModDependencyResolver.VersionRange 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){
LOGGER.info("Opening: {}", url);
try {
if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(url);
} catch (IOException e) {
} else {
throw new UnsupportedOperationException("Browse action is not supported");
}
} catch (IOException | UnsupportedOperationException e) {
ProcessBuilder builder = new ProcessBuilder("xdg-open", url.toString());
try {
builder.start();