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
103 changes: 76 additions & 27 deletions src/org/omegat/gui/glossary/GlossaryTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
Expand All @@ -52,6 +51,7 @@
import java.util.List;
import java.util.Locale;

import javax.swing.Action;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
Expand All @@ -63,6 +63,8 @@
import javax.swing.text.JTextComponent;
import javax.swing.text.StyledDocument;

import org.openide.awt.AbstractMnemonicsAction;

import org.omegat.core.Core;
import org.omegat.core.data.ProjectProperties;
import org.omegat.core.data.SourceTextEntry;
Expand All @@ -72,6 +74,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 Down Expand Up @@ -296,18 +299,10 @@ 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(new InsertSectionAction(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(new AddEntryAction(this));
item.setEnabled(projectLoaded);
item.addActionListener(
e -> showCreateGlossaryEntryDialog(Core.getMainWindow().getApplicationFrame()));
}

@Override
Expand Down Expand Up @@ -410,30 +405,16 @@ 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());
}
});
final JMenuItem openFile = new JMenuItem(new SettingsOpenFileAction());
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 = new JCheckBoxMenuItem(new SettingsNotifications());
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 +432,72 @@ public void actionPerformed(ActionEvent e) {
.setPreference(Preferences.GLOSSARY_SORT_BY_LENGTH, sortOrderLocLength.isSelected()));
menu.add(sortOrderLocLength);
}

private static final class AddEntryAction extends AbstractMnemonicsAction {
private static final long serialVersionUID = 1L;
GlossaryTextArea glossaryTextArea;

private AddEntryAction(GlossaryTextArea glossaryTextArea) {
super(OStrings.getString("GUI_GLOSSARYWINDOW_addentry"), OStrings.getLocale());
this.glossaryTextArea = glossaryTextArea;
}

@Override
public void actionPerformed(final ActionEvent e) {
glossaryTextArea.showCreateGlossaryEntryDialog(Core.getMainWindow().getApplicationFrame());
}
}

private static final class InsertSectionAction extends AbstractMnemonicsAction {
private static final long serialVersionUID = 1L;
private final String selection;

private InsertSectionAction(String selection) {
super(OStrings.getString("GUI_GLOSSARYWINDOW_insertselection"), OStrings.getLocale());
this.selection = selection;
}

@Override
public void actionPerformed(final ActionEvent e) {
Core.getEditor().insertText(selection);
}
}

private static final class SettingsNotifications extends AbstractMnemonicsAction {
private static final long serialVersionUID = 1L;

private SettingsNotifications() {
super(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_NOTIFICATIONS"), OStrings.getLocale());
}

@Override
public void actionPerformed(final ActionEvent e) {
Object notify = e.getSource();
if (notify instanceof JCheckBoxMenuItem) {
Preferences.setPreference(Preferences.NOTIFY_GLOSSARY_HITS, ((JCheckBoxMenuItem) notify).isSelected());
}
}
}

private static final class SettingsOpenFileAction extends AbstractMnemonicsAction {
private static final long serialVersionUID = 1L;

private SettingsOpenFileAction() {
super(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE"), OStrings.getLocale());
final String key = "projectAccessWriteableGlossaryMenuItem";
putValue(Action.ACTION_COMMAND_KEY, key);
try {
KeyStroke keyStoroke = PropertiesShortcuts.getMainMenuShortcuts().getKeyStroke(key);
putValue(Action.ACCELERATOR_KEY, keyStoroke);
} catch (Exception ignored) {
}
miurahr marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void actionPerformed(final ActionEvent e) {
Log.logInfoRB("LOG_MENU_CLICK", e.getActionCommand());
int modifier = e.getModifiers();
ProjectUICommands.openWritableGlossaryFile((modifier & ActionEvent.ALT_MASK) != 0);
}
}
}
35 changes: 35 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,40 @@ public static void doWikiImport() {
}
}

public static void openWritableGlossaryFile(boolean parent) {
miurahr marked this conversation as resolved.
Show resolved Hide resolved
if (!Core.getProject().isProjectLoaded()) {
return;
}
String path = Core.getProject().getProjectProperties().getWriteableGlossary();
if (StringUtil.isEmpty(path)) {
return;
}
File toOpen = new File(path);
if (parent) {
toOpen = toOpen.getParentFile();
}
openFile(toOpen);
}

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