package org.helioviewer.jhv.gui.components.calendar;

import java.awt.Component;
import java.util.HashSet;

import javax.swing.ButtonGroup;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import org.helioviewer.jhv.astronomy.Carrington;
import org.helioviewer.jhv.gui.components.base.MenuScroller;
import org.helioviewer.jhv.gui.components.base.JHVButton;
import org.helioviewer.jhv.time.JHVDate;
import org.helioviewer.jhv.time.TimeUtils;

@SuppressWarnings("serial")
public class JHVCarringtonPicker extends JHVButton implements PopupMenuListener {

    private final HashSet<JHVCalendarListener> listeners = new HashSet<>();

    private final JPopupMenu popup = new JPopupMenu();
    private final MenuScroller menuScroller = new MenuScroller(popup);
    private long time;

    public JHVCarringtonPicker() {
        setText("CR");
        setToolTipText("Select Carrington rotation");

        ButtonGroup group = new ButtonGroup();
        for (int i = 0; i < Carrington.CR_start.length; i++) {
            JRadioButtonMenuItem item = new JRadioButtonMenuItem(Integer.toString(i + Carrington.CR_MINIMAL));
            item.addActionListener(e -> setTimeFromCarrington(Carrington.CR_start[Integer.valueOf(item.getText()) - Carrington.CR_MINIMAL]));
            group.add(item);
            popup.add(item);
        }
        popup.addPopupMenuListener(this);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                popup.show(e.getComponent(), 0, e.getComponent().getHeight());
            }
        });
    }

    @Override
    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        int cr = (int) Math.round(Carrington.time2CR(new JHVDate(time)) - Carrington.CR_MINIMAL);
        menuScroller.keepVisible(cr + 1);
        Component component = popup.getComponent(cr);
        if (component instanceof JMenuItem) {
            ((JMenuItem) component).setSelected(true);
        }
    }

    @Override
    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
    }

    @Override
    public void popupMenuCanceled(PopupMenuEvent e) {
    }

    public void addJHVCalendarListener(JHVCalendarListener l) {
        listeners.add(l);
    }

    public void removeJHVCalendarListener(JHVCalendarListener l) {
        listeners.remove(l);
    }

    private void setTimeFromCarrington(long _time) {
        setTime(_time);
        informAllJHVCalendarListeners();
    }

    public void setTime(long _time) {
        if (_time > TimeUtils.MINIMAL_DATE.milli && _time < TimeUtils.MAXIMAL_DATE.milli) {
            time = _time;
        }
    }

    public long getTime() {
        return time;
    }

    private void informAllJHVCalendarListeners() {
        JHVCalendarEvent e = new JHVCalendarEvent(this);
        for (JHVCalendarListener l : listeners) {
            l.actionPerformed(e);
        }
    }

}

@@ -2170,24 +2170,84 @@
 cted(true);%0A
+            setTimeFromCarrington(Carrington.CR_start%5Bcr%5D);%0A
         %7D%0A  

Set time from JHVCarringtonPicker