📄 clientbillingui.java
字号:
dateColumn = new TableColumn(infoTable, SWT.RIGHT);
dateColumn.setText("Date");
dateColumn.setWidth(80);
dateColumn.addSelectionListener(sortListener);
descColumn = new TableColumn(infoTable, SWT.LEFT);
descColumn.setText("Description");
descColumn.setWidth(171);
descColumn.addSelectionListener(sortListener);
Label totalLabel = new Label(transInfo, SWT.NONE);
totalLabel.setText("Total:");
totalText = new Text(transInfo, SWT.BORDER | SWT.RIGHT);
totalText.setEditable(false);
data = new GridData();
data.grabExcessHorizontalSpace = true;
data.horizontalSpan = 3;
totalText.setLayoutData(data);
Label idLabel = new Label(transInfo, SWT.NONE);
idLabel.setText("ID:");
idText = new Text(transInfo, SWT.SINGLE | SWT.BORDER);
idText.setEditable(false);
Label descLabel = new Label(transInfo, SWT.NONE);
descLabel.setText("Description:");
data = new GridData();
data.verticalSpan = 3;
descLabel.setLayoutData(data);
descText = new Text(transInfo, SWT.MULTI | SWT.BORDER | SWT.WRAP);
descText.setEditable(false);
data = new GridData(GridData.FILL_BOTH);
data.verticalSpan = 3;
descText.setLayoutData(data);
Label amtLabel = new Label(transInfo, SWT.NONE);
amtLabel.setText("Amount:");
amtText = new Text(transInfo, SWT.SINGLE | SWT.BORDER);
amtText.setEditable(false);
Label dateLabel = new Label(transInfo, SWT.NONE);
dateLabel.setText("Date");
dateText = new Text(transInfo, SWT.SINGLE | SWT.BORDER);
dateText.setEditable(false);
//buttonGrp has a FillLayout
Composite buttonGrp = new Composite(transInfo, SWT.NONE);
buttonGrp.setLayout(new FillLayout());
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 4;
buttonGrp.setLayoutData(data);
newTransButton = new Button(buttonGrp, SWT.PUSH);
newTransButton.setText("&New");
newTransButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
setTransMode(ADD_MODE);
}
});
delTransButton = new Button(buttonGrp, SWT.PUSH);
delTransButton.setText("&Delete");
delTransButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
MessageBox msgBox =
new MessageBox(
shell,
SWT.APPLICATION_MODAL | SWT.YES | SWT.NO);
msgBox.setText("Confirm Deletion");
msgBox.setMessage(
"Are you certain you wish to delete the selected transaction?");
int index = infoTable.getSelectionIndex();
if (index >= 0 && msgBox.open() == SWT.YES) {
processor.deleteTransaction(index);
refreshData();
}
else if (index < 0) {
MessageBox msgBox2 =
new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.OK);
msgBox2.setText("Input Error");
msgBox2.setMessage(
"In order to delete a transaction, one must be selected.");
msgBox2.open();
}
}
});
editTransButton = new Button(buttonGrp, SWT.PUSH);
editTransButton.setText("&Edit");
editTransButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
editIndex = infoTable.getSelectionIndex();
setTransMode(EDIT_MODE);
}
});
//filler
Label fill = new Label(buttonGrp, SWT.NONE);
saveTransButton = new Button(buttonGrp, SWT.PUSH);
saveTransButton.setText("&Save");
saveTransButton.setEnabled(false);
saveTransButton.addSelectionListener(new SaveTransButtonListener());
cancelTransButton = new Button(buttonGrp, SWT.PUSH);
cancelTransButton.setText("&Cancel");
cancelTransButton.setEnabled(false);
cancelTransButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
setTransMode(VIEW_MODE);
}
});
}
private void setupFileMenu() {
//create the menu bar
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
//add the File option to it
MenuItem file = new MenuItem(menu, SWT.CASCADE);
file.setText("File");
//create a menu for the File option
Menu filemenu = new Menu(shell, SWT.DROP_DOWN);
file.setMenu(filemenu);
//add MenuItems to the File menu
MenuItem aboutItem = new MenuItem(filemenu, SWT.PUSH);
aboutItem.setText("About...");
MenuItem quitItem = new MenuItem(filemenu, SWT.PUSH);
quitItem.setText("Quit");
//add a listener for the action
quitItem.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
shell.dispose();
}
});
aboutItem.addListener(SWT.Selection, new Listener() {
private Image aboutImage;
private Shell aboutShell;
public void handleEvent(Event e) {
aboutShell = new Shell(shell, SWT.DIALOG_TRIM);
aboutShell.setSize(200, 250);
aboutShell.setText("About...");
//load an Image
aboutImage = new Image(display, "splash.jpg");
aboutShell.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
//Get the graphics context
GC gc = new GC(aboutShell);
//Write a string to the shell
gc.drawString(
"Client Billing Application " + VERSION,
30,
10);
//draw a blue rectangle
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
gc.setForeground(blue);
gc.drawRectangle(30, 30, 135, 165);
//draw an image
gc.drawImage(aboutImage, 48, 48);
}
});
aboutShell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
//when the shell is closed, dispose of the Image
aboutImage.dispose();
}
});
aboutShell.open();
}
});
}
private String toDollarAmt(int amt) {
boolean neg = false;
if (amt < 0) {
neg = true;
amt = -amt;
}
int dollars = amt / 100;
int cents = amt % 100;
return (neg ? "-" : "")
+ dollars
+ "."
+ (cents < 10 ? "0" : "")
+ cents;
}
private int toCentsAmt(String amt) throws NumberFormatException {
//convert dollars and cents String, into integer cents
int dollars = 0;
int cents = 0;
int val = 0;
StringTokenizer st = new StringTokenizer(amt, ".");
if (st.countTokens() == 1) {
if (amt.charAt(0) == '.')
cents = Integer.parseInt(st.nextToken());
else
dollars = Integer.parseInt(st.nextToken());
}
else if (st.countTokens() == 2) {
dollars = Integer.parseInt(st.nextToken());
cents = Integer.parseInt(st.nextToken());
}
if (cents < 0)
throw new NumberFormatException();
while (cents > 100)
cents /= 10;
val = dollars * 100;
if (dollars > 0)
val += cents;
else
val -= cents;
return val;
}
class SaveTransButtonListener extends SelectionAdapter {
//respond to presses of the Save button on the transaction page
public void widgetSelected(SelectionEvent arg0) {
int transID;
try {
transID = Integer.parseInt(idText.getText());
}
catch (NumberFormatException e) {
MessageBox msgBox =
new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.OK);
msgBox.setText("Input Error");
msgBox.setMessage(
"The transaction ID entered must be a valid integer unique among transactions for this client.");
msgBox.open();
return;
}
int amt = 0;
try {
amt = toCentsAmt(amtText.getText());
}
catch (NumberFormatException e) {
MessageBox msgBox =
new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.OK);
msgBox.setText("Input Error");
msgBox.setMessage("The transaction amount was not valid.");
msgBox.open();
return;
}
boolean success = false;
if (transactionMode == ADD_MODE) {
success =
processor.addTransaction(
transID,
amt,
dateText.getText(),
descText.getText());
}
else if (transactionMode == EDIT_MODE) {
success =
processor.updateTransaction(
editIndex,
transID,
amt,
dateText.getText(),
descText.getText());
}
if (!success) {
MessageBox msgBox =
new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.OK);
msgBox.setText("Input Error");
msgBox.setMessage(
"The Transaction ID entered must be a valid integer unique among transactions for this client.");
msgBox.open();
return;
}
refreshData();
setTransMode(VIEW_MODE);
}
}
class SaveClientButtonListener extends SelectionAdapter {
//respond to presses of the Save button on the transaction page
public void widgetSelected(SelectionEvent arg0) {
int clientID;
try {
clientID = Integer.parseInt(acctIDText.getText());
}
catch (NumberFormatException e) {
MessageBox msgBox =
new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.OK);
msgBox.setText("Input Error");
msgBox.setMessage(
"The client ID entered must be a valid integer unique among all clients.");
msgBox.open();
return;
}
boolean success = false;
if (clientMode == ADD_MODE) {
success =
processor.addClient(
clientID,
fnameText.getText(),
snameText.getText(),
phoneText.getText(),
emailText.getText(),
dobText.getText(),
addressText.getText(),
miscInfoText.getText());
}
else if (clientMode == EDIT_MODE) {
success =
processor.updateClient(
clientID,
fnameText.getText(),
snameText.getText(),
phoneText.getText(),
emailText.getText(),
dobText.getText(),
addressText.getText(),
miscInfoText.getText());
}
if (!success) {
MessageBox msgBox =
new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.OK);
msgBox.setText("Input Error");
msgBox.setMessage(
"The Client ID entered must be a valid integer unique among all clients.");
msgBox.open();
return;
}
refreshData();
setClientMode(VIEW_MODE);
}
}
private void sortTransactions(int criteria) {
TableItem[] items = infoTable.getItems();
Arrays.sort(items, new TransactionComparator(criteria));
for (int i = 0; i < items.length; i++) {
TableItem t = new TableItem(infoTable, SWT.NONE);
String[] data = new String[infoTable.getColumnCount()];
for (int j = 0; j < data.length; j++)
data[j] = items[i].getText(j);
items[i].dispose();
t.setText(data);
}
}
class TransactionComparator implements java.util.Comparator {
private int criteria;
TransactionComparator(int c) {
criteria = c;
}
public int compare(Object arg0, Object arg1) {
boolean numeric = criteria == SORT_BY_AMT || criteria == SORT_BY_ID;
TableItem first = (TableItem) arg0;
TableItem second = (TableItem) arg1;
if (criteria == SORT_BY_AMT) {
Float f = new Float(first.getText(criteria));
Float s = new Float(second.getText(criteria));
return f.compareTo(s) < 0 ? 1 : -1;
}
else if (criteria == SORT_BY_ID) {
int f = Integer.parseInt(first.getText(criteria));
int s = Integer.parseInt(second.getText(criteria));
return f - s;
}
else if (criteria == SORT_BY_DATE) {
return first.getText(criteria).compareTo(
second.getText(criteria))
< 0
? 1
: -1;
}
else if (criteria == SORT_BY_DESC) {
return first.getText(criteria).compareTo(
second.getText(criteria))
< 0
? -1
: 1;
}
else
throw new RuntimeException("Unexpected Internal Error.");
}
}
class ColumnSelectionListener extends SelectionAdapter {
public void widgetSelected(SelectionEvent arg0) {
int criteria = -1;
if (arg0.getSource() == numberColumn) {
criteria = SORT_BY_ID;
}
else if (arg0.getSource() == dateColumn) {
criteria = SORT_BY_DATE;
}
else if (arg0.getSource() == descColumn) {
criteria = SORT_BY_DESC;
}
else if (arg0.getSource() == amountColumn) {
criteria = SORT_BY_AMT;
}
else {
throw new RuntimeException("Unexpected Internal Error.");
}
sortTransactions(criteria);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -