From 4628ca7c4bf516a549dc0e92fbecb0c4fa5ffc23 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Mon, 16 Dec 2024 15:28:18 +0900 Subject: [PATCH] reafactor: use StaticUIUtils#isGUI for env detections Signed-off-by: Hiroshi Miura --- .../core/team2/impl/GITCredentialsProvider.java | 15 +++++++-------- .../core/team2/impl/SVNAuthenticationManager.java | 14 +++++++------- .../org/omegat/core/TestCoreInitializer.java | 7 +++---- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/org/omegat/core/team2/impl/GITCredentialsProvider.java b/src/org/omegat/core/team2/impl/GITCredentialsProvider.java index e92364caf5..0767e07ec2 100644 --- a/src/org/omegat/core/team2/impl/GITCredentialsProvider.java +++ b/src/org/omegat/core/team2/impl/GITCredentialsProvider.java @@ -37,6 +37,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.swing.JFrame; import javax.swing.JOptionPane; import org.eclipse.jgit.errors.UnsupportedCredentialItem; @@ -49,7 +50,6 @@ import org.omegat.core.team2.gui.PassphraseDialog; import org.omegat.core.team2.gui.UserPassDialog; import org.omegat.core.team2.impl.TeamUtils.Credentials; -import org.omegat.gui.main.IMainWindow; import org.omegat.util.Log; import org.omegat.util.OStrings; import org.omegat.util.gui.StaticUIUtils; @@ -281,8 +281,7 @@ public boolean supports(CredentialItem... items) { } private void askYesNoGUI(CredentialItem item, String promptText, URIish uri, String promptedFingerprint) { - IMainWindow mw = Core.getMainWindow(); - int choice = mw.showConfirmDialog(promptText, null, JOptionPane.YES_NO_OPTION, + int choice = Core.getMainWindow().showConfirmDialog(promptText, null, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (choice == JOptionPane.YES_OPTION) { ((CredentialItem.YesNoType) item).setValue(true); @@ -325,10 +324,10 @@ private void askYesNo(CredentialItem item, String promptText, URIish uri, String */ private Credentials askCredentialsGUI(URIish uri, Credentials credentials, boolean passwordOnly, String msg) { - IMainWindow mw = Core.getMainWindow(); + JFrame parent = Core.getMainWindow().getApplicationFrame(); if (passwordOnly) { - PassphraseDialog passphraseDialog = new PassphraseDialog(mw.getApplicationFrame()); - passphraseDialog.setLocationRelativeTo(Core.getMainWindow().getApplicationFrame()); + PassphraseDialog passphraseDialog = new PassphraseDialog(parent); + passphraseDialog.setLocationRelativeTo(parent); if (uri.getScheme() == null) { // asked passphrase passphraseDialog.setTitleDesc(OStrings.getString( @@ -347,8 +346,8 @@ private Credentials askCredentialsGUI(URIish uri, Credentials credentials, boole return credentials; } } else { - UserPassDialog userPassDialog = new UserPassDialog(mw.getApplicationFrame()); - userPassDialog.setLocationRelativeTo(Core.getMainWindow().getApplicationFrame()); + UserPassDialog userPassDialog = new UserPassDialog(parent); + userPassDialog.setLocationRelativeTo(parent); userPassDialog.setDescription(OStrings.getString( credentials.username == null ? "TEAM_USERPASS_FIRST" : "TEAM_USERPASS_WRONG", uri.getHumanishName())); diff --git a/src/org/omegat/core/team2/impl/SVNAuthenticationManager.java b/src/org/omegat/core/team2/impl/SVNAuthenticationManager.java index ea0ab16aae..f35e6a40d9 100644 --- a/src/org/omegat/core/team2/impl/SVNAuthenticationManager.java +++ b/src/org/omegat/core/team2/impl/SVNAuthenticationManager.java @@ -49,9 +49,9 @@ import org.omegat.core.team2.ProjectTeamSettings; import org.omegat.core.team2.gui.UserPassDialog; import org.omegat.core.team2.impl.TeamUtils.Credentials; -import org.omegat.gui.main.ConsoleWindow; import org.omegat.util.Log; import org.omegat.util.OStrings; +import org.omegat.util.gui.StaticUIUtils; import gen.core.project.RepositoryDefinition; @@ -194,11 +194,11 @@ public SVNAuthentication getFirstAuthentication(String kind, String realm, SVNUR return getAuthenticatorInstance(kind, url, credentials); } - if (Core.getMainWindow() == null || Core.getMainWindow() instanceof ConsoleWindow) { + if (StaticUIUtils.isGUI()) { + return ask(kind, url, OStrings.getString("TEAM_USERPASS_FIRST", url.getPath())); + } else { // run on headless. return askCUI(kind, url, OStrings.getString("TEAM_USERPASS_FIRST", url.getPath())); - } else { - return ask(kind, url, OStrings.getString("TEAM_USERPASS_FIRST", url.getPath())); } } @@ -207,11 +207,11 @@ public SVNAuthentication getNextAuthentication(String kind, String realm, SVNURL if (predefinedUser != null && predefinedPass != null) { throw new KnownException("TEAM_PREDEFINED_CREDENTIALS_ERROR"); } - if (Core.getMainWindow() == null) { + if (StaticUIUtils.isGUI()) { + return ask(kind, url, OStrings.getString("TEAM_USERPASS_WRONG", url.getPath())); + } else { // run on headless. return askCUI(kind, url, OStrings.getString("TEAM_USERPASS_WRONG", url.getPath())); - } else { - return ask(kind, url, OStrings.getString("TEAM_USERPASS_WRONG", url.getPath())); } } diff --git a/test/fixtures/org/omegat/core/TestCoreInitializer.java b/test/fixtures/org/omegat/core/TestCoreInitializer.java index 28396a304b..92189cd2f5 100644 --- a/test/fixtures/org/omegat/core/TestCoreInitializer.java +++ b/test/fixtures/org/omegat/core/TestCoreInitializer.java @@ -28,8 +28,8 @@ import org.omegat.core.threads.IAutoSave; import org.omegat.gui.editor.IEditor; import org.omegat.gui.glossary.IGlossaries; -import org.omegat.gui.main.ConsoleWindow; import org.omegat.gui.main.IMainWindow; +import org.omegat.util.gui.StaticUIUtils; /** * Core initializer for unit tests. @@ -52,10 +52,9 @@ public static void initAutoSave(IAutoSave autoSave) { public static void initMainWindow(IMainWindow mainWindow) throws Exception { Core.setMainWindow(mainWindow); - if (mainWindow instanceof ConsoleWindow) { - return; + if (StaticUIUtils.isGUI()) { + Core.initializeGUIimpl(mainWindow); } - Core.initializeGUIimpl(mainWindow); } public static void initGlossary(IGlossaries glossaries) {