/*
 * Autopsy Forensic Browser
 * 
 * Copyright 2011 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 * 
 * 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.sleuthkit.autopsy.ingest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import org.openide.windows.Mode;
import org.openide.windows.WindowManager;
import org.sleuthkit.autopsy.casemodule.Case;

/**
 * Toolbar for Ingest
 * 
 */
public class IngestMessagesToolbar extends javax.swing.JPanel {

    private IngestMessagesButton ingestMessagesButton = new IngestMessagesButton();

    /** Creates new form IngestMessagesToolbar */
    public IngestMessagesToolbar() {
        initComponents();
        customizeComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setToolTipText(org.openide.util.NbBundle.getMessage(IngestMessagesToolbar.class, "IngestMessagesToolbar.toolTipText")); // NOI18N
        setMaximumSize(new java.awt.Dimension(32767, 25));
        setPreferredSize(new java.awt.Dimension(80, 25));
        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private void customizeButton() {
        ingestMessagesButton = new IngestMessagesButton();

        setMaximumSize(new java.awt.Dimension(32767, 25));
        setPreferredSize(new java.awt.Dimension(80, 25));

        ingestMessagesButton.setFocusPainted(false);
        ingestMessagesButton.setContentAreaFilled(false);
        ingestMessagesButton.setFont(new java.awt.Font("Tahoma", 0, 10));
        ingestMessagesButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/ingest/eye-icon-25.png")));
        ingestMessagesButton.setToolTipText("Ingest Messages");
        ingestMessagesButton.setBorder(null);
        ingestMessagesButton.setBorderPainted(false);
        ingestMessagesButton.setEnabled(false);
        ingestMessagesButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        ingestMessagesButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ingestMessagesButton.setMaximumSize(new java.awt.Dimension(44, 25));
        ingestMessagesButton.setMinimumSize(new java.awt.Dimension(44, 25));
        ingestMessagesButton.setPreferredSize(new java.awt.Dimension(44, 25));
        ingestMessagesButton.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showIngestMessages();
            }
        });
        this.add(ingestMessagesButton, BorderLayout.CENTER);
    }

    private void customizeComponents() {

        customizeButton();
        
        this.setBorder(null);

        IngestMessagePanel.addPropertyChangeSupportListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                final int numberMessages = (Integer) evt.getNewValue();
                //ingestMessagesButton.setText(Integer.toString(numberMessages));
                //repaint button
                ingestMessagesButton.setMessages(numberMessages);
                ingestMessagesButton.repaint();
            }
        });

        Case.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(Case.CASE_CURRENT_CASE)) {
                    setEnabled(evt.getNewValue() != null);
                }
            }
        });
    }

    private void showIngestMessages() {
        IngestMessageTopComponent tc = IngestMessageTopComponent.findInstance();

        Mode mode = WindowManager.getDefault().findMode("floatingLeftBottom");
        if (mode != null) {
            //TopComponent[] tcs = mode.getTopComponents();
            mode.dockInto(tc);
            tc.open();
            //tc.requestActive();   
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        ingestMessagesButton.setEnabled(enabled);
    }

    private static class IngestMessagesButton extends JButton {

        private static final Font messagesFont = new java.awt.Font("Tahoma", 0, 9);
        private int messages = 0;

        @Override
        public void paint(Graphics g) {           
            super.paint(g);

            if (messages == 0)
                return;
            //paint text
            String messageStr = Integer.toString(messages);
            final int len = messageStr.length();
            g.setFont(messagesFont);
            g.setColor(Color.BLUE);
            int x = getSize().width - len * 5 - 1;
            if (x<0)
                x = 0;
            g.drawString(messageStr, x, 10);      
        }

        void setMessages(int messages) {
            this.messages = messages;
        }
    }
}

@@ -5297,24 +5297,71 @@
  JButton %7B%0A%0A
+        private static final int fontSize = 9;%0A
         priv
@@ -5429,12 +5429,28 @@
 a%22, 
-0, 9
+Font.PLAIN, fontSize
 );%0A 
@@ -5823,30 +5823,41 @@
     
-g.setColor(Color.BLUE)
+%0A            int dx = len * 5 + 1
 ;%0A  
@@ -5896,19 +5896,10 @@
 h - 
-len * 5 - 1
+dx
 ;%0A  
@@ -5940,16 +5940,133 @@
  x = 0;%0A
+            g.setColor(Color.WHITE);%0A            g.fillRect(x, 1, dx, fontSize);%0A            g.setColor(Color.BLUE);%0A
         
@@ -6097,18 +6097,24 @@
 Str, x, 
-10
+fontSize
 );      

Set white background for message count text on the button for better contrast.