From 6aa779138cc3006043e4c083ea33acdb9139731a Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Fri, 19 Apr 2024 17:15:31 +0200 Subject: [PATCH] impl'd utils and base undecorated dialog Signed-off-by: Andre Dietisheim --- .../intellij/common/ui/UndecoratedDialog.java | 86 +++++++++++++++++++ .../common/utils/ApplicationUtils.java | 25 ++++++ .../intellij/common/utils/SwingUtils.java | 38 ++++++++ .../intellij/common/utils/UrlUtils.java | 37 ++++++++ .../intellij/common/utils/UrlUtilsTest.java | 46 ++++++++++ 5 files changed, 232 insertions(+) create mode 100644 src/main/java/com/redhat/devtools/intellij/common/ui/UndecoratedDialog.java create mode 100644 src/main/java/com/redhat/devtools/intellij/common/utils/ApplicationUtils.java create mode 100644 src/main/java/com/redhat/devtools/intellij/common/utils/SwingUtils.java create mode 100644 src/main/java/com/redhat/devtools/intellij/common/utils/UrlUtils.java create mode 100644 src/test/java/com/redhat/devtools/intellij/common/utils/UrlUtilsTest.java diff --git a/src/main/java/com/redhat/devtools/intellij/common/ui/UndecoratedDialog.java b/src/main/java/com/redhat/devtools/intellij/common/ui/UndecoratedDialog.java new file mode 100644 index 0000000..26c45bc --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/common/ui/UndecoratedDialog.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.common.ui; + +import com.intellij.openapi.actionSystem.ActionManager; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonShortcuts; +import com.intellij.openapi.project.DumbAwareAction; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.wm.impl.IdeGlassPaneEx; +import com.intellij.ui.PopupBorder; +import com.intellij.ui.WindowMoveListener; +import com.intellij.ui.WindowResizeListener; +import com.intellij.util.Consumer; +import com.intellij.util.ui.JBUI; +import java.awt.Component; +import java.awt.Window; +import java.util.stream.Stream; +import javax.swing.JComponent; +import javax.swing.JRootPane; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +abstract public class UndecoratedDialog extends DialogWrapper { + + public UndecoratedDialog(@Nullable Project project, + @Nullable Component parentComponent, + boolean canBeParent, + @NotNull IdeModalityType ideModalityType, + boolean createSouth) { + super(project, parentComponent, canBeParent, ideModalityType, createSouth); + init(); + } + + @Override + protected void init() { + super.init(); + setUndecorated(true); + Window dialogWindow = getPeer().getWindow(); + setBorders(); + } + + protected void closeImmediately() { + if (isVisible()) { + doCancelAction(); + } + } + + protected void registerEscapeShortcut(Consumer onEscape) { + AnAction escape = ActionManager.getInstance().getAction("EditorEscape"); + DumbAwareAction.create(onEscape::accept) + .registerCustomShortcutSet( + escape == null ? CommonShortcuts.ESCAPE : escape.getShortcutSet(), + getRootPane(), + getDisposable()); + } + + protected void setGlassPaneResizable() { + WindowResizeListener resizeListener = new WindowResizeListener(getRootPane(), JBUI.insets(10), null); + IdeGlassPaneEx glassPane = (IdeGlassPaneEx) getRootPane().getGlassPane(); + glassPane.addMousePreprocessor(resizeListener, getDisposable()); + glassPane.addMouseMotionPreprocessor(resizeListener, getDisposable()); + } + + protected void setMovableUsing(JComponent... movableComponents) { + WindowMoveListener windowMoveListener = new WindowMoveListener(getRootPane()); + Stream.of(movableComponents).forEach( + component -> component.addMouseListener(windowMoveListener)); + } + + private void setBorders() { + getRootPane().setBorder(PopupBorder.Factory.create(true, true)); + getRootPane().setWindowDecorationStyle(JRootPane.NONE); + } + +} diff --git a/src/main/java/com/redhat/devtools/intellij/common/utils/ApplicationUtils.java b/src/main/java/com/redhat/devtools/intellij/common/utils/ApplicationUtils.java new file mode 100644 index 0000000..cdc4891 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/common/utils/ApplicationUtils.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.common.utils; + +import com.intellij.openapi.application.ApplicationManager; + +import java.util.concurrent.Executor; + +public class ApplicationUtils { + + public static final Executor UI_EXECUTOR = (Runnable runnable) -> + ApplicationManager.getApplication().invokeLater(runnable); + + public static final Executor PLATFORM_EXECUTOR = (Runnable runnable) -> + ApplicationManager.getApplication().executeOnPooledThread(runnable); + +} diff --git a/src/main/java/com/redhat/devtools/intellij/common/utils/SwingUtils.java b/src/main/java/com/redhat/devtools/intellij/common/utils/SwingUtils.java new file mode 100644 index 0000000..0cffdc6 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/common/utils/SwingUtils.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. + ******************************************************************************/ +package com.redhat.devtools.intellij.common.utils; + +import com.intellij.openapi.actionSystem.AnActionEvent; + +import java.awt.Point; +import java.awt.event.MouseEvent; + +public class SwingUtils { + + private SwingUtils() { + } + + /** + * Returns the location of the mouse for a given {@link AnActionEvent}. + * Returns {@code null} if the location could not be determined. + * + * @param actionEvent the action event to retrieve the mouse location from + * @return the mouse location + */ + public static Point getMouseLocation(AnActionEvent actionEvent) { + Point location = null; + MouseEvent event = ((MouseEvent) actionEvent.getInputEvent()); + if (event != null) { + location = event.getLocationOnScreen(); + } + return location; + } +} diff --git a/src/main/java/com/redhat/devtools/intellij/common/utils/UrlUtils.java b/src/main/java/com/redhat/devtools/intellij/common/utils/UrlUtils.java new file mode 100644 index 0000000..6ca527d --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/common/utils/UrlUtils.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.common.utils; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class UrlUtils { + + private static final Pattern HTTP_URL_REGEX = Pattern.compile("https?://((www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6})\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)"); + private UrlUtils() {} + + /** + * Returns the host portion of the given url. Returns {@code null} otherwise. + * @param url the url to get the host portion from + * @return the host portion of the given url + */ + public static String getHost(String url) { + if (url == null) { + return null; + } + Matcher matcher = HTTP_URL_REGEX.matcher(url); + if (!matcher.matches()) { + return null; + } + return matcher.group(1); + } + +} diff --git a/src/test/java/com/redhat/devtools/intellij/common/utils/UrlUtilsTest.java b/src/test/java/com/redhat/devtools/intellij/common/utils/UrlUtilsTest.java new file mode 100644 index 0000000..cf87b52 --- /dev/null +++ b/src/test/java/com/redhat/devtools/intellij/common/utils/UrlUtilsTest.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.common.utils; + +import org.junit.Test; + +public class UrlUtilsTest { + + @Test + public void should_return_host_for_valid_url() { + // given + String url = "https://www.redhat.com"; + // when + String host = UrlUtils.getHost(url); + // then + org.assertj.core.api.Assertions.assertThat(host).isEqualTo("www.redhat.com"); + } + + @Test + public void should_return_null_for_invalid_url() { + // given + String url = "red-carpet"; + // when + String host = UrlUtils.getHost(url); + // then + org.assertj.core.api.Assertions.assertThat(host).isNull(); + } + + @Test + public void should_return_host_for_url_with_path() { + // given + String url = "https://www.redhat.com/en/about/open-source"; + // when + String host = UrlUtils.getHost(url); + // then + org.assertj.core.api.Assertions.assertThat(host).isEqualTo("www.redhat.com"); + } +}