Skip to content

Commit

Permalink
Updates GUI when user selects a language
Browse files Browse the repository at this point in the history
  • Loading branch information
edimoral committed Mar 8, 2024
1 parent 411b4e1 commit 9583a46
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
44 changes: 24 additions & 20 deletions src/main/java/jpass/ui/JPassFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JTable;
Expand Down Expand Up @@ -126,8 +125,8 @@ private JPassFrame(String fileName, Locale locale) {
LOG.log(Level.CONFIG, "Could not set application icon.", e);
}

setLocalizedMessages(locale);
setSupportedLanguages();
setLocalizedMessages(locale);

this.toolBar = new JToolBar();
this.toolBar.setFloatable(false);
Expand Down Expand Up @@ -199,13 +198,13 @@ private JPassFrame(String fileName, Locale locale) {
this.settingsMenu.setMnemonic(KeyEvent.VK_S);

JMenu languageMenu = new JMenu(localizedMessages.getString(SETTINGS_MENU_LANGUAGE));
languageMenu.setActionCommand(SETTINGS_MENU_LANGUAGE);

SUPPORTED_LANGUAGES.forEach((key, value) -> {
JMenuItem language = new JMenuItem(localizedMessages.getString(value));
JMenuItem language = new JMenuItem(localizedMessages.getString(key));
language.setActionCommand(key);
language.addActionListener(e -> {
String command = e.getActionCommand();
refreshComponentsWithLanguage(command);
refreshComponentsWithLanguage(e.getActionCommand());
});
languageMenu.add(language);
});
Expand Down Expand Up @@ -343,11 +342,9 @@ public void refreshAll() {
refreshEntryTitleList(null);
}

public void refreshComponentsWithLanguage(String localeTag) {
Locale locale = Locale.forLanguageTag(localeTag);

private void refreshComponentsWithLanguage(String actionCommand) {
Locale locale = Locale.forLanguageTag(getSupportedLanguages().get(actionCommand));
setLocalizedMessages(locale);
setSupportedLanguages();

fileMenu.setText(localizedMessages.getString(FILE_MENU));
editMenu.setText(localizedMessages.getString(EDIT_MENU));
Expand All @@ -364,14 +361,17 @@ public void refreshComponentsWithLanguage(String localeTag) {
refreshEntryTitleList(null);
}

public void updateMenuComponents(JMenu menu) {
private void updateMenuComponents(JMenu menu) {
for (int i = 0; i < menu.getItemCount(); i++) {
JMenuItem item = menu.getItem(i);
if (null != item && null != item.getAction()) {
String actionName = (String) item.getAction().getValue(Action.NAME);
if (null != actionName) {
LOG.log(Level.INFO, String.format("ActionName: %s", actionName));
item.setText(localizedMessages.getString(actionName));
if (null != item) {
if (null != item.getActionCommand()) {
LOG.log(Level.INFO, String.format("ActionCommand: %s ", item.getActionCommand()));
item.setText(localizedMessages.getString(item.getActionCommand()));
}

if (item instanceof JMenu) {
updateMenuComponents((JMenu) item);
}
}
}
Expand Down Expand Up @@ -454,10 +454,14 @@ public static ResourceBundle getLocalizedMessages() {
return localizedMessages;
}

public static void setSupportedLanguages() {
SUPPORTED_LANGUAGES.put("en-US", LANGUAGE_EN_US);
SUPPORTED_LANGUAGES.put("es-MX", LANGUAGE_ES_MX);
SUPPORTED_LANGUAGES.put("hu-HU", LANGUAGE_HU_HU);
SUPPORTED_LANGUAGES.put("it-IT", LANGUAGE_IT_IT);
private static void setSupportedLanguages() {
SUPPORTED_LANGUAGES.put(LANGUAGE_EN_US, "en-US");
SUPPORTED_LANGUAGES.put(LANGUAGE_ES_MX, "es-MX");
SUPPORTED_LANGUAGES.put(LANGUAGE_HU_HU, "hu-HU");
SUPPORTED_LANGUAGES.put(LANGUAGE_IT_IT, "it-IT");
}

private static Map<String, String> getSupportedLanguages() {
return SUPPORTED_LANGUAGES;
}
}
10 changes: 6 additions & 4 deletions src/main/java/jpass/ui/action/AbstractMenuAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ public abstract class AbstractMenuAction extends AbstractAction {
/**
* Creates a new menu action.
*
* @param text title of the action that appears on UI
* @param icon icon of action
* @param accelerator accelerator key
* @param text title of the action that appears on UI
* @param actionCommandKey
* @param icon icon of action
* @param accelerator accelerator key
*/
public AbstractMenuAction(String text, Icon icon, KeyStroke accelerator) {
public AbstractMenuAction(String text, String actionCommandKey, Icon icon, KeyStroke accelerator) {
super(text, icon);
putValue(ACTION_COMMAND_KEY, actionCommandKey);
putValue(SHORT_DESCRIPTION, text);
if (accelerator != null) {
putValue(ACCELERATOR_KEY, accelerator);
Expand Down
46 changes: 23 additions & 23 deletions src/main/java/jpass/ui/action/MenuActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import jpass.ui.JPassFrame;
import jpass.ui.MessageDialog;
import jpass.ui.helper.EntryHelper;
import jpass.util.Constants;
import jpass.xml.bind.Entry;

import static javax.swing.KeyStroke.getKeyStroke;
Expand All @@ -56,8 +55,6 @@
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import static java.awt.event.InputEvent.ALT_DOWN_MASK;

import static jpass.util.Constants.PASSWORD_PASSWORD_NOT_MODIFIED;
import static jpass.util.Constants.PASSWORD_SUCCESSFULLY_MODIFIED;
import static jpass.util.Constants.EDIT_MENU_ADD_ENTRY;
import static jpass.util.Constants.EDIT_MENU_CLEAR_CLIPBOARD;
import static jpass.util.Constants.EDIT_MENU_COPY_PASSWORD;
Expand All @@ -71,11 +68,14 @@
import static jpass.util.Constants.FILE_MENU_EXIT;
import static jpass.util.Constants.FILE_MENU_EXPORT_TO_XML;
import static jpass.util.Constants.FILE_MENU_IMPORT_FROM_XML;
import static jpass.util.Constants.FILE_MENU_OPEN_FILE;
import static jpass.util.Constants.FILE_MENU_NEW;
import static jpass.util.Constants.FILE_MENU_SAVE;
import static jpass.util.Constants.FILE_MENU_SAVE_AS;
import static jpass.util.Constants.HELP_MENU_ABOUT_JPASS;
import static jpass.util.Constants.HELP_MENU_LICENSE;
import static jpass.util.Constants.PASSWORD_PASSWORD_NOT_MODIFIED;
import static jpass.util.Constants.PASSWORD_SUCCESSFULLY_MODIFIED;
import static jpass.util.Constants.TOOLS_MENU_GENERATE_PASSWORD;

/**
Expand All @@ -85,43 +85,43 @@
*
*/
public enum MenuActionType {
NEW_FILE(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_NEW), getIcon("new"), getKeyStroke(KeyEvent.VK_N, CTRL_DOWN_MASK)) {
NEW_FILE(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_NEW), FILE_MENU_NEW, getIcon("new"), getKeyStroke(KeyEvent.VK_N, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
createNew(JPassFrame.getInstance());
}
}),
OPEN_FILE(new AbstractMenuAction(getLocalizedMessages().getString(Constants.FILE_MENU_OPEN_FILE), getIcon("open"), getKeyStroke(KeyEvent.VK_O, CTRL_DOWN_MASK)) {
OPEN_FILE(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_OPEN_FILE), FILE_MENU_OPEN_FILE, getIcon("open"), getKeyStroke(KeyEvent.VK_O, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
openFile(JPassFrame.getInstance());
}
}),
SAVE_FILE(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_SAVE), getIcon("save"), getKeyStroke(KeyEvent.VK_S, CTRL_DOWN_MASK)) {
SAVE_FILE(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_SAVE), FILE_MENU_SAVE, getIcon("save"), getKeyStroke(KeyEvent.VK_S, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
saveFile(JPassFrame.getInstance(), false);
}
}),
SAVE_AS_FILE(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_SAVE_AS), getIcon("save_as"), null) {
SAVE_AS_FILE(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_SAVE_AS), FILE_MENU_SAVE_AS, getIcon("save_as"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
saveFile(JPassFrame.getInstance(), true);
}
}),
EXPORT_XML(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_EXPORT_TO_XML), getIcon("export"), null) {
EXPORT_XML(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_EXPORT_TO_XML), FILE_MENU_EXPORT_TO_XML, getIcon("export"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
exportFile(JPassFrame.getInstance());
}
}),
IMPORT_XML(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_IMPORT_FROM_XML), getIcon("import"), null) {
IMPORT_XML(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_IMPORT_FROM_XML), FILE_MENU_IMPORT_FROM_XML, getIcon("import"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
importFile(JPassFrame.getInstance());
}
}),
CHANGE_PASSWORD(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_CHANGE_PASSWORD), getIcon("lock"), null) {
CHANGE_PASSWORD(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_CHANGE_PASSWORD), FILE_MENU_CHANGE_PASSWORD, getIcon("lock"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame parent = JPassFrame.getInstance();
Expand All @@ -137,19 +137,19 @@ public void actionPerformed(ActionEvent ev) {
}
}
}),
GENERATE_PASSWORD(new AbstractMenuAction(getLocalizedMessages().getString(TOOLS_MENU_GENERATE_PASSWORD), getIcon("generate"), getKeyStroke(KeyEvent.VK_Z, CTRL_DOWN_MASK)) {
GENERATE_PASSWORD(new AbstractMenuAction(getLocalizedMessages().getString(TOOLS_MENU_GENERATE_PASSWORD), TOOLS_MENU_GENERATE_PASSWORD, getIcon("generate"), getKeyStroke(KeyEvent.VK_Z, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
new GeneratePasswordDialog(JPassFrame.getInstance());
}
}),
EXIT(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_EXIT), getIcon("exit"), getKeyStroke(KeyEvent.VK_F4, ALT_DOWN_MASK)) {
EXIT(new AbstractMenuAction(getLocalizedMessages().getString(FILE_MENU_EXIT), FILE_MENU_EXIT, getIcon("exit"), getKeyStroke(KeyEvent.VK_F4, ALT_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame.getInstance().exitFrame();
}
}),
ABOUT(new AbstractMenuAction(getLocalizedMessages().getString(HELP_MENU_ABOUT_JPASS), getIcon("info"), getKeyStroke(KeyEvent.VK_F1, 0)) {
ABOUT(new AbstractMenuAction(getLocalizedMessages().getString(HELP_MENU_ABOUT_JPASS), HELP_MENU_ABOUT_JPASS, getIcon("info"), getKeyStroke(KeyEvent.VK_F1, 0)) {
@Override
public void actionPerformed(ActionEvent ev) {
StringBuilder sb = new StringBuilder();
Expand All @@ -162,37 +162,37 @@ public void actionPerformed(ActionEvent ev) {
MessageDialog.showInformationMessage(JPassFrame.getInstance(), sb.toString());
}
}),
LICENSE(new AbstractMenuAction(getLocalizedMessages().getString(HELP_MENU_LICENSE), getIcon("license"), null) {
LICENSE(new AbstractMenuAction(getLocalizedMessages().getString(HELP_MENU_LICENSE), HELP_MENU_LICENSE, getIcon("license"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
MessageDialog.showTextFile(JPassFrame.getInstance(), getLocalizedMessages().getString(HELP_MENU_LICENSE), "license.txt");
}
}),
ADD_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_ADD_ENTRY), getIcon("entry_new"), getKeyStroke(KeyEvent.VK_Y, CTRL_DOWN_MASK)) {
ADD_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_ADD_ENTRY), EDIT_MENU_ADD_ENTRY, getIcon("entry_new"), getKeyStroke(KeyEvent.VK_Y, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
EntryHelper.addEntry(JPassFrame.getInstance());
}
}),
EDIT_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_EDIT_ENTRY), getIcon("entry_edit"), getKeyStroke(KeyEvent.VK_E, CTRL_DOWN_MASK)) {
EDIT_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_EDIT_ENTRY), EDIT_MENU_EDIT_ENTRY, getIcon("entry_edit"), getKeyStroke(KeyEvent.VK_E, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
EntryHelper.editEntry(JPassFrame.getInstance());
}
}),
DUPLICATE_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_DUPLICATE_ENTRY), getIcon("entry_duplicate"), getKeyStroke(KeyEvent.VK_K, CTRL_DOWN_MASK)) {
DUPLICATE_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_DUPLICATE_ENTRY), EDIT_MENU_DUPLICATE_ENTRY, getIcon("entry_duplicate"), getKeyStroke(KeyEvent.VK_K, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
EntryHelper.duplicateEntry(JPassFrame.getInstance());
}
}),
DELETE_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_DELETE_ENTRY), getIcon("entry_delete"), getKeyStroke(KeyEvent.VK_D, CTRL_DOWN_MASK)) {
DELETE_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_DELETE_ENTRY), EDIT_MENU_DELETE_ENTRY, getIcon("entry_delete"), getKeyStroke(KeyEvent.VK_D, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
EntryHelper.deleteEntry(JPassFrame.getInstance());
}
}),
COPY_URL(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_COPY_URL), getIcon("url"), getKeyStroke(KeyEvent.VK_U, CTRL_DOWN_MASK)) {
COPY_URL(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_COPY_URL), EDIT_MENU_COPY_URL, getIcon("url"), getKeyStroke(KeyEvent.VK_U, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame parent = JPassFrame.getInstance();
Expand All @@ -202,7 +202,7 @@ public void actionPerformed(ActionEvent ev) {
}
}
}),
COPY_USER(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_COPY_USERNAME), getIcon("user"), getKeyStroke(KeyEvent.VK_B, CTRL_DOWN_MASK)) {
COPY_USER(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_COPY_USERNAME), EDIT_MENU_COPY_USERNAME, getIcon("user"), getKeyStroke(KeyEvent.VK_B, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame parent = JPassFrame.getInstance();
Expand All @@ -212,7 +212,7 @@ public void actionPerformed(ActionEvent ev) {
}
}
}),
COPY_PASSWORD(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_COPY_PASSWORD), getIcon("keyring"), getKeyStroke(KeyEvent.VK_C, CTRL_DOWN_MASK)) {
COPY_PASSWORD(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_COPY_PASSWORD), EDIT_MENU_COPY_PASSWORD, getIcon("keyring"), getKeyStroke(KeyEvent.VK_C, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame parent = JPassFrame.getInstance();
Expand All @@ -222,13 +222,13 @@ public void actionPerformed(ActionEvent ev) {
}
}
}),
CLEAR_CLIPBOARD(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_CLEAR_CLIPBOARD), getIcon("clear"), getKeyStroke(KeyEvent.VK_X, CTRL_DOWN_MASK)) {
CLEAR_CLIPBOARD(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_CLEAR_CLIPBOARD), EDIT_MENU_CLEAR_CLIPBOARD, getIcon("clear"), getKeyStroke(KeyEvent.VK_X, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
EntryHelper.copyEntryField(JPassFrame.getInstance(), null);
}
}),
FIND_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_FIND_ENTRY), getIcon("find"), getKeyStroke(KeyEvent.VK_F, CTRL_DOWN_MASK)) {
FIND_ENTRY(new AbstractMenuAction(getLocalizedMessages().getString(EDIT_MENU_FIND_ENTRY), EDIT_MENU_FIND_ENTRY, getIcon("find"), getKeyStroke(KeyEvent.VK_F, CTRL_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame.getInstance().getSearchPanel().setVisible(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
menuBar.fileMenu=Fájl
menuBar.editMenu=Szerkesztés
menuBar.toolsMenu=Eszközök
menuBar.settingsMenu=Settings
menuBar.helpMenu=Súgó

fileMenu.new=Új
Expand Down Expand Up @@ -34,6 +35,8 @@ editMenu.findEntry=Bejegyzés keresése

toolsMenu.generatePassword=Jelszó generálás...

settingsMenu.language=Language

helpMenu.license=Licenc
helpMenu.aboutJpass=JPass névjegy...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
menuBar.fileMenu=File
menuBar.editMenu=Modifica
menuBar.toolsMenu=Strumenti
menuBar.settingsMenu=Settings
menuBar.helpMenu=Aiuto

fileMenu.new=Nuovo
Expand Down Expand Up @@ -35,6 +36,8 @@ editMenu.findEntry=Trova elemento

toolsMenu.generatePassword=Genera password...

settingsMenu.language=Language

helpMenu.license=Licenza
helpMenu.aboutJpass=Info su JPass...

Expand Down

0 comments on commit 9583a46

Please sign in to comment.