Skip to content

Commit

Permalink
Import settings
Browse files Browse the repository at this point in the history
  • Loading branch information
SJuliez committed Dec 28, 2023
1 parent aa40a0d commit 1528ef0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mostRecentNotFound=The most recent unit file could not be opened. Starting in MML's main UI instead.
noMostRecentUnit=There is no most recent unit. Starting in MML's main UI instead.
fileReadError=An error occurred while trying to read the file %s. This can happen for various reasons. The file may not exist, MML may lack the rights to read the file or the file may be malformed or not conform to its file extension. The error message is:\n\n%s
fileReadError=A problem occurred while trying to read the file %s. This can happen for various reasons. The file may not exist, MML may lack the rights to read the file or the file may be malformed or not conform to its file extension. The error message is:\n\n%s
invalidUnit=The unit is invalid for the reasons listed below and may load incorrectly!\n\n%s
validUnit=Validation Passed.
fileWriteError=A problem occurred while trying to write the file. The error message is:\n\n%s
Expand All @@ -9,4 +9,6 @@ lookAndFeelError=A problem occurred while trying to change the Java Look-and-Fee
uncaughtException=Uncaught %s detected. Please open up an issue containing all logs and the current unit file at https://github.com/MegaMek/megameklab/issues
loadUiError=A problem occurred while trying to create the unit UI, reverting to a new Mek!
locationFull=Could not add %s because the chosen location is full!
invalidLocation=%s can't be placed in %s!
invalidLocation=%s can't be placed in %s!
importSettingsHelp=To import settings, find the main megameklab directory of a previous setup of MML and select this directory (e.g. D:/BT_Stuff/megameklab04915). This directory should contain among others a directory called mmconf (D:/BT_Stuff/megameklab04915/mmconf) which in turn contains the file megameklab.properties.
settingsImported=The settings have been imported.\n\nPlease note that due to the way MegaMekLab uses some of MegaMek's settings not all settings can currently be imported. The settings that are not imported are, e.g., the user directory and display settings for the unit preview.\n\nIt is strongly advised to close and restart MML. Some settings will only take effect after restarting MML.
37 changes: 31 additions & 6 deletions megameklab/src/megameklab/ui/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import megamek.common.templates.TROView;
import megameklab.MMLConstants;
import megameklab.ui.dialog.MMLFileChooser;
import megameklab.ui.dialog.UiLoader;
import megameklab.ui.dialog.MegaMekLabUnitSelectorDialog;
import megameklab.ui.dialog.PrintQueueDialog;
import megameklab.ui.dialog.UiLoader;
import megameklab.ui.dialog.settings.SettingsDialog;
import megameklab.util.CConfig;
import megameklab.util.ImageHelper;
Expand Down Expand Up @@ -67,6 +67,12 @@ public MenuBar(MenuBarOwner owner) {
initialize();
}

/**
* Returns the unit main UI, if this menubar is attached to one (instead of the StartupGUI
* aka splash screen), null otherwise.
*
* @return The unit main UI of this menubar or null
*/
public @Nullable MegaMekLabMainUI getUnitMainUi() {
if (owner instanceof MegaMekLabMainUI) {
return (MegaMekLabMainUI) owner;
Expand Down Expand Up @@ -1246,6 +1252,12 @@ private void loadUnitFromFile(int fileNumber) {
}
}

/**
* Adds a new most recent unit, moving the previous recent units down the list. Renews this
* menubar to reflect the change.
*
* @param latestUnit The filename of the new most recent unit.
*/
private void newRecentUnit(String latestUnit) {
CConfig.setMostRecentFile(latestUnit);
createFileMenu();
Expand All @@ -1270,19 +1282,32 @@ private boolean isUnitGui() {
return !isStartupGui();
}

/**
* Refreshes this menubar. At least this updates the file menu, showing the latest recent unit
* changes.
*/
public void refreshMenuBar() {
createFileMenu();
}

private void importSettings() {
/**
* Performs a settings import, currently only for the megameklab.properties file (CConfig).
* Shows a help message before showing a file chooser for selecting a directory. The
* directory should always be MML's main directory (which contains the mmconf directory
* with the megameklab.properties file).
*/
public void importSettings() {
PopupMessages.showSettingsImportHelp(owner.getFrame());

MMLFileChooser fileChooser = new MMLFileChooser();
fileChooser.setDialogTitle(resources.getString("dialog.importSettings"));
fileChooser.setFileFilter(new FileNameExtensionFilter("MML Settings", "properties"));
fileChooser.setSelectedFile(new File("megameklab.properties"));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showOpenDialog(owner.getFrame());
if ((result != JFileChooser.APPROVE_OPTION) || (fileChooser.getSelectedFile() == null)) {
if ((result != JFileChooser.APPROVE_OPTION) || (fileChooser.getSelectedFile() == null)
|| !fileChooser.getSelectedFile().isDirectory()) {
return;
}
CConfig.importSettings(fileChooser.getSelectedFile());
File settingsFile = new File(fileChooser.getSelectedFile(), CConfig.CONFIG_FILE);
CConfig.importSettings(owner, settingsFile);
}
}
19 changes: 15 additions & 4 deletions megameklab/src/megameklab/ui/MenuBarOwner.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,25 @@ default void newUnit(long type, boolean primitive) {
}

/**
* Sets the look and feel for the application.
* Sets the look and feel for the application and lets Swing update the current
* components.
*
* @param plaf The look and feel to use for the application.
* @param lookAndFeelInfo The look and feel to use for the application.
*/
default void changeTheme(UIManager.LookAndFeelInfo plaf) {
default void changeTheme(UIManager.LookAndFeelInfo lookAndFeelInfo) {
changeTheme(lookAndFeelInfo.getClassName());
}

/**
* Sets the look and feel for the application and lets Swing update the current
* components.
* *
* @param lookAndFeelInfo The name of the look and feel to use for the application.
*/
default void changeTheme(String lookAndFeelInfo) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(plaf.getClassName());
UIManager.setLookAndFeel(lookAndFeelInfo);
SwingUtilities.updateComponentTreeUI(getFrame());
} catch (Exception ex) {
PopupMessages.showLookAndFeelError(getFrame(), ex.getMessage());
Expand Down
8 changes: 8 additions & 0 deletions megameklab/src/megameklab/ui/PopupMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public static void showNoMostRecentUnitError(Component parent) {
showInfoMessage(parent, resources.getString("noMostRecentUnit"));
}

public static void showSettingsImported(Component parent) {
showInfoMessage(parent, resources.getString("settingsImported"));
}

public static void showSettingsImportHelp(Component parent) {
showInfoMessage(parent, resources.getString("importSettingsHelp"));
}

public static void showUiLoadError(Component parent) {
showErrorMessage(parent, resources.getString("loadUiError"));
}
Expand Down
22 changes: 21 additions & 1 deletion megameklab/src/megameklab/ui/StartupGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.ResourceBundle;
import java.util.TreeMap;

import static javax.swing.JOptionPane.YES_NO_OPTION;

/**
* A startup splash screen for MegaMekLab
* @author Taharqa
Expand All @@ -59,9 +61,27 @@ public StartupGUI() {
initComponents();
}

@Override
public void setVisible(boolean aFlag) {
super.setVisible(aFlag);
if (aFlag && CConfig.getBooleanParam(CConfig.NAG_IMPORT_SETTINGS)) {
showImportSettingsDialog();
}
}

private void showImportSettingsDialog() {
CConfig.setParam(CConfig.NAG_IMPORT_SETTINGS, Boolean.toString(false));
int choice = JOptionPane.showConfirmDialog(this,
"Do you wish to import settings from another MML" +
"? You can also do this later from the main menu.", "Import Settings?", YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
mmlMenuBar.importSettings();
}
}

private void initComponents() {
SkinSpecification skinSpec = SkinXMLHandler.getSkin(UIComponents.MainMenuBorder.getComp(), true);

frame = new JFrame("MegaMekLab");
setBackground(UIManager.getColor("controlHighlight"));
mmlMenuBar = new MenuBar(this);
Expand Down
24 changes: 22 additions & 2 deletions megameklab/src/megameklab/util/CConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import megamek.common.Configuration;
import megameklab.ui.MMLStartUp;
import megameklab.ui.MegaMekLabMainUI;
import megameklab.ui.MenuBarOwner;
import megameklab.ui.PopupMessages;
import megameklab.ui.battleArmor.BAMainUI;
import megameklab.ui.combatVehicle.CVMainUI;
import megameklab.ui.fighterAero.ASMainUI;
Expand Down Expand Up @@ -99,6 +101,7 @@ public final class CConfig {
public static final String RS_SCALE_UNITS = "rs_scale_units";

public static final String NAG_EQUIPMENT_CTRLCLICK = "nag_equipment_ctrlclick";
public static final String NAG_IMPORT_SETTINGS = "nag_import_settings";

public static final String MEK_AUTOFILL = "mekAutofill";
public static final String MEK_AUTOSORT = "mekAutosort";
Expand Down Expand Up @@ -131,6 +134,7 @@ private static Properties getDefaults() {
defaults.setProperty(FILE_LAST_DIRECTORY, Configuration.unitsDir().toString());
defaults.setProperty(FILE_CHOOSER_WINDOW, "");
defaults.setProperty(MISC_STARTUP, MMLStartUp.SPLASH_SCREEN.name());
defaults.setProperty(NAG_IMPORT_SETTINGS, Boolean.toString(true));
return defaults;
}

Expand All @@ -142,12 +146,23 @@ public static void load() {
loadConfigFile();
}

public static void importSettings(File settingsFile) {
/**
* Tries to import settings from the given properties file. When successful, also applies
* some of the settings and shows a popup message.
*
* @param menuBarOwner The MenuBar owner frame calling this
* @param settingsFile The file (should always be megameklab.properties in another MML install)
*/
public static void importSettings(MenuBarOwner menuBarOwner, File settingsFile) {
try (FileInputStream fis = new FileInputStream(settingsFile)) {
config.load(fis);
} catch (Exception ex) {
PopupMessages.showFileReadError(menuBarOwner.getFrame(), settingsFile.toString(), ex.getMessage());
LogManager.getLogger().error("", ex);
return;
}
applyImportedSettings(menuBarOwner);
PopupMessages.showSettingsImported(menuBarOwner.getFrame());
}

/**
Expand Down Expand Up @@ -453,6 +468,7 @@ private static void writeWindowSettings(String cconfigSetting, Component compone
Dimension size = component.getSize();
Point pos = component.getLocation();
setParam(cconfigSetting, pos.x + ";" + pos.y + ";" + size.width + ";" + size.height);
saveConfig();
}

private static String settingForMainUi(MegaMekLabMainUI ui) {
Expand All @@ -477,6 +493,10 @@ private static String settingForMainUi(MegaMekLabMainUI ui) {
}
}

private CConfig() {
private static void applyImportedSettings(MenuBarOwner menuBarOwner) {
menuBarOwner.changeTheme(getParam(GUI_PLAF));
menuBarOwner.refreshAll();
}

private CConfig() { }
}

0 comments on commit 1528ef0

Please sign in to comment.