📄 calculator.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 Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.grid.ed;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;
import org.compiere.swing.*;
import org.compiere.apps.*;
import org.compiere.model.*;
import org.compiere.util.*;
/**
* Calculator with currency conversion
*
* @author Jorg Janke
* @version $Id: Calculator.java,v 1.13 2006/01/28 01:29:11 jjanke Exp $
*/
public final class Calculator extends CDialog
implements ActionListener, KeyListener
{
/**
* Create Calculator
* @param frame parent
* @param title title
* @param displayType date or datetime or time
* @param format display format
* @param number initial amount
*/
public Calculator(Frame frame, String title, int displayType,
DecimalFormat format, BigDecimal number)
{
super(frame, title, true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// Get WindowNo for Currency
m_WindowNo = Env.getWindowNo(frame);
//
m_DisplayType = displayType;
if (!DisplayType.isNumeric(m_DisplayType))
m_DisplayType = DisplayType.Number;
//
m_format = format;
if (m_format == null)
m_format = DisplayType.getNumberFormat(m_DisplayType);
//
m_number = number;
if (m_number == null)
m_number = new BigDecimal(0.0);
//
try
{
jbInit();
finishSetup();
}
catch(Exception ex)
{
log.log(Level.SEVERE, "Calculator" + ex);
}
} // Calculator
/**
* Abbreviated Constructor
* @param frame parent
*/
public Calculator(Frame frame)
{
this (frame, Msg.getMsg(Env.getCtx(), "Calculator"), DisplayType.Number, null, null);
} // Calculator
/**
* Abbreviated Constructor
* @param frame parent
* @param number initial amount
*/
public Calculator(Frame frame, BigDecimal number)
{
this (frame, Msg.getMsg(Env.getCtx(), "Calculator"), DisplayType.Number, null, number);
} // Calculator
private BigDecimal m_number; // the current number
private String m_display = ""; // what is displayed
private int m_DisplayType;
private DecimalFormat m_format;
private int m_WindowNo;
private boolean m_abort = true;
private boolean m_currencyOK = false;
private final static String OPERANDS = "/*-+%";
private char m_decimal = '.';
/** Logger */
private static CLogger log = CLogger.getCLogger(Calculator.class);
//
private CPanel mainPanel = new CPanel();
private CPanel displayPanel = new CPanel();
private BorderLayout mainLayout = new BorderLayout();
private CPanel keyPanel = new CPanel();
private JLabel display = new JLabel();
private BorderLayout displayLayout = new BorderLayout();
private JButton b7 = new JButton();
private JButton b8 = new JButton();
private JButton b9 = new JButton();
private JButton b4 = new JButton();
private JButton b5 = new JButton();
private JButton b6 = new JButton();
private JButton b1 = new JButton();
private JButton b2 = new JButton();
private JButton b3 = new JButton();
private GridLayout keyLayout = new GridLayout();
private JButton bCur = new JButton();
private JButton bC = new JButton();
private JButton bDiv = new JButton();
private JButton bM = new JButton();
private JButton bMin = new JButton();
private JButton bProc = new JButton();
private JButton bAC = new JButton();
private JButton bResult = new JButton();
private JButton bDec = new JButton();
private JButton b0 = new JButton();
private JButton bPlus = new JButton();
private CPanel bordPanel = new CPanel();
private CPanel currencyPanel = new CPanel();
private BorderLayout bordLayout = new BorderLayout();
private JComboBox curFrom = new JComboBox();
private JComboBox curTo = new JComboBox();
private JLabel curLabel = new JLabel();
private FlowLayout currencyLayout = new FlowLayout();
/**
* Static init
* @throws Exception
*/
void jbInit() throws Exception
{
mainPanel.setLayout(mainLayout);
displayPanel.setLayout(displayLayout);
keyPanel.setLayout(keyLayout);
mainLayout.setHgap(2);
mainLayout.setVgap(2);
mainPanel.setBorder(BorderFactory.createLoweredBevelBorder());
mainPanel.addKeyListener(this);
display.setBackground(Color.white);
display.setFont(new java.awt.Font("SansSerif", 0, 14));
display.setBorder(BorderFactory.createLoweredBevelBorder());
display.setText("0");
display.setHorizontalAlignment(SwingConstants.RIGHT);
b7.setText("7");
b8.setText("8");
b9.setText("9");
b4.setText("4");
b5.setText("5");
b6.setText("6");
b1.setText("1");
b2.setText("2");
b3.setText("3");
keyLayout.setColumns(5);
keyLayout.setHgap(3);
keyLayout.setRows(4);
keyLayout.setVgap(3);
bCur.setForeground(Color.yellow);
bCur.setToolTipText(Msg.getMsg(Env.getCtx(), "CurrencyConversion"));
bCur.setText("$");
bC.setForeground(Color.red);
bC.setText("C");
bDiv.setForeground(Color.blue);
bDiv.setText("/");
bM.setForeground(Color.blue);
bM.setText("*");
bMin.setForeground(Color.blue);
bMin.setText("-");
bProc.setForeground(Color.blue);
bProc.setText("%");
bAC.setForeground(Color.red);
bAC.setText("AC");
bResult.setForeground(Color.green);
bResult.setText("=");
bDec.setText(".");
b0.setText("0");
bPlus.setForeground(Color.blue);
bPlus.setText("+");
bordPanel.setLayout(bordLayout);
curLabel.setHorizontalAlignment(SwingConstants.CENTER);
curLabel.setHorizontalTextPosition(SwingConstants.CENTER);
curLabel.setText(" >> ");
currencyPanel.setLayout(currencyLayout);
bordLayout.setHgap(2);
bordLayout.setVgap(2);
displayLayout.setHgap(2);
displayLayout.setVgap(2);
currencyLayout.setHgap(3);
currencyLayout.setVgap(2);
displayPanel.setBackground(Color.white);
getContentPane().add(mainPanel);
mainPanel.add(displayPanel, BorderLayout.NORTH);
displayPanel.add(display, BorderLayout.CENTER);
mainPanel.add(bordPanel, BorderLayout.CENTER);
bordPanel.add(currencyPanel, BorderLayout.NORTH);
currencyPanel.add(curFrom, null);
currencyPanel.add(curLabel, null);
currencyPanel.add(curTo, null);
bordPanel.add(keyPanel, BorderLayout.CENTER);
keyPanel.add(bAC, null);
keyPanel.add(b7, null);
keyPanel.add(b8, null);
keyPanel.add(b9, null);
keyPanel.add(bM, null);
keyPanel.add(bC, null);
keyPanel.add(b4, null);
keyPanel.add(b5, null);
keyPanel.add(b6, null);
keyPanel.add(bDiv, null);
keyPanel.add(bProc, null);
keyPanel.add(b1, null);
keyPanel.add(b2, null);
keyPanel.add(b3, null);
keyPanel.add(bMin, null);
keyPanel.add(bCur, null);
keyPanel.add(b0, null);
keyPanel.add(bDec, null);
keyPanel.add(bResult, null);
keyPanel.add(bPlus, null);
} // jbInit
/**
* Finish Setup
*/
private void finishSetup()
{
Insets in = new Insets(2, 2, 2, 2);
// For all buttons
Component[] comp = keyPanel.getComponents();
for (int i = 0; i < comp.length; i++)
{
if (comp[i] instanceof JButton)
{
JButton b = (JButton)comp[i];
b.setMargin(in);
b.addActionListener(this);
b.addKeyListener(this);
}
}
// Currency
toggleCurrency();
// Format setting
m_decimal = m_format.getDecimalFormatSymbols().getDecimalSeparator();
// display start number
m_display = m_format.format(m_number);
display.setText(m_display);
} // finishSetup
/**
* Action Listener
* @param e event
*/
public void actionPerformed(ActionEvent e)
{
// Handle Button input
if (e.getSource() instanceof JButton)
{
String cmd = e.getActionCommand();
if (cmd != null && cmd.length() > 0)
handleInput(cmd.charAt(0));
}
// Convert Amount
else if (e.getSource() == curTo)
{
KeyNamePair p = (KeyNamePair)curFrom.getSelectedItem();
int curFromID = p.getKey();
p = (KeyNamePair)curTo.getSelectedItem();
int curToID = p.getKey();
// convert
int AD_Client_ID = Env.getAD_Client_ID(Env.getCtx());
int AD_Org_ID = Env.getAD_Org_ID(Env.getCtx());
m_number = MConversionRate.convert(Env.getCtx(),
evaluate(), curFromID, curToID, AD_Client_ID, AD_Org_ID);
m_display = m_format.format(m_number);
display.setText(m_display);
curFrom.setSelectedItem(p);
}
} // actionPerformed
/**
* handle input
* @param c input character
*/
public void handleInput(char c)
{
// System.out.println("Input: " + c);
switch (c)
{
// Number ===============================
case '0': case '1': case '2':
case '3': case '4': case '5':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -