Skip to content

Commit

Permalink
refactor: SearchWindowController manage its instance
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed May 3, 2024
1 parent e4248fe commit 80bf094
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 131 deletions.
13 changes: 0 additions & 13 deletions src/org/omegat/gui/main/ConsoleWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 0 additions & 8 deletions src/org/omegat/gui/main/IMainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -206,10 +204,4 @@ public interface IMainWindow {
* Retrieve main docking desktop.
*/
DockingDesktop getDesktop();

void doRecycleTrans();

void doInsertTrans();

void addSearchWindow(SearchWindowController search);
}
48 changes: 5 additions & 43 deletions src/org/omegat/gui/main/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,9 +119,6 @@ public class MainWindow implements IMainWindow {
*/
private FontUIResource font;

/** Set of all open search windows. */
private final List<SearchWindowController> searches = new ArrayList<>();

protected MainWindowStatusBar mainWindowStatusBar;

protected DockingDesktop desktop;
Expand All @@ -144,7 +138,7 @@ public MainWindow() throws IOException {
CoreEvents.registerProjectChangeListener(eventType -> {
updateTitle();
if (eventType == IProjectEventListener.PROJECT_CHANGE_TYPE.CLOSE) {
closeSearchWindows();
SearchWindowController.closeSearchWindows();
}
});

Expand Down Expand Up @@ -208,7 +202,7 @@ private void initMainMenu() {

applicationFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MainWindowMenuHandler.projectExitAction();
MainWindowMenuHandler.projectExitAction();
}

@Override
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<SearchWindowController> getSearchWindows() {
return Collections.unmodifiableList(searches);
}

/**
* {@inheritDoc}
*/
Expand Down
45 changes: 25 additions & 20 deletions src/org/omegat/gui/main/MainWindowMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand All @@ -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();
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -934,7 +937,7 @@ public EditOverwriteTranslationMenuItemAction() {

@Override
public void actionPerformed(final ActionEvent e) {
Core.getMainWindow().doRecycleTrans();
MainWindow.doRecycleTrans();
}
}

Expand All @@ -947,7 +950,7 @@ public EditInsertTranslationMenuItemAction() {

@Override
public void actionPerformed(final ActionEvent e) {
Core.getMainWindow().doInsertTrans();
MainWindow.doInsertTrans();
}
}

Expand Down Expand Up @@ -1102,40 +1105,41 @@ 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() {
if (!Core.getProject().isProjectLoaded()) {
return;
}

List<SearchWindowController> windows = mainWindow.getSearchWindows();
List<SearchWindowController> windows = SearchWindowController.getSearchWindows();
for (int i = windows.size() - 1; i >= 0; i--) {
SearchWindowController swc = windows.get(i);
if (swc.getMode() == SearchMode.SEARCH) {
swc.makeVisible(getTrimmedSelectedTextInMainWindow());
return;
}
}
editFindInProject();
if (!Core.getProject().isProjectLoaded()) {
return;
}
SearchWindowController search = new SearchWindowController(SearchMode.SEARCH);
SearchWindowController.addSearchWindow(search);
search.makeVisible(getTrimmedSelectedTextInMainWindow());
}

@SuppressWarnings("serial")
Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 0 additions & 20 deletions src/org/omegat/gui/main/MainWindowUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/org/omegat/gui/search/SearchWindowController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SearchWindowController> 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<SearchWindowController> getSearchWindows() {
return Collections.unmodifiableList(searches);
}

public SearchWindowController(SearchMode mode) {
form = new SearchWindowForm();
form.setJMenuBar(new SearchWindowMenu(this));
Expand Down
Loading

0 comments on commit 80bf094

Please sign in to comment.