From cb487133d830e1ddb55f66c69896fc517753bdae Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Fri, 3 May 2024 18:08:36 +0900 Subject: [PATCH] refactor: SearchWindowController to manage its instances 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 | 46 ++----------------- .../gui/main/MainWindowMenuHandler.java | 29 ++++++------ src/org/omegat/gui/main/MainWindowUI.java | 20 -------- .../gui/search/SearchWindowController.java | 38 +++++++++++++++ .../core/data/TestTeamIntegrationChild.java | 12 ----- test/fixtures/org/omegat/core/TestCore.java | 12 ----- 8 files changed, 56 insertions(+), 122 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..67b16efd0c 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(); } }); @@ -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..76eec1d594 100644 --- a/src/org/omegat/gui/main/MainWindowMenuHandler.java +++ b/src/org/omegat/gui/main/MainWindowMenuHandler.java @@ -53,7 +53,6 @@ import javax.swing.UIManager; import javax.swing.text.JTextComponent; -import org.omegat.util.gui.ResourcesUtil; import org.openide.awt.AbstractMnemonicsAction; import org.omegat.Main; @@ -97,6 +96,7 @@ import org.omegat.util.TagUtil; import org.omegat.util.TagUtil.Tag; import org.omegat.util.gui.DesktopWrapper; +import org.omegat.util.gui.ResourcesUtil; /** * Handler for main menu items. @@ -934,7 +934,7 @@ public EditOverwriteTranslationMenuItemAction() { @Override public void actionPerformed(final ActionEvent e) { - Core.getMainWindow().doRecycleTrans(); + MainWindow.doRecycleTrans(); } } @@ -947,7 +947,7 @@ public EditInsertTranslationMenuItemAction() { @Override public void actionPerformed(final ActionEvent e) { - Core.getMainWindow().doInsertTrans(); + MainWindow.doInsertTrans(); } } @@ -1108,18 +1108,13 @@ public EditFindInProjectMenuItemAction() { @Override public void actionPerformed(ActionEvent e) { - editFindInProject(); - } - } - - private static void editFindInProject() { - if (!Core.getProject().isProjectLoaded()) { + 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 +1122,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 +1130,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 +1152,7 @@ public void actionPerformed(final ActionEvent e) { return; } SearchWindowController search = new SearchWindowController(SearchMode.REPLACE); - Core.getMainWindow().addSearchWindow(search); - + SearchWindowController.addSearchWindow(search); search.makeVisible(getTrimmedSelectedTextInMainWindow()); } } 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..81ad7dc1fc 100644 --- a/test-integration/src/org/omegat/core/data/TestTeamIntegrationChild.java +++ b/test-integration/src/org/omegat/core/data/TestTeamIntegrationChild.java @@ -679,18 +679,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..78e9ccfc5d 100644 --- a/test/fixtures/org/omegat/core/TestCore.java +++ b/test/fixtures/org/omegat/core/TestCore.java @@ -275,18 +275,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; }