/*
 * Copyright (c) 2012 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.project.Project;
import com.intellij.openapi.wm.CustomStatusBarWidget;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.StatusBarWidget;
import org.codinjutsu.tools.jenkins.util.GuiUtil;
import org.codinjutsu.tools.jenkins.view.util.BuildStatusIcon;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

public class JenkinsRssWidget extends JPanel implements CustomStatusBarWidget, StatusBarWidget.Multiframe {

    private final Project project;
    private StatusBar myStatusBar;

    public JenkinsRssWidget(Project project) {
        this.project = project;

        setOpaque(false);

        init();
    }

    private void init() {
        BuildStatusIcon buildStatusIcon = createStatusIcon(0);
        setLayout(new BorderLayout());
        add(buildStatusIcon, BorderLayout.CENTER);
    }

    private BuildStatusIcon createStatusIcon(int remainingBrokenBuilds) {
        BuildStatusIcon buildStatusIcon = BuildStatusIcon.createIcon(remainingBrokenBuilds);
        buildStatusIcon.setBorder(WidgetBorder.INSTANCE);
        return buildStatusIcon;
    }

    public void updateIcon(int nbRemainingBrokenBuilds) {
        final BuildStatusIcon buildIcon = createStatusIcon(nbRemainingBrokenBuilds);
        GuiUtil.runInSwingThread(new Runnable() {
            @Override
            public void run() {
                invalidate();
                removeAll();
                add(buildIcon, BorderLayout.CENTER);
                validate();
            }
        });
    }

    @NotNull
    public String ID() {
        return JenkinsRssWidget.class.getName();
    }

    public WidgetPresentation getPresentation(@NotNull PlatformType platformType) {
        return null;
    }

    public void install(@NotNull StatusBar statusBar) {
        this.myStatusBar = statusBar;
    }

    public void dispose() {
        myStatusBar = null;
    }

    public StatusBarWidget copy() {
        return new JenkinsRssWidget(project);
    }

    public JComponent getComponent() {
        return this;
    }
}

@@ -833,58 +833,8 @@
 et;%0A
-import org.codinjutsu.tools.jenkins.util.GuiUtil;%0A
 impo
@@ -1884,120 +1884,9 @@
 s);%0A
-        GuiUtil.runInSwingThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
+%0A
     
@@ -1899,32 +1899,24 @@
 validate();%0A
-        
         remo
@@ -1932,24 +1932,16 @@
         
-  
-      
 add(buil
@@ -1969,24 +1969,16 @@
 ENTER);%0A
-        
         
@@ -1993,34 +1993,8 @@
 ();%0A
-            %7D%0A        %7D);%0A
     

remove unecessary Swing thread call