Skip to content

Commit

Permalink
refactor: GlossaryTextArea to use Actions classes
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed May 31, 2024
1 parent 58ea3fd commit 13d60e7
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 23 deletions.
31 changes: 8 additions & 23 deletions src/org/omegat/gui/glossary/GlossaryTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import java.awt.GraphicsEnvironment;
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 Down Expand Up @@ -70,9 +68,12 @@
import org.omegat.gui.common.EntryInfoThreadPane;
import org.omegat.gui.dialogs.CreateGlossaryEntry;
import org.omegat.gui.editor.EditorUtils;
import org.omegat.gui.glossary.actions.AddEntryAction;
import org.omegat.gui.glossary.actions.InsertSectionAction;
import org.omegat.gui.glossary.actions.SettingsNotifications;
import org.omegat.gui.glossary.actions.SettingsOpenFileAction;
import org.omegat.gui.main.DockableScrollPane;
import org.omegat.gui.main.IMainWindow;
import org.omegat.gui.main.MainWindowMenuHandler;
import org.omegat.gui.shortcuts.PropertiesShortcuts;
import org.omegat.util.HttpConnectionUtils;
import org.omegat.util.Log;
Expand Down Expand Up @@ -297,18 +298,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 @@ -411,24 +404,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.setAction(new MainWindowMenuHandler.ProjectAccessWriteableGlossaryMenuItemAction());
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 Down
48 changes: 48 additions & 0 deletions src/org/omegat/gui/glossary/actions/AddEntryAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2024 Hiroshi Miura
Home page: https://www.omegat.org/
Support center: https://omegat.org/support
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**************************************************************************/

package org.omegat.gui.glossary.actions;

import java.awt.event.ActionEvent;

import org.openide.awt.AbstractMnemonicsAction;

import org.omegat.core.Core;
import org.omegat.gui.glossary.GlossaryTextArea;
import org.omegat.util.OStrings;

public class AddEntryAction extends AbstractMnemonicsAction {
GlossaryTextArea glossaryTextArea;

public 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());
}
}
47 changes: 47 additions & 0 deletions src/org/omegat/gui/glossary/actions/InsertSectionAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2024 Hiroshi Miura
Home page: https://www.omegat.org/
Support center: https://omegat.org/support
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**************************************************************************/

package org.omegat.gui.glossary.actions;

import java.awt.event.ActionEvent;

import org.openide.awt.AbstractMnemonicsAction;

import org.omegat.core.Core;
import org.omegat.util.OStrings;

public class InsertSectionAction extends AbstractMnemonicsAction {
private final String selection;

public 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);
}
}
49 changes: 49 additions & 0 deletions src/org/omegat/gui/glossary/actions/SettingsNotifications.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2024 Hiroshi Miura
Home page: https://www.omegat.org/
Support center: https://omegat.org/support
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**************************************************************************/

package org.omegat.gui.glossary.actions;

import java.awt.event.ActionEvent;

import javax.swing.JCheckBoxMenuItem;

import org.openide.awt.AbstractMnemonicsAction;

import org.omegat.util.OStrings;
import org.omegat.util.Preferences;

public class SettingsNotifications extends AbstractMnemonicsAction {
public 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());
}
}
}
54 changes: 54 additions & 0 deletions src/org/omegat/gui/glossary/actions/SettingsOpenFileAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2024 Hiroshi Miura
Home page: https://www.omegat.org/
Support center: https://omegat.org/support
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**************************************************************************/

package org.omegat.gui.glossary.actions;

import java.awt.event.ActionEvent;

import javax.swing.Action;

import org.openide.awt.AbstractMnemonicsAction;

import org.omegat.gui.main.ProjectUICommands;
import org.omegat.gui.shortcuts.PropertiesShortcuts;
import org.omegat.util.Log;
import org.omegat.util.OStrings;

@SuppressWarnings("serial")
public class SettingsOpenFileAction extends AbstractMnemonicsAction {
public SettingsOpenFileAction() {
super(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE"), OStrings.getLocale());
final String key = "projectAccessWriteableGlossaryMenuItem";
putValue(Action.ACTION_COMMAND_KEY, key);
putValue(Action.ACCELERATOR_KEY, PropertiesShortcuts.getMainMenuShortcuts().getKeyStrokeOrNull(key));
}

@Override
public void actionPerformed(final ActionEvent e) {
Log.logInfoRB("LOG_MENU_CLICK", e.getActionCommand());
int modifier = e.getModifiers();
ProjectUICommands.openWritableGlossaryFile((modifier & ActionEvent.ALT_MASK) != modifier);
}
}

0 comments on commit 13d60e7

Please sign in to comment.