package ch.psi.plot.utils;

/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 */
public class MonitoredPanel extends JPanel {

    public MonitoredPanel() {
        addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) != 0) {
                    if (isShowing()) {
                        try {
                            onShown();
                        } catch (Exception ex) {
                            Logger.getLogger(MonitoredPanel.class.getName()).log(Level.FINE, null, ex);
                        }
                    } else {
                        try {
                            onHidden();
                        } catch (Exception ex) {
                            Logger.getLogger(MonitoredPanel.class.getName()).log(Level.FINE, null, ex);
                        }
                    }
                }
            }
        });
    }

    protected void onShown() {
    }

    protected void onHidden() {
    }
}
