Package realcix20.guis.components

Source Code of realcix20.guis.components.JMonthPicker$MetalDateComboBoxUI

/*
* JMonthPicker.java
*
* Created on 2006��10��12��, ����9:40
*
* RealCix2.0
*/

package realcix20.guis.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;

import realcix20.utils.Resources;

import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;

public class JMonthPicker extends JComboBox {
   
    public static final SimpleDateFormat monthFormat = new SimpleDateFormat(
      "yyyyMM");
   
    public JMonthPicker() {
        setEditable(true);
        setSelectedItem(monthFormat.format(new Date()));
    }
   
    public void setSelectedItem(Object item) {
        removeAllItems();
        addItem(item);
        super.setSelectedItem(item);
    }
   
    public String getSelectedMonth() {
        try {
                String month = getSelectedItem().toString();               
                if ( (month.length() < 6) || (month.length() > 6) ) {                   
                    month = "000001";//error
                    this.setSelectedItem(month);
                } else if (month.length() == 6) {
                    try {
                        int year = Integer.parseInt(month.substring(0, 4));
                        int month1 = Integer.parseInt(month.substring(4, 6));
                        if ( (year < 0) || (month1 < 0) || (month1 > 12) ) {
                            month = "000001";
                        } else if ( (year == 0) && (month1 == 0) )
                            month = "000000";
                    } catch (Exception e) {
                        month = "000001";
                    }
                }
                return month;
        } catch (Exception ex) {
                return monthFormat.format(new Date());
        }
    }
   
    public void updateUI() {
        ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this);
        if (cui instanceof MetalComboBoxUI) {
                cui = new MetalDateComboBoxUI();
        } else if (cui instanceof MotifComboBoxUI) {
                cui = new MotifDateComboBoxUI();
        } else if (cui instanceof WindowsComboBoxUI) {
                cui = new WindowsDateComboBoxUI();
        }else{
                cui = new WindowsDateComboBoxUI();
        }
        setUI(cui);
    }
   
    class MetalDateComboBoxUI extends MetalComboBoxUI {
            protected ComboPopup createPopup() {
                    return new MonthPopup(comboBox);
            }
    }

    class WindowsDateComboBoxUI extends WindowsComboBoxUI {
            protected ComboPopup createPopup() {
                    return new MonthPopup(comboBox);
            }
    }

    class MotifDateComboBoxUI extends MotifComboBoxUI {
            protected ComboPopup createPopup() {
                    return new MonthPopup(comboBox);
            }
    }

    class MonthPopup extends BasicComboPopup {
            protected Calendar calendar;

            protected JLabel monthLabel;

            protected Color selectedBackground;

            protected Color selectedForeground;

            protected Color background;

            protected Color foreground;

            private JPanel header;

            private DateFormatSymbols sy;

            public MonthPopup(JComboBox box) {
                    super(box);
                    //todayIcon=new ImageIcon(Application.instance().getServices().getImage("today"));
                    if (Resources.getLanguage().equals("EN")) {
                        sy = new DateFormatSymbols(Locale.US);
                    } else if (Resources.getLanguage().equals("ZH")) {
                        sy = new DateFormatSymbols(Locale.CHINA);
                    } else if (Resources.getLanguage().equals("DE")) {
                        sy = new DateFormatSymbols(Locale.GERMAN);
                    } else {
                        sy = new DateFormatSymbols(Locale.US);
                    }
                    calendar = Calendar.getInstance();
                    background = UIManager.getColor("ComboBox.background");
                    foreground = UIManager.getColor("ComboBox.foreground");
                    selectedBackground = UIManager.getColor("ComboBox.selectionBackground");
                    selectedForeground = UIManager.getColor("ComboBox.selectionForeground");
                    initializePopup();
            }

            protected void initializePopup() {
                    header = new JPanel();
                    header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
                    header.setBackground(Color.LIGHT_GRAY);
                    header.setForeground(Color.white);
                    //header.setPreferredSize(new Dimension(1, 25));

                    JLabel label;
                    label = createUpdateButton(Calendar.YEAR, -1);
                    label.setText("<<");
                    header.add(Box.createHorizontalStrut(12));
                    header.add(label);
                    header.add(Box.createHorizontalStrut(12));

                    label = createUpdateButton(Calendar.MONTH, -1);
                    label.setText("<   ");
                    header.add(label);

                    monthLabel = createMonthSelectButton();
                    header.add(Box.createHorizontalGlue());
                    header.add(monthLabel);
                    header.add(Box.createHorizontalGlue());

                    label = createUpdateButton(Calendar.MONTH, 1);
                    label.setText("   >");
                    header.add(label);

                    label = createUpdateButton(Calendar.YEAR, 1);
                    label.setText(">>");

                    header.add(Box.createHorizontalStrut(12));
                    header.add(label);
                    header.add(Box.createHorizontalStrut(12));
                   
                    setForeground(foreground);
                    setBackground(background);
                    setBorder(BorderFactory.createLineBorder(Color.black));
                    setLayout(new BorderLayout());
                    removeAll();
                   
                    add(BorderLayout.CENTER, header);
                    pack();
            }

            //�·ݿؼ�.
            public JPanel getMonthControl(){
                    initializePopup();
                    return header;
            }

            public void show() {
                    updatePopup();
                    super.show();
            }          
           
            protected JLabel createMonthSelectButton() {
                    final JLabel label = new JLabel();
                    label.setBackground(new Color(0, 0, 128));
                    label.setForeground(Color.white);
                    label.setRequestFocusEnabled(false);
                    label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                    label.addMouseListener(new MouseAdapter() {
                            public void mouseReleased(MouseEvent e) {
                                    comboBox.setSelectedItem(monthLabel.getText());
                                    comboBox.setPopupVisible(false);
                            }

                            public void mouseEntered(MouseEvent e) {
                            }

                            public void mouseExited(MouseEvent e) {
                            }
                    });
                    return label;
            }
           
            protected JLabel createUpdateButton(final int field, final int amount) {
                    final JLabel label = new JLabel();
                    final Border selectedBorder = new LineBorder(Color.black);
                    final Border unselectedBorder = new EmptyBorder(selectedBorder
                                    .getBorderInsets(new JLabel()));
                    label.setBorder(unselectedBorder);
                    label.setBackground(new Color(0, 0, 128));
                    label.setForeground(Color.white);
                    label.setRequestFocusEnabled(false);
                    label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                    label.addMouseListener(new MouseAdapter() {
                            public void mouseReleased(MouseEvent e) {
                                    calendar.add(field, amount);
                                    updatePopup();
                            }

                            public void mouseEntered(MouseEvent e) {
                                    label.setBorder(selectedBorder);
                            }

                            public void mouseExited(MouseEvent e) {
                                    label.setBorder(unselectedBorder);
                            }
                    });
                    return label;
            }

            protected void updatePopup() {               
                    monthLabel.setText(monthFormat.format(calendar.getTime()));
                    pack();
            }
           
    }
 
}
TOP

Related Classes of realcix20.guis.components.JMonthPicker$MetalDateComboBoxUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.