📄 ccombobox.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.swing;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import org.compiere.plaf.*;
import org.compiere.util.*;
/**
* Compiere Colored Combo Box.
*
* @author Jorg Janke
* @version $Id: CComboBox.java,v 1.14 2002/09/01 22:34:37 jjanke Exp $
*/
public class CComboBox extends JComboBox
implements CEditor
{
/**
* Creates a <code>JComboBox</code> that takes it's items from an
* existing <code>ComboBoxModel</code>. Since the
* <code>ComboBoxModel</code> is provided, a combo box created using
* this constructor does not create a default combo box model and
* may impact how the insert, remove and add methods behave.
*
* @param aModel the <code>ComboBoxModel</code> that provides the
* displayed list of items
* @see DefaultComboBoxModel
*/
public CComboBox(ComboBoxModel aModel)
{
super(aModel);
init();
} // CComboBox
/**
* Creates a <code>JComboBox</code> that contains the elements
* in the specified array. By default the first item in the array
* (and therefore the data model) becomes selected.
*
* @param items an array of objects to insert into the combo box
* @see DefaultComboBoxModel
*/
public CComboBox(final Object items[])
{
super(items);
init();
} // CComboBox
/**
* Creates a <code>JComboBox</code> that contains the elements
* in the specified Vector. By default the first item in the vector
* and therefore the data model) becomes selected.
*
* @param items an array of vectors to insert into the combo box
* @see DefaultComboBoxModel
*/
public CComboBox(Vector items)
{
super(items);
init();
} // CComboBox
/**
* Creates a <code>JComboBox</code> with a default data model.
* The default data model is an empty list of objects.
* Use <code>addItem</code> to add items. By default the first item
* in the data model becomes selected.
*
* @see DefaultComboBoxModel
*/
public CComboBox()
{
super();
init();
} // CComboBox
/**
* Common Init
*/
private void init()
{
// overwrite - otherwise Label Font
setFont(CompierePLAF.getFont_Field());
setForeground(CompierePLAF.getTextColor_Normal());
setBackground(false);
} // init
/*************************************************************************/
/** Icon */
private Icon m_icon = null;
/**
* Set Icon of arrow button to icon
* @param defaultIcon Icon to be displayed
*/
public void setIcon (Icon defaultIcon)
{
if (getUI() instanceof CompiereComboBoxUI)
((CompiereComboBoxUI)getUI()).setIcon(defaultIcon);
m_icon = defaultIcon;
} // setIcon
/**
* Set UI and re-set Icon for arrow button
* @param ui
*/
public void setUI (ComboBoxUI ui)
{
super.setUI(ui);
if (m_icon != null && ui instanceof CompiereComboBoxUI)
((CompiereComboBoxUI)getUI()).setIcon(m_icon);
} // setUI
/**
* Display Popup.
* Called from CompiereComboPopup and allows to implement
* alternative actions than showing the popup
* @return if true, the popup should be displayed
*/
public boolean displayPopup()
{
return true;
} // displayPopup
/*************************************************************************/
/** Mandatory (default false) */
private boolean m_mandatory = false;
/**
* Set Editor Mandatory
* @param mandatory true, if you have to enter data
*/
public void setMandatory (boolean mandatory)
{
m_mandatory = mandatory;
setBackground(false);
} // setMandatory
/**
* Is Field mandatory
* @return true, if mandatory
*/
public boolean isMandatory()
{
return m_mandatory;
} // isMandatory
/**
* Enable Editor
* @param rw true, if you can enter/select data
*/
public void setReadWrite (boolean rw)
{
if (super.isEnabled() != rw)
super.setEnabled (rw);
setBackground(false);
} // setReadWrite
/**
* Is it possible to edit
* @return true, if editable
*/
public boolean isReadWrite()
{
return super.isEnabled();
} // isReadWrite
/**
* Set Background based on editable / mandatory / error
* @param error if true, set background to error color, otherwise mandatory/editable
*/
public void setBackground (boolean error)
{
if (error)
setBackground(CompierePLAF.getFieldBackground_Error());
else if (!isReadWrite())
setBackground(CompierePLAF.getFieldBackground_Inactive());
else if (m_mandatory)
setBackground(CompierePLAF.getFieldBackground_Mandatory());
else
setBackground(CompierePLAF.getFieldBackground_Normal());
} // setBackground
/**
* Set Background
* @param bg
*/
public void setBackground (Color bg)
{
if (bg.equals(getBackground()))
return;
super.setBackground(bg);
} // setBackground
/**
* Set Editor to value
* @param value value of the editor
*/
public void setValue (Object value)
{
super.setSelectedItem(value);
} // setValue
/**
* Return Editor value
* @return current value
*/
public Object getValue()
{
return super.getSelectedItem();
} // getValue
/**
* Return Display Value
* @return displayed String value
*/
public String getDisplay()
{
Object o = super.getSelectedItem();
if (o == null)
return "";
return o.toString();
} // getDisplay
/**
* Add Mouse Listener - 1-4-0 Bug.
* Bug in 1.4.0 Metal: arrowButton gets Mouse Events, so add the JComboBox
* MouseListeners to the arrowButton - No context menu if right-click
* @see also CompiereComboBoxUI#installUI
* @param ml
*/
public void addMouseListener (MouseListener ml)
{
super.addMouseListener(ml);
// ignore calls from javax.swing.plaf.basic.BasicComboBoxUI.installListeners(BasicComboBoxUI.java:271)
if (getUI() instanceof CompiereComboBoxUI && !Trace.getCallerClass(1).startsWith("javax"))
{
JButton b = ((CompiereComboBoxUI)getUI()).getArrowButton();
if (b != null)
b.addMouseListener(ml);
}
} // addMouseListener
/**
* Remove Mouse Listener.
* @param ml
*/
public void removeMouseListener (MouseListener ml)
{
super.removeMouseListener(ml);
if (getUI() instanceof CompiereComboBoxUI)
{
JButton b = ((CompiereComboBoxUI)getUI()).getArrowButton();
if (b != null)
b.removeMouseListener(ml);
}
} // removeMouseListener
} // CComboBox
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -