Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: GlossaryTextArea: action with lambda #1046

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
74 changes: 47 additions & 27 deletions src/org/omegat/gui/glossary/GlossaryTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.omegat.gui.editor.EditorUtils;
import org.omegat.gui.main.DockableScrollPane;
import org.omegat.gui.main.IMainWindow;
import org.omegat.gui.main.ProjectUICommands;
import org.omegat.gui.shortcuts.PropertiesShortcuts;
import org.omegat.util.HttpConnectionUtils;
import org.omegat.util.Log;
Expand All @@ -87,6 +88,8 @@
import org.omegat.util.gui.TooltipAttribute;
import org.omegat.util.gui.UIThreadsUtil;

import static org.openide.awt.Mnemonics.setLocalizedText;

/**
* This is a Glossary pane that displays glossary entries.
*
Expand Down Expand Up @@ -294,20 +297,13 @@ private void doPopup(Point p) {

private void populateContextMenu(JPopupMenu popup) {
boolean projectLoaded = Core.getProject().isProjectLoaded();

final String selection = getSelectedText();
JMenuItem item = popup.add(OStrings.getString("GUI_GLOSSARYWINDOW_insertselection"));
JMenuItem item = popup.add(createMenuItem("GUI_GLOSSARYWINDOW_insertselection",
"glossaryWindowInsertSelectionPopupMenuItem", () -> Core.getEditor().insertText(selection)));
item.setEnabled(projectLoaded && !StringUtil.isEmpty(selection));
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Core.getEditor().insertText(selection);
}
});
item = popup.add(OStrings.getString("GUI_GLOSSARYWINDOW_addentry"));
item = popup.add(createMenuItem("GUI_GLOSSARYWINDOW_addentry", "glossaryWindowAddEntryPopupMenuItem",
() -> showCreateGlossaryEntryDialog(Core.getMainWindow().getApplicationFrame())));
item.setEnabled(projectLoaded);
item.addActionListener(
e -> showCreateGlossaryEntryDialog(Core.getMainWindow().getApplicationFrame()));
}

@Override
Expand Down Expand Up @@ -410,30 +406,29 @@ public void windowClosed(WindowEvent e) {
public void populatePaneMenu(JPopupMenu menu) {
populateContextMenu(menu);
menu.addSeparator();
final JMenuItem openFile = new JMenuItem(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE"));
openFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Core.getMainWindow().getMainMenu().invokeAction("projectAccessWritableGlossaryMenuItem",
e.getModifiers());
}
});
JMenuItem openFile = createMenuItem("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE",
"glossaryWindowSettingsOpenWriteableFileMenuItem", evt -> {
Log.logInfoRB("LOG_MENU_CLICK", evt.getActionCommand());
final int modifier = evt.getModifiers();
ProjectUICommands.openWritableGlossaryFile((modifier & ActionEvent.ALT_MASK) != 0);
});
openFile.setEnabled(false);
if (Core.getProject().isProjectLoaded()) {
String glossaryPath = Core.getProject().getProjectProperties().getWriteableGlossary();
openFile.setEnabled(!StringUtil.isEmpty(glossaryPath) && new File(glossaryPath).isFile());
}
menu.add(openFile);
menu.addSeparator();
final JMenuItem notify = new JCheckBoxMenuItem(
OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_NOTIFICATIONS"));
final JMenuItem notify = createCheckBoxMenuItem("GUI_GLOSSARYWINDOW_SETTINGS_NOTIFICATIONS",
"glossaryWindowSettingsNotificationMenuItem", evt -> {
Log.logInfoRB("LOG_MENU_CLICK", evt.getActionCommand());
Object o = evt.getSource();
if (o instanceof JCheckBoxMenuItem) {
Preferences.setPreference(Preferences.NOTIFY_GLOSSARY_HITS,
((JCheckBoxMenuItem) o).isSelected());
}
});
notify.setSelected(Preferences.isPreference(Preferences.NOTIFY_GLOSSARY_HITS));
notify.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Preferences.setPreference(Preferences.NOTIFY_GLOSSARY_HITS, notify.isSelected());
}
});
menu.add(notify);
menu.addSeparator();
final JMenuItem sortOrderSrcLength = new JCheckBoxMenuItem(
Expand All @@ -451,4 +446,29 @@ public void actionPerformed(ActionEvent e) {
.setPreference(Preferences.GLOSSARY_SORT_BY_LENGTH, sortOrderLocLength.isSelected()));
menu.add(sortOrderLocLength);
}

private static JMenuItem createMenuItem(String titleKey, String command, Runnable action) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you plan to use the same methodology for main menu, you should move these methods to a common class, for example org.omegat.gui.common.MenuFactory

return createMenuItem(titleKey, command, ev -> {
Log.logInfoRB("LOG_MENU_CLICK", ev.getActionCommand());
action.run();
});
}

private static JMenuItem createMenuItem(String titleKey, String command, ActionListener actionListener) {
JMenuItem result = new JMenuItem();
setLocalizedText(result, OStrings.getString(titleKey));
result.setActionCommand(command);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think also action command is not necessary. In the past it was used when same action listener was shared by multiple menus, which is the case in MainWindowMenu actually.
But with a distinct lambda for each menu, does not seem necessary

result.addActionListener(actionListener);
return result;
}

private static JCheckBoxMenuItem createCheckBoxMenuItem(String titleKey, String command,
ActionListener actionListener) {
JCheckBoxMenuItem result = new JCheckBoxMenuItem();
result.setActionCommand(command);
setLocalizedText(result, OStrings.getString(titleKey));
result.setSelected(Preferences.isPreference(Preferences.NOTIFY_GLOSSARY_HITS));
result.addActionListener(actionListener);
return result;
}
}
48 changes: 48 additions & 0 deletions src/org/omegat/gui/main/ProjectUICommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.omegat.util.StaticUtils;
import org.omegat.util.StringUtil;
import org.omegat.util.WikiGet;
import org.omegat.util.gui.DesktopWrapper;
import org.omegat.util.gui.OmegaTFileChooser;
import org.omegat.util.gui.OpenProjectFileChooser;
import org.omegat.util.gui.UIThreadsUtil;
Expand Down Expand Up @@ -1315,6 +1316,53 @@ public static void doWikiImport() {
}
}

/**
* Open file or parent directory of writeable glossary with OS's standard
* file manager.
*
* @param parentDirectory
* true when opening parent folder, otherwise false.
*/
public static void openWritableGlossaryFile(boolean parentDirectory) {
if (!Core.getProject().isProjectLoaded()) {
return;
}
String path = Core.getProject().getProjectProperties().getWriteableGlossary();
if (StringUtil.isEmpty(path)) {
return;
}
File toOpen = new File(path);
if (parentDirectory) {
toOpen = toOpen.getParentFile();
}
openFile(toOpen);
}

/**
* Open specified path with OS standard file manager.
*
* @param path
* to open.
*/
public static void openFile(File path) {
try {
path = path.getCanonicalFile(); // Normalize file name in case it is
// displayed
} catch (Exception ex) {
// Ignore
}
if (!path.exists()) {
Core.getMainWindow().showStatusMessageRB("LFC_ERROR_FILE_DOESNT_EXIST", path);
return;
}
try {
DesktopWrapper.open(path);
} catch (Exception ex) {
Log.logErrorRB(ex, "RPF_ERROR");
Core.getMainWindow().displayErrorRB(ex, "RPF_ERROR");
}
}

private static boolean ensureProjectDir(File dir) {
if (!dir.isDirectory() && !dir.mkdirs()) {
Log.logErrorRB("CT_ERROR_CREATING_PROJECT_DIR", dir);
Expand Down
5 changes: 5 additions & 0 deletions src/org/omegat/util/OStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.function.Function;
Expand Down Expand Up @@ -81,6 +82,10 @@ public static ResourceBundle getResourceBundle() {
return bundle;
}

public static Locale getLocale() {
return bundle.getLocale();
}

/**
* Loads resources from the specified file. If the file cannot be loaded,
* resources are reverted to the default locale. Useful when testing
Expand Down
Loading