Skip to content

Commit

Permalink
feat: add IMainWindow#getSelectedText
Browse files Browse the repository at this point in the history
- Introduce API IMainWindow#getSelectedText
- MainWindow#getSelectedText calls MainWindowUI.getTrimmedSelectedTextInMainWindow as implementation
- Menu handler use it
- SearchWindowManager#createSearch use it

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Jun 15, 2024
1 parent 190cfae commit 16e7c5e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/org/omegat/gui/main/IMainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ public interface IMainWindow {
*/
Cursor getCursor();

default String getSelectedText() {
return "";
}

/**
* Retrieve main manu instance.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/org/omegat/gui/main/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ public Cursor getCursor() {
return applicationFrame.getCursor();
}

@Override
public String getSelectedText() {
return MainWindowUI.getTrimmedSelectedTextInMainWindow(this);
}

/**
* Sets the title of the main window appropriately
*/
Expand Down
5 changes: 4 additions & 1 deletion src/org/omegat/gui/main/MainWindowMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,10 @@ void findInProjectReuseLastWindow() {
if (!Core.getProject().isProjectLoaded()) {
return;
}
String text = MainWindowUI.getTrimmedSelectedTextInMainWindow();
String text = Core.getMainWindow().getSelectedText();
if (text == null) {
return;
}
if (!SearchWindowManager.reuseSearchWindow(text)) {
SearchWindowManager.createSearchWindow(SearchMode.SEARCH, text);
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/omegat/gui/main/MainWindowUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ public static void resetDesktopLayout(MainWindow mainWindow) {
}
}

public static String getTrimmedSelectedTextInMainWindow() {
public static String getTrimmedSelectedTextInMainWindow(MainWindow mainWindow) {
String selection = null;
Component component = Core.getMainWindow().getApplicationFrame().getMostRecentFocusOwner();
Component component = mainWindow.getApplicationFrame().getMostRecentFocusOwner();
if (component instanceof JTextComponent) {
selection = ((JTextComponent) component).getSelectedText();
if (!StringUtil.isEmpty(selection)) {
Expand Down
5 changes: 3 additions & 2 deletions src/org/omegat/gui/search/SearchWindowManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

package org.omegat.gui.search;

import org.omegat.core.Core;
import org.omegat.core.search.SearchMode;
import org.omegat.gui.main.MainWindowUI;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
Expand All @@ -41,7 +41,8 @@ public final class SearchWindowManager {
private static final List<SearchWindowController> searches = new ArrayList<>();

public static void createSearchWindow(SearchMode mode) {
createSearchWindow(mode, MainWindowUI.getTrimmedSelectedTextInMainWindow());
String text = Core.getMainWindow().getSelectedText();
createSearchWindow(mode, text);
}

public static void createSearchWindow(SearchMode mode, String query) {
Expand Down

0 comments on commit 16e7c5e

Please sign in to comment.