📄 basicdateentryui.java
字号:
/** Copyright (c) 2001 Martin Newstead (mseries@brundell.fsnet.co.uk). All Rights Reserved.** The author makes no representations or warranties about the suitability of the* software, either express or implied, including but not limited to the* implied warranties of merchantability, fitness for a particular* purpose, or non-infringement. The author shall not be liable for any damages* suffered by licensee as a result of using, modifying or distributing* this software or its derivatives.** The author requests that he be notified of any application, applet, or other binary that* makes use of this code and that some acknowedgement is given. Comments, questions and* requests for change will be welcomed.*/package mseries.plaf.basic;import mseries.Calendar.MDateSelectorPanel;import mseries.Calendar.MMonthEvent;import mseries.Calendar.MMonthListener;import mseries.ui.*;import mseries.utils.MComboBoxLayout;import javax.swing.*;import javax.swing.border.Border;import javax.swing.event.AncestorEvent;import javax.swing.event.AncestorListener;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.metal.MetalLookAndFeel;import java.awt.*;import java.awt.event.*;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.Date;import java.util.Vector;public class BasicDateEntryUI extends ComponentUI implements PropertyChangeListener{ // The arrow button that invokes the popup. protected JButton arrowButton; boolean isShowing = false; boolean mustClose = false; Action activator; Action openAction = new OpenAction(); Action closeAction = new CloseAction(); Action cancelAction = new CancelAction(); MDateField display; MDateSelectorPanel panel; MPopup popup; Border border = null; MMonthListener mMonthListener; AncestorListener ancListener; MouseListener mouseListener; protected MDateEntryField dateEntry; private final Object classLock = new Object(); private static final int MAX_CACHE_SIZE = 1; private final Vector lightPopupCache = new Vector(MAX_CACHE_SIZE); private static final Vector heavyPopupCache = new Vector(MAX_CACHE_SIZE); Dimension d; /** * This method is called by the UIManager to get an instance of * this class and must be overridden in subclasses. */ public static ComponentUI createUI(JComponent x) { return new BasicDateEntryUI(); } /* * Called by the UIManager to install the UI of the component */ public void installUI(JComponent c) { dateEntry = (MDateEntryField) c; installComponents(); configureDisplay(dateEntry.getDisplay()); configureBorder(dateEntry); dateEntry.setLayout(createLayoutManager()); installListeners(); } public void uninstallUI(JComponent c) { dateEntry.setLayout(null); uninstallListeners(); uninstallComponents(); } public void update(Graphics g, JComponent c) { paint(g, c); } public void paint(Graphics g, JComponent c) { } protected void installListeners() { if ((activator = createActionListener()) != null) { arrowButton.addActionListener(activator); } if ((ancListener = createAncestorListener()) != null) { dateEntry.addAncestorListener(ancListener); } if ((mouseListener = createMouseListener()) != null) { arrowButton.addMouseListener(mouseListener); } registerKeyboardActions(); mMonthListener = createMMonthListener(); dateEntry.addPropertyChangeListener(this); } protected void uninstallListeners() { arrowButton.removeActionListener(activator); arrowButton.removeMouseListener(mouseListener); dateEntry.addPropertyChangeListener(this); unRegisterKeyboardActions(); dateEntry.removeAncestorListener(ancListener); } /** * Creates the standard combo box layout manager that has the arrow button to * the right and the editor to the left. * Returns an instance of BasicComboBoxUI$ComboBoxLayoutManager. */ protected LayoutManager createLayoutManager() { return new MComboBoxLayout(); } /** * The editor and arrow button are added to the JComboBox here. */ protected void installComponents() { display = dateEntry.getDisplay(); dateEntry.add(display); arrowButton = createArrowButton(); dateEntry.add(arrowButton); } protected void uninstallComponents() { arrowButton = null; dateEntry.removeAll(); } /** * Creates the arrow button. Subclasses can create any button they like. * The default behavior of this class is to attach various listeners to the * button returned by this method. * Returns an instance of BasicArrowButton. */ protected JButton createArrowButton() { JButton x = new ArrowButton(ArrowButton.SOUTH); x.setBackground(dateEntry.getBackground()); x.setForeground(dateEntry.getBackground()); return x; } /** * Gets the insets from the JComboBox. */ protected Insets getInsets() { return dateEntry.getInsets(); } /** * This is where we add a border the component, (the display field and the button) * @param c the entire component */ protected void configureBorder(JComponent c) { c.setBorder(BorderFactory.createEtchedBorder(MetalLookAndFeel.getControlHighlight(), MetalLookAndFeel.getControlDarkShadow() )); } /** * This is where we would configure the display field part of the component, such as remove * the dfault border and change preferred size. * @param display the display part of the component */ public void configureDisplay(JComponent display) { } /** * Class to encapsulate the open and close action, activated by the button and * keyboard keys */ protected class OpenCloseAction extends AbstractAction { public void actionPerformed(ActionEvent e) { String command; command = getActionCommand(e); if (command.equals("OPEN")) { mustClose = false; } if (!isShowing) { if (mustClose) { display.requestFocus(); return; } isShowing = true; showPopup(); } else { isShowing = false; panel.close(command); } } protected String getActionCommand(ActionEvent e) { return e.getActionCommand(); } } protected class OpenAction extends OpenCloseAction { protected String getActionCommand(ActionEvent e) { return "OPEN"; } } protected class CloseAction extends OpenCloseAction { protected String getActionCommand(ActionEvent e) { return "CLOSE"; } } protected class CancelAction extends OpenCloseAction { protected String getActionCommand(ActionEvent e) { return "CANCEL"; } } protected Action createActionListener() { return new OpenCloseAction(); } protected MouseListener createMouseListener() { /* There follows a major cludge to get the popup to close * properly. */ return new MouseAdapter() { public void mousePressed(MouseEvent e) { display.requestFocus(); } public void mouseReleased(MouseEvent e) { mustClose = isShowing; } public void mouseEntered(MouseEvent e) { mustClose = isShowing; } public void mouseExited(MouseEvent e) { mustClose = isShowing; } }; } protected void registerKeyboardActions() { display.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK), "OPEN"); display.getActionMap().put("OPEN", openAction); } protected void unRegisterKeyboardActions() { //display.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_MASK)); } private void showPopup() { if (panel == null) { panel = createMDateSelectorPanel(); } if (popup == null) { dateEntry.notifyListeners(new FocusEvent(dateEntry, FocusEvent.FOCUS_GAINED)); /* * Remove the focus listener so that when the pull down receives the focus, the * focusLost event is not caught */ dateEntry.opened(); Point p = dateEntry.getLocationOnScreen(); panel.setShowTodayButton(dateEntry.getShowTodayButton()); panel.setCloseOnToday(dateEntry.getCloseOnToday()); panel.setPullDownConstraints(dateEntry.getConstraints()); panel.setBorder(createBorder()); d = panel.getPreferredSize(); if (checkLightPosition(dateEntry, p)) { popup = createLightWeightPopup(); } else { checkHeavyPosition(dateEntry, p); popup = createHeavyWeightPopup(); } /* This is where the MCalendarPanel is configured */ if (display.getMinimum() != null) { panel.setMinimum(display.getMinimum()); } if (display.getMaximum() != null) { panel.setMaximum(display.getMaximum()); } panel.setDate(display.getValue(new Date())); panel.addMMonthListener(mMonthListener); popup.setShadow(dateEntry.getConstraints().hasShadow()); popup.addComponent(panel, BorderLayout.CENTER); popup.pack(); popup.setLocationOnScreen(p.x, p.y); popup.setParent(display); popup.setVisible(true); popup.requestFocus(); dateEntry.notifyListeners(MChangeEvent.PULLDOWN_OPENED); } } private void destroyPopup() { if (popup != null) { panel.removeMMonthListener(mMonthListener); popup.setVisible(false); dateEntry.notifyListeners(MChangeEvent.PULLDOWN_CLOSED); isShowing = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -