Skip to content

Commit

Permalink
Fix build warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
AB-xdev committed Mar 4, 2024
1 parent b143e98 commit ab526c5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package software.xdev.openrewriter.ui.toolwindow;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import javax.swing.Box;
import javax.swing.JComponent;

import org.jetbrains.annotations.NotNull;

Expand All @@ -12,7 +17,6 @@
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.ui.JBSplitter;


public class ORSimpleToolWindowPanel extends SimpleToolWindowPanel implements Disposable, DumbAware
Expand All @@ -21,11 +25,6 @@ public class ORSimpleToolWindowPanel extends SimpleToolWindowPanel implements Di

private ActionToolbar toolbar;

public ORSimpleToolWindowPanel(final boolean vertical)
{
super(vertical);
}

public ORSimpleToolWindowPanel(final boolean vertical, final boolean borderless)
{
super(vertical, borderless);
Expand All @@ -41,52 +40,6 @@ public void setProject(final Project project)
this.project = project;
}

@SuppressWarnings("checkstyle:MagicNumber")
protected JBSplitter createSplitter(
final JComponent c1,
final JComponent c2,
final String proportionProperty)
{
return this.createSplitter(
c1,
c2,
proportionProperty,
0.5f);
}

protected JBSplitter createSplitter(
final JComponent c1,
final JComponent c2,
final String proportionProperty,
final float defaultSplit)
{
return this.createSplitter(
this,
this,
c1,
c2,
proportionProperty,
defaultSplit);
}

protected JBSplitter createSplitter(
final JComponent parentComponent,
final Disposable parentDisposable,
final JComponent c1,
final JComponent c2,
final String proportionProperty,
final float defaultSplit)
{
return ORToolWindowUIUtil.createSplitter(
this::getProject,
parentComponent,
parentDisposable,
c1,
c2,
proportionProperty,
defaultSplit);
}

protected <T> T getService(@NotNull final Class<T> serviceClass)
{
return this.getProject().getService(serviceClass);
Expand All @@ -111,9 +64,72 @@ protected void setToolbar(final String id, final ActionGroup group)
this.toolbar.getComponent().setVisible(true);
}

private static final Consumer<ActionToolbar> REFRESH_TOOLBAR_RUNNABLE = buildRefreshToolbarRunnable();

static Consumer<ActionToolbar> buildRefreshToolbarRunnable()
{
Optional<BiConsumer<ActionToolbar, Exception>> fallback;
try
{
// Deprecated since 241
final Method mUpdateActionsImmediately =
ActionToolbar.class.getDeclaredMethod("updateActionsImmediately");

fallback = Optional.of((tb, cause) -> {
try
{
mUpdateActionsImmediately.invoke(tb);
}
catch(final IllegalAccessException | InvocationTargetException e)
{
final RuntimeException ex = new RuntimeException("Failed to invoke updateActionsImmediately", e);
if(cause != null)
{
ex.addSuppressed(cause);
}
throw ex;
}
});
}
catch(final NoSuchMethodException e)
{
fallback = Optional.empty();
}

final Optional<BiConsumer<ActionToolbar, Exception>> finalFallback = fallback;

try
{
// Only available since 241
final Method mUpdateActionsAsync = ActionToolbar.class.getDeclaredMethod("updateActionsAsync");

return tb -> {
try
{
mUpdateActionsAsync.invoke(tb);
}
catch(final IllegalAccessException | InvocationTargetException e)
{
final RuntimeException ex = new RuntimeException("Failed to invoke updateActionsAsync", e);
finalFallback.ifPresentOrElse(
f -> f.accept(tb, ex),
() -> {
throw ex;
});
}
};
}
catch(final NoSuchMethodException e)
{
return finalFallback
.map(f -> (Consumer<ActionToolbar>)tb -> f.accept(tb, null))
.orElseThrow();
}
}

protected void refreshToolbar()
{
this.toolbar.updateActionsImmediately();
REFRESH_TOOLBAR_RUNNABLE.accept(this.toolbar);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<!-- https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html -->
<idea-version since-build="231"/>

<depends>com.intellij.modules.platform</depends>
<depends optional="true" config-file="plugin-maven.xml">org.jetbrains.idea.maven</depends>
<depends optional="true" config-file="plugin-gradle.xml">org.jetbrains.plugins.gradle</depends>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package software.xdev.openrewriter.ui.toolwindow;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;


class TestORSimpleToolWindowPanel
{
@Test
@DisplayName("buildRefreshToolbarRunnable/Reflection works")
void buildRefreshToolbarRunnableWorks()
{
Assertions.assertDoesNotThrow(ORSimpleToolWindowPanel::buildRefreshToolbarRunnable);
}
}

0 comments on commit ab526c5

Please sign in to comment.