Skip to content

Commit

Permalink
refactor: update TestMainWindowMenuHandler
Browse files Browse the repository at this point in the history
- Add more actions
- Extend IMainWindow#addSearchWindow and IMainWindow#getSearchWindows
- Move internal TestMainWindow class as regular class
- refactor ProjectUICommands.doRecycleTrans and ProjectUICommands.doInsertTrans

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Mar 6, 2024
1 parent 175f60d commit cc164e1
Show file tree
Hide file tree
Showing 11 changed files with 783 additions and 368 deletions.
12 changes: 12 additions & 0 deletions src/org/omegat/gui/main/ConsoleWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import java.awt.Cursor;
import java.awt.Font;
import java.awt.HeadlessException;
import java.util.Collections;
import java.util.List;

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 @@ -146,6 +149,15 @@ public DockingDesktop getDesktop() {
public void resetDesktopLayout() {
}

@Override
public void addSearchWindow(final SearchWindowController newSearchWindow) {
}

@Override
public List<SearchWindowController> getSearchWindows() {
return Collections.emptyList();
}

public Cursor getCursor() {
return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
}
Expand Down
7 changes: 7 additions & 0 deletions src/org/omegat/gui/main/IMainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import java.awt.Cursor;
import java.awt.Font;
import java.awt.HeadlessException;
import java.util.List;

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,4 +209,8 @@ public interface IMainWindow {
DockingDesktop getDesktop();

void resetDesktopLayout();

void addSearchWindow(SearchWindowController newSearchWindow);

List<SearchWindowController> getSearchWindows();
}
73 changes: 2 additions & 71 deletions src/org/omegat/gui/main/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,11 @@
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.JTextComponent;

import org.omegat.core.Core;
import org.omegat.core.CoreEvents;
import org.omegat.core.data.DataUtils;
import org.omegat.core.events.IApplicationEventListener;
import org.omegat.core.events.IProjectEventListener;
import org.omegat.core.matching.NearString;
import org.omegat.gui.matches.IMatcher;
import org.omegat.gui.search.SearchWindowController;
import org.omegat.util.Log;
import org.omegat.util.OConsts;
Expand Down Expand Up @@ -356,72 +352,7 @@ private void updateTitle() {
applicationFrame.setTitle(s);
}

/** insert current fuzzy match or selection at cursor position */
public void doInsertTrans() {
if (!Core.getProject().isProjectLoaded()) {
return;
}

String text = getSelectedTextInMatcher();
boolean fromMT = false;
if (StringUtil.isEmpty(text)) {
NearString near = Core.getMatcher().getActiveMatch();
if (near != null) {
text = near.translation;
if (Preferences.isPreference(Preferences.CONVERT_NUMBERS)) {
text = Core.getMatcher().substituteNumbers(
Core.getEditor().getCurrentEntry().getSrcText(), near.source, near.translation);
}

if (DataUtils.isFromMTMemory(near)) {
fromMT = true;
}
}
}
if (!StringUtil.isEmpty(text)) {
if (fromMT) {
Core.getEditor().insertTextAndMark(text);
} else {
Core.getEditor().insertText(text);
}
Core.getEditor().requestFocus();
}
}

/** replace entire edit area with active fuzzy match or selection */
public void doRecycleTrans() {
if (!Core.getProject().isProjectLoaded()) {
return;
}

String selection = getSelectedTextInMatcher();
if (!StringUtil.isEmpty(selection)) {
Core.getEditor().replaceEditText(selection);
Core.getEditor().requestFocus();
return;
}

NearString near = Core.getMatcher().getActiveMatch();
if (near != null) {
String translation = near.translation;
if (Preferences.isPreference(Preferences.CONVERT_NUMBERS)) {
translation = Core.getMatcher().substituteNumbers(
Core.getEditor().getCurrentEntry().getSrcText(), near.source, near.translation);
}
if (DataUtils.isFromMTMemory(near)) {
Core.getEditor().replaceEditTextAndMark(translation, "TM:[tm/mt]");
} else {
Core.getEditor().replaceEditText(translation, "TM:[generic]");
}
Core.getEditor().requestFocus();
}
}

private String getSelectedTextInMatcher() {
IMatcher matcher = Core.getMatcher();
return matcher instanceof JTextComponent ? ((JTextComponent) matcher).getSelectedText() : null;
}

@Override
public void addSearchWindow(final SearchWindowController newSearchWindow) {
newSearchWindow.addWindowListener(new WindowAdapter() {
@Override
Expand Down Expand Up @@ -450,7 +381,7 @@ private void closeSearchWindows() {
}
}

protected List<SearchWindowController> getSearchWindows() {
public List<SearchWindowController> getSearchWindows() {
return Collections.unmodifiableList(searches);
}

Expand Down
4 changes: 2 additions & 2 deletions src/org/omegat/gui/main/MainWindowMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,11 @@ public void editRedoMenuItemActionPerformed() {
}

public void editOverwriteTranslationMenuItemActionPerformed() {
mainWindow.doRecycleTrans();
ProjectUICommands.doRecycleTrans();
}

public void editInsertTranslationMenuItemActionPerformed() {
mainWindow.doInsertTrans();
ProjectUICommands.doInsertTrans();
}

public void editOverwriteMachineTranslationMenuItemActionPerformed() {
Expand Down
75 changes: 75 additions & 0 deletions src/org/omegat/gui/main/ProjectUICommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.text.JTextComponent;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;

import org.omegat.CLIParameters;
import org.omegat.convert.ConvertProject;
import org.omegat.core.Core;
import org.omegat.core.CoreEvents;
import org.omegat.core.KnownException;
import org.omegat.core.data.DataUtils;
import org.omegat.core.data.ProjectFactory;
import org.omegat.core.data.ProjectProperties;
import org.omegat.core.events.IProjectEventListener;
import org.omegat.core.matching.NearString;
import org.omegat.core.segmentation.SRX;
import org.omegat.core.segmentation.Segmenter;
import org.omegat.core.team2.IRemoteRepository2;
Expand All @@ -69,6 +73,7 @@
import org.omegat.gui.dialogs.NewProjectFileChooser;
import org.omegat.gui.dialogs.NewTeamProjectController;
import org.omegat.gui.dialogs.ProjectPropertiesDialogController;
import org.omegat.gui.matches.IMatcher;
import org.omegat.util.FileUtil;
import org.omegat.util.FileUtil.ICollisionCallback;
import org.omegat.util.HttpConnectionUtils;
Expand Down Expand Up @@ -1344,4 +1349,74 @@ private static void processSwingWorkerException(Exception ex, String errorCode)
Core.getMainWindow().displayErrorRB(ex, errorCode);
}
}

/* Edit UI command helpers.
*/

/** insert current fuzzy match or selection at cursor position */
public static void doInsertTrans() {
if (!Core.getProject().isProjectLoaded()) {
return;
}

String text = getSelectedTextInMatcher();
boolean fromMT = false;
if (StringUtil.isEmpty(text)) {
NearString near = Core.getMatcher().getActiveMatch();
if (near != null) {
text = near.translation;
if (Preferences.isPreference(Preferences.CONVERT_NUMBERS)) {
text = Core.getMatcher().substituteNumbers(
Core.getEditor().getCurrentEntry().getSrcText(), near.source, near.translation);
}

if (DataUtils.isFromMTMemory(near)) {
fromMT = true;
}
}
}
if (!StringUtil.isEmpty(text)) {
if (fromMT) {
Core.getEditor().insertTextAndMark(text);
} else {
Core.getEditor().insertText(text);
}
Core.getEditor().requestFocus();
}
}

/** replace entire edit area with active fuzzy match or selection */
public static void doRecycleTrans() {
if (!Core.getProject().isProjectLoaded()) {
return;
}

String selection = getSelectedTextInMatcher();
if (!StringUtil.isEmpty(selection)) {
Core.getEditor().replaceEditText(selection);
Core.getEditor().requestFocus();
return;
}

NearString near = Core.getMatcher().getActiveMatch();
if (near != null) {
String translation = near.translation;
if (Preferences.isPreference(Preferences.CONVERT_NUMBERS)) {
translation = Core.getMatcher().substituteNumbers(
Core.getEditor().getCurrentEntry().getSrcText(), near.source, near.translation);
}
if (DataUtils.isFromMTMemory(near)) {
Core.getEditor().replaceEditTextAndMark(translation, "TM:[tm/mt]");
} else {
Core.getEditor().replaceEditText(translation, "TM:[generic]");
}
Core.getEditor().requestFocus();
}
}

private static String getSelectedTextInMatcher() {
IMatcher matcher = Core.getMatcher();
return matcher instanceof JTextComponent ? ((JTextComponent) matcher).getSelectedText() : null;
}

}
12 changes: 4 additions & 8 deletions test-acceptance/src/org/omegat/gui/GlossaryEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.File;
import java.nio.file.Files;

import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -42,23 +41,20 @@ public void testOpenGlossaryEntryDialog() throws Exception {
Preferences.setPreference(Preferences.GLOSSARY_SORT_BY_LENGTH, false);
Preferences.setPreference(Preferences.GLOSSARY_SORT_BY_SRC_LENGTH, false);
assertFalse(Preferences.isPreferenceDefault(Preferences.PROJECT_FILES_SHOW_ON_LOAD, true));
// 2. Operate Glossary preference from menu.
// 2. Open a sample project.
SwingUtilities.invokeAndWait(() -> ProjectUICommands.projectOpen(tmpDir));
// 3. Operate Glossary preference from menu.
Preferences.setPreference(Preferences.GLOSSARY_STEMMING, true);
window.menuItem(BaseMainWindowMenu.OPTIONS_MENU).click();
window.menuItem(BaseMainWindowMenu.OPTIONS_GLOSSARY_SUBMENU).click();
assertTrue(window.menuItem(BaseMainWindowMenu.OPTIONS_GLOSSARY_FUZZY_MATCHING_CHECKBOX_MENUITEM).target()
.getModel().isSelected());
window.menuItem(BaseMainWindowMenu.OPTIONS_GLOSSARY_FUZZY_MATCHING_CHECKBOX_MENUITEM).click();
// 3. toggle options > glossary menu
// 4. toggle options > glossary menu
window.menuItem(BaseMainWindowMenu.OPTIONS_MENU).click();
window.menuItem(BaseMainWindowMenu.OPTIONS_GLOSSARY_SUBMENU).click();
assertFalse(window.menuItem(BaseMainWindowMenu.OPTIONS_GLOSSARY_FUZZY_MATCHING_CHECKBOX_MENUITEM).target()
.getModel().isSelected());
// 4. Open a sample project.
SwingUtilities.invokeAndWait(() -> ProjectUICommands.projectOpen(tmpDir));
// Enforce "Edit" menu enabled.
final JMenuItem menuItem = window.menuItem(BaseMainWindowMenu.EDIT_MENU).target();
SwingUtilities.invokeAndWait(() -> menuItem.setEnabled(true));
// 5. Click menu Edit > Create glossary menu items.
window.menuItem(BaseMainWindowMenu.EDIT_MENU).click();
window.menuItem(BaseMainWindowMenu.EDIT_CREATE_GLOSSARY_MENUITEM).click();
Expand Down

0 comments on commit cc164e1

Please sign in to comment.