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

import java.awt.BorderLayout;
import java.util.HashSet;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButtonMenuItem;

import javax.swing.JPanel;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

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

import com.jidesoft.swing.JideSplitButton;

@SuppressWarnings("serial")
public class JHVCarringtonPicker extends JPanel implements MenuListener {

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

    private final JideSplitButton crButton = new JideSplitButton("CR");
    private final MenuScroller menuScroller = new MenuScroller(crButton);
    private long time;

    public JHVCarringtonPicker() {
        setLayout(new BorderLayout());

        crButton.setAlwaysDropdown(true);
        crButton.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);
            crButton.add(item);
        }
        crButton.addMenuListener(this);

        add(crButton);
    }

    @Override
    public void menuCanceled(MenuEvent e) {
    }

    @Override
    public void menuDeselected(MenuEvent e) {
    }

    @Override
    public void menuSelected(MenuEvent e) {
        int cr = (int) Math.round(Carrington.time2CR(new JHVDate(time)) - Carrington.CR_MINIMAL);
        menuScroller.keepVisible(cr + 1);
        crButton.getItem(cr).setSelected(true);
    }

    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);
        }
    }

}

@@ -63,27 +63,24 @@
 ava.awt.
-BorderLayou
+Componen
 t;%0Aimpor
@@ -133,16 +133,77 @@
 nGroup;%0A
+import javax.swing.JMenuItem;%0Aimport javax.swing.JPopupMenu;%0A
 import j
@@ -235,17 +235,16 @@
 nuItem;%0A
-%0A
 import j
@@ -250,22 +250,65 @@
 java
-x.swing.JPanel
+.awt.event.MouseAdapter;%0Aimport java.awt.event.MouseEvent
 ;%0Aim
@@ -326,24 +326,29 @@
 swing.event.
+Popup
 MenuEvent;%0Ai
@@ -371,16 +371,21 @@
 g.event.
+Popup
 MenuList
@@ -497,24 +497,82 @@
 nuScroller;%0A
+import org.helioviewer.jhv.gui.components.base.JHVButton;%0A
 import org.h
@@ -648,52 +648,8 @@
 s;%0A%0A
-import com.jidesoft.swing.JideSplitButton;%0A%0A
 @Sup
@@ -718,13 +718,16 @@
 ds J
-Panel
+HVButton
  imp
@@ -734,16 +734,21 @@
 lements 
+Popup
 MenuList
@@ -855,58 +855,41 @@
 al J
-ideSplitButton crButton = new JideSplitButton(%22CR%22
+PopupMenu popup = new JPopupMenu(
 );%0A 
@@ -950,24 +950,21 @@
 croller(
-crButton
+popup
 );%0A    p
@@ -1032,76 +1032,17 @@
  set
-Layout(new BorderLayout());%0A%0A        crButton.setAlwaysDropdown(true
+Text(%22CR%22
 );%0A 
@@ -1048,25 +1048,16 @@
         
-crButton.
 setToolT
@@ -1495,32 +1495,29 @@
             
-crButton
+popup
 .add(item);%0A
@@ -1538,20 +1538,22 @@
     
-crButton.add
+popup.addPopup
 Menu
@@ -1584,27 +1584,51 @@
  add
-(crButton);
+MouseListener(new MouseAdapter() %7B
 %0A    
-%7D%0A%0A
+    
     
@@ -1629,32 +1629,40 @@
       @Override%0A
+        
     public void 
@@ -1666,24 +1666,25 @@
 id m
-enuCanceled(Menu
+ousePressed(Mouse
 Even
@@ -1698,70 +1698,108 @@
     
-%7D%0A%0A
     
-@Override%0A    public void menuDeselected(MenuEvent e) %7B
+        popup.show(e.getComponent(), 0, e.getComponent().getHeight());%0A            %7D%0A        %7D);
 %0A   
@@ -1836,21 +1836,40 @@
 oid 
-menuSelected(
+popupMenuWillBecomeVisible(Popup
 Menu
@@ -2031,47 +2031,325 @@
     
-crButton.getItem(cr).setSelected(true);
+Component component = popup.getComponent(cr);%0A        if (component instanceof JMenuItem) %7B%0A            ((JMenuItem) component).setSelected(true);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) %7B%0A    %7D%0A%0A    @Override%0A    public void popupMenuCanceled(PopupMenuEvent e) %7B
 %0A   

Simplify JHVCarringtonPicker