📄 xpcomboboxui.java
字号:
// Beta
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* XP Look and Feel *
* *
* (C) Copyright 2002, by Stefan Krause *
* *
* *
* 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 General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.stefankrause.xplookandfeel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxIcon;
public class XPComboBoxUI extends BasicComboBoxUI {
static int comboBoxButtonSize = 19;
public static ComponentUI createUI(JComponent c) {
return new XPComboBoxUI();
}
public void paint(Graphics g, JComponent c) {
}
protected ComboBoxEditor createEditor() {
return new XPComboBoxEditor.UIResource();
}
protected ComboPopup createPopup() {
return new MetalComboPopup(comboBox);
}
protected JButton createArrowButton() {
JButton button = new XPComboBoxButton(comboBox, //
new MetalComboBoxIcon(), comboBox.isEditable(), currentValuePane, listBox);
button.setMargin(new Insets(0, 0, 0, 0));
return button;
}
public PropertyChangeListener createPropertyChangeListener() {
return new XPPropertyChangeListener();
}
/**
* This inner class is marked "public" due to a compiler bug.
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of <FooUI>.
*/
public class XPPropertyChangeListener extends BasicComboBoxUI.PropertyChangeHandler {
public void propertyChange(PropertyChangeEvent e) {
super.propertyChange(e);
String propertyName = e.getPropertyName();
if (propertyName.equals("editable")) {
XPComboBoxButton button = (XPComboBoxButton) arrowButton;
button.setIconOnly(comboBox.isEditable());
comboBox.repaint();
} else if (propertyName.equals("background")) {
Color color = (Color) e.getNewValue();
listBox.setBackground(color);
} else if (propertyName.equals("foreground")) {
Color color = (Color) e.getNewValue();
listBox.setForeground(color);
}
}
}
/**
* As of Java 2 platform v1.4 this method is no longer used. Do not call or
* override. All the functionality of this method is in the
* MetalPropertyChangeListener.
*
* @deprecated As of Java 2 platform v1.4.
*/
protected void editablePropertyChanged(PropertyChangeEvent e) {
}
protected LayoutManager createLayoutManager() {
return new MetouiaComboBoxLayoutManager();
}
/**
* This inner class is marked "public" due to a compiler bug.
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of <FooUI>.
*/
public class MetouiaComboBoxLayoutManager implements LayoutManager {
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container parent) {
JComboBox cb = (JComboBox) parent;
return parent.getPreferredSize();
}
public Dimension minimumLayoutSize(Container parent) {
JComboBox cb = (JComboBox) parent;
return parent.getMinimumSize();
}
public void layoutContainer(Container parent) {
JComboBox cb = (JComboBox) parent;
int width = cb.getWidth();
int height = cb.getHeight();
Rectangle cvb;
if (comboBox.isEditable()) {
if (arrowButton != null) {
arrowButton.setBounds(width - comboBoxButtonSize, 0, comboBoxButtonSize, height);
}
if (editor != null) {
cvb = rectangleForCurrentValue2();
editor.setBounds(cvb);
}
} else {
arrowButton.setBounds(0, 0, width, height);
}
}
}
protected Rectangle rectangleForCurrentValue2() {
int width = comboBox.getWidth();
int height = comboBox.getHeight();
Insets insets = getInsets();
int buttonSize = height - (insets.top + insets.bottom);
if (arrowButton != null) {
buttonSize = comboBoxButtonSize;
}
if (comboBox.getComponentOrientation().isLeftToRight()) {
return new Rectangle(insets.left, insets.top, width - (insets.left + insets.right + buttonSize), height - (insets.top + insets.bottom));
} else {
return new Rectangle(
insets.left + buttonSize,
insets.top,
width - (insets.left + insets.right + buttonSize),
height - (insets.top + insets.bottom));
}
}
/**
* As of Java 2 platform v1.4 this method is no
* longer used.
*
* @deprecated As of Java 2 platform v1.4.
*/
protected void removeListeners() {
if (propertyChangeListener != null) {
comboBox.removePropertyChangeListener(propertyChangeListener);
}
}
// These two methods were overloaded and made public. This was probably a
// mistake in the implementation. The functionality that they used to
// provide is no longer necessary and should be removed. However,
// removing them will create an uncompatible API change.
public void configureEditor() {
super.configureEditor();
}
public void unconfigureEditor() {
super.unconfigureEditor();
}
public Dimension getMinimumSize(JComponent c) {
if (!isMinimumSizeDirty) {
return new Dimension(cachedMinimumSize);
}
Dimension size = null;
if (!comboBox.isEditable() && arrowButton != null && arrowButton instanceof XPComboBoxButton) {
XPComboBoxButton button = (XPComboBoxButton) arrowButton;
Insets buttonInsets = new Insets(0, 0, 0, 0);
Insets insets = comboBox.getInsets();
size = getDisplaySize();
size.width += comboBoxButtonSize + insets.left + insets.right; // Hack
size.width += buttonInsets.left + buttonInsets.right;
size.width += buttonInsets.right + button.getComboIcon().getIconWidth();
size.height += insets.top + insets.bottom;
size.height += buttonInsets.top + buttonInsets.bottom;
size.height = Math.max(21,size.height);
} else if (comboBox.isEditable() && arrowButton != null && editor != null) {
size = super.getMinimumSize(c);
Insets margin = arrowButton.getMargin();
Insets insets = comboBox.getInsets();
if (editor instanceof JComponent) {
Insets editorInsets = ((JComponent) editor).getInsets();
}
size.height += margin.top + margin.bottom;
size.height += insets.top + insets.bottom;
// size.height = Math.max(20,size.height);
} else {
size = super.getMinimumSize(c);
}
cachedMinimumSize.setSize(size.width, size.height);
isMinimumSizeDirty = false;
return new Dimension(cachedMinimumSize);
}
/**
* This inner class is marked "public" due to a compiler bug.
* This class should be treated as a "protected" inner class.
* Instantiate it only within subclasses of <FooUI>.
*
* This class is now obsolete and doesn't do anything and
* is only included for backwards API compatibility. Do not call or
* override.
*
* @deprecated As of Java 2 platform v1.4.
*/
public class MetalComboPopup extends BasicComboPopup {
public MetalComboPopup(JComboBox cBox) {
super(cBox);
}
// This method was overloaded and made public. This was probably
// mistake in the implementation. The functionality that they used to
// provide is no longer necessary and should be removed. However,
// removing them will create an uncompatible API change.
public void delegateFocus(MouseEvent e) {
super.delegateFocus(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -