Skip to content

Commit

Permalink
feat: impl'd utils and base undecorated dialog
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Apr 25, 2024
1 parent 9a84fd0 commit 5dbda3b
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 0 deletions.
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);
}

}
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);

}
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;
}
}
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);
}

}
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");
}
}

0 comments on commit 5dbda3b

Please sign in to comment.