From 80bf0949c08f7589b4bde2acafa75bf37a6f8324 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Fri, 3 May 2024 18:19:39 +0900 Subject: [PATCH] refactor: SearchWindowController manage its instance Signed-off-by: Hiroshi Miura --- src/org/omegat/gui/main/ConsoleWindow.java | 13 ----- src/org/omegat/gui/main/IMainWindow.java | 8 ---- src/org/omegat/gui/main/MainWindow.java | 48 ++----------------- .../gui/main/MainWindowMenuHandler.java | 45 +++++++++-------- src/org/omegat/gui/main/MainWindowUI.java | 20 -------- .../gui/search/SearchWindowController.java | 38 +++++++++++++++ .../core/data/TestTeamIntegrationChild.java | 13 ----- test/fixtures/org/omegat/core/TestCore.java | 13 ----- .../omegat/gui/main/MainWindowMenuTest.java | 1 - 9 files changed, 68 insertions(+), 131 deletions(-) diff --git a/src/org/omegat/gui/main/ConsoleWindow.java b/src/org/omegat/gui/main/ConsoleWindow.java index 8fc23ea093..30aa0a3003 100644 --- a/src/org/omegat/gui/main/ConsoleWindow.java +++ b/src/org/omegat/gui/main/ConsoleWindow.java @@ -31,7 +31,6 @@ import javax.swing.JFrame; -import org.omegat.gui.search.SearchWindowController; import org.omegat.util.OStrings; import org.omegat.util.RuntimePreferences; import org.omegat.util.StringUtil; @@ -143,18 +142,6 @@ public DockingDesktop getDesktop() { return null; } - @Override - public void doRecycleTrans() { - } - - @Override - public void doInsertTrans() { - } - - @Override - public void addSearchWindow(final SearchWindowController search) { - } - public Cursor getCursor() { return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); } diff --git a/src/org/omegat/gui/main/IMainWindow.java b/src/org/omegat/gui/main/IMainWindow.java index 3de35051b1..50b7f7b6ba 100644 --- a/src/org/omegat/gui/main/IMainWindow.java +++ b/src/org/omegat/gui/main/IMainWindow.java @@ -31,8 +31,6 @@ import javax.swing.JFrame; -import org.omegat.gui.search.SearchWindowController; - import com.vlsolutions.swing.docking.Dockable; import com.vlsolutions.swing.docking.DockingDesktop; @@ -206,10 +204,4 @@ public interface IMainWindow { * Retrieve main docking desktop. */ DockingDesktop getDesktop(); - - void doRecycleTrans(); - - void doInsertTrans(); - - void addSearchWindow(SearchWindowController search); } diff --git a/src/org/omegat/gui/main/MainWindow.java b/src/org/omegat/gui/main/MainWindow.java index d2bec5a209..1b7e7d586f 100644 --- a/src/org/omegat/gui/main/MainWindow.java +++ b/src/org/omegat/gui/main/MainWindow.java @@ -48,10 +48,7 @@ import java.awt.event.WindowEvent; import java.io.IOException; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; -import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; @@ -122,9 +119,6 @@ public class MainWindow implements IMainWindow { */ private FontUIResource font; - /** Set of all open search windows. */ - private final List searches = new ArrayList<>(); - protected MainWindowStatusBar mainWindowStatusBar; protected DockingDesktop desktop; @@ -144,7 +138,7 @@ public MainWindow() throws IOException { CoreEvents.registerProjectChangeListener(eventType -> { updateTitle(); if (eventType == IProjectEventListener.PROJECT_CHANGE_TYPE.CLOSE) { - closeSearchWindows(); + SearchWindowController.closeSearchWindows(); } }); @@ -208,7 +202,7 @@ private void initMainMenu() { applicationFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { - MainWindowMenuHandler.projectExitAction(); + MainWindowMenuHandler.projectExitAction(); } @Override @@ -297,7 +291,7 @@ private void updateTitle() { } /** insert current fuzzy match or selection at cursor position */ - public void doInsertTrans() { + public static void doInsertTrans() { if (!Core.getProject().isProjectLoaded()) { return; } @@ -329,7 +323,7 @@ public void doInsertTrans() { } /** replace entire edit area with active fuzzy match or selection */ - public void doRecycleTrans() { + public static void doRecycleTrans() { if (!Core.getProject().isProjectLoaded()) { return; } @@ -357,43 +351,11 @@ public void doRecycleTrans() { } } - private String getSelectedTextInMatcher() { + private static String getSelectedTextInMatcher() { IMatcher matcher = Core.getMatcher(); return matcher instanceof JTextComponent ? ((JTextComponent) matcher).getSelectedText() : null; } - public void addSearchWindow(final SearchWindowController newSearchWindow) { - newSearchWindow.addWindowListener(new WindowAdapter() { - @Override - public void windowClosed(WindowEvent e) { - removeSearchWindow(newSearchWindow); - } - }); - synchronized (searches) { - searches.add(newSearchWindow); - } - } - - private void removeSearchWindow(SearchWindowController searchWindow) { - synchronized (searches) { - searches.remove(searchWindow); - } - } - - private void closeSearchWindows() { - synchronized (searches) { - // dispose other windows - for (SearchWindowController sw : searches) { - sw.dispose(); - } - searches.clear(); - } - } - - protected List getSearchWindows() { - return Collections.unmodifiableList(searches); - } - /** * {@inheritDoc} */ diff --git a/src/org/omegat/gui/main/MainWindowMenuHandler.java b/src/org/omegat/gui/main/MainWindowMenuHandler.java index a50b6385ac..a0f4aaaeb9 100644 --- a/src/org/omegat/gui/main/MainWindowMenuHandler.java +++ b/src/org/omegat/gui/main/MainWindowMenuHandler.java @@ -415,6 +415,7 @@ public void actionPerformed(final ActionEvent e) { /** * Check whether tags are OK + * * @return false is there is a tag issue, true otherwise */ private static boolean checkTags() { @@ -802,7 +803,8 @@ protected static void projectExitAction() { private static void openFile(File path) { try { - path = path.getCanonicalFile(); // Normalize file name in case it is displayed + path = path.getCanonicalFile(); // Normalize file name in case it is + // displayed } catch (Exception ex) { // Ignore } @@ -820,7 +822,8 @@ private static void openFile(File path) { private static void prepareForExit(Runnable onCompletion) { // Bug #902: commit the current entry first - // We do it before checking project status, so that it can eventually change it + // We do it before checking project status, so that it can eventually + // change it if (Core.getProject().isProjectLoaded()) { Core.getEditor().commitAndLeave(); } @@ -832,9 +835,9 @@ private static void prepareForExit(Runnable onCompletion) { // RFE 1302358 // Add Yes/No Warning before OmegaT quits if (projectModified || Preferences.isPreference(Preferences.ALWAYS_CONFIRM_QUIT)) { - if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(Core.getMainWindow().getApplicationFrame(), - OStrings.getString("MW_QUIT_CONFIRM"), OStrings.getString("CONFIRM_DIALOG_TITLE"), - JOptionPane.YES_NO_OPTION)) { + if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog( + Core.getMainWindow().getApplicationFrame(), OStrings.getString("MW_QUIT_CONFIRM"), + OStrings.getString("CONFIRM_DIALOG_TITLE"), JOptionPane.YES_NO_OPTION)) { return; } } @@ -934,7 +937,7 @@ public EditOverwriteTranslationMenuItemAction() { @Override public void actionPerformed(final ActionEvent e) { - Core.getMainWindow().doRecycleTrans(); + MainWindow.doRecycleTrans(); } } @@ -947,7 +950,7 @@ public EditInsertTranslationMenuItemAction() { @Override public void actionPerformed(final ActionEvent e) { - Core.getMainWindow().doInsertTrans(); + MainWindow.doInsertTrans(); } } @@ -1102,24 +1105,20 @@ public static class EditFindInProjectMenuItemAction extends AbstractMnemonicsAct public EditFindInProjectMenuItemAction() { super(OStrings.getString("TF_MENU_EDIT_FIND"), OStrings.getLocale()); putValue(Action.ACTION_COMMAND_KEY, "EditFindInProjectMenuItem"); - putValue(Action.SMALL_ICON, Objects.requireNonNullElseGet(UIManager.getIcon("OmegaT.newUI.search.icon"), - () -> MainMenuIcons.newImageIcon(ResourcesUtil.getBundledImage("newUI.search.png")))); + putValue(Action.SMALL_ICON, Objects.requireNonNullElseGet( + UIManager.getIcon("OmegaT.newUI.search.icon"), + () -> MainMenuIcons.newImageIcon(ResourcesUtil.getBundledImage("newUI.search.png")))); } @Override public void actionPerformed(ActionEvent e) { - editFindInProject(); - } - } - - private static void editFindInProject() { if (!Core.getProject().isProjectLoaded()) { return; } SearchWindowController search = new SearchWindowController(SearchMode.SEARCH); - Core.getMainWindow().addSearchWindow(search); - + SearchWindowController.addSearchWindow(search); search.makeVisible(getTrimmedSelectedTextInMainWindow()); + } } void findInProjectReuseLastWindow() { @@ -1127,7 +1126,7 @@ void findInProjectReuseLastWindow() { return; } - List windows = mainWindow.getSearchWindows(); + List windows = SearchWindowController.getSearchWindows(); for (int i = windows.size() - 1; i >= 0; i--) { SearchWindowController swc = windows.get(i); if (swc.getMode() == SearchMode.SEARCH) { @@ -1135,7 +1134,12 @@ void findInProjectReuseLastWindow() { return; } } - editFindInProject(); + if (!Core.getProject().isProjectLoaded()) { + return; + } + SearchWindowController search = new SearchWindowController(SearchMode.SEARCH); + SearchWindowController.addSearchWindow(search); + search.makeVisible(getTrimmedSelectedTextInMainWindow()); } @SuppressWarnings("serial") @@ -1152,8 +1156,7 @@ public void actionPerformed(final ActionEvent e) { return; } SearchWindowController search = new SearchWindowController(SearchMode.REPLACE); - Core.getMainWindow().addSearchWindow(search); - + SearchWindowController.addSearchWindow(search); search.makeVisible(getTrimmedSelectedTextInMainWindow()); } } @@ -1263,6 +1266,7 @@ public EditSelectFuzzyPrevMenuItemAction() { super(OStrings.getString("TF_MENU_EDIT_COMPARE_PREV"), OStrings.getLocale()); putValue(Action.ACTION_COMMAND_KEY, "EditSelectFuzzyPrevMenuItem"); } + @Override public void actionPerformed(ActionEvent e) { Core.getMatcher().setPrevActiveMatch(); @@ -1487,6 +1491,7 @@ public GotoNextUntranslatedMenuItemAction() { super(OStrings.getString("TF_MENU_EDIT_UNTRANS"), OStrings.getLocale()); putValue(Action.ACTION_COMMAND_KEY, "GotoNextUntranslatedMenuItem"); } + @Override public void actionPerformed(ActionEvent e) { Core.getEditor().nextUntranslatedEntry(); diff --git a/src/org/omegat/gui/main/MainWindowUI.java b/src/org/omegat/gui/main/MainWindowUI.java index 13dbea1eee..f8bbf84ea4 100644 --- a/src/org/omegat/gui/main/MainWindowUI.java +++ b/src/org/omegat/gui/main/MainWindowUI.java @@ -49,10 +49,6 @@ import org.omegat.util.StaticUtils; import org.omegat.util.gui.UIDesignManager; -import com.vlsolutions.swing.docking.DockingDesktop; -import com.vlsolutions.swing.docking.event.DockableStateWillChangeEvent; -import com.vlsolutions.swing.docking.event.DockableStateWillChangeListener; - /** * Class for initialize, load/save, etc. for main window UI components. * @@ -74,22 +70,6 @@ private MainWindowUI() { public static final String UI_LAYOUT_FILE = "uiLayout" + OStrings.getBrandingToken() + ".xml"; - /** - * Create docking desktop panel. - */ - public static DockingDesktop initDocking(final MainWindow mainWindow) { - mainWindow.desktop = new DockingDesktop(); - mainWindow.desktop.addDockableStateWillChangeListener(new DockableStateWillChangeListener() { - public void dockableStateWillChange(DockableStateWillChangeEvent event) { - if (event.getFutureState().isClosed()) { - event.cancel(); - } - } - }); - - return mainWindow.desktop; - } - /** * Installs a {@link IProjectEventListener} that handles loading, storing, * and restoring the main window layout when a project-specific layout is diff --git a/src/org/omegat/gui/search/SearchWindowController.java b/src/org/omegat/gui/search/SearchWindowController.java index 4d94ebd20f..52923daa81 100644 --- a/src/org/omegat/gui/search/SearchWindowController.java +++ b/src/org/omegat/gui/search/SearchWindowController.java @@ -46,8 +46,11 @@ import java.text.MessageFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; import java.util.Date; +import java.util.List; import javax.swing.AbstractAction; import javax.swing.DefaultComboBoxModel; @@ -115,6 +118,41 @@ public class SearchWindowController { private final int initialEntry; private final CaretPosition initialCaret; + /** Set of all open search windows. */ + private static final List searches = new ArrayList<>(); + + public static void addSearchWindow(final SearchWindowController newSearchWindow) { + newSearchWindow.addWindowListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + removeSearchWindow(newSearchWindow); + } + }); + synchronized (searches) { + searches.add(newSearchWindow); + } + } + + private static void removeSearchWindow(SearchWindowController searchWindow) { + synchronized (searches) { + searches.remove(searchWindow); + } + } + + public static void closeSearchWindows() { + synchronized (searches) { + // dispose other windows + for (SearchWindowController sw : searches) { + sw.dispose(); + } + searches.clear(); + } + } + + public static List getSearchWindows() { + return Collections.unmodifiableList(searches); + } + public SearchWindowController(SearchMode mode) { form = new SearchWindowForm(); form.setJMenuBar(new SearchWindowMenu(this)); diff --git a/test-integration/src/org/omegat/core/data/TestTeamIntegrationChild.java b/test-integration/src/org/omegat/core/data/TestTeamIntegrationChild.java index 86d94bba38..aeada09dd3 100644 --- a/test-integration/src/org/omegat/core/data/TestTeamIntegrationChild.java +++ b/test-integration/src/org/omegat/core/data/TestTeamIntegrationChild.java @@ -72,7 +72,6 @@ import org.omegat.gui.glossary.IGlossaries; import org.omegat.gui.main.IMainMenu; import org.omegat.gui.main.IMainWindow; -import org.omegat.gui.search.SearchWindowController; import org.omegat.util.Log; import org.omegat.util.OConsts; import org.omegat.util.OStrings; @@ -679,18 +678,6 @@ public DockingDesktop getDesktop() { return null; } - @Override - public void doRecycleTrans() { - } - - @Override - public void doInsertTrans() { - } - - @Override - public void addSearchWindow(final SearchWindowController search) { - } - public Cursor getCursor() { return null; } diff --git a/test/fixtures/org/omegat/core/TestCore.java b/test/fixtures/org/omegat/core/TestCore.java index 4880d11e1a..bda24d9a09 100644 --- a/test/fixtures/org/omegat/core/TestCore.java +++ b/test/fixtures/org/omegat/core/TestCore.java @@ -51,7 +51,6 @@ import org.omegat.gui.editor.mark.Mark; import org.omegat.gui.main.IMainMenu; import org.omegat.gui.main.IMainWindow; -import org.omegat.gui.search.SearchWindowController; import org.omegat.util.Platform; import org.omegat.util.TestPreferencesInitializer; import org.omegat.util.gui.MenuExtender; @@ -275,18 +274,6 @@ public DockingDesktop getDesktop() { return null; } - @Override - public void doRecycleTrans() { - } - - @Override - public void doInsertTrans() { - } - - @Override - public void addSearchWindow(final SearchWindowController search) { - } - public Cursor getCursor() { return null; } diff --git a/test/src/org/omegat/gui/main/MainWindowMenuTest.java b/test/src/org/omegat/gui/main/MainWindowMenuTest.java index 6f18960c6a..914d337184 100644 --- a/test/src/org/omegat/gui/main/MainWindowMenuTest.java +++ b/test/src/org/omegat/gui/main/MainWindowMenuTest.java @@ -342,4 +342,3 @@ protected void assertMenuKeyMatches(String[] targets) throws Exception { }); } } -