diff --git a/designer-realize/src/main/java/com/fr/design/deeplink/DeepLink.java b/designer-base/src/main/java/com/fr/design/deeplink/DeepLink.java similarity index 100% rename from designer-realize/src/main/java/com/fr/design/deeplink/DeepLink.java rename to designer-base/src/main/java/com/fr/design/deeplink/DeepLink.java diff --git a/designer-base/src/main/java/com/fr/design/deeplink/DeepLinkCore.java b/designer-base/src/main/java/com/fr/design/deeplink/DeepLinkCore.java new file mode 100644 index 0000000000..55a45709bf --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/deeplink/DeepLinkCore.java @@ -0,0 +1,184 @@ +package com.fr.design.deeplink; + +import com.fr.design.constants.DesignerLaunchStatus; +import com.fr.design.startup.FineStartupNotificationFactory; +import com.fr.design.startup.FineStartupNotificationProvider; +import com.fr.event.Event; +import com.fr.event.EventDispatcher; +import com.fr.event.Listener; +import com.fr.event.Null; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StringUtils; +import com.fr.stable.os.OperatingSystem; +import com.fr.third.org.apache.http.NameValuePair; +import com.fr.web.URLUtils; + +import javax.swing.SwingUtilities; +import java.awt.Color; +import java.awt.Frame; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Starryi + * @version 1.0 + * Created by Starryi on 2022/1/6 + */ +public class DeepLinkCore { + + protected DeepLinkCore(){} + private static final DeepLinkCore instance = new DeepLinkCore(); + public static DeepLinkCore getInstance(){ + return instance; + } + + private String pendingURL; + + private final List deepLinkList = new ArrayList<>(); + + private boolean isDesignerStartupCompleted = false; + + public void register(DeepLink deepLink) { + if (deepLink != null) { + deepLinkList.add(deepLink); + } + } + + public void start(String[] args) { + if (OperatingSystem.isWindows()) { + if (args.length > 0) { + receiveDeeplink(args[0]); + } + } + + if (OperatingSystem.isWindows() && args.length > 0) { + receiveDeeplink(args[0]); + } + + FineStartupNotificationFactory.getNotification() + .registerStartupListener(new FineStartupNotificationProvider.Listener() { + @Override + public void startupPerformed(String parameters) { + receiveDeeplink(parameters); + } + }); + + EventDispatcher.listen(DesignerLaunchStatus.STARTUP_COMPLETE, new Listener() { + @Override + public void on(Event event, Null param) { + EventDispatcher.stopListen(this); + isDesignerStartupCompleted = true; + if (canConsumePendingURL()) { + consumePendingURL(); + } + } + }); + } + + public void receiveDeeplink(String url) { + if (canAcceptNewURL()) { + acceptNewURL(url); + if (canConsumePendingURL()) { + consumePendingURL(); + } + } + } + + public void receiveDeeplink2(String url) { + if (canAcceptNewURL()) { + acceptNewURL(url); + if (canConsumePendingURL()) { + consumePendingURL(); + } else { + Frame frame = new Frame("can not ConsumePendingURL"); + frame.setSize(400, 400); + frame.setBackground(Color.BLACK); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent windowEvent){ + frame.dispose(); + } + }); + frame.setVisible(true); + } + } else { + Frame frame = new Frame("can not AcceptNewURL"); + frame.setSize(400, 400); + frame.setBackground(Color.BLACK); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent windowEvent){ + frame.dispose(); + } + }); + frame.setVisible(true); + } + } + + private boolean canAcceptNewURL() { + return StringUtils.isEmpty(this.pendingURL); + } + + private void acceptNewURL(String url) { + this.pendingURL = url; + } + + private boolean canConsumePendingURL() { + return StringUtils.isNotEmpty(this.pendingURL) && isDesignerStartupCompleted; + } + + private void consumePendingURL() { + String host = null; + String path = null; + Map params = new HashMap<>(); + + URL url = null; + try { + url = new URL(null, this.pendingURL, new URLStreamHandler() { + @Override + protected URLConnection openConnection(URL u) throws IOException { + return null; + } + }); + } catch (MalformedURLException ignored) {} + + if (url != null) { + host = url.getHost(); + path = url.getPath(); + + List pairs = URLUtils.parse(url.getQuery()); + for (NameValuePair pair: pairs) { + params.put(pair.getName(), pair.getValue()); + } + } + + FineLoggerFactory.getLogger().info("consume deep link: " + this.pendingURL); + performDeepLinks(this.pendingURL, host, path, params); + + markPendingURLConsumed(); + } + + private void performDeepLinks(String url, String host, String path, Map params) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + for (DeepLink deepLink: deepLinkList) { + if (deepLink.accept(url, host, path, params)) { + deepLink.run(url, host, path, params); + } + } + } + }); + } + + private void markPendingURLConsumed() { + this.pendingURL = null; + } +} diff --git a/designer-base/src/main/java/com/fr/design/utils/DesignUtils.java b/designer-base/src/main/java/com/fr/design/utils/DesignUtils.java index f066d648a2..f9fb484a6a 100644 --- a/designer-base/src/main/java/com/fr/design/utils/DesignUtils.java +++ b/designer-base/src/main/java/com/fr/design/utils/DesignUtils.java @@ -5,6 +5,7 @@ import com.fr.base.ServerConfig; import com.fr.concurrent.NamedThreadFactory; import com.fr.design.DesignerEnvManager; import com.fr.design.ExtraDesignClassManager; +import com.fr.design.deeplink.DeepLinkCore; import com.fr.design.dialog.FineJOptionPane; import com.fr.design.fun.DesignerEnvProcessor; import com.fr.design.gui.UILookAndFeel; @@ -26,10 +27,15 @@ import com.fr.start.ServerStarter; import com.fr.value.NotNullLazyValue; import com.fr.workspace.WorkContext; import org.jetbrains.annotations.NotNull; + import javax.swing.SwingUtilities; import javax.swing.UIManager; +import java.awt.Color; import java.awt.Desktop; import java.awt.Font; +import java.awt.Frame; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -213,6 +219,14 @@ public class DesignUtils { DesignerContext.getDesignerFrame().openTemplate(new FileFILE(f)); } }); + } else { + String url = line; + UIUtil.invokeLaterIfNeeded(new Runnable() { + @Override + public void run() { + DeepLinkCore.getInstance().receiveDeeplink2(url); + } + }); } } } diff --git a/designer-realize/src/main/java/com/fr/design/deeplink/DeepLinkManager.java b/designer-realize/src/main/java/com/fr/design/deeplink/DeepLinkManager.java index 9cef24dcc8..14723e84d3 100644 --- a/designer-realize/src/main/java/com/fr/design/deeplink/DeepLinkManager.java +++ b/designer-realize/src/main/java/com/fr/design/deeplink/DeepLinkManager.java @@ -1,153 +1,21 @@ package com.fr.design.deeplink; -import com.fr.design.constants.DesignerLaunchStatus; -import com.fr.design.startup.FineStartupNotificationFactory; -import com.fr.design.startup.FineStartupNotificationProvider; -import com.fr.event.Event; -import com.fr.event.EventDispatcher; -import com.fr.event.Listener; -import com.fr.event.Null; -import com.fr.log.FineLoggerFactory; -import com.fr.stable.StringUtils; -import com.fr.stable.os.OperatingSystem; -import com.fr.third.org.apache.http.NameValuePair; -import com.fr.web.URLUtils; - -import javax.swing.SwingUtilities; -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLStreamHandler; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * @author Starryi * @version 1.0 - * Created by Starryi on 2022/1/6 + * Created by Starryi on 2022/2/10 */ public class DeepLinkManager { - - private DeepLinkManager(){} + protected DeepLinkManager(){} private static final DeepLinkManager instance = new DeepLinkManager(); public static DeepLinkManager getInstance(){ return instance; } - private String pendingURL; - - private final List deepLinkList = new ArrayList<>(); - - private boolean isDesignerStartUpCompleted = false; - - private void register(DeepLink deepLink) { - if (deepLink != null) { - deepLinkList.add(deepLink); - } - } - - public void prepare() { - register(new FileOpen4MacDeepLink()); - register(new TemplateThemeInstallationDeepLink()); - - FineStartupNotificationFactory.getNotification() - .registerStartupListener(new FineStartupNotificationProvider.Listener() { - @Override - public void startupPerformed(String parameters) { - if (canAcceptNewURL()) { - acceptNewURL(parameters); - if (canConsumePendingURL()) { - consumePendingURL(); - } - } - } - }); - - EventDispatcher.listen(DesignerLaunchStatus.STARTUP_COMPLETE, new Listener() { - @Override - public void on(Event event, Null param) { - isDesignerStartUpCompleted = true; - if (canConsumePendingURL()) { - consumePendingURL(); - } - } - }); - } - - private boolean canAcceptNewURL() { - return StringUtils.isEmpty(this.pendingURL); - } - - private void acceptNewURL(String url) { - this.pendingURL = url; - } - - private boolean canConsumePendingURL() { - return StringUtils.isNotEmpty(this.pendingURL) && isDesignerStartUpCompleted; - } - - private void consumePendingURL() { - String host = null; - String path = null; - Map params = new HashMap<>(); - - URL url = null; - try { - url = new URL(null, this.pendingURL, new URLStreamHandler() { - @Override - protected URLConnection openConnection(URL u) throws IOException { - return null; - } - }); - } catch (MalformedURLException ignored) {} - - if (url != null) { - host = url.getHost(); - path = url.getPath(); - - List pairs = URLUtils.parse(url.getQuery()); - for (NameValuePair pair: pairs) { - params.put(pair.getName(), pair.getValue()); - } - } - - FineLoggerFactory.getLogger().info("consume deep link: " + this.pendingURL); - performDeepLinks(this.pendingURL, host, path, params); - - markPendingURLConsumed(); - } - - private void performDeepLinks(String url, String host, String path, Map params) { - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - for (DeepLink deepLink: deepLinkList) { - if (deepLink.accept(url, host, path, params)) { - deepLink.run(url, host, path, params); - } - } - } - }); - } - - public String[] createNewArgs(String[] args) { - String filePath = this.pendingURL; - if (OperatingSystem.isMacos() && StringUtils.isNotEmpty(filePath) && new File(filePath).exists()) { - List argList = new ArrayList<>(Arrays.asList(args)); - argList.add(filePath); - markPendingURLConsumed(); - return argList.toArray(new String[]{}); - } else { - return args; - } - } + public void start(String[] args) { + DeepLinkCore.getInstance().register(new FileOpen4MacDeepLink()); + DeepLinkCore.getInstance().register(new TemplateThemeInstallationDeepLink()); - private void markPendingURLConsumed() { - this.pendingURL = null; + DeepLinkCore.getInstance().start(args); } } diff --git a/designer-realize/src/main/java/com/fr/start/MainDesigner.java b/designer-realize/src/main/java/com/fr/start/MainDesigner.java index 4b66c362c2..e29a4e804a 100644 --- a/designer-realize/src/main/java/com/fr/start/MainDesigner.java +++ b/designer-realize/src/main/java/com/fr/start/MainDesigner.java @@ -107,7 +107,7 @@ public class MainDesigner extends BaseDesigner { */ public static void main(String[] args) { - DeepLinkManager.getInstance().prepare(); + DeepLinkManager.getInstance().start(args); StopWatch watch = new StopWatch(); watch.start(); DesignerLifecycleMonitorContext.getMonitor().beforeStart(); @@ -122,7 +122,6 @@ public class MainDesigner extends BaseDesigner { }); Module designerRoot = ModuleContext.parseRoot("designer-startup.xml"); //传递启动参数 - args = DeepLinkManager.getInstance().createNewArgs(args); designerRoot.setSingleton(StartupArgs.class, new StartupArgs(args)); try { designerRoot.start();