⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vnumber.java

📁 Java写的ERP系统
💻 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.grid.ed;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.math.*;
import java.beans.*;

import org.compiere.util.*;
import org.compiere.apps.*;
import org.compiere.plaf.*;
import org.compiere.swing.*;
import org.compiere.model.*;

/**
 *	Number Control
 *
 * 	@author 	Jorg Janke
 * 	@version 	$Id: VNumber.java,v 1.23 2003/04/29 09:21:40 jpedersen Exp $
 */
public final class VNumber extends JComponent
	implements VEditor, ActionListener, KeyListener, FocusListener
{
	public final static int SIZE = 12;			//	width of field

	/**
	 *  IDE Bean Constructor
	 */
	public VNumber()
	{
		this("Number", false, false, true, DisplayType.Number, "Number");
	}   //  VNumber

	/**
	 *	Create right aligned Number field.
	 *	no popup, if WindowNo == 0 (for IDs)
	 *  @param columnName column name
	 *  @param mandatory mandatory
	 *  @param isReadOnly read only
	 *  @param isUpdateable updateable
	 *  @param displayType display type
	 *  @param title title
	 */
	public VNumber(String columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
		int displayType, String title)
	{
		super();
		m_columnName = columnName;
		m_title = title;
		setDisplayType(displayType);
		//
		LookAndFeel.installBorder(this, "TextField.border");
		this.setLayout(new BorderLayout());
		//  Size
		this.setPreferredSize(m_text.getPreferredSize());		//	causes r/o to be the same length
		int height = m_text.getPreferredSize().height;

		//	***	Text	***
		m_text.setBorder(null);
		m_text.setHorizontalAlignment(JTextField.TRAILING);
		m_text.addKeyListener(this);
		m_text.addFocusListener(this);
		m_text.setInputVerifier(new CInputVerifier()); //Must be called AFTER FocusListener in order to work
		this.addFocusListener(this);
		//	Background
		setMandatory(mandatory);
		this.add(m_text, BorderLayout.CENTER);

		//	***	Button	***
		m_button.setIcon(Env.getImageIcon("Calculator10.gif"));
		m_button.setMargin(new Insets(0, 0, 0, 0));
		m_button.setFocusable(false);
		m_button.setPreferredSize(new Dimension(height, height));
		m_button.addActionListener(this);
		this.add (m_button, BorderLayout.EAST);

		//	Prefereed Size
		this.setPreferredSize(this.getPreferredSize());		//	causes r/o to be the same length

		//	ReadWrite
		if (isReadOnly || !isUpdateable)
			setReadWrite(false);
		else
			setReadWrite(true);
	}	//	VNumber

	/**
	 *  Dispose
	 */
	public void dispose()
	{
		m_text = null;
		m_button = null;
		m_mField = null;
	}   //  dispose

	/**
	 *	Set Document
	 *  @param doc document
	 */
	protected void setDocument(Document doc)
	{
		m_text.setDocument(doc);
	}	//	getDocument

	private String			m_columnName;
	protected int			m_displayType;	//  Currency / UoM via Context
	private DecimalFormat	m_format;
	private String			m_title;
	private boolean			m_firstChange;
	private String			m_oldText;

	private boolean			m_rangeSet = false;
	private Double			m_minValue;
	private Double			m_maxValue;

	/**  The Field                  */
	private CTextField		m_text = new CTextField(SIZE);	//	Standard
	/** The Button                  */
	private CButton		    m_button = new CButton();

	private MField          m_mField = null;

	/**
	 *	Set Range with min & max
	 *  @param minValue min value
	 *  @param maxValue max value
	 *	@return true, if accepted
	 */
	public boolean setRange(Double minValue, Double maxValue)
	{
		m_rangeSet = true;
		m_minValue = minValue;
		m_maxValue = maxValue;
		return m_rangeSet;
	}	//	setRange

	/**
	 *	Set Range with min & max = parse US style number w/o Gouping
	 *  @param minValue min value
	 *  @param maxValue max value
	 *  @return true if accepted
	 */
	public boolean setRange(String minValue, String maxValue)
	{
		if (minValue == null || maxValue == null)
			return false;
		try
		{
			m_minValue = Double.valueOf(minValue);
			m_maxValue = Double.valueOf(maxValue);
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		m_rangeSet = true;
		return m_rangeSet;
	}	//	setRange

	/**
	 *  Set and check DisplayType
	 *  @param displayType display type
	 */
	public void setDisplayType (int displayType)
	{
		m_displayType = displayType;
		if (!DisplayType.isNumeric(displayType))
			m_displayType = DisplayType.Number;
		m_format = DisplayType.getNumberFormat(displayType);
		m_text.setDocument (new MDocNumber(displayType, m_format, m_text, m_title));
	}   //  setDisplayType

	/**
	 *	Set ReadWrite
	 *  @param value value
	 */
	public void setReadWrite (boolean value)
	{
		if (m_text.isReadWrite() != value)
			m_text.setReadWrite(value);
		if (m_button.isReadWrite() != value)
			m_button.setReadWrite(value);
		//	Don't show button if not ReadWrite
		if (m_button.isVisible() != value)
			m_button.setVisible(value);
	}	//	setReadWrite

	/**
	 *	IsReadWrite
	 *  @return true if rw
	 */
	public boolean isReadWrite()
	{
		return m_text.isReadWrite();
	}	//	isReadWrite

	/**
	 *	Set Mandatory (and back bolor)
	 *  @param mandatory mandatory
	 */
	public void setMandatory (boolean mandatory)
	{
		m_text.setMandatory(mandatory);
	}	//	setMandatory

	/**
	 *	Is it mandatory
	 *  @return true if mandatory
	 */
	public boolean isMandatory()
	{
		return m_text.isMandatory();
	}	//	isMandatory

	/**
	 *	Set Background
	 *  @param color color
	 */
	public void setBackground(Color color)
	{
		m_text.setBackground(color);
	}	//	setBackground

	/**
	 *	Set Background
	 *  @param error error
	 */
	public void setBackground (boolean error)
	{
		m_text.setBackground(error);
	}	//	setBackground

	/**
	 *  Set Foreground
	 *  @param fg foreground
	 */
	public void setForeground(Color fg)
	{
		m_text.setForeground(fg);
	}   //  setForeground

	/**
	 *	Set Editor to value
	 *  @param value value
	 */
	public void setValue(Object value)
	{
		if (value == null)
			m_oldText = "";
		else
			m_oldText = m_format.format(value);
		//
		m_text.setText(m_oldText);
		m_firstChange = true;
	}	//	setValue

	/**
	 *  Property Change Listener
	 *  @param evt event
	 */
	public void propertyChange (PropertyChangeEvent evt)
	{
		if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
			setValue(evt.getNewValue());
	}   //  propertyChange

	/**
	 *	Return Editor value
	 *  @return value value
	 */
	public Object getValue()
	{
		String value = m_text.getText();
		if (value == null || value.length() == 0)
			return null;
		if (value.equals(".") || value.equals(",") || value.equals("-"))
			value = "0";
		try
		{
			Number number = m_format.parse(value);
			value = number.toString();      //	converts it to US w/o thousands
			BigDecimal bd = new BigDecimal(value);
			return bd.setScale(m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
		}
		catch (Exception e)
		{
			Log.error("VNumber.getValue", e);
		}
		return Env.ZERO;
	}	//	getValue

	/**
	 *  Return Display Value
	 *  @return value
	 */
	public String getDisplay()
	{
		return m_text.getText();
	}   //  getDisplay


	/*************************************************************************/

	/**
	 *	Action Listener
	 *  @param e event
	 */
	public void actionPerformed (ActionEvent e)
	{
		Log.trace(Log.l4_Data, "VNumber.actionPerformed", e.getActionCommand());
		if (e.getActionCommand().equals(ValuePreference.NAME))
		{
			ValuePreference.start (m_mField, getValue());
			return;
		}

		if (e.getSource() == m_button)
		{
			m_button.setEnabled(false);
			requestFocus();
			String str = startCalculator(this, m_text.getText(), m_format, m_displayType, m_title);
			m_text.setText(str);
			fireChange(false);
			m_button.setEnabled(true);
			requestFocus();
		}
	}	//	actionPerformed

	/**************************************************************************
	 *	Key Listener Interface
	 *  @param e event
	 */
	public void keyTyped(KeyEvent e)    {}
	public void keyPressed(KeyEvent e)  {}

	/**
	 *	Key Listener.
	 *		- Escape 		- Restore old Text
	 *		- firstChange	- signal change
	 *  @param e event
	 */
	public void keyReleased(KeyEvent e)
	{
	//	Log.trace(Log.l4_Data, "VNumber.keyReleased - " + e.getKeyCode());
		//	Enter
		if (e.getKeyCode() == KeyEvent.VK_ENTER)
			fireChange(false);
		//  Esc
		else if (e.getKeyCode() == KeyEvent.VK_ESCAPE && !m_text.getText().equals(m_oldText))
		{
			m_text.setText(m_oldText);
			m_firstChange = true;
		}
		//  First Change
		else if (m_firstChange && !m_text.getText().equals(m_oldText))
		{
			fireChange(true);
			m_firstChange = false;
		}	//	firstChange
	}	//	keyReleased

	/**
	 *	Focus Gained	- Save for Escape
	 *  @param e event
	 */
	public void focusGained (FocusEvent e)
	{
		if (e.isTemporary())
			return;
		//Log.trace(Log.l4_Data, "VNumber.focusGained");
		if (e.getSource() == this)
			m_text.requestFocus();
		else
			m_oldText = m_text.getText();
	}	//	focusGained

	/**
	 *	Data Binding to MTable (via GridController).
	 *  @param e event
	 */
	public void focusLost (FocusEvent e)
	{
	//Log.trace(Log.l4_Data, "VNumber.focusLost");
	return;
	}   //  focusLost

	/**
	 *  Initiate Change
	 *  @param first first cahnge
	 */
	private void fireChange (boolean first)
	{
		try
		{
			if (first)
				fireVetoableChange(m_columnName, getValue(), null);
			else
			{
				fireVetoableChange(m_columnName, null, getValue());
				m_oldText = m_text.getText();
				fireActionPerformed();
			}
		}
		catch (PropertyVetoException pve)
		{
			Log.error("VNumber.fireChange", pve);
		}
	}	//	fireChange

	/**
	 *	Invalid Entry - Start Calculator
	 *  @param jc parent
	 *  @param value value
	 *  @param format format
	 *  @param displayType display type
	 *  @param title title
	 *  @return value
	 */
	public static String startCalculator(Container jc, String value,
		DecimalFormat format, int displayType, String title)
	{
		Log.trace(Log.l3_Util, "VNumber startCalculator - " + value);
		BigDecimal startValue = new BigDecimal(0.0);
		try
		{
			if (value != null && value.length() > 0)
			{
				Number number = format.parse(value);
				startValue = new BigDecimal (number.toString());
			}
		}
		catch (ParseException pe)
		{
			Log.error("VNumber invalidEntry - " + pe.getMessage());
		}

		//	Find frame
		Frame frame = Env.getFrame(jc);
		//	Actual Call
		Calculator calc = new Calculator(frame, title,
			displayType, format, startValue);
		AEnv.showCenterWindow(frame, calc);
		BigDecimal result = calc.getNumber();
		Log.trace(Log.l4_Data, "Result=" + result);
		//
		calc = null;
		if (result != null)
			return format.format(result);
		else
			return value;		//	original value
	}	//	startCalculator

	/**
	 *  Set Field/WindowNo for ValuePreference
	 *  @param mField field
	 */
	public void setField (MField mField)
	{
	//	m_mField = mField;
	//	if (m_mField != null)
	//		ValuePreference.addMenu (this, popupMenu);
	}   //  setField

	/*************************************************************************/

	/**
	 * 	Remove Action Listner
	 * 	@param l Action Listener
	 */
	public void removeActionListener(ActionListener l)
	{
		listenerList.remove(ActionListener.class, l);
	}	//	removeActionListener

	/**
	 * 	Add Action Listner
	 * 	@param l Action Listener
	 */
	public void addActionListener(ActionListener l)
	{
		listenerList.add(ActionListener.class, l);
	}	//	addActionListener

	/**
	 * 	Fire Action Event to listeners
	 */
	protected void fireActionPerformed()
	{
		int modifiers = 0;
		AWTEvent currentEvent = EventQueue.getCurrentEvent();
		if (currentEvent instanceof InputEvent)
			modifiers = ((InputEvent)currentEvent).getModifiers();
		else if (currentEvent instanceof ActionEvent)
			modifiers = ((ActionEvent)currentEvent).getModifiers();
		ActionEvent ae = new ActionEvent (this, ActionEvent.ACTION_PERFORMED,
			"VDate", EventQueue.getMostRecentEventTime(), modifiers);

		// Guaranteed to return a non-null array
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first, notifying those that are interested in this event
		for (int i = listeners.length-2; i>=0; i-=2)
		{
			if (listeners[i]==ActionListener.class)
			{
				((ActionListener)listeners[i+1]).actionPerformed(ae);
			}
		}
	}	//	fireActionPerformed





class CInputVerifier extends InputVerifier {


     public boolean verify(JComponent input) {


		//Note: We always return true since Inputverifier is just used to guarantee that fireChange is called in due time

		//	something changed?
		if (m_text == null || m_text.getText() == null && m_oldText == null)
			return true;
		else if (m_text.getText().equals(m_oldText))
			return true;


		//  Set Value
		fireChange(false);
		return true;

	} // verify


} // CInputVerifier




}	//	VNumber

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -