<commit_before>package org.helioviewer.jhv.gui.components.timeselector;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;

import org.helioviewer.jhv.gui.components.Buttons;
import org.helioviewer.jhv.gui.components.base.JHVButton;
import org.helioviewer.jhv.time.TimeUtils;

import com.jidesoft.swing.ButtonStyle;

@SuppressWarnings("serial")
class TimePanel extends JPanel {

    private final ArrayList<CalendarListener> listeners = new ArrayList<>();
    private final Calendar calendar = new GregorianCalendar();

    private final CalendarPicker calendarPicker = new CalendarPicker();
    private final JTextField textField = new JTextField("2000-01-01T00:00:00");
    private final JHVButton calendarButton = new JHVButton(Buttons.calendar);
    private Popup calPopup = null;

    TimePanel(String tip) {
        setLayout(new GridBagLayout());
        setBackground(textField.getBackground());
        setBorder(textField.getBorder());

        textField.setBorder(null);
        textField.setToolTipText(tip);
        calendarButton.setButtonStyle(ButtonStyle.FLAT_STYLE);
        calendarButton.setMargin(new Insets(0, 0, 0, 0));
        calendarButton.setToolTipText(tip);

        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                setTimeFromTextField(true);
            }
        });
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    setTimeFromTextField(true);
                }
            }
        });
        calendarButton.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                hidePopup();
            }
        });
        calendarButton.addActionListener(e -> {
            setTimeFromTextField(false);
            if (calPopup == null) {
                calendarButton.requestFocus();
                showPopup();
            } else {
                hidePopup();
            }
        });

        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;

        c.weightx = 1;
        c.gridx = 0;
        add(textField, c);
        c.weightx = 0;
        c.gridx = 1;
        add(calendarButton, c);

        calendarPicker.setPreferredSize(calendarPicker.getMinimumSize());
        calendarPicker.addListener(this::hidePopup);
    }

    private void informListeners() {
        listeners.forEach(CalendarListener::calendarAction);
    }

    private void setTimeFromTextField(boolean propagate) {
        setTime(TimeUtils.optParse(textField.getText(), getTime()));
        if (propagate)
            informListeners();
    }

    private void setTimeFromCalendar() {
        setTime(calendarPicker.getTime());
        informListeners();
    }

    // Opens an new popup window where the user can select a date
    private void showPopup() {
        // set up the popup content
        calendarPicker.setTime(calendar.getTimeInMillis());

        // get position for popup
        int x = textField.getLocationOnScreen().x;
        int y = textField.getLocationOnScreen().y + textField.getSize().height;

        // create popup
        PopupFactory factory = PopupFactory.getSharedInstance();
        calPopup = factory.getPopup(calendarButton, calendarPicker, x, y);
        calPopup.show();

        calendarPicker.resizeSelectionPanel();

        // correct position of popup when it does not fit into screen area
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension pickerSize = calendarPicker.getSize();
        x = x + pickerSize.width > screenSize.width ? screenSize.width - pickerSize.width : x;
        x = Math.max(x, 0);<commit_msg>Satisfy coverity<commit_after>package org.helioviewer.jhv.gui.components.timeselector;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;

import org.helioviewer.jhv.gui.components.Buttons;
import org.helioviewer.jhv.gui.components.base.JHVButton;
import org.helioviewer.jhv.time.TimeUtils;

import com.jidesoft.swing.ButtonStyle;

@SuppressWarnings("serial")
class TimePanel extends JPanel {

    private final ArrayList<CalendarListener> listeners = new ArrayList<>();
    private final Calendar calendar = new GregorianCalendar();

    private final CalendarPicker calendarPicker = new CalendarPicker();
    private final JTextField textField = new JTextField("2000-01-01T00:00:00");
    private final JHVButton calendarButton = new JHVButton(Buttons.calendar);
    private Popup calPopup = null;

    TimePanel(String tip) {
        setLayout(new GridBagLayout());
        setBackground(textField.getBackground());
        setBorder(textField.getBorder());

        textField.setBorder(null);
        textField.setToolTipText(tip);
        calendarButton.setButtonStyle(ButtonStyle.FLAT_STYLE);
        calendarButton.setMargin(new Insets(0, 0, 0, 0));
        calendarButton.setToolTipText(tip);

        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                setTimeFromTextField(true);
            }
        });
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    setTimeFromTextField(true);
                }
            }
        });
        calendarButton.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                hidePopup();
            }
        });
        calendarButton.addActionListener(e -> {
            setTimeFromTextField(false);
            if (calPopup == null) {
                calendarButton.requestFocus();
                showPopup();
            } else {
                hidePopup();
            }
        });

        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;

        c.weightx = 1;
        c.gridx = 0;
        add(textField, c);
        c.weightx = 0;
        c.gridx = 1;
        add(calendarButton, c);

        calendarPicker.setPreferredSize(calendarPicker.getMinimumSize());
        calendarPicker.addListener(this::hidePopup);
    }

    private void informListeners() {
        listeners.forEach(CalendarListener::calendarAction);
    }

    private void setTimeFromTextField(boolean propagate) {
        String text = textField.getText();
        if (text != null) { // satisfy coverity
            setTime(TimeUtils.optParse(text, getTime()));
            if (propagate)
                informListeners();
        }
    }

    private void setTimeFromCalendar() {
        setTime(calendarPicker.getTime());
        informListeners();
    }

    // Opens an new popup window where the user can select a date
    private void showPopup() {
        // set up the popup content
        calendarPicker.setTime(calendar.getTimeInMillis());

        // get position for popup
        int x = textField.getLocationOnScreen().x;
        int y = textField.getLocationOnScreen().y + textField.getSize().height;

        // create popup
        PopupFactory factory = PopupFactory.getSharedInstance();
        calPopup = factory.getPopup(calendarButton, calendarPicker, x, y);
        calPopup.show();

        calendarPicker.resizeSelectionPanel();

        // correct position of popup when it does not fit into screen area
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension pickerSize = calendarPicker.getSize();
        x = x + pickerSize.width > screenSize.width ? screenSize.width - pickerSize.width : x;
        x = Math.max(x, 0);
{'fraction_non_alphanumeric': 0.0994663335982741, 'fraction_numerical': 0.005223117974338594, 'mean_word_length': 2.6113161131611315, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6743962', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2391, 'n_words': 496}