-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl'd utils and base undecorated dialog
Signed-off-by: Andre Dietisheim <[email protected]>
- Loading branch information
Showing
5 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/com/redhat/devtools/intellij/common/ui/UndecoratedDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
setBorders(); | ||
} | ||
|
||
protected void closeImmediately() { | ||
if (isVisible()) { | ||
doCancelAction(); | ||
} | ||
} | ||
|
||
protected void registerEscapeShortcut(Consumer<AnActionEvent> onEscape) { | ||
AnAction escape = ActionManager.getInstance().getAction("EditorEscape"); | ||
DumbAwareAction | ||
.create(onEscape) | ||
.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); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/redhat/devtools/intellij/common/utils/ApplicationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/redhat/devtools/intellij/common/utils/SwingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/redhat/devtools/intellij/common/utils/UrlUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/com/redhat/devtools/intellij/common/utils/UrlUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |