/*
 * Copyright (c) 2013 David Boissier
 *
 * 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 org.codinjutsu.tools.jenkins.view;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.CustomStatusBarWidget;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.components.panels.NonOpaquePanel;
import org.codinjutsu.tools.jenkins.BuildStatusSummaryFactory;
import org.codinjutsu.tools.jenkins.JenkinsAppSettings;
import org.codinjutsu.tools.jenkins.JenkinsToolWindowFactory;
import org.codinjutsu.tools.jenkins.logic.BuildStatusAggregator;
import org.codinjutsu.tools.jenkins.view.util.BuildStatusIcon;
import org.codinjutsu.tools.jenkins.view.util.WidgetBorderUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Jenkins status bar widget
 */
public class JenkinsWidget extends NonOpaquePanel implements CustomStatusBarWidget {

    private final Project project;
    private final JenkinsAppSettings jenkinsAppSettings;

    public static JenkinsWidget getInstance(Project project) {
        return ServiceManager.getService(project, JenkinsWidget.class);
    }

    public JenkinsWidget(Project project) {
        this.project = project;
        jenkinsAppSettings = JenkinsAppSettings.getSafeInstance(project);

        JComponent buildStatusIcon = createStatusIcon(new BuildStatusAggregator());
        setLayout(new BorderLayout());
        add(buildStatusIcon, BorderLayout.CENTER);
    }

    public void updateStatusIcon(final BuildStatusAggregator buildStatusAggregator) {
        ApplicationManager.getApplication().invokeLater(() -> {
            removeAll();

            final JComponent buildIcon = createStatusIcon(buildStatusAggregator);
            add(buildIcon, BorderLayout.CENTER);

            updateUI();
        });

    }

    private JComponent createStatusIcon(BuildStatusAggregator aggregator) {
        JComponent statusIcon = BuildStatusIcon.createIcon(jenkinsAppSettings.isShowAllInStatusbar(), aggregator, BuildStatusEnumRenderer.getInstance(project));
        statusIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                activateBrowserToolWindow();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                activateBrowserToolWindow();
            }
        });

        setBorder(WidgetBorderUtil.getBorderInstance());

        return statusIcon;
    }

    private void activateBrowserToolWindow() {
        ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(JenkinsToolWindowFactory.JENKINS_BROWSER);

        if (toolWindow == null) {
            return;
        }

        toolWindow.activate(null);
    }

    @NotNull
    public String ID() {
        return BuildStatusSummaryFactory.BUILD_STATUS_SUMMARY_ID;
    }

    public void install(@NotNull StatusBar statusBar) {}

    public void dispose() {}

    public JComponent getComponent() {
        return this;
    }
}

@@ -1481,17 +1481,26 @@
 x.swing.
-*
+JComponent
 ;%0Aimport
@@ -1509,17 +1509,28 @@
 ava.awt.
-*
+BorderLayout
 ;%0Aimport
@@ -1592,16 +1592,48 @@
 seEvent;
+%0Aimport java.util.stream.Stream;
 %0A%0A/**%0A *
@@ -2917,36 +2917,31 @@
     
-statusIcon.addMouseListener(
+MouseAdapter adapter = 
 new 
@@ -3228,32 +3228,158 @@
      %7D%0A        %7D
+;%0A        Stream.concat(Stream.of(statusIcon.getComponents()), Stream.of(statusIcon)).forEach(c -%3E c.addMouseListener(adapter)
 );%0A%0A        setB
@@ -3737,24 +3737,38 @@
 ll);%0A    %7D%0A%0A
+    @Override%0A
     @NotNull
@@ -3858,32 +3858,46 @@
 MARY_ID;%0A    %7D%0A%0A
+    @Override%0A
     public void 
@@ -3934,24 +3934,38 @@
 tusBar) %7B%7D%0A%0A
+    @Override%0A
     public v
@@ -3978,24 +3978,38 @@
 spose() %7B%7D%0A%0A
+    @Override%0A
     public J

Allow opening the Jenkins panel from the entire panel