Skip to content

Commit

Permalink
refactor: direct instantiate Action classes
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed May 28, 2024
1 parent 43ef472 commit 1c61262
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 346 deletions.
4 changes: 2 additions & 2 deletions src/org/omegat/gui/accesstool/AccessTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public void initComponents() {
add(sourceFilesCB);

searchButton = new JButton();
searchButton.setAction(MainWindowMenuHandler.getAction("EditFindInProjectMenuItem"));
searchButton.setAction(new MainWindowMenuHandler.EditFindInProjectMenuItemAction());
searchButton.setText("");
searchButton.setBorderPainted(false);
settingsButton = new JButton();
settingsButton.setAction(MainWindowMenuHandler.getAction("OptionsPreferencesMenuItem"));
settingsButton.setAction(new MainWindowMenuHandler.OptionsPreferencesMenuItemAction());
settingsButton.setText("");
settingsButton.setBorderPainted(false);

Expand Down
2 changes: 1 addition & 1 deletion src/org/omegat/gui/glossary/GlossaryTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public void populatePaneMenu(JPopupMenu menu) {
populateContextMenu(menu);
menu.addSeparator();
final JMenuItem openFile = new JMenuItem(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE"));
openFile.setAction(MainWindowMenuHandler.getAction("ProjectAccessWritableGlossaryMenuItem"));
openFile.setAction(new MainWindowMenuHandler.ProjectAccessWriteableGlossaryMenuItemAction());
openFile.setEnabled(false);
if (Core.getProject().isProjectLoaded()) {
String glossaryPath = Core.getProject().getProjectProperties().getWriteableGlossary();
Expand Down
417 changes: 200 additions & 217 deletions src/org/omegat/gui/main/BaseMainWindowMenu.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/org/omegat/gui/main/MainWindowBurgerMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class MainWindowBurgerMenu extends BaseMainWindowMenu implements IM
JMenu burgerMenu;

public MainWindowBurgerMenu(MainWindow mainWindow) {
super(mainWindow);
super();
initComponents();
}

Expand Down
91 changes: 0 additions & 91 deletions src/org/omegat/gui/main/MainWindowMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -125,95 +123,6 @@ private MainWindowMenuHandler() {
public MainWindowMenuHandler(final MainWindow mainWindow) {
}

private static final Action[] ACTIONS = { new ProjectNewMenuItemAction(),
new ProjectTeamNewMenuItemAction(), new ProjectOpenMenuItemAction(),
new ProjectCloseMenuItemAction(), new ProjectClearRecentMenuItemAction(),
new ProjectImportMenuItemAction(), new ProjectWikiImportMenuItemAction(),
new ProjectSaveMenuItemAction(), new ProjectReloadMenuItemAction(),
new ProjectCommitSourceFilesAction(), new ProjectCommitTargetFilesAction(),
new ProjectCompileMenuItemAction(), new ProjectSingleCompileMenuItemAction(),
new ProjectMedOpenMenuItemAction(), new ProjectMedCreateMenuItemAction(),
new ProjectEditMenuItemAction(), new ProjectAccessRootMenuItemAction(),
new ProjectAccessDictionaryMenuItemAction(), new ProjectAccessGlossaryMenuItemAction(),
new ProjectAccessSourceMenuItemAction(), new ProjectAccessTargetMenuItemAction(),
new ProjectAccessTMMenuItemAction(), new ProjectAccessExportTMMenuItemAction(),
new ProjectRestartMenuItemAction(), new ProjectExitMenuItemAction(),
new ProjectAccessCurrentSourceDocumentMenuItemAction(),
new ProjectAccessCurrentTargetDocumentMenuItemAction(),
new ProjectAccessWriteableGlossaryMenuItemAction(), new EditUndoMenuItemAction(),
new EditRedoMenuItemAction(), new EditFindInProjectMenuItemAction(),
new EditOverwriteTranslationMenuItemAction(), new EditInsertSourceMenuItemAction(),
new EditSelectSourceMenuItemAction(), new EditInsertTranslationMenuItemAction(),
new EditOverwriteMachineTranslationMenuItemAction(), new EditReplaceInProjectMenuItemAction(),
new EditExportSelectionMenuItemAction(), new EditOverwriteSourceMenuItemAction(),
new EditSearchDictionaryMenuItemAction(), new EditCreateGlossaryEntryMenuItemAction(),
new EditSelectFuzzy1MenuItemAction(), new EditSelectFuzzy2MenuItemAction(),
new EditSelectFuzzy3MenuItemAction(), new EditSelectFuzzy4MenuItemAction(),
new EditSelectFuzzy5MenuItemAction(), new EditSelectFuzzyPrevMenuItemAction(),
new EditSelectFuzzyNextMenuItemAction(), new EditTagPainterMenuItemAction(),
new EditTagNextMissedMenuItemAction(), new InsertCharsLRMAction(), new InsertCharsRLMAction(),
new InsertCharsLREAction(), new InsertCharsRLEAction(), new InsertCharsPDFAction(),
new EditMultipleDefaultAction(), new EditMultipleAlternateAction(),
new EditRegisterUntranslatedMenuItemAction(), new EditRegisterEmptyMenuItemAction(),
new EditRegisterIdenticalMenuItemAction(), new TitleCaseMenuItemAction(),
new SentenceCaseMenuItemAction(), new CycleSwitchCaseMenuItemAction(),
new UpperCaseMenuItemAction(), new LowerCaseMenuItemAction(),
new GotoNextUntranslatedMenuItemAction(), new GotoNextUniqueMenuItemAction(),
new GotoNextTranslatedMenuItemAction(), new GotoNextSegmentMenuItemAction(),
new GotoPreviousSegmentMenuItemAction(), new GotoSegmentMenuItemAction(),
new GotoNextUniqueMenuItemAction(), new GotoNextNoteMenuItemAction(),
new GotoPreviousNoteMenuItemAction(), new GotoMatchSourceSegmentAction(),
new GotoNextXAutoMenuItemAction(), new GotoPrevXAutoMenuItemAction(),
new GotoNextXEnforcedMenuItemAction(), new GotoPrevXEnforcedMenuItemAction(),
new GotoHistoryBackMenuItemAction(), new GotoHistoryForwardMenuItemAction(),
new GotoNotesPanelMenuItemAction(), new GotoEditorPanelMenuItemAction(),
new ViewMarkTranslatedSegmentsCheckBoxMenuItemAction(),
new ViewMarkUntranslatedSegmentsCheckBoxMenuItemAction(),
new ViewMarkParagraphStartCheckBoxMenuItemAction(),
new ViewDisplaySegmentSourceCheckBoxMenuItemAction(),
new ViewMarkNonUniqueSegmentsCheckBoxMenuItemAction(),
new ViewMarkNotedSegmentsCheckBoxMenuItemAction(), new ViewMarkNBSPCheckBoxMenuItemAction(),
new ViewMarkWhitespaceCheckBoxMenuItemAction(), new ViewMarkBidiCheckBoxMenuItemAction(),
new ViewMarkAutoPopulatedCheckBoxMenuItemAction(),
new ViewMarkGlossaryMatchesCheckBoxMenuItemAction(),
new ViewMarkLanguageCheckerCheckBoxMenuItemAction(),
new ViewMarkFontFallbackCheckBoxMenuItemAction(),
new ViewDisplayModificationInfoNoneRadioButtonMenuItemAction(),
new ViewDisplayModificationInfoSelectedRadioButtonMenuItemAction(),
new ViewDisplayModificationInfoAllRadioButtonMenuItemAction(), new ViewRestoreGUIMenuItemAction(),
new ViewFileListMenuItemAction(), new ToolsCheckIssuesMenuItemAction(),
new ToolsCheckIssuesCurrentFileMenuItemAction(), new ToolsShowStatisticsStandardMenuItemAction(),
new ToolsShowStatisticsMatchesMenuItemAction(),
new ToolsShowStatisticsMatchesPerFileMenuItemAction(), new OptionsPreferencesMenuItemAction(),
new OptionsAutoCompleteShowAutomaticallyItemAction(),
new OptionsAutoCompleteHistoryCompletionMenuItemAction(),
new OptionsAutoCompleteHistoryPredictionMenuItemAction(),
new OptionsMTAutoFetchCheckboxMenuItemAction(),
new OptionsGlossaryFuzzyMatchingCheckBoxMenuItemAction(),
new OptionsDictionaryFuzzyMatchingCheckBoxMenuItemAction(),
new OptionsSetupFileFiltersMenuItemAction(), new OptionsSentsegMenuItemAction(),
new OptionsWorkflowMenuItemAction(), new OptionsAccessConfigDirMenuItemAction(),
new HelpAboutMenuItemAction(), new HelpLastChangesMenuItemAction(),
new HelpContentsMenuItemAction(), new HelpLogMenuItemAction(),
new HelpUpdateCheckMenuItemAction() };

public static Map<Object, Action> getActions() {
Map<Object, Action> result = new HashMap<>();
for (Action a : ACTIONS) {
result.put(a.getValue(Action.ACTION_COMMAND_KEY), a);
}
return result;
}

public static Action getAction(String key) {
for (Action a : ACTIONS) {
if (a.getValue(Action.ACTION_COMMAND_KEY).equals(key)) {
return a;
}
}
return null;
}

/**
* Create a new project.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/org/omegat/gui/matches/MatchesTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ private void populateContextMenu(JPopupMenu popup, final int index) {
for (int i = 0; i < m.projs.length; i++) {
String proj = m.projs[i];
StringBuilder b = new StringBuilder();
if (proj.equals("")) {
if (proj.isEmpty()) {
b.append(OStrings.getString("MATCHES_THIS_PROJECT"));
} else {
b.append(proj);
Expand All @@ -606,11 +606,11 @@ private void populateContextMenu(JPopupMenu popup, final int index) {
}

JMenuItem item = popup.add(OStrings.getString("MATCHES_INSERT"));
item.setAction(MainWindowMenuHandler.getAction("EditInsertTranslationMenuItem"));
item.setAction(new MainWindowMenuHandler.EditInsertTranslationMenuItemAction());
item.setEnabled(hasMatches);

item = popup.add(OStrings.getString("MATCHES_REPLACE"));
item.setAction(MainWindowMenuHandler.getAction("EditOverwriteTranslationMenuItem"));
item.setAction(new MainWindowMenuHandler.EditOverwriteTranslationMenuItemAction());
item.setEnabled(hasMatches);

popup.addSeparator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@

import org.junit.Test;

import org.omegat.gui.main.BaseMainWindowMenu;
import org.omegat.gui.main.TestCoreGUI;
import org.omegat.util.OStrings;

public class AboutDialogTest extends TestCoreGUI {

@Test
public void testAboutDialog() throws InterruptedException {
window.menuItem(OStrings.getString("TF_MENU_HELP")).click();
window.menuItem(BaseMainWindowMenu.HELP_ABOUT_MENUITEM).click();
window.menuItem("TF_MENU_HELP").click();
window.menuItem("HELP_ABOUT_MENUITEM").click();
// Check about dialog
window.dialog(AboutDialog.DIALOG_NAME).requireModal();
Pattern pattern = Pattern.compile(OStrings.getApplicationDisplayName()
Expand Down
41 changes: 13 additions & 28 deletions test/src/org/omegat/gui/main/MainWindowMenuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import java.awt.Component;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
Expand All @@ -48,7 +48,6 @@

import org.omegat.core.Core;
import org.omegat.core.TestCore;
import org.omegat.util.CommonVerifications;
import org.omegat.util.OStrings;
import org.omegat.util.Platform;
import org.omegat.util.StaticUtils;
Expand All @@ -66,25 +65,28 @@ public class MainWindowMenuTest extends TestCore {
*
*/
@Test
public void testMenuActions() {
public void testMenuActionsClassNames() {
int count = 0;

Set<Object> actionTargets = MainWindowMenuHandler.getActions().keySet();
// List all the inner class implements Action
List<String> actions = Arrays.stream(MainWindowMenuHandler.class.getDeclaredClasses())
.filter(Action.class::isAssignableFrom)
.map(Class::getSimpleName).collect(Collectors.toList());

for (Field f : StaticUtils.getAllModelFields(MainWindowMenu.class)) {
if (JMenuItem.class.isAssignableFrom(f.getType()) && f.getType() != JMenu.class) {
count++;
String actionClassName = StringUtil.capitalizeFirst(f.getName(), Locale.ENGLISH);
if (!actionTargets.contains(actionClassName)) {
String actionClassName = StringUtil.capitalizeFirst(f.getName(), Locale.ENGLISH) + "Action";
if (!actions.contains(actionClassName)) {
fail("Action method or class not defined for " + actionClassName);
} else {
assertTrue(actionTargets.remove(actionClassName));
assertTrue(actions.remove(actionClassName));
}
}
}
assertTrue("menu items not found", count > 30);
assertTrue("There is action handlers in MainWindow which doesn't used in menu: "
+ actionTargets, actionTargets.isEmpty());
+ actions, actions.isEmpty());
}

@Test
Expand Down Expand Up @@ -123,7 +125,7 @@ private String getLocalizedText(final String key) {
static class TestMainMenu extends BaseMainWindowMenu {

TestMainMenu() {
super(null);
super();
}

@Override
Expand Down Expand Up @@ -293,21 +295,4 @@ private List<JMenuItem> addMenuItemsToPager(MenuKey target) {
newMenuItems.forEach(pager::add);
return pager.getFirstPage();
}

@Test
public void actionMenuCheck() throws Exception {
assertMenuKeyMatches(new String[] {"src/org/omegat/gui/main/"});
}

protected void assertMenuKeyMatches(String[] targets) throws Exception {
Set<Object> actions = MainWindowMenuHandler.getActions().keySet();
Locale.setDefault(Locale.ENGLISH);
Pattern pattern = Pattern.compile("createMenuItemFromAction\\(\\s*\"([^\"]+)\"\\s*[,)]");
CommonVerifications.processSourceContent(targets, (path, chars) -> {
Matcher m = pattern.matcher(chars);
while (m.find()) {
assertTrue(actions.contains(m.group(1)));
}
});
}
}

0 comments on commit 1c61262

Please sign in to comment.