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

Refector: MainMenuHandler: action handlers takes ActionEvent object #1055

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions src/org/omegat/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@
import org.omegat.gui.glossary.IGlossaries;
import org.omegat.gui.issues.IIssues;
import org.omegat.gui.issues.IssuesPanelController;
import org.omegat.gui.main.BaseMainWindowMenu;
import org.omegat.gui.main.ConsoleWindow;
import org.omegat.gui.main.IMainMenu;
import org.omegat.gui.main.IMainWindow;
import org.omegat.gui.main.MainWindow;
import org.omegat.gui.main.MainWindowUI;
import org.omegat.gui.matches.IMatcher;
import org.omegat.gui.matches.MatchesTextArea;
import org.omegat.gui.multtrans.MultipleTransPane;
Expand Down Expand Up @@ -100,30 +103,28 @@ private Core() {

private static IProject currentProject;
private static IMainWindow mainWindow;
protected static IEditor editor;
private static IEditor editor;
private static ITagValidation tagValidation;
private static IIssues issuesWindow;
private static IMatcher matcher;
private static FilterMaster filterMaster;
private static IProjectFilesList projWin;

protected static IAutoSave saveThread;
private static IAutoSave saveThread;
private static final ReentrantLock EXCLUSIVE_RUN_LOCK = new ReentrantLock();

protected static IGlossaries glossary;
private static IGlossaries glossary;
private static GlossaryManager glossaryManager;
private static MachineTranslateTextArea machineTranslatePane;
private static DictionariesTextArea dictionaries;
@SuppressWarnings("unused")
private static MultipleTransPane multiple;
private static INotes notes;
private static IComments comments;
private static Segmenter segmenter;
private static BaseMainWindowMenu menu;

private static Map<String, String> cmdLineParams = Collections.emptyMap();

private static final List<String> PLUGINS_LOADING_ERRORS = Collections
.synchronizedList(new ArrayList<String>());
.synchronizedList(new ArrayList<>());

private static final List<IMarker> MARKERS = new ArrayList<IMarker>();

Expand All @@ -142,6 +143,10 @@ public static IMainWindow getMainWindow() {
return mainWindow;
}

public static IMainMenu getMainMenu() {
return menu;
}

/** Get editor instance. */
public static IEditor getEditor() {
return editor;
Expand Down Expand Up @@ -252,8 +257,10 @@ public static void initializeGUI(final Map<String, String> params) throws Except
MainWindow me = new MainWindow();
mainWindow = me;

// 4. initialize rests of UI parts.
initializeGUIimpl(me);

// 5. start threads.
SaveThread th = new SaveThread();
saveThread = th;
th.start();
Expand All @@ -266,9 +273,14 @@ public static void initializeGUI(final Map<String, String> params) throws Except
* @throws Exception
*/
static void initializeGUIimpl(MainWindow me) throws Exception {
// 1. initialize main menu.
menu = MainWindowUI.initMainMenu(me);

// 2. initialize markers.
MarkerController.init();
LanguageToolWrapper.init();

// 3. initialize segmenter and filters.
segmenter = new Segmenter(Preferences.getSRX());
filterMaster = new FilterMaster(Preferences.getFilters());

Expand Down Expand Up @@ -302,7 +314,7 @@ public static void initializeConsole(final Map<String, String> params) throws Ex
/**
* Set main window instance for unit tests.
*
* @param mainWindow
* @param mainWindow IMainWindow object.
*/
protected static void setMainWindow(IMainWindow mainWindow) {
Core.mainWindow = mainWindow;
Expand Down Expand Up @@ -408,6 +420,26 @@ public static void executeExclusively(boolean waitForUnlock, RunnableWithExcepti

private static StackTraceElement[] runningStackTrace;

static MultipleTransPane getMultiple() {
return multiple;
}

/**
* For test.
* @param newEditor editor controller for test.
*/
static void setEditor(IEditor newEditor) {
editor = newEditor;
}

static void setSaveThread(final IAutoSave autoSave) {
saveThread = autoSave;
}

static void setGlossary(IGlossaries glossary) {
Core.glossary = glossary;
}

public interface RunnableWithException {
void run() throws Exception;
}
Expand Down
17 changes: 8 additions & 9 deletions src/org/omegat/gui/accesstool/AccessTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.omegat.core.Core;
import org.omegat.core.CoreEvents;
import org.omegat.core.events.IProjectEventListener;
import org.omegat.gui.main.BaseMainWindowMenuHandler;
import org.omegat.gui.main.MainMenuIcons;
import org.omegat.gui.main.ProjectUICommands;
import org.omegat.util.OConsts;
Expand All @@ -71,15 +70,13 @@ public class AccessTools extends JPanel {

private ProjectComboBoxModel projectComboBoxModel;
private SourceComboBoxModel sourceComboBoxModel;
private final BaseMainWindowMenuHandler mainWindowMenuHandler;

private URI selectedProject = null;

private static final int MAX_PATH_LENGTH_SHOWN = 25;
private static final float CHECKBOX_HEIGHT_RATIO = 1.8f;

public AccessTools(final BaseMainWindowMenuHandler mainWindowMenuHandler) {
this.mainWindowMenuHandler = mainWindowMenuHandler;
public AccessTools() {
initComponents();
}

Expand Down Expand Up @@ -126,21 +123,23 @@ public void initComponents() {
Objects.requireNonNullElseGet(UIManager.getIcon("OmegaT.newUI.search.icon"),
() -> MainMenuIcons.newImageIcon(ResourcesUtil.getBundledImage("newUI.search.png"))));
searchButton.setBorderPainted(false);
searchButton.setActionCommand("editFindInProjectMenuItem");
settingsButton = new JButton("", Objects.requireNonNullElseGet(
UIManager.getIcon("OmegaT.newUI.settings.icon"),
() -> MainMenuIcons.newImageIcon(ResourcesUtil.getBundledImage("newUI.settings.png"))));
settingsButton.setBorderPainted(false);
settingsButton.setActionCommand("optionsPreferencesMenuItem");

// -- right side
add(Box.createGlue());
add(searchButton);
add(settingsButton);

searchButton.addActionListener(actionEvent -> {
mainWindowMenuHandler.editFindInProjectMenuItemActionPerformed();
Core.getMainMenu().invokeAction(actionEvent);
});
settingsButton.addActionListener(actionEvent -> {
mainWindowMenuHandler.optionsPreferencesMenuItemActionPerformed();
Core.getMainMenu().invokeAction(actionEvent);
});
recentProjectCB.addActionListener(actionEvent -> {
// when select a project from the list, we open it.
Expand All @@ -153,13 +152,13 @@ public void initComponents() {
if (projectUri.getScheme().equals("omegat")) {
switch (projectUri.getSchemeSpecificPart()) {
case "new":
mainWindowMenuHandler.projectNewMenuItemActionPerformed();
Core.getMainWindow().getMainMenu().invokeAction("projectNewMenuItem", 0);
break;
case "open":
mainWindowMenuHandler.projectOpenMenuItemActionPerformed();
Core.getMainWindow().getMainMenu().invokeAction("projectOpenMenuItem", 0);
break;
case "team":
mainWindowMenuHandler.projectTeamNewMenuItemActionPerformed();
Core.getMainWindow().getMainMenu().invokeAction("projectTeamNewMenuItem", 0);
break;
default:
break;
Expand Down
14 changes: 10 additions & 4 deletions src/org/omegat/gui/editor/EditorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@
import org.omegat.gui.editor.mark.ComesFromMTMarker;
import org.omegat.gui.editor.mark.EntryMarks;
import org.omegat.gui.editor.mark.Mark;
import org.omegat.gui.main.BaseMainWindowMenu;
import org.omegat.gui.main.DockablePanel;
import org.omegat.gui.main.IMainMenu;
import org.omegat.gui.main.MainWindow;
import org.omegat.gui.main.MainWindowStatusBar;
import org.omegat.gui.main.ProjectUICommands;
Expand Down Expand Up @@ -857,10 +859,14 @@ public void activateEntry(CaretPosition pos) {

private void setMenuEnabled() {
// update history menu items
mw.menu.gotoHistoryBackMenuItem.setEnabled(history.hasPrev());
mw.menu.gotoHistoryForwardMenuItem.setEnabled(history.hasNext());
mw.menu.editMultipleDefault.setEnabled(!m_docSegList[displayedEntryIndex].isDefaultTranslation());
mw.menu.editMultipleAlternate.setEnabled(m_docSegList[displayedEntryIndex].isDefaultTranslation());
IMainMenu menu = Core.getMainMenu();
if (menu instanceof BaseMainWindowMenu) {
BaseMainWindowMenu mainMenu = (BaseMainWindowMenu) menu;
mainMenu.gotoHistoryBackMenuItem.setEnabled(history.hasPrev());
mainMenu.gotoHistoryForwardMenuItem.setEnabled(history.hasNext());
mainMenu.editMultipleDefault.setEnabled(!m_docSegList[displayedEntryIndex].isDefaultTranslation());
mainMenu.editMultipleAlternate.setEnabled(m_docSegList[displayedEntryIndex].isDefaultTranslation());
}
}

/**
Expand Down
16 changes: 3 additions & 13 deletions src/org/omegat/gui/glossary/GlossaryTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,8 @@ 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());
}
});
openFile.setActionCommand("projectAccessWritableGlossaryMenuItem");
openFile.addActionListener(e -> Core.getMainWindow().getMainMenu().invokeAction(e));
openFile.setEnabled(false);
if (Core.getProject().isProjectLoaded()) {
String glossaryPath = Core.getProject().getProjectProperties().getWriteableGlossary();
Expand All @@ -428,12 +423,7 @@ public void actionPerformed(ActionEvent e) {
final JMenuItem notify = new JCheckBoxMenuItem(
OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_NOTIFICATIONS"));
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());
}
});
notify.addActionListener(e -> Preferences.setPreference(Preferences.NOTIFY_GLOSSARY_HITS, notify.isSelected()));
menu.add(notify);
menu.addSeparator();
final JMenuItem sortOrderSrcLength = new JCheckBoxMenuItem(
Expand Down
54 changes: 30 additions & 24 deletions src/org/omegat/gui/main/BaseMainWindowMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,28 @@ public abstract class BaseMainWindowMenu implements ActionListener, MenuListener
/** MainWindow instance. */
protected final IMainWindow mainWindow;

public JMenuBar getMainMenu() {
return mainMenu;
}

/** menu bar instance */
protected final JMenuBar mainMenu = new JMenuBar();

/** MainWindow menu handler instance. */
protected final BaseMainWindowMenuHandler mainWindowMenuHandler;
protected final MainWindowMenuHandler mainWindowMenuHandler;

private final Map<MenuExtender.MenuKey, JMenu> menus = new EnumMap<>(MenuExtender.MenuKey.class);

public BaseMainWindowMenu(final IMainWindow mainWindow,
final BaseMainWindowMenuHandler mainWindowMenuHandler) {
final MainWindowMenuHandler mainWindowMenuHandler) {
this.mainWindow = mainWindow;
this.mainWindowMenuHandler = mainWindowMenuHandler;
}

@Override
public void actionPerformed(ActionEvent evt) {
// Get item name from actionCommand.
String action = evt.getActionCommand();

Log.logInfoRB("LOG_MENU_CLICK", action);

invokeAction(action, evt.getModifiers());
Log.logInfoRB("LOG_MENU_CLICK", evt.getActionCommand());
invokeAction(evt);
}

/**
Expand Down Expand Up @@ -658,12 +658,13 @@ public void menuCanceled(MenuEvent e) {

String key = "findInProjectReuseLastWindow";
KeyStroke stroke = PropertiesShortcuts.getMainMenuShortcuts().getKeyStroke(key);
mainWindow.getApplicationFrame().getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, key);
mainWindow.getApplicationFrame().getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(stroke, key);
mainWindow.getApplicationFrame().getRootPane().getActionMap().put(key, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Log.logInfoRB("LOG_MENU_CLICK", key);
mainWindowMenuHandler.findInProjectReuseLastWindow();
MainWindowUI.findInProjectReuseLastWindow();
}
});

Expand Down Expand Up @@ -704,22 +705,27 @@ public void onApplicationShutdown() {
*/
@Override
public void invokeAction(String action, int modifiers) {
// Find method by item name.
String methodName = action + "ActionPerformed";
Method method = null;
ActionEvent evt = new ActionEvent("", 0, action, 0L, modifiers);
invokeAction(evt);
}

@Override
public void invokeAction(ActionEvent evt) {
/*
* Get an item name from actionCommand. And find a method by the item
* name.
*/
String methodName = evt.getActionCommand() + "ActionPerformed";
Method method;
try {
method = mainWindowMenuHandler.getClass().getMethod(methodName);
method = mainWindowMenuHandler.getClass().getMethod(methodName, ActionEvent.class);
} catch (NoSuchMethodException ignore) {
try {
method = mainWindowMenuHandler.getClass().getMethod(methodName, Integer.TYPE);
} catch (NoSuchMethodException ex) {
throw new IncompatibleClassChangeError(
"Error invoke method handler for main menu: there is no method " + methodName);
}
throw new IncompatibleClassChangeError(
"Error invoke method handler for main menu: there is no method " + methodName);
}

// Call ...MenuItemActionPerformed method.
Object[] args = method.getParameterTypes().length == 0 ? null : new Object[] { modifiers };
Object[] args = new Object[] { evt };
try {
method.invoke(mainWindowMenuHandler, args);
} catch (IllegalAccessException ex) {
Expand Down Expand Up @@ -933,10 +939,10 @@ protected void updateCheckboxesOnStart() {
protected void initMacSpecific() {
try {
// MacOSX-specific
OSXIntegration.setQuitHandler(e -> mainWindowMenuHandler.projectExitMenuItemActionPerformed());
OSXIntegration.setAboutHandler(e -> mainWindowMenuHandler.helpAboutMenuItemActionPerformed());
OSXIntegration.setQuitHandler(e -> MainWindowUI.projectExit());
OSXIntegration.setAboutHandler(mainWindowMenuHandler::helpAboutMenuItemActionPerformed);
OSXIntegration.setPreferencesHandler(
e -> mainWindowMenuHandler.optionsPreferencesMenuItemActionPerformed());
mainWindowMenuHandler::optionsPreferencesMenuItemActionPerformed);
} catch (NoClassDefFoundError e) {
Log.log(e);
}
Expand Down
Loading
Loading