TavernerPOS - Source Code
Clone:
git clone http://www.librarysmith.co.uk/tavernerPOS
package com.floreantpos.ui.dialog;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import com.floreantpos.IconFactory;
import com.floreantpos.Messages;
import com.floreantpos.POSConstants;
import com.floreantpos.swing.PosButton;
public class NumberSelectionView extends JPanel implements ActionListener {
private JTextField tfNumber;
private boolean floatingPoint;
private String defaultValue;
public NumberSelectionView() {
MigLayout layout = new MigLayout("fillx", "[60px,fill][60px,fill][60px,fill]", "[][][][][]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
setLayout(layout);
tfNumber = new JTextField();
tfNumber.setFont(tfNumber.getFont().deriveFont(Font.BOLD, 24));
tfNumber.setFocusable(true);
tfNumber.requestFocus();
tfNumber.setBackground(Color.WHITE);
add(tfNumber, "span 2, grow"); //$NON-NLS-1$
PosButton posButton = new PosButton(POSConstants.CLEAR_ALL);
posButton.setFocusable(false);
posButton.setMinimumSize(new Dimension(25, 23));
posButton.addActionListener(this);
add(posButton, "growy,height 55,wrap"); //$NON-NLS-1$
String[][] numbers = { { "7", "8", "9" }, { "4", "5", "6" }, { "1", "2", "3" }, { ".", "0", Messages.getString("NumberSelectionView.16") } }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$
String[][] iconNames = new String[][] { { "7.png", "8.png", "9.png" }, { "4.png", "5.png", "6.png" }, { "1.png", "2.png", "3.png" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
{ "dot.png", "0.png", "clear.png" } }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
posButton = new PosButton();
posButton.setFocusable(false);
ImageIcon icon = IconFactory.getIcon("/ui_icons/", iconNames[i][j]); //$NON-NLS-1$
String buttonText = String.valueOf(numbers[i][j]);
if (icon == null) {
posButton.setText(buttonText);
}
else {
posButton.setIcon(icon);
if (POSConstants.CLEAR.equals(buttonText)) {
posButton.setText(buttonText);
}
}
posButton.setActionCommand(buttonText);
posButton.addActionListener(this);
String constraints = "grow, height 55"; //$NON-NLS-1$
if (j == numbers[i].length - 1) {
constraints += ", wrap"; //$NON-NLS-1$
}
add(posButton, constraints);
}
}
}
// public Number getSelectedNumber() {
//
// }
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals(POSConstants.CLEAR_ALL)) {
doClearAll();
}
else if (actionCommand.equals(POSConstants.CLEAR)) {
doClear();
}
else if (actionCommand.equals(".")) { //$NON-NLS-1$
doInsertDot();
}
else {
doInsertNumber(actionCommand);
}
}
private void doClearAll() {
tfNumber.setText(String.valueOf(defaultValue));
}
private void doClear() {
String s = tfNumber.getText();
if (s.length() > 1) {
s = s.substring(0, s.length() - 1);
}
else {
s = String.valueOf(defaultValue);
}
tfNumber.setText(s);
}
private void doInsertNumber(String number) {
String s = tfNumber.getText();
double d = 0;
try {
d = Double.parseDouble(s);
} catch (Exception x) {
}
if (d == 0) {
tfNumber.setText(number);
return;
}
s = s + number;
if (!validate(s)) {
POSMessageDialog.showError(this, POSConstants.INVALID_NUMBER);
return;
}
tfNumber.setText(s);
}
private void doInsertDot() {
String string = tfNumber.getText() + "."; //$NON-NLS-1$
if (!validate(string)) {
POSMessageDialog.showError(this, POSConstants.INVALID_NUMBER);
return;
}
tfNumber.setText(string);
}
private boolean validate(String str) {
if (isFloatingPoint()) {
try {
Double.parseDouble(str);
} catch (Exception x) {
return false;
}
}
else {
try {
Integer.parseInt(str);
} catch (Exception x) {
return false;
}
}
return true;
}
public boolean isFloatingPoint() {
return floatingPoint;
}
public void setFloatingPoint(boolean decimalAllowed) {
this.floatingPoint = decimalAllowed;
}
}
- Details
- Last Updated: Monday, 04 April 2016 02:04
- Hits: 35820440