📄 mdocnumber.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 javax.swing.text.*;
import javax.swing.event.DocumentEvent;
import java.text.*;
import java.util.*;
import org.compiere.util.*;
import org.compiere.apps.*;
/**
* Number Document Model.
* Locale independent editing of numbers by removing thousands
* and treating ., as decimal separator. Final formatting in VNumber.setValue
*
* @see VNumber
* @author Jorg Janke
* @version $Id: MDocNumber.java,v 1.3 2002/05/26 01:22:24 jjanke Exp $
*/
public final class MDocNumber extends PlainDocument
{
/**
* Constructor
* @param displayType
* @param format
* @param tc
* @param title
*/
public MDocNumber(int displayType, DecimalFormat format,
JTextComponent tc, String title)
{
super();
if (format == null || tc == null || title == null)
throw new IllegalArgumentException("MDocNumber - invalid argument");
//
m_displayType = displayType;
m_format = format;
m_sym = m_format.getDecimalFormatSymbols();
m_tc = tc;
m_title = title;
} // MDocNumber
/** DisplayType used */
private int m_displayType = 0;
/** Number Format */
private DecimalFormat m_format = null;
/** Number Format Symbols */
private DecimalFormatSymbols m_sym = null;
/** The 'owning' component */
private JTextComponent m_tc = null;
/** Title for calculator */
private String m_title = null;
/*************************************************************************/
/**
* Insert String
* @param origOffset
* @param string
* @param attr
* @throws BadLocationException
*/
public void insertString(int origOffset, String string, AttributeSet attr)
throws BadLocationException
{
// ADebug.trace(ADebug.l5_DData, "MDocNumber.insert - O=" + origOffset + " S=" + string + " L=" + string.length());
if (origOffset < 0 || string == null)
throw new IllegalArgumentException("MDocNumber.insertString - invalid argument");
int offset = origOffset;
int length = string.length();
// From DataBinder (assuming correct format)
if (length != 1)
{
super.insertString(offset, string, attr);
return;
}
/**
* Manual Entry
*/
String content = getText();
// remove all Thousands
if (content.indexOf(m_sym.getGroupingSeparator()) != -1)
{
StringBuffer result = new StringBuffer();
for (int i = 0; i < content.length(); i++)
{
if (content.charAt(i) == m_sym.getGroupingSeparator())
{
if (i < offset)
offset--;
}
else
result.append(content.charAt(i));
}
super.remove(0, content.length());
super.insertString(0, result.toString(), attr);
//
m_tc.setCaretPosition(offset);
// ADebug.trace(ADebug.l6_Database, "Clear Thousands (" + m_format.toPattern() + ")" + content + " -> " + result.toString());
content = result.toString();
} // remove Thousands
/**********************************************************************
* Check Character entered
*/
char c = string.charAt(0);
if (Character.isDigit(c)) // c >= '0' && c <= '9')
{
// ADebug.trace(ADebug.l6_Database, "Digit=" + c);
super.insertString(offset, string, attr);
return;
}
// Plus - remove minus sign
if (c == '+')
{
// ADebug.trace(ADebug.l6_Database, "Plus=" + c);
// only positive numbers
if (m_displayType == DisplayType.Integer)
return;
if (content.charAt(0) == '-')
super.remove(0, 1);
}
// Toggle Minus - put minus on start of string
else if (c == '-' || c == m_sym.getMinusSign())
{
// ADebug.trace(ADebug.l6_Database, "Minus=" + c);
// no minus possible
if (m_displayType == DisplayType.Integer)
return;
// remove or add
if (content.length() > 0 && content.charAt(0) == '-')
super.remove(0, 1);
else
super.insertString(0, "-", attr);
}
// Decimal - remove other decimals
// Thousand - treat as Decimal
else if (c == m_sym.getDecimalSeparator() || c == m_sym.getGroupingSeparator())
{
// ADebug.trace(ADebug.l6_Database, "decimal=" + c + " (ds=" + m_sym.getDecimalSeparator() + "; gs=" + m_sym.getGroupingSeparator() + ")");
// no decimals on integers
if (m_displayType == DisplayType.Integer)
return;
int pos = content.indexOf(m_sym.getDecimalSeparator());
// put decimal in
String decimal = String.valueOf(m_sym.getDecimalSeparator());
super.insertString(offset, decimal, attr);
// remove other decimals
if (pos != 0)
{
content = getText();
StringBuffer result = new StringBuffer();
int correction = 0;
for (int i = 0; i < content.length(); i++)
{
if (content.charAt(i) == m_sym.getDecimalSeparator())
{
if (i == offset)
result.append(content.charAt(i));
else if (i < offset)
correction++;
}
else
result.append(content.charAt(i));
}
super.remove(0, content.length());
super.insertString(0, result.toString(), attr);
m_tc.setCaretPosition(offset-correction+1);
} // remove other decimals
} // decial or thousand
// something else
else
{
String result = VNumber.startCalculator(m_tc, getText(),
m_format, m_displayType, m_title);
super.remove(0, content.length());
super.insertString(0, result, attr);
}
} // insertString
/*************************************************************************/
/**
* Delete String
* @param origOffset
* @param length
* @throws BadLocationException
*/
public void remove (int origOffset, int length)
throws BadLocationException
{
// ADebug.trace(ADebug.l5_DData, "MDocNumber.remove - Offset=" + offset + " Length=" + length);
if (origOffset < 0 || length < 0)
throw new IllegalArgumentException("MDocNumber.remove - invalid argument");
int offset = origOffset;
if (length != 1)
{
super.remove(offset, length);
return;
}
/**
* Manual Entry
*/
String content = getText();
// remove all Thousands
if (content.indexOf(m_sym.getGroupingSeparator()) != -1)
{
StringBuffer result = new StringBuffer();
for (int i = 0; i < content.length(); i++)
{
if (content.charAt(i) == m_sym.getGroupingSeparator() && i != origOffset)
{
if (i < offset)
offset--;
}
else
result.append(content.charAt(i));
}
super.remove(0, content.length());
super.insertString(0, result.toString(), null);
m_tc.setCaretPosition(offset);
} // remove Thousands
super.remove(offset, length);
} // remove
/**
* Get Full Text
* @return text
*/
private String getText()
{
Content c = getContent();
String str = "";
try
{
str = c.getString(0, c.length()-1); // cr at end
}
catch (Exception e)
{}
return str;
} // getString
} // MDocNumber
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -