io.ballerina.plugins.idea.webview.diagram.preview.HtmlPanelProvider.java Source code

Java tutorial

Introduction

Here is the source code for io.ballerina.plugins.idea.webview.diagram.preview.HtmlPanelProvider.java

Source

/*
 *  Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package io.ballerina.plugins.idea.webview.diagram.preview;

import com.intellij.CommonBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.xmlb.annotations.Attribute;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

import javax.swing.JComponent;

/**
 * HTML panel provider for ballerina diagram editor.
 */
public abstract class HtmlPanelProvider {

    public static final ExtensionPointName<HtmlPanelProvider> EP_NAME = ExtensionPointName
            .create("io.ballerina.html.panel.provider");

    private static HtmlPanelProvider[] ourProviders = null;

    @NotNull
    public abstract DiagramHtmlPanel createHtmlPanel(Project project, VirtualFile file);

    @NotNull
    public abstract AvailabilityInfo isAvailable();

    @NotNull
    public abstract ProviderInfo getProviderInfo();

    @NotNull
    static HtmlPanelProvider[] getProviders() {
        if (ourProviders == null) {
            ourProviders = EP_NAME.getExtensions();
        }
        return ourProviders;
    }

    @NotNull
    static HtmlPanelProvider createFromInfo(@NotNull ProviderInfo providerInfo) {
        try {
            return ((HtmlPanelProvider) Class.forName(providerInfo.getClassName()).newInstance());
        } catch (Exception e) {
            Messages.showMessageDialog(
                    "Cannot set preview panel provider (" + providerInfo.getName() + "):\n" + e.getMessage(),
                    CommonBundle.getErrorTitle(), Messages.getErrorIcon());
            Logger.getInstance(HtmlPanelProvider.class).error(e);
            return getProviders()[0];
        }
    }

    public static boolean hasAvailableProviders() {
        return Arrays.stream(getProviders())
                .anyMatch(provider -> provider.isAvailable() == AvailabilityInfo.AVAILABLE);
    }

    /**
     * Represents diagram editor html panel provider information.
     */
    public static class ProviderInfo {
        @NotNull
        @Attribute("name")
        private String myName;
        @NotNull
        @Attribute("className")
        private String className;

        @SuppressWarnings("unused")
        private ProviderInfo() {
            myName = "";
            className = "";
        }

        public ProviderInfo(@NotNull String name, @NotNull String className) {
            myName = name;
            this.className = className;
        }

        @NotNull
        public String getName() {
            return myName;
        }

        @NotNull
        public String getClassName() {
            return className;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            ProviderInfo info = (ProviderInfo) o;
            if (!myName.equals(info.myName)) {
                return false;
            }
            if (!className.equals(info.className)) {
                return false;
            }

            return true;
        }

        @Override
        public int hashCode() {
            int result = myName.hashCode();
            result = 31 * result + className.hashCode();
            return result;
        }

        @Override
        public String toString() {
            return myName;
        }
    }

    /**
     * Diagram panel Availability Information.
     */
    public abstract static class AvailabilityInfo {
        public static final AvailabilityInfo AVAILABLE = new AvailabilityInfo() {
            @Override
            public boolean checkAvailability(@NotNull JComponent parentComponent) {
                return true;
            }
        };

        public static final AvailabilityInfo UNAVAILABLE = new AvailabilityInfo() {
            @Override
            public boolean checkAvailability(@NotNull JComponent parentComponent) {
                return false;
            }
        };

        public abstract boolean checkAvailability(@NotNull JComponent parentComponent);
    }
}