Skip to content

Commit

Permalink
Adds localization feature from gaborbata#29
Browse files Browse the repository at this point in the history
  • Loading branch information
edimoral committed Mar 3, 2024
1 parent 1ad189a commit 8cb05d5
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 41 deletions.
17 changes: 12 additions & 5 deletions src/main/java/jpass/ui/EntryDetailsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
import jpass.util.DateUtils;
import jpass.xml.bind.Entry;

import static jpass.ui.JPassFrame.MESSAGES;
import static jpass.util.Constants.VIEW_WINDOW_CREATED;
import static jpass.util.Constants.VIEW_WINDOW_MODIFIED;
import static jpass.util.Constants.VIEW_WINDOW_TITLE;
import static jpass.util.Constants.VIEW_WINDOW_URL;
import static jpass.util.Constants.VIEW_WINDOW_USER;

/**
* Table to display entry details.
*/
Expand All @@ -54,11 +61,11 @@ public class EntryDetailsTable extends JTable {
= DateUtils.createFormatter(Configuration.getInstance().get("date.format", "yyyy-MM-dd"));

private enum DetailType {
TITLE("Title", Entry::getTitle),
URL("URL", Entry::getUrl),
USER("User", Entry::getUser),
MODIFIED("Modified", entry -> DateUtils.formatIsoDateTime(entry.getLastModification(), FORMATTER)),
CREATED("Created", entry -> DateUtils.formatIsoDateTime(entry.getCreationDate(), FORMATTER));
TITLE(MESSAGES.getString(VIEW_WINDOW_TITLE), Entry::getTitle),
URL(MESSAGES.getString(VIEW_WINDOW_URL), Entry::getUrl),
USER(MESSAGES.getString(VIEW_WINDOW_USER), Entry::getUser),
MODIFIED(MESSAGES.getString(VIEW_WINDOW_MODIFIED), entry -> DateUtils.formatIsoDateTime(entry.getLastModification(), FORMATTER)),
CREATED(MESSAGES.getString(VIEW_WINDOW_CREATED), entry -> DateUtils.formatIsoDateTime(entry.getCreationDate(), FORMATTER));

private final String description;
private final Function<Entry, String> valueMapper;
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/jpass/ui/JPassFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -63,6 +65,12 @@
import static jpass.ui.MessageDialog.YES_OPTION;
import static jpass.ui.MessageDialog.getIcon;
import static jpass.ui.MessageDialog.showQuestionMessage;
import static jpass.util.Constants.BOTTOM_MENU_ENTRIES_COUNT;
import static jpass.util.Constants.BOTTOM_MENU_ENTRIES_FOUND;
import static jpass.util.Constants.EDIT_MENU;
import static jpass.util.Constants.FILE_MENU;
import static jpass.util.Constants.HELP_MENU;
import static jpass.util.Constants.TOOLS_MENU;

/**
* The main frame for JPass.
Expand All @@ -72,6 +80,7 @@
*/
public final class JPassFrame extends JFrame {

public static ResourceBundle MESSAGES = null;
private static final Logger LOG = Logger.getLogger(JPassFrame.class.getName());

private static JPassFrame instance;
Expand All @@ -95,7 +104,7 @@ public final class JPassFrame extends JFrame {
private final StatusPanel statusPanel;
private volatile boolean processing = false;

private JPassFrame(String fileName) {
private JPassFrame(String fileName, Locale locale) {
try {
setIconImages(Stream.of(16, 20, 32, 40, 64, 80, 128, 160)
.map(size -> getIcon("jpass", size, size).getImage())
Expand All @@ -104,6 +113,8 @@ private JPassFrame(String fileName) {
LOG.log(Level.CONFIG, "Could not set application icon.", e);
}

MESSAGES = ResourceBundle.getBundle("resources.languages.Messages", locale);

this.toolBar = new JToolBar();
this.toolBar.setFloatable(false);
this.toolBar.add(MenuActionType.NEW_FILE.getAction());
Expand Down Expand Up @@ -135,7 +146,7 @@ private JPassFrame(String fileName) {

this.jpassMenuBar = new JMenuBar();

this.fileMenu = new JMenu("File");
this.fileMenu = new JMenu(MESSAGES.getString(FILE_MENU));
this.fileMenu.setMnemonic(KeyEvent.VK_F);
this.fileMenu.add(MenuActionType.NEW_FILE.getAction());
this.fileMenu.add(MenuActionType.OPEN_FILE.getAction());
Expand All @@ -150,7 +161,7 @@ private JPassFrame(String fileName) {
this.fileMenu.add(MenuActionType.EXIT.getAction());
this.jpassMenuBar.add(this.fileMenu);

this.editMenu = new JMenu("Edit");
this.editMenu = new JMenu(MESSAGES.getString(EDIT_MENU));
this.editMenu.setMnemonic(KeyEvent.VK_E);
this.editMenu.add(MenuActionType.ADD_ENTRY.getAction());
this.editMenu.add(MenuActionType.EDIT_ENTRY.getAction());
Expand All @@ -164,13 +175,13 @@ private JPassFrame(String fileName) {
this.editMenu.add(MenuActionType.FIND_ENTRY.getAction());
this.jpassMenuBar.add(this.editMenu);

this.toolsMenu = new JMenu("Tools");
this.toolsMenu = new JMenu(MESSAGES.getString(TOOLS_MENU));
this.toolsMenu.setMnemonic(KeyEvent.VK_T);
this.toolsMenu.add(MenuActionType.GENERATE_PASSWORD.getAction());
this.toolsMenu.add(MenuActionType.CLEAR_CLIPBOARD.getAction());
this.jpassMenuBar.add(this.toolsMenu);

this.helpMenu = new JMenu("Help");
this.helpMenu = new JMenu(MESSAGES.getString(HELP_MENU));
this.helpMenu.setMnemonic(KeyEvent.VK_H);
this.helpMenu.add(MenuActionType.LICENSE.getAction());
this.helpMenu.addSeparator();
Expand Down Expand Up @@ -220,7 +231,8 @@ public static JPassFrame getInstance() {

public static synchronized JPassFrame getInstance(String fileName) {
if (instance == null) {
instance = new JPassFrame(fileName);
Locale locale = new Locale("es", "US");
instance = new JPassFrame(fileName, locale);
}
return instance;
}
Expand Down Expand Up @@ -286,9 +298,9 @@ public void refreshEntryTitleList(String selectTitle) {
}

if (searchCriteria.isEmpty()) {
this.statusPanel.setText("Entries count: " + entries.size());
this.statusPanel.setText(String.format("%s: %d", MESSAGES.getString(BOTTOM_MENU_ENTRIES_COUNT), entries.size()));
} else {
this.statusPanel.setText("Entries found: " + this.entryDetailsTable.getRowCount() + " / " + entries.size());
this.statusPanel.setText(String.format("%s: %d / %d", MESSAGES.getString(BOTTOM_MENU_ENTRIES_FOUND), this.entryDetailsTable.getRowCount(), entries.size()));
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/main/java/jpass/ui/MessageDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@

import jpass.util.SpringUtilities;

import static jpass.ui.JPassFrame.MESSAGES;
import static jpass.util.Constants.CHANGE_PASSWORD_ENTER_PASSWORD_REQUEST;
import static jpass.util.Constants.SHOW_MESSAGE_CONFIRMATION;
import static jpass.util.Constants.SHOW_MESSAGE_ERROR;
import static jpass.util.Constants.SHOW_MESSAGE_INFORMATION;
import static jpass.util.Constants.SHOW_MESSAGE_WARNING;

/**
* Utility class for displaying message dialog.
*
Expand Down Expand Up @@ -203,7 +210,7 @@ private static int showMessageDialog(final Component parent, final Object messag
* @param message dialog message
*/
public static void showWarningMessage(final Component parent, final String message) {
showMessageDialog(parent, message, "Warning", getIcon("dialog_warning"));
showMessageDialog(parent, message, MESSAGES.getString(SHOW_MESSAGE_WARNING), getIcon("dialog_warning"));
}

/**
Expand All @@ -213,7 +220,7 @@ public static void showWarningMessage(final Component parent, final String messa
* @param message dialog message
*/
public static void showErrorMessage(final Component parent, final String message) {
showMessageDialog(parent, message, "Error", getIcon("dialog_error"));
showMessageDialog(parent, message, MESSAGES.getString(SHOW_MESSAGE_ERROR), getIcon("dialog_error"));
}

/**
Expand All @@ -223,7 +230,7 @@ public static void showErrorMessage(final Component parent, final String message
* @param message dialog message
*/
public static void showInformationMessage(final Component parent, final String message) {
showMessageDialog(parent, message, "Information", getIcon("dialog_info"));
showMessageDialog(parent, message, MESSAGES.getString(SHOW_MESSAGE_INFORMATION), getIcon("dialog_info"));
}

/**
Expand All @@ -235,7 +242,7 @@ public static void showInformationMessage(final Component parent, final String m
* @return selected option
*/
public static int showQuestionMessage(final Component parent, final String message, final int optionType) {
return showMessageDialog(parent, message, "Confirmation", getIcon("dialog_question"), optionType);
return showMessageDialog(parent, message, MESSAGES.getString(SHOW_MESSAGE_CONFIRMATION), getIcon("dialog_question"), optionType);
}

/**
Expand All @@ -261,7 +268,7 @@ public static char[] showPasswordDialog(final Component parent, final boolean co

boolean incorrect = true;
while (incorrect) {
int option = showMessageDialog(parent, panel, "Enter Password", getIcon("dialog_lock"), OK_CANCEL_OPTION);
int option = showMessageDialog(parent, panel, MESSAGES.getString(CHANGE_PASSWORD_ENTER_PASSWORD_REQUEST), getIcon("dialog_lock"), OK_CANCEL_OPTION);
if (option == OK_OPTION) {
if (password.getPassword().length == 0) {
showWarningMessage(parent, "Please enter a password.");
Expand Down
71 changes: 48 additions & 23 deletions src/main/java/jpass/ui/action/MenuActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
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;

import static jpass.ui.JPassFrame.MESSAGES;
import static jpass.ui.helper.FileHelper.exportFile;
import static jpass.ui.helper.FileHelper.importFile;
import static jpass.ui.helper.FileHelper.openFile;
Expand All @@ -53,78 +56,100 @@
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import static java.awt.event.InputEvent.ALT_DOWN_MASK;

import static jpass.util.Constants.CHANGE_PASSWORD_PASSWORD_NOT_MODIFIED;
import static jpass.util.Constants.CHANGE_PASSWORD_SUCCESSFULLY_MODIFED;
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;
import static jpass.util.Constants.EDIT_MENU_COPY_URL;
import static jpass.util.Constants.EDIT_MENU_COPY_USERNAME;
import static jpass.util.Constants.EDIT_MENU_DELETE_ENTRY;
import static jpass.util.Constants.EDIT_MENU_DUPLICATE_ENTRY;
import static jpass.util.Constants.EDIT_MENU_EDIT_ENTRY;
import static jpass.util.Constants.EDIT_MENU_FIND_ENTRY;
import static jpass.util.Constants.FILE_MENU_CHANGE_PASSWORD;
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_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.TOOLS_MENU_GENERATE_PASSWORD;

/**
* Enumeration which holds menu actions and related data.
*
* @author Gabor_Bata
*
*/
public enum MenuActionType {
NEW_FILE(new AbstractMenuAction("New", getIcon("new"), getKeyStroke(KeyEvent.VK_N, CTRL_DOWN_MASK)) {
NEW_FILE(new AbstractMenuAction(MESSAGES.getString(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("Open File...", getIcon("open"), getKeyStroke(KeyEvent.VK_O, CTRL_DOWN_MASK)) {
OPEN_FILE(new AbstractMenuAction(MESSAGES.getString(Constants.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("Save", getIcon("save"), getKeyStroke(KeyEvent.VK_S, CTRL_DOWN_MASK)) {
SAVE_FILE(new AbstractMenuAction(MESSAGES.getString(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("Save As...", getIcon("save_as"), null) {
SAVE_AS_FILE(new AbstractMenuAction(MESSAGES.getString(FILE_MENU_SAVE_AS), getIcon("save_as"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
saveFile(JPassFrame.getInstance(), true);
}
}),
EXPORT_XML(new AbstractMenuAction("Export to XML...", getIcon("export"), null) {
EXPORT_XML(new AbstractMenuAction(MESSAGES.getString(FILE_MENU_EXPORT_TO_XML), getIcon("export"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
exportFile(JPassFrame.getInstance());
}
}),
IMPORT_XML(new AbstractMenuAction("Import from XML...", getIcon("import"), null) {
IMPORT_XML(new AbstractMenuAction(MESSAGES.getString(FILE_MENU_IMPORT_FROM_XML), getIcon("import"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
importFile(JPassFrame.getInstance());
}
}),
CHANGE_PASSWORD(new AbstractMenuAction("Change Password...", getIcon("lock"), null) {
CHANGE_PASSWORD(new AbstractMenuAction(MESSAGES.getString(FILE_MENU_CHANGE_PASSWORD), getIcon("lock"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame parent = JPassFrame.getInstance();
char[] password = MessageDialog.showPasswordDialog(parent, true);
if (password == null) {
MessageDialog.showInformationMessage(parent, "Password has not been modified.");
MessageDialog.showInformationMessage(parent, MESSAGES.getString(CHANGE_PASSWORD_PASSWORD_NOT_MODIFIED));
} else {
parent.getModel().setPassword(password);
parent.getModel().setModified(true);
parent.refreshFrameTitle();
MessageDialog.showInformationMessage(parent,
"Password has been successfully modified.\n\nSave the file now in order to\nget the new password applied.");
MESSAGES.getString(CHANGE_PASSWORD_SUCCESSFULLY_MODIFED));
}
}
}),
GENERATE_PASSWORD(new AbstractMenuAction("Generate Password...", getIcon("generate"), getKeyStroke(KeyEvent.VK_Z, CTRL_DOWN_MASK)) {
GENERATE_PASSWORD(new AbstractMenuAction(MESSAGES.getString(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("Exit", getIcon("exit"), getKeyStroke(KeyEvent.VK_F4, ALT_DOWN_MASK)) {
EXIT(new AbstractMenuAction(MESSAGES.getString(FILE_MENU_EXIT), getIcon("exit"), getKeyStroke(KeyEvent.VK_F4, ALT_DOWN_MASK)) {
@Override
public void actionPerformed(ActionEvent ev) {
JPassFrame.getInstance().exitFrame();
}
}),
ABOUT(new AbstractMenuAction("About JPass...", getIcon("info"), getKeyStroke(KeyEvent.VK_F1, 0)) {
ABOUT(new AbstractMenuAction(MESSAGES.getString(HELP_MENU_ABOUT_JPASS), getIcon("info"), getKeyStroke(KeyEvent.VK_F1, 0)) {
@Override
public void actionPerformed(ActionEvent ev) {
StringBuilder sb = new StringBuilder();
Expand All @@ -137,37 +162,37 @@ public void actionPerformed(ActionEvent ev) {
MessageDialog.showInformationMessage(JPassFrame.getInstance(), sb.toString());
}
}),
LICENSE(new AbstractMenuAction("License", getIcon("license"), null) {
LICENSE(new AbstractMenuAction(MESSAGES.getString(HELP_MENU_LICENSE), getIcon("license"), null) {
@Override
public void actionPerformed(ActionEvent ev) {
MessageDialog.showTextFile(JPassFrame.getInstance(), "License", "license.txt");
MessageDialog.showTextFile(JPassFrame.getInstance(), MESSAGES.getString(HELP_MENU_LICENSE), "license.txt");
}
}),
ADD_ENTRY(new AbstractMenuAction("Add Entry...", getIcon("entry_new"), getKeyStroke(KeyEvent.VK_Y, CTRL_DOWN_MASK)) {
ADD_ENTRY(new AbstractMenuAction(MESSAGES.getString(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("Edit Entry...", getIcon("entry_edit"), getKeyStroke(KeyEvent.VK_E, CTRL_DOWN_MASK)) {
EDIT_ENTRY(new AbstractMenuAction(MESSAGES.getString(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("Duplicate Entry...", getIcon("entry_duplicate"), getKeyStroke(KeyEvent.VK_K, CTRL_DOWN_MASK)) {
DUPLICATE_ENTRY(new AbstractMenuAction(MESSAGES.getString(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("Delete Entry...", getIcon("entry_delete"), getKeyStroke(KeyEvent.VK_D, CTRL_DOWN_MASK)) {
DELETE_ENTRY(new AbstractMenuAction(MESSAGES.getString(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("Copy URL", getIcon("url"), getKeyStroke(KeyEvent.VK_U, CTRL_DOWN_MASK)) {
COPY_URL(new AbstractMenuAction(MESSAGES.getString(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 @@ -177,7 +202,7 @@ public void actionPerformed(ActionEvent ev) {
}
}
}),
COPY_USER(new AbstractMenuAction("Copy User Name", getIcon("user"), getKeyStroke(KeyEvent.VK_B, CTRL_DOWN_MASK)) {
COPY_USER(new AbstractMenuAction(MESSAGES.getString(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 @@ -187,7 +212,7 @@ public void actionPerformed(ActionEvent ev) {
}
}
}),
COPY_PASSWORD(new AbstractMenuAction("Copy Password", getIcon("keyring"), getKeyStroke(KeyEvent.VK_C, CTRL_DOWN_MASK)) {
COPY_PASSWORD(new AbstractMenuAction(MESSAGES.getString(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 @@ -197,13 +222,13 @@ public void actionPerformed(ActionEvent ev) {
}
}
}),
CLEAR_CLIPBOARD(new AbstractMenuAction("Clear Clipboard", getIcon("clear"), getKeyStroke(KeyEvent.VK_X, CTRL_DOWN_MASK)) {
CLEAR_CLIPBOARD(new AbstractMenuAction(MESSAGES.getString(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("Find Entry", getIcon("find"), getKeyStroke(KeyEvent.VK_F, CTRL_DOWN_MASK)) {
FIND_ENTRY(new AbstractMenuAction(MESSAGES.getString(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
Loading

0 comments on commit 8cb05d5

Please sign in to comment.