diff options
| author | 2020-06-03 20:00:40 +0200 | |
|---|---|---|
| committer | 2020-06-03 19:00:40 +0100 | |
| commit | 687c14632455d2ccc2a14e5d8bc98347621a45ce (patch) | |
| tree | ef49ee3e253ca6e9f5b9561fb4325a371352e149 | |
| parent | Fix publishing to maven (diff) | |
| download | enigma-687c14632455d2ccc2a14e5d8bc98347621a45ce.tar.gz enigma-687c14632455d2ccc2a14e5d8bc98347621a45ce.tar.xz enigma-687c14632455d2ccc2a14e5d8bc98347621a45ce.zip | |
Refactor MenuBar (#251)
* Make MenuBar not suck
* Remove unneeded generated menu item lists
| -rw-r--r-- | enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java | 23 | ||||
| -rw-r--r-- | enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java | 666 |
2 files changed, 337 insertions, 352 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java index 2ed1010f..c67fc5b3 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java | |||
| @@ -340,7 +340,7 @@ public class Gui { | |||
| 340 | 340 | ||
| 341 | // init menus | 341 | // init menus |
| 342 | this.menuBar = new MenuBar(this); | 342 | this.menuBar = new MenuBar(this); |
| 343 | this.frame.setJMenuBar(this.menuBar); | 343 | this.frame.setJMenuBar(this.menuBar.getUi()); |
| 344 | 344 | ||
| 345 | // init status bar | 345 | // init status bar |
| 346 | statusBar = new JPanel(new BorderLayout()); | 346 | statusBar = new JPanel(new BorderLayout()); |
| @@ -1025,18 +1025,7 @@ public class Gui { | |||
| 1025 | * causing inconsistencies. | 1025 | * causing inconsistencies. |
| 1026 | */ | 1026 | */ |
| 1027 | public void updateUiState() { | 1027 | public void updateUiState() { |
| 1028 | menuBar.connectToServerMenu.setEnabled(isJarOpen && connectionState != ConnectionState.HOSTING); | 1028 | menuBar.updateUiState(); |
| 1029 | menuBar.connectToServerMenu.setText(I18n.translate(connectionState != ConnectionState.CONNECTED ? "menu.collab.connect" : "menu.collab.disconnect")); | ||
| 1030 | menuBar.startServerMenu.setEnabled(isJarOpen && connectionState != ConnectionState.CONNECTED); | ||
| 1031 | menuBar.startServerMenu.setText(I18n.translate(connectionState != ConnectionState.HOSTING ? "menu.collab.server.start" : "menu.collab.server.stop")); | ||
| 1032 | |||
| 1033 | menuBar.closeJarMenu.setEnabled(isJarOpen); | ||
| 1034 | menuBar.openMappingsMenus.forEach(item -> item.setEnabled(isJarOpen)); | ||
| 1035 | menuBar.saveMappingsMenu.setEnabled(isJarOpen && enigmaMappingsFileChooser.getSelectedFile() != null && connectionState != ConnectionState.CONNECTED); | ||
| 1036 | menuBar.saveMappingsMenus.forEach(item -> item.setEnabled(isJarOpen)); | ||
| 1037 | menuBar.closeMappingsMenu.setEnabled(isJarOpen); | ||
| 1038 | menuBar.exportSourceMenu.setEnabled(isJarOpen); | ||
| 1039 | menuBar.exportJarMenu.setEnabled(isJarOpen); | ||
| 1040 | 1029 | ||
| 1041 | connectionStatusLabel.setText(I18n.translate(connectionState == ConnectionState.NOT_CONNECTED ? "status.disconnected" : "status.connected")); | 1030 | connectionStatusLabel.setText(I18n.translate(connectionState == ConnectionState.NOT_CONNECTED ? "status.disconnected" : "status.connected")); |
| 1042 | 1031 | ||
| @@ -1055,4 +1044,12 @@ public class Gui { | |||
| 1055 | updateUiState(); | 1044 | updateUiState(); |
| 1056 | } | 1045 | } |
| 1057 | 1046 | ||
| 1047 | public boolean isJarOpen() { | ||
| 1048 | return isJarOpen; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | public ConnectionState getConnectionState() { | ||
| 1052 | return this.connectionState; | ||
| 1053 | } | ||
| 1054 | |||
| 1058 | } | 1055 | } |
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java index 24f42ff0..948798ad 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java | |||
| @@ -1,18 +1,5 @@ | |||
| 1 | package cuchaz.enigma.gui.elements; | 1 | package cuchaz.enigma.gui.elements; |
| 2 | 2 | ||
| 3 | import cuchaz.enigma.gui.config.Config; | ||
| 4 | import cuchaz.enigma.gui.config.Themes; | ||
| 5 | import cuchaz.enigma.gui.Gui; | ||
| 6 | import cuchaz.enigma.gui.dialog.AboutDialog; | ||
| 7 | import cuchaz.enigma.gui.dialog.ChangeDialog; | ||
| 8 | import cuchaz.enigma.gui.dialog.ConnectToServerDialog; | ||
| 9 | import cuchaz.enigma.gui.dialog.CreateServerDialog; | ||
| 10 | import cuchaz.enigma.gui.dialog.StatsDialog; | ||
| 11 | import cuchaz.enigma.gui.util.ScaleUtil; | ||
| 12 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; | ||
| 13 | import cuchaz.enigma.utils.I18n; | ||
| 14 | import cuchaz.enigma.utils.Pair; | ||
| 15 | |||
| 16 | import java.awt.Desktop; | 3 | import java.awt.Desktop; |
| 17 | import java.awt.event.InputEvent; | 4 | import java.awt.event.InputEvent; |
| 18 | import java.awt.event.KeyEvent; | 5 | import java.awt.event.KeyEvent; |
| @@ -23,364 +10,365 @@ import java.net.URL; | |||
| 23 | import java.nio.file.Files; | 10 | import java.nio.file.Files; |
| 24 | import java.nio.file.Path; | 11 | import java.nio.file.Path; |
| 25 | import java.nio.file.Paths; | 12 | import java.nio.file.Paths; |
| 26 | import java.util.*; | 13 | import java.util.Arrays; |
| 27 | import java.util.List; | 14 | import java.util.Locale; |
| 15 | import java.util.Map; | ||
| 28 | import java.util.stream.Collectors; | 16 | import java.util.stream.Collectors; |
| 29 | import java.util.stream.IntStream; | 17 | import java.util.stream.IntStream; |
| 18 | |||
| 30 | import javax.swing.*; | 19 | import javax.swing.*; |
| 31 | 20 | ||
| 32 | public class MenuBar extends JMenuBar { | 21 | import cuchaz.enigma.gui.ConnectionState; |
| 33 | 22 | import cuchaz.enigma.gui.Gui; | |
| 34 | public final JMenuItem closeJarMenu; | 23 | import cuchaz.enigma.gui.config.Config; |
| 35 | public final List<JMenuItem> openMappingsMenus; | 24 | import cuchaz.enigma.gui.config.Themes; |
| 36 | public final JMenuItem saveMappingsMenu; | 25 | import cuchaz.enigma.gui.dialog.*; |
| 37 | public final List<JMenuItem> saveMappingsMenus; | 26 | import cuchaz.enigma.gui.util.ScaleUtil; |
| 38 | public final JMenuItem closeMappingsMenu; | 27 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; |
| 39 | public final JMenuItem dropMappingsMenu; | 28 | import cuchaz.enigma.utils.I18n; |
| 40 | public final JMenuItem exportSourceMenu; | 29 | import cuchaz.enigma.utils.Pair; |
| 41 | public final JMenuItem exportJarMenu; | 30 | |
| 42 | public final JMenuItem connectToServerMenu; | 31 | public class MenuBar { |
| 43 | public final JMenuItem startServerMenu; | 32 | |
| 33 | private final JMenuBar ui = new JMenuBar(); | ||
| 34 | |||
| 35 | private final JMenu fileMenu = new JMenu(I18n.translate("menu.file")); | ||
| 36 | private final JMenuItem jarOpenItem = new JMenuItem(I18n.translate("menu.file.jar.open")); | ||
| 37 | private final JMenuItem jarCloseItem = new JMenuItem(I18n.translate("menu.file.jar.close")); | ||
| 38 | private final JMenu openMenu = new JMenu(I18n.translate("menu.file.mappings.open")); | ||
| 39 | private final JMenuItem saveMappingsItem = new JMenuItem(I18n.translate("menu.file.mappings.save")); | ||
| 40 | private final JMenu saveMappingsAsMenu = new JMenu(I18n.translate("menu.file.mappings.save_as")); | ||
| 41 | private final JMenuItem closeMappingsItem = new JMenuItem(I18n.translate("menu.file.mappings.close")); | ||
| 42 | private final JMenuItem dropMappingsItem = new JMenuItem(I18n.translate("menu.file.mappings.drop")); | ||
| 43 | private final JMenuItem exportSourceItem = new JMenuItem(I18n.translate("menu.file.export.source")); | ||
| 44 | private final JMenuItem exportJarItem = new JMenuItem(I18n.translate("menu.file.export.jar")); | ||
| 45 | private final JMenuItem statsItem = new JMenuItem(I18n.translate("menu.file.stats")); | ||
| 46 | private final JMenuItem exitItem = new JMenuItem(I18n.translate("menu.file.exit")); | ||
| 47 | |||
| 48 | private final JMenu decompilerMenu = new JMenu(I18n.translate("menu.decompiler")); | ||
| 49 | |||
| 50 | private final JMenu viewMenu = new JMenu(I18n.translate("menu.view")); | ||
| 51 | private final JMenu themesMenu = new JMenu(I18n.translate("menu.view.themes")); | ||
| 52 | private final JMenu languagesMenu = new JMenu(I18n.translate("menu.view.languages")); | ||
| 53 | private final JMenu scaleMenu = new JMenu(I18n.translate("menu.view.scale")); | ||
| 54 | private final JMenuItem customScaleItem = new JMenuItem(I18n.translate("menu.view.scale.custom")); | ||
| 55 | private final JMenuItem searchItem = new JMenuItem(I18n.translate("menu.view.search")); | ||
| 56 | |||
| 57 | private final JMenu collabMenu = new JMenu(I18n.translate("menu.collab")); | ||
| 58 | private final JMenuItem connectItem = new JMenuItem(I18n.translate("menu.collab.connect")); | ||
| 59 | private final JMenuItem startServerItem = new JMenuItem(I18n.translate("menu.collab.server.start")); | ||
| 60 | |||
| 61 | private final JMenu helpMenu = new JMenu(I18n.translate("menu.help")); | ||
| 62 | private final JMenuItem aboutItem = new JMenuItem(I18n.translate("menu.help.about")); | ||
| 63 | private final JMenuItem githubItem = new JMenuItem(I18n.translate("menu.help.github")); | ||
| 64 | |||
| 44 | private final Gui gui; | 65 | private final Gui gui; |
| 45 | 66 | ||
| 46 | public MenuBar(Gui gui) { | 67 | public MenuBar(Gui gui) { |
| 47 | this.gui = gui; | 68 | this.gui = gui; |
| 48 | 69 | ||
| 49 | /* | 70 | prepareOpenMenu(this.openMenu, gui); |
| 50 | * File menu | 71 | prepareSaveMappingsAsMenu(this.saveMappingsAsMenu, this.saveMappingsItem, gui); |
| 51 | */ | 72 | prepareDecompilerMenu(this.decompilerMenu, gui); |
| 52 | { | 73 | prepareThemesMenu(this.themesMenu, gui); |
| 53 | JMenu menu = new JMenu(I18n.translate("menu.file")); | 74 | prepareLanguagesMenu(this.languagesMenu, gui); |
| 54 | this.add(menu); | 75 | prepareScaleMenu(this.scaleMenu, gui); |
| 55 | { | ||
| 56 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.jar.open")); | ||
| 57 | menu.add(item); | ||
| 58 | item.addActionListener(event -> { | ||
| 59 | this.gui.jarFileChooser.setVisible(true); | ||
| 60 | String file = this.gui.jarFileChooser.getFile(); | ||
| 61 | // checks if the file name is not empty | ||
| 62 | if (file != null) { | ||
| 63 | Path path = Paths.get(this.gui.jarFileChooser.getDirectory()).resolve(file); | ||
| 64 | // checks if the file name corresponds to an existing file | ||
| 65 | if (Files.exists(path)) { | ||
| 66 | gui.getController().openJar(path); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | }); | ||
| 70 | } | ||
| 71 | { | ||
| 72 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.jar.close")); | ||
| 73 | menu.add(item); | ||
| 74 | item.addActionListener(event -> this.gui.getController().closeJar()); | ||
| 75 | this.closeJarMenu = item; | ||
| 76 | } | ||
| 77 | menu.addSeparator(); | ||
| 78 | JMenu openMenu = new JMenu(I18n.translate("menu.file.mappings.open")); | ||
| 79 | menu.add(openMenu); | ||
| 80 | { | ||
| 81 | openMappingsMenus = new ArrayList<>(); | ||
| 82 | for (MappingFormat format : MappingFormat.values()) { | ||
| 83 | if (format.getReader() != null) { | ||
| 84 | JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); | ||
| 85 | openMenu.add(item); | ||
| 86 | item.addActionListener(event -> { | ||
| 87 | if (this.gui.enigmaMappingsFileChooser.showOpenDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 88 | File selectedFile = this.gui.enigmaMappingsFileChooser.getSelectedFile(); | ||
| 89 | this.gui.getController().openMappings(format, selectedFile.toPath()); | ||
| 90 | } | ||
| 91 | }); | ||
| 92 | openMappingsMenus.add(item); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | { | ||
| 97 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.mappings.save")); | ||
| 98 | menu.add(item); | ||
| 99 | item.addActionListener(event -> { | ||
| 100 | this.gui.getController().saveMappings(this.gui.enigmaMappingsFileChooser.getSelectedFile().toPath()); | ||
| 101 | }); | ||
| 102 | item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK)); | ||
| 103 | this.saveMappingsMenu = item; | ||
| 104 | } | ||
| 105 | JMenu saveMenu = new JMenu(I18n.translate("menu.file.mappings.save_as")); | ||
| 106 | menu.add(saveMenu); | ||
| 107 | { | ||
| 108 | saveMappingsMenus = new ArrayList<>(); | ||
| 109 | for (MappingFormat format : MappingFormat.values()) { | ||
| 110 | if (format.getWriter() != null) { | ||
| 111 | JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); | ||
| 112 | saveMenu.add(item); | ||
| 113 | item.addActionListener(event -> { | ||
| 114 | // TODO: Use a specific file chooser for it | ||
| 115 | if (this.gui.enigmaMappingsFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 116 | this.gui.getController().saveMappings(this.gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format); | ||
| 117 | this.saveMappingsMenu.setEnabled(true); | ||
| 118 | } | ||
| 119 | }); | ||
| 120 | saveMappingsMenus.add(item); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | { | ||
| 125 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.mappings.close")); | ||
| 126 | menu.add(item); | ||
| 127 | item.addActionListener(event -> { | ||
| 128 | if (this.gui.getController().isDirty()) { | ||
| 129 | this.gui.showDiscardDiag((response -> { | ||
| 130 | if (response == JOptionPane.YES_OPTION) { | ||
| 131 | gui.saveMapping(); | ||
| 132 | this.gui.getController().closeMappings(); | ||
| 133 | } else if (response == JOptionPane.NO_OPTION) | ||
| 134 | this.gui.getController().closeMappings(); | ||
| 135 | return null; | ||
| 136 | }), I18n.translate("prompt.close.save"), I18n.translate("prompt.close.discard"), I18n.translate("prompt.close.cancel")); | ||
| 137 | } else | ||
| 138 | this.gui.getController().closeMappings(); | ||
| 139 | 76 | ||
| 140 | }); | 77 | this.fileMenu.add(this.jarOpenItem); |
| 141 | this.closeMappingsMenu = item; | 78 | this.fileMenu.add(this.jarCloseItem); |
| 142 | } | 79 | this.fileMenu.addSeparator(); |
| 143 | { | 80 | this.fileMenu.add(this.openMenu); |
| 144 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.mappings.drop")); | 81 | this.fileMenu.add(this.saveMappingsAsMenu); |
| 145 | menu.add(item); | 82 | this.fileMenu.add(this.closeMappingsItem); |
| 146 | item.addActionListener(event -> this.gui.getController().dropMappings()); | 83 | this.fileMenu.add(this.dropMappingsItem); |
| 147 | this.dropMappingsMenu = item; | 84 | this.fileMenu.addSeparator(); |
| 85 | this.fileMenu.add(this.exportSourceItem); | ||
| 86 | this.fileMenu.add(this.exportJarItem); | ||
| 87 | this.fileMenu.addSeparator(); | ||
| 88 | this.fileMenu.add(this.statsItem); | ||
| 89 | this.fileMenu.addSeparator(); | ||
| 90 | this.fileMenu.add(this.exitItem); | ||
| 91 | this.ui.add(this.fileMenu); | ||
| 92 | |||
| 93 | this.ui.add(this.decompilerMenu); | ||
| 94 | |||
| 95 | this.viewMenu.add(this.themesMenu); | ||
| 96 | this.viewMenu.add(this.languagesMenu); | ||
| 97 | this.scaleMenu.add(this.customScaleItem); | ||
| 98 | this.viewMenu.add(this.scaleMenu); | ||
| 99 | this.viewMenu.addSeparator(); | ||
| 100 | this.viewMenu.add(this.searchItem); | ||
| 101 | this.ui.add(this.viewMenu); | ||
| 102 | |||
| 103 | this.collabMenu.add(this.connectItem); | ||
| 104 | this.collabMenu.add(this.startServerItem); | ||
| 105 | this.ui.add(this.collabMenu); | ||
| 106 | |||
| 107 | this.helpMenu.add(this.aboutItem); | ||
| 108 | this.helpMenu.add(this.githubItem); | ||
| 109 | this.ui.add(this.helpMenu); | ||
| 110 | |||
| 111 | this.saveMappingsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK)); | ||
| 112 | this.searchItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.SHIFT_DOWN_MASK)); | ||
| 113 | |||
| 114 | this.jarOpenItem.addActionListener(_e -> this.onOpenJarClicked()); | ||
| 115 | this.jarCloseItem.addActionListener(_e -> this.gui.getController().closeJar()); | ||
| 116 | this.saveMappingsItem.addActionListener(_e -> this.onSaveMappingsClicked()); | ||
| 117 | this.closeMappingsItem.addActionListener(_e -> this.onCloseMappingsClicked()); | ||
| 118 | this.dropMappingsItem.addActionListener(_e -> this.gui.getController().dropMappings()); | ||
| 119 | this.exportSourceItem.addActionListener(_e -> this.onExportSourceClicked()); | ||
| 120 | this.exportJarItem.addActionListener(_e -> this.onExportJarClicked()); | ||
| 121 | this.statsItem.addActionListener(_e -> StatsDialog.show(this.gui)); | ||
| 122 | this.exitItem.addActionListener(_e -> this.gui.close()); | ||
| 123 | this.customScaleItem.addActionListener(_e -> this.onCustomScaleClicked()); | ||
| 124 | this.searchItem.addActionListener(_e -> this.onSearchClicked()); | ||
| 125 | this.connectItem.addActionListener(_e -> this.onConnectClicked()); | ||
| 126 | this.startServerItem.addActionListener(_e -> this.onStartServerClicked()); | ||
| 127 | this.aboutItem.addActionListener(_e -> AboutDialog.show(this.gui.getFrame())); | ||
| 128 | this.githubItem.addActionListener(_e -> this.onGithubClicked()); | ||
| 129 | } | ||
| 130 | |||
| 131 | public void updateUiState() { | ||
| 132 | boolean jarOpen = this.gui.isJarOpen(); | ||
| 133 | ConnectionState connectionState = this.gui.getConnectionState(); | ||
| 134 | |||
| 135 | this.connectItem.setEnabled(jarOpen && connectionState != ConnectionState.HOSTING); | ||
| 136 | this.connectItem.setText(I18n.translate(connectionState != ConnectionState.CONNECTED ? "menu.collab.connect" : "menu.collab.disconnect")); | ||
| 137 | this.startServerItem.setEnabled(jarOpen && connectionState != ConnectionState.CONNECTED); | ||
| 138 | this.startServerItem.setText(I18n.translate(connectionState != ConnectionState.HOSTING ? "menu.collab.server.start" : "menu.collab.server.stop")); | ||
| 139 | |||
| 140 | this.jarCloseItem.setEnabled(jarOpen); | ||
| 141 | this.openMenu.setEnabled(jarOpen); | ||
| 142 | this.saveMappingsItem.setEnabled(jarOpen && this.gui.enigmaMappingsFileChooser.getSelectedFile() != null && connectionState != ConnectionState.CONNECTED); | ||
| 143 | this.saveMappingsAsMenu.setEnabled(jarOpen); | ||
| 144 | this.closeMappingsItem.setEnabled(jarOpen); | ||
| 145 | this.exportSourceItem.setEnabled(jarOpen); | ||
| 146 | this.exportJarItem.setEnabled(jarOpen); | ||
| 147 | } | ||
| 148 | |||
| 149 | public JMenuBar getUi() { | ||
| 150 | return this.ui; | ||
| 151 | } | ||
| 152 | |||
| 153 | private void onOpenJarClicked() { | ||
| 154 | this.gui.jarFileChooser.setVisible(true); | ||
| 155 | String file = this.gui.jarFileChooser.getFile(); | ||
| 156 | // checks if the file name is not empty | ||
| 157 | if (file != null) { | ||
| 158 | Path path = Paths.get(this.gui.jarFileChooser.getDirectory()).resolve(file); | ||
| 159 | // checks if the file name corresponds to an existing file | ||
| 160 | if (Files.exists(path)) { | ||
| 161 | this.gui.getController().openJar(path); | ||
| 148 | } | 162 | } |
| 149 | menu.addSeparator(); | 163 | } |
| 150 | { | 164 | } |
| 151 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.export.source")); | 165 | |
| 152 | menu.add(item); | 166 | private void onSaveMappingsClicked() { |
| 167 | this.gui.getController().saveMappings(this.gui.enigmaMappingsFileChooser.getSelectedFile().toPath()); | ||
| 168 | } | ||
| 169 | |||
| 170 | private void onCloseMappingsClicked() { | ||
| 171 | if (this.gui.getController().isDirty()) { | ||
| 172 | this.gui.showDiscardDiag((response -> { | ||
| 173 | if (response == JOptionPane.YES_OPTION) { | ||
| 174 | this.gui.saveMapping(); | ||
| 175 | this.gui.getController().closeMappings(); | ||
| 176 | } else if (response == JOptionPane.NO_OPTION) | ||
| 177 | this.gui.getController().closeMappings(); | ||
| 178 | return null; | ||
| 179 | }), I18n.translate("prompt.close.save"), I18n.translate("prompt.close.discard"), I18n.translate("prompt.close.cancel")); | ||
| 180 | } else { | ||
| 181 | this.gui.getController().closeMappings(); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | private void onExportSourceClicked() { | ||
| 186 | if (this.gui.exportSourceFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | ||
| 187 | this.gui.getController().exportSource(this.gui.exportSourceFileChooser.getSelectedFile().toPath()); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | private void onExportJarClicked() { | ||
| 192 | this.gui.exportJarFileChooser.setVisible(true); | ||
| 193 | if (this.gui.exportJarFileChooser.getFile() != null) { | ||
| 194 | Path path = Paths.get(this.gui.exportJarFileChooser.getDirectory(), this.gui.exportJarFileChooser.getFile()); | ||
| 195 | this.gui.getController().exportJar(path); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | private void onCustomScaleClicked() { | ||
| 200 | String answer = (String) JOptionPane.showInputDialog(this.gui.getFrame(), I18n.translate("menu.view.scale.custom.title"), I18n.translate("menu.view.scale.custom.title"), | ||
| 201 | JOptionPane.QUESTION_MESSAGE, null, null, Float.toString(ScaleUtil.getScaleFactor() * 100)); | ||
| 202 | if (answer == null) return; | ||
| 203 | float newScale = 1.0f; | ||
| 204 | try { | ||
| 205 | newScale = Float.parseFloat(answer) / 100f; | ||
| 206 | } catch (NumberFormatException ignored) { | ||
| 207 | } | ||
| 208 | ScaleUtil.setScaleFactor(newScale); | ||
| 209 | ChangeDialog.show(this.gui); | ||
| 210 | } | ||
| 211 | |||
| 212 | private void onSearchClicked() { | ||
| 213 | if (this.gui.getController().project != null) { | ||
| 214 | this.gui.getSearchDialog().show(); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | private void onConnectClicked() { | ||
| 219 | if (this.gui.getController().getClient() != null) { | ||
| 220 | this.gui.getController().disconnectIfConnected(null); | ||
| 221 | return; | ||
| 222 | } | ||
| 223 | ConnectToServerDialog.Result result = ConnectToServerDialog.show(this.gui.getFrame()); | ||
| 224 | if (result == null) { | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | this.gui.getController().disconnectIfConnected(null); | ||
| 228 | try { | ||
| 229 | this.gui.getController().createClient(result.getUsername(), result.getIp(), result.getPort(), result.getPassword()); | ||
| 230 | } catch (IOException e) { | ||
| 231 | JOptionPane.showMessageDialog(this.gui.getFrame(), e.toString(), I18n.translate("menu.collab.connect.error"), JOptionPane.ERROR_MESSAGE); | ||
| 232 | this.gui.getController().disconnectIfConnected(null); | ||
| 233 | } | ||
| 234 | Arrays.fill(result.getPassword(), (char) 0); | ||
| 235 | } | ||
| 236 | |||
| 237 | private void onStartServerClicked() { | ||
| 238 | if (this.gui.getController().getServer() != null) { | ||
| 239 | this.gui.getController().disconnectIfConnected(null); | ||
| 240 | return; | ||
| 241 | } | ||
| 242 | CreateServerDialog.Result result = CreateServerDialog.show(this.gui.getFrame()); | ||
| 243 | if (result == null) { | ||
| 244 | return; | ||
| 245 | } | ||
| 246 | this.gui.getController().disconnectIfConnected(null); | ||
| 247 | try { | ||
| 248 | this.gui.getController().createServer(result.getPort(), result.getPassword()); | ||
| 249 | } catch (IOException e) { | ||
| 250 | JOptionPane.showMessageDialog(this.gui.getFrame(), e.toString(), I18n.translate("menu.collab.server.start.error"), JOptionPane.ERROR_MESSAGE); | ||
| 251 | this.gui.getController().disconnectIfConnected(null); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | private void onGithubClicked() { | ||
| 256 | try { | ||
| 257 | Desktop.getDesktop().browse(new URL("https://github.com/FabricMC/Enigma").toURI()); | ||
| 258 | } catch (URISyntaxException | IOException ignored) { | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | private static void prepareOpenMenu(JMenu openMenu, Gui gui) { | ||
| 263 | for (MappingFormat format : MappingFormat.values()) { | ||
| 264 | if (format.getReader() != null) { | ||
| 265 | JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); | ||
| 153 | item.addActionListener(event -> { | 266 | item.addActionListener(event -> { |
| 154 | if (this.gui.exportSourceFileChooser.showSaveDialog(this.gui.getFrame()) == JFileChooser.APPROVE_OPTION) { | 267 | if (gui.enigmaMappingsFileChooser.showOpenDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) { |
| 155 | this.gui.getController().exportSource(this.gui.exportSourceFileChooser.getSelectedFile().toPath()); | 268 | File selectedFile = gui.enigmaMappingsFileChooser.getSelectedFile(); |
| 269 | gui.getController().openMappings(format, selectedFile.toPath()); | ||
| 156 | } | 270 | } |
| 157 | }); | 271 | }); |
| 158 | this.exportSourceMenu = item; | 272 | openMenu.add(item); |
| 159 | } | 273 | } |
| 160 | { | 274 | } |
| 161 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.export.jar")); | 275 | } |
| 162 | menu.add(item); | 276 | |
| 277 | private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuItem saveMappingsItem, Gui gui) { | ||
| 278 | for (MappingFormat format : MappingFormat.values()) { | ||
| 279 | if (format.getWriter() != null) { | ||
| 280 | JMenuItem item = new JMenuItem(I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT))); | ||
| 163 | item.addActionListener(event -> { | 281 | item.addActionListener(event -> { |
| 164 | this.gui.exportJarFileChooser.setVisible(true); | 282 | // TODO: Use a specific file chooser for it |
| 165 | if (this.gui.exportJarFileChooser.getFile() != null) { | 283 | if (gui.enigmaMappingsFileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) { |
| 166 | Path path = Paths.get(this.gui.exportJarFileChooser.getDirectory(), this.gui.exportJarFileChooser.getFile()); | 284 | gui.getController().saveMappings(gui.enigmaMappingsFileChooser.getSelectedFile().toPath(), format); |
| 167 | this.gui.getController().exportJar(path); | 285 | saveMappingsItem.setEnabled(true); |
| 168 | } | 286 | } |
| 169 | }); | 287 | }); |
| 170 | this.exportJarMenu = item; | 288 | saveMappingsAsMenu.add(item); |
| 171 | } | ||
| 172 | menu.addSeparator(); | ||
| 173 | { | ||
| 174 | JMenuItem stats = new JMenuItem(I18n.translate("menu.file.stats")); | ||
| 175 | menu.add(stats); | ||
| 176 | stats.addActionListener(event -> StatsDialog.show(this.gui)); | ||
| 177 | } | ||
| 178 | menu.addSeparator(); | ||
| 179 | { | ||
| 180 | JMenuItem item = new JMenuItem(I18n.translate("menu.file.exit")); | ||
| 181 | menu.add(item); | ||
| 182 | item.addActionListener(event -> this.gui.close()); | ||
| 183 | } | 289 | } |
| 184 | } | 290 | } |
| 291 | } | ||
| 185 | 292 | ||
| 186 | /* | 293 | private static void prepareDecompilerMenu(JMenu decompilerMenu, Gui gui) { |
| 187 | * Decompiler menu | 294 | ButtonGroup decompilerGroup = new ButtonGroup(); |
| 188 | */ | ||
| 189 | { | ||
| 190 | JMenu menu = new JMenu(I18n.translate("menu.decompiler")); | ||
| 191 | this.add(menu); | ||
| 192 | 295 | ||
| 193 | ButtonGroup decompilerGroup = new ButtonGroup(); | 296 | for (Config.Decompiler decompiler : Config.Decompiler.values()) { |
| 297 | JRadioButtonMenuItem decompilerButton = new JRadioButtonMenuItem(decompiler.name); | ||
| 298 | decompilerGroup.add(decompilerButton); | ||
| 299 | if (decompiler.equals(Config.getInstance().decompiler)) { | ||
| 300 | decompilerButton.setSelected(true); | ||
| 301 | } | ||
| 302 | decompilerButton.addActionListener(event -> { | ||
| 303 | gui.getController().setDecompiler(decompiler.service); | ||
| 194 | 304 | ||
| 195 | for (Config.Decompiler decompiler : Config.Decompiler.values()) { | 305 | try { |
| 196 | JRadioButtonMenuItem decompilerButton = new JRadioButtonMenuItem(decompiler.name); | 306 | Config.getInstance().decompiler = decompiler; |
| 197 | decompilerGroup.add(decompilerButton); | 307 | Config.getInstance().saveConfig(); |
| 198 | if (decompiler.equals(Config.getInstance().decompiler)) { | 308 | } catch (IOException e) { |
| 199 | decompilerButton.setSelected(true); | 309 | throw new RuntimeException(e); |
| 200 | } | 310 | } |
| 201 | menu.add(decompilerButton); | 311 | }); |
| 202 | decompilerButton.addActionListener(event -> { | 312 | decompilerMenu.add(decompilerButton); |
| 203 | gui.getController().setDecompiler(decompiler.service); | ||
| 204 | |||
| 205 | try { | ||
| 206 | Config.getInstance().decompiler = decompiler; | ||
| 207 | Config.getInstance().saveConfig(); | ||
| 208 | } catch (IOException e) { | ||
| 209 | throw new RuntimeException(e); | ||
| 210 | } | ||
| 211 | }); | ||
| 212 | } | ||
| 213 | } | 313 | } |
| 314 | } | ||
| 214 | 315 | ||
| 215 | /* | 316 | private static void prepareThemesMenu(JMenu themesMenu, Gui gui) { |
| 216 | * View menu | 317 | ButtonGroup themeGroup = new ButtonGroup(); |
| 217 | */ | 318 | for (Config.LookAndFeel lookAndFeel : Config.LookAndFeel.values()) { |
| 218 | { | 319 | JRadioButtonMenuItem themeButton = new JRadioButtonMenuItem(I18n.translate("menu.view.themes." + lookAndFeel.name().toLowerCase(Locale.ROOT))); |
| 219 | JMenu menu = new JMenu(I18n.translate("menu.view")); | 320 | themeGroup.add(themeButton); |
| 220 | this.add(menu); | 321 | if (lookAndFeel.equals(Config.getInstance().lookAndFeel)) { |
| 221 | { | 322 | themeButton.setSelected(true); |
| 222 | JMenu themes = new JMenu(I18n.translate("menu.view.themes")); | ||
| 223 | menu.add(themes); | ||
| 224 | ButtonGroup themeGroup = new ButtonGroup(); | ||
| 225 | for (Config.LookAndFeel lookAndFeel : Config.LookAndFeel.values()) { | ||
| 226 | JRadioButtonMenuItem themeButton = new JRadioButtonMenuItem(I18n.translate("menu.view.themes." + lookAndFeel.name().toLowerCase(Locale.ROOT))); | ||
| 227 | themeGroup.add(themeButton); | ||
| 228 | if (lookAndFeel.equals(Config.getInstance().lookAndFeel)) { | ||
| 229 | themeButton.setSelected(true); | ||
| 230 | } | ||
| 231 | themes.add(themeButton); | ||
| 232 | themeButton.addActionListener(event -> Themes.setLookAndFeel(gui, lookAndFeel)); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | { | ||
| 236 | JMenu languages = new JMenu(I18n.translate("menu.view.languages")); | ||
| 237 | menu.add(languages); | ||
| 238 | ButtonGroup languageGroup = new ButtonGroup(); | ||
| 239 | for (String lang : I18n.getAvailableLanguages()) { | ||
| 240 | JRadioButtonMenuItem languageButton = new JRadioButtonMenuItem(I18n.getLanguageName(lang)); | ||
| 241 | languageGroup.add(languageButton); | ||
| 242 | if (lang.equals(Config.getInstance().language)) { | ||
| 243 | languageButton.setSelected(true); | ||
| 244 | } | ||
| 245 | languages.add(languageButton); | ||
| 246 | languageButton.addActionListener(event -> { | ||
| 247 | I18n.setLanguage(lang); | ||
| 248 | ChangeDialog.show(this.gui); | ||
| 249 | }); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | { | ||
| 253 | JMenu scale = new JMenu(I18n.translate("menu.view.scale")); | ||
| 254 | { | ||
| 255 | ButtonGroup scaleGroup = new ButtonGroup(); | ||
| 256 | Map<Float, JRadioButtonMenuItem> map = IntStream.of(100, 125, 150, 175, 200) | ||
| 257 | .mapToObj(scaleFactor -> { | ||
| 258 | float realScaleFactor = scaleFactor / 100f; | ||
| 259 | JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(String.format("%d%%", scaleFactor)); | ||
| 260 | menuItem.addActionListener(event -> ScaleUtil.setScaleFactor(realScaleFactor)); | ||
| 261 | menuItem.addActionListener(event -> ChangeDialog.show(this.gui)); | ||
| 262 | scaleGroup.add(menuItem); | ||
| 263 | scale.add(menuItem); | ||
| 264 | return new Pair<>(realScaleFactor, menuItem); | ||
| 265 | }) | ||
| 266 | .collect(Collectors.toMap(x -> x.a, x -> x.b)); | ||
| 267 | |||
| 268 | JMenuItem customScale = new JMenuItem(I18n.translate("menu.view.scale.custom")); | ||
| 269 | customScale.addActionListener(event -> { | ||
| 270 | String answer = (String) JOptionPane.showInputDialog(gui.getFrame(), I18n.translate("menu.view.scale.custom.title"), I18n.translate("menu.view.scale.custom.title"), | ||
| 271 | JOptionPane.QUESTION_MESSAGE, null, null, Float.toString(ScaleUtil.getScaleFactor() * 100)); | ||
| 272 | if (answer == null) return; | ||
| 273 | float newScale = 1.0f; | ||
| 274 | try { | ||
| 275 | newScale = Float.parseFloat(answer) / 100f; | ||
| 276 | } catch (NumberFormatException ignored) { | ||
| 277 | } | ||
| 278 | ScaleUtil.setScaleFactor(newScale); | ||
| 279 | ChangeDialog.show(this.gui); | ||
| 280 | }); | ||
| 281 | scale.add(customScale); | ||
| 282 | ScaleUtil.addListener((newScale, _oldScale) -> { | ||
| 283 | JRadioButtonMenuItem mi = map.get(newScale); | ||
| 284 | if (mi != null) { | ||
| 285 | mi.setSelected(true); | ||
| 286 | } else { | ||
| 287 | scaleGroup.clearSelection(); | ||
| 288 | } | ||
| 289 | }); | ||
| 290 | JRadioButtonMenuItem mi = map.get(ScaleUtil.getScaleFactor()); | ||
| 291 | if (mi != null) { | ||
| 292 | mi.setSelected(true); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | menu.add(scale); | ||
| 296 | } | ||
| 297 | menu.addSeparator(); | ||
| 298 | { | ||
| 299 | JMenuItem search = new JMenuItem(I18n.translate("menu.view.search")); | ||
| 300 | search.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.SHIFT_MASK)); | ||
| 301 | menu.add(search); | ||
| 302 | search.addActionListener(event -> { | ||
| 303 | if (this.gui.getController().project != null) { | ||
| 304 | this.gui.getSearchDialog().show(); | ||
| 305 | } | ||
| 306 | }); | ||
| 307 | } | 323 | } |
| 324 | themeButton.addActionListener(_e -> Themes.setLookAndFeel(gui, lookAndFeel)); | ||
| 325 | themesMenu.add(themeButton); | ||
| 308 | } | 326 | } |
| 327 | } | ||
| 309 | 328 | ||
| 310 | /* | 329 | private static void prepareLanguagesMenu(JMenu languagesMenu, Gui gui) { |
| 311 | * Collab menu | 330 | ButtonGroup languageGroup = new ButtonGroup(); |
| 312 | */ | 331 | for (String lang : I18n.getAvailableLanguages()) { |
| 313 | { | 332 | JRadioButtonMenuItem languageButton = new JRadioButtonMenuItem(I18n.getLanguageName(lang)); |
| 314 | JMenu menu = new JMenu(I18n.translate("menu.collab")); | 333 | languageGroup.add(languageButton); |
| 315 | this.add(menu); | 334 | if (lang.equals(Config.getInstance().language)) { |
| 316 | { | 335 | languageButton.setSelected(true); |
| 317 | JMenuItem item = new JMenuItem(I18n.translate("menu.collab.connect")); | ||
| 318 | menu.add(item); | ||
| 319 | item.addActionListener(event -> { | ||
| 320 | if (this.gui.getController().getClient() != null) { | ||
| 321 | this.gui.getController().disconnectIfConnected(null); | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | ConnectToServerDialog.Result result = ConnectToServerDialog.show(this.gui.getFrame()); | ||
| 325 | if (result == null) { | ||
| 326 | return; | ||
| 327 | } | ||
| 328 | this.gui.getController().disconnectIfConnected(null); | ||
| 329 | try { | ||
| 330 | this.gui.getController().createClient(result.getUsername(), result.getIp(), result.getPort(), result.getPassword()); | ||
| 331 | } catch (IOException e) { | ||
| 332 | JOptionPane.showMessageDialog(this.gui.getFrame(), e.toString(), I18n.translate("menu.collab.connect.error"), JOptionPane.ERROR_MESSAGE); | ||
| 333 | this.gui.getController().disconnectIfConnected(null); | ||
| 334 | } | ||
| 335 | Arrays.fill(result.getPassword(), (char)0); | ||
| 336 | }); | ||
| 337 | this.connectToServerMenu = item; | ||
| 338 | } | ||
| 339 | { | ||
| 340 | JMenuItem item = new JMenuItem(I18n.translate("menu.collab.server.start")); | ||
| 341 | menu.add(item); | ||
| 342 | item.addActionListener(event -> { | ||
| 343 | if (this.gui.getController().getServer() != null) { | ||
| 344 | this.gui.getController().disconnectIfConnected(null); | ||
| 345 | return; | ||
| 346 | } | ||
| 347 | CreateServerDialog.Result result = CreateServerDialog.show(this.gui.getFrame()); | ||
| 348 | if (result == null) { | ||
| 349 | return; | ||
| 350 | } | ||
| 351 | this.gui.getController().disconnectIfConnected(null); | ||
| 352 | try { | ||
| 353 | this.gui.getController().createServer(result.getPort(), result.getPassword()); | ||
| 354 | } catch (IOException e) { | ||
| 355 | JOptionPane.showMessageDialog(this.gui.getFrame(), e.toString(), I18n.translate("menu.collab.server.start.error"), JOptionPane.ERROR_MESSAGE); | ||
| 356 | this.gui.getController().disconnectIfConnected(null); | ||
| 357 | } | ||
| 358 | }); | ||
| 359 | this.startServerMenu = item; | ||
| 360 | } | 336 | } |
| 337 | languageButton.addActionListener(event -> { | ||
| 338 | I18n.setLanguage(lang); | ||
| 339 | ChangeDialog.show(gui); | ||
| 340 | }); | ||
| 341 | languagesMenu.add(languageButton); | ||
| 361 | } | 342 | } |
| 343 | } | ||
| 362 | 344 | ||
| 363 | /* | 345 | private static void prepareScaleMenu(JMenu scaleMenu, Gui gui) { |
| 364 | * Help menu | 346 | ButtonGroup scaleGroup = new ButtonGroup(); |
| 365 | */ | 347 | Map<Float, JRadioButtonMenuItem> scaleButtons = IntStream.of(100, 125, 150, 175, 200) |
| 366 | { | 348 | .mapToObj(scaleFactor -> { |
| 367 | JMenu menu = new JMenu(I18n.translate("menu.help")); | 349 | float realScaleFactor = scaleFactor / 100f; |
| 368 | this.add(menu); | 350 | JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(String.format("%d%%", scaleFactor)); |
| 369 | { | 351 | menuItem.addActionListener(event -> ScaleUtil.setScaleFactor(realScaleFactor)); |
| 370 | JMenuItem item = new JMenuItem(I18n.translate("menu.help.about")); | 352 | menuItem.addActionListener(event -> ChangeDialog.show(gui)); |
| 371 | menu.add(item); | 353 | scaleGroup.add(menuItem); |
| 372 | item.addActionListener(event -> AboutDialog.show(this.gui.getFrame())); | 354 | scaleMenu.add(menuItem); |
| 373 | } | 355 | return new Pair<>(realScaleFactor, menuItem); |
| 374 | { | 356 | }) |
| 375 | JMenuItem item = new JMenuItem(I18n.translate("menu.help.github")); | 357 | .collect(Collectors.toMap(x -> x.a, x -> x.b)); |
| 376 | menu.add(item); | 358 | |
| 377 | item.addActionListener(event -> { | 359 | JRadioButtonMenuItem currentScaleButton = scaleButtons.get(ScaleUtil.getScaleFactor()); |
| 378 | try { | 360 | if (currentScaleButton != null) { |
| 379 | Desktop.getDesktop().browse(new URL("https://github.com/FabricMC/Enigma").toURI()); | 361 | currentScaleButton.setSelected(true); |
| 380 | } catch (URISyntaxException | IOException ignored) { | ||
| 381 | } | ||
| 382 | }); | ||
| 383 | } | ||
| 384 | } | 362 | } |
| 363 | |||
| 364 | ScaleUtil.addListener((newScale, _oldScale) -> { | ||
| 365 | JRadioButtonMenuItem mi = scaleButtons.get(newScale); | ||
| 366 | if (mi != null) { | ||
| 367 | mi.setSelected(true); | ||
| 368 | } else { | ||
| 369 | scaleGroup.clearSelection(); | ||
| 370 | } | ||
| 371 | }); | ||
| 385 | } | 372 | } |
| 373 | |||
| 386 | } | 374 | } |