1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2024-11-30 13:13:58 +01:00

Improve creator tools plugin menu

This commit is contained in:
Henry Le Grys 2021-03-29 01:57:22 +01:00
parent 66b7b44ac8
commit 652d6bf5ec
3 changed files with 37 additions and 11 deletions

View File

@ -62,6 +62,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class PackManagerController { public class PackManagerController {
@ -111,8 +112,6 @@ public class PackManagerController {
} }
public void show() { public void show() {
initPluginMenu();
frame.setVisible(true); frame.setVisible(true);
frame.setTitle("Modpack Creator - [" + workspaceDir.getAbsolutePath() + "]"); frame.setTitle("Modpack Creator - [" + workspaceDir.getAbsolutePath() + "]");
@ -319,18 +318,38 @@ public class PackManagerController {
config.setUserFiles(userFiles); config.setUserFiles(userFiles);
} }
private void initPluginMenu() { private void refreshPluginMenu() {
JMenu pluginsMenu = frame.getPluginsMenu(); JMenu pluginsMenu = frame.getPluginsMenu();
pluginsMenu.removeAll();
for (CreatorPluginWrapper<?> wrapper : creator.getPlugins()) { if (frame.getPackTable().getSelectedRow() == -1) {
pluginsMenu.add("No pack selected.").setEnabled(false);
return;
}
pluginsMenu.add("Enabled Plugins...").addActionListener(e -> frame.getEditPluginsMenuItem().doClick());
pluginsMenu.addSeparator();
Optional<Pack> pack = getSelectedPack(true);
List<CreatorPluginWrapper<?>> enabledPlugins = pack.transform(p ->
creator.getPlugins().stream().filter(wrapper ->
p.getEnabledPlugins().contains(wrapper.getInfo().getId())).collect(Collectors.toList()
)
).or(creator.getPlugins());
if (enabledPlugins.isEmpty()) {
pluginsMenu.add("No plugins enabled").setEnabled(false);
return;
}
for (CreatorPluginWrapper<?> wrapper : enabledPlugins) {
CreatorToolsPlugin plugin = wrapper.getInstance(); CreatorToolsPlugin plugin = wrapper.getInstance();
JMenu submenu = new JMenu(plugin.getName()); JMenu submenu = new JMenu(plugin.getName());
for (PluginMenu menu : plugin.getPluginMenus()) { for (PluginMenu menu : plugin.getPluginMenus()) {
JMenuItem item = new JMenuItem(menu.getTitle()); JMenuItem item = new JMenuItem(menu.getTitle());
item.addActionListener(e -> { item.addActionListener(e -> {
Optional<Pack> pack = getSelectedPack(true);
if (menu.requiresPack() && !pack.isPresent()) { if (menu.requiresPack() && !pack.isPresent()) {
SwingHelper.showErrorDialog(frame, "You must select a pack first", "Error"); SwingHelper.showErrorDialog(frame, "You must select a pack first", "Error");
return; return;
@ -358,6 +377,10 @@ public class PackManagerController {
} }
}); });
frame.getPackTable().getSelectionModel().addListSelectionListener(e -> {
refreshPluginMenu();
});
frame.getPackTable().addMouseListener(new MouseAdapter() { frame.getPackTable().addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 2) { if (e.getClickCount() == 2) {
@ -502,6 +525,7 @@ public class PackManagerController {
if (optional.isPresent()) { if (optional.isPresent()) {
PluginSelectionDialog.showPluginDialog(frame, creator, optional.get()); PluginSelectionDialog.showPluginDialog(frame, creator, optional.get());
updatePackInWorkspace(optional.get()); updatePackInWorkspace(optional.get());
refreshPluginMenu();
} }
}); });

View File

@ -26,8 +26,9 @@ public class PluginSelectionDialog extends JDialog {
this.plugins = creator.getPlugins().stream() this.plugins = creator.getPlugins().stream()
.map(wrapper -> { .map(wrapper -> {
PluginsTableModel.PluginModel model = new PluginsTableModel.PluginModel(wrapper.getInfo().getId()); PluginsTableModel.PluginModel model = new PluginsTableModel.PluginModel(wrapper.getInfo().getId(),
model.setEnabled(pack.getEnabledPlugins().contains(model.getPluginId())); wrapper.getInstance().getName());
model.setEnabled(pack.getEnabledPlugins().contains(model.getId()));
return model; return model;
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -65,7 +66,7 @@ public class PluginSelectionDialog extends JDialog {
Set<String> enabled = dialog.plugins.stream() Set<String> enabled = dialog.plugins.stream()
.filter(PluginsTableModel.PluginModel::isEnabled) .filter(PluginsTableModel.PluginModel::isEnabled)
.map(PluginsTableModel.PluginModel::getPluginId) .map(PluginsTableModel.PluginModel::getId)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
pack.setEnabledPlugins(enabled); pack.setEnabledPlugins(enabled);

View File

@ -25,7 +25,7 @@ public class PluginsTableModel extends AbstractTableModel {
if (columnIndex == 0) { if (columnIndex == 0) {
return plugins.get(rowIndex).isEnabled(); return plugins.get(rowIndex).isEnabled();
} else { } else {
return plugins.get(rowIndex).getPluginId(); return plugins.get(rowIndex).getName();
} }
} }
@ -64,7 +64,8 @@ public class PluginsTableModel extends AbstractTableModel {
@Data @Data
public static class PluginModel { public static class PluginModel {
private final String pluginId; private final String id;
private final String name;
private boolean enabled; private boolean enabled;
} }
} }