<commit_before>/*
 * $Id$
 *
 * Copyright 2007 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package org.jdesktop.swingx.plaf.basic;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Logger;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.UIResource;

import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.action.AbstractActionExt;

/**
 * Provides and wires a component appropriate as a calendar navigation header. The 
 * design idea is to support a pluggable header for a zoomable (PENDING JW: naming!)
 * JXMonthView. Then custom implementations can be tailored to exactly fit their needs.<p>
 * 
 * Basic navigation action should be defined by the ui delegate itself (PENDING JW: still
 * incomplete in BasicMonthViewUI). This handler 
 * can modify/enhance them as appropriate for its context.<p>
 *   
 * PENDING JW: those icons ... who's responsible? Shouldn't we use any of the default arrows
 *   as defined in the laf anyway (are there any?)<p>
 *   
 * <b>Note</b>: this is work-in-progress, be prepared to change if subclassing for custom
 * requirements!   
 * 
 * @author <NAME>
 */
public abstract class CalendarHeaderHandler {

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(CalendarHeaderHandler.class.getName());
    
    protected JXMonthView monthView;
    private JComponent calendarHeader;
    protected Icon monthDownImage;
    protected Icon monthUpImage;
    
    private PropertyChangeListener monthViewPropertyChangeListener;
    
    /**
     * Installs this handler to the given month view.
     * 
     * @param monthView the target month view to install to.
     */
    public void install(JXMonthView monthView) {
        this.monthView = monthView;
        // PENDING JW: remove here if rendererHandler takes over control completely
        // as is, some properties are duplicated
        monthDownImage = UIManager.getIcon("JXMonthView.monthDownFileName");
        monthUpImage = UIManager.getIcon("JXMonthView.monthUpFileName");
        installNavigationActions();
        installListeners();
        componentOrientationChanged();
        monthStringBackgroundChanged();
        fontChanged();
    }
    
    /**
     * Uninstalls this handler from the given target month view.
     * 
     * @param monthView the target month view to install from.
     */
    public void uninstall(JXMonthView monthView) {
        this.monthView.remove(getHeaderComponent());
        uninstallListeners();
        this.monthView = null;
    }
    
    /**
     * Returns a component to be used as header in a zoomable month view, guaranteed
     * to be not null.
     * 
     * @return a component to be used as header in a zoomable JXMonthView
     */
    public JComponent getHeaderComponent(){
        if (calendarHeader == null) {
            calendarHeader = createCalendarHeader();
        }
        return calendarHeader;
    }
    
    /**
     * Creates and registered listeners on the monthView as appropriate. This 
     * implementation registers a PropertyChangeListener which synchronizes
     * internal state on changes of componentOrientation, font and monthStringBackground.
     */
    protected void installListeners() {
        monthView.addPropertyChangeListener(getMonthViewPropertyChangeListener());
    }

    /**
     * Unregisters listeners which had been installed to the monthView.
     */
    protected void uninstallListeners() {
        monthView.removePropertyChangeListener(monthViewPropertyChangeListener);
    }
    
    /**
     * Returns the propertyChangelistener for the monthView. Lazily created. 
     * @return the propertyChangeListener for the monthView.
     */
    private PropertyChangeListener getMonthViewPropertyChangeListener() {
        if (monthViewPropertyChangeListener == null) {
            monthViewPropertyChangeListener = new PropertyChangeListener() {

                public void propertyChange(PropertyChangeEvent evt) {
                    if ("componentOrientation".equals(evt.getPropertyName())) {
                        componentOrientationChanged();
                    } else if ("font".equals(evt.getPropertyName())) {
                        fontChanged();
                    } else if ("monthStringBackground".equals(evt.getPropertyName())) {
                        monthStringBackgroundChanged();
                    }
                    
                }};
        }
        return monthViewPropertyChangeListener;
    }




    /**
     * Synchronizes internal state which depends on the month view's monthStringBackground.
     */
    protected void monthStringBackgroundChanged() {
        getHeaderComponent().setBackground(monthView.getMonthStringBackground());
        
    }
<commit_msg>Issue #1074: regression JXMonthView - header color lost in updateUI if zoomable - fixed in CalendarHeaderHandler: color/font must be set as not-UIResource on the header (which _is_ part of the component hierarchy, so updateUI will jump in to reset the color to default if not)<commit_after>/*
 * $Id$
 *
 * Copyright 2007 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package org.jdesktop.swingx.plaf.basic;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Logger;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.UIResource;

import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.action.AbstractActionExt;

/**
 * Provides and wires a component appropriate as a calendar navigation header. The 
 * design idea is to support a pluggable header for a zoomable (PENDING JW: naming!)
 * JXMonthView. Then custom implementations can be tailored to exactly fit their needs.<p>
 * 
 * Basic navigation action should be defined by the ui delegate itself (PENDING JW: still
 * incomplete in BasicMonthViewUI). This handler 
 * can modify/enhance them as appropriate for its context.<p>
 *   
 * PENDING JW: those icons ... who's responsible? Shouldn't we use any of the default arrows
 *   as defined in the laf anyway (are there any?)<p>
 *   
 * <b>Note</b>: this is work-in-progress, be prepared to change if subclassing for custom
 * requirements!   
 * 
 * @author <NAME>
 */
public abstract class CalendarHeaderHandler {

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(CalendarHeaderHandler.class.getName());
    
    protected JXMonthView monthView;
    private JComponent calendarHeader;
    protected Icon monthDownImage;
    protected Icon monthUpImage;
    
    private PropertyChangeListener monthViewPropertyChangeListener;
    
    /**
     * Installs this handler to the given month view.
     * 
     * @param monthView the target month view to install to.
     */
    public void install(JXMonthView monthView) {
        this.monthView = monthView;
        // PENDING JW: remove here if rendererHandler takes over control completely
        // as is, some properties are duplicated
        monthDownImage = UIManager.getIcon("JXMonthView.monthDownFileName");
        monthUpImage = UIManager.getIcon("JXMonthView.monthUpFileName");
        installNavigationActions();
        installListeners();
        componentOrientationChanged();
        monthStringBackgroundChanged();
        fontChanged();
    }
    
    /**
     * Uninstalls this handler from the given target month view.
     * 
     * @param monthView the target month view to install from.
     */
    public void uninstall(JXMonthView monthView) {
        this.monthView.remove(getHeaderComponent());
        uninstallListeners();
        this.monthView = null;
    }
    
    /**
     * Returns a component to be used as header in a zoomable month view, guaranteed
     * to be not null.
     * 
     * @return a component to be used as header in a zoomable JXMonthView
     */
    public JComponent getHeaderComponent(){
        if (calendarHeader == null) {
            calendarHeader = createCalendarHeader();
        }
        return calendarHeader;
    }
    
    /**
     * Creates and registered listeners on the monthView as appropriate. This 
     * implementation registers a PropertyChangeListener which synchronizes
     * internal state on changes of componentOrientation, font and monthStringBackground.
     */
    protected void installListeners() {
        monthView.addPropertyChangeListener(getMonthViewPropertyChangeListener());
    }

    /**
     * Unregisters listeners which had been installed to the monthView.
     */
    protected void uninstallListeners() {
        monthView.removePropertyChangeListener(monthViewPropertyChangeListener);
    }
    
    /**
     * Returns the propertyChangelistener for the monthView. Lazily created. 
     * @return the propertyChangeListener for the monthView.
     */
    private PropertyChangeListener getMonthViewPropertyChangeListener() {
        if (monthViewPropertyChangeListener == null) {
            monthViewPropertyChangeListener = new PropertyChangeListener() {

                public void propertyChange(PropertyChangeEvent evt) {
                    if ("componentOrientation".equals(evt.getPropertyName())) {
                        componentOrientationChanged();
                    } else if ("font".equals(evt.getPropertyName())) {
                        fontChanged();
                    } else if ("monthStringBackground".equals(evt.getPropertyName())) {
                        monthStringBackgroundChanged();
                    }
                    
                }};
        }
        return monthViewPropertyChangeListener;
    }




    /**
     * Synchronizes internal state which depends on the month view's monthStringBackground.
     */
    protected void monthStringBackgroundChanged() {
        getHeaderComponent().setBackground(getAsNotUIResource(monthView.getMonthStringBackground()));
        
    }

{'fraction_non_alphanumeric': 0.07421119057635675, 'fraction_numerical': 0.004711821623895667, 'mean_word_length': 2.9266600594648167, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '6889437', 'n_tokens_mistral': 3010, 'n_tokens_neox': 2907, 'n_words': 1046}