📄 clientbillingui.java
字号:
// Use layout managers to define the client billing interface
import java.text.SimpleDateFormat;
import java.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.printing.Printer;
public class ClientBillingUI {
private static final String VERSION = "V0.7";
private static final int VIEW_MODE = 0;
private static final int ADD_MODE = 1;
private static final int EDIT_MODE = 2;
//Sorting criterion for transactions table
private static final int SORT_BY_ID = 0;
private static final int SORT_BY_AMT = 1;
private static final int SORT_BY_DATE = 2;
private static final int SORT_BY_DESC = 3;
//Size constants
private static final int SHELL_HEIGHT = 480;
private static final int SHELL_WIDTH = 640;
private Display display;
private Shell shell;
private List clientList;
private Text filterText;
private TabFolder folder;
private TabItem clientsItem, transItem;
private Composite clientsPane, transPane;
private Group clientListComp;
private Group clientInfo, transInfo;
//Client page widgets
private Text acctIDText;
private Text addressText;
private Text fnameText;
private Text snameText;
private Text dobText;
private Text phoneText;
private Text miscInfoText;
private Text emailText;
//Client modification buttons
private Button newClientButton;
private Button editClientButton;
private Button delClientButton;
private Button saveClientButton;
private Button cancelClientButton;
//Transaction page widgets
private Table infoTable;
private Text idText;
private Text amtText;
private Text dateText;
private Text descText;
private Text totalText;
//Transaction modification buttons
private Button newTransButton;
private Button editTransButton;
private Button delTransButton;
private Button saveTransButton;
private Button cancelTransButton;
//Processor, acts as middle-man between database class and UI
private ClientBillingProc processor;
//Index of the transaction being edited. -1 if none.
private int editIndex = -1;
//Current mode
private int transactionMode = VIEW_MODE;
private int clientMode = VIEW_MODE;
private TableColumn numberColumn;
private TableColumn amountColumn;
private TableColumn dateColumn;
private TableColumn descColumn;
ClientBillingUI() {
try {
processor = new ClientBillingProc();
}
catch (Exception e) {
System.err.println("Couldn't open a connection to the database.");
}
processor.setClientView(ClientBillingProc.VIEW_BY_NAME);
display = new Display();
shell = new Shell(display);
shell.setSize(SHELL_WIDTH, SHELL_HEIGHT);
shell.setText("Client Billing Application");
Image image1 = new Image(null, "splash.jpg");
shell.setImage(image1);
shell.setLayout(new FormLayout());
setupFolders();
setupFileMenu();
}
public void run() {
refreshData();
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
//Terminate the database connection
processor.close();
display.dispose();
}
private void setupFolders() {
folder = new TabFolder(shell, SWT.NONE);
FormLayout folderLayout = new FormLayout();
folderLayout.marginHeight = 0;
folderLayout.marginWidth = 0;
folder.setLayout(folderLayout);
FormData data = new FormData();
data.top = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.bottom = new FormAttachment(100, 0);
folder.setLayoutData(data);
setupClientList();
setupClientsPane();
setupTransPane();
}
private void refreshData() {
//Load data from processor and fill the list of clients.
int selected = processor.getActiveClientIndex();
String[] data = processor.getClientList();
clientList.removeAll();
for (int i = 0; i < data.length; i++)
clientList.add(data[i]);
clientList.setSelection(selected);
//Set the label for the active client.
Client c = processor.getActiveClient();
clientInfo.setText("Client: " + c);
transInfo.setText("Transactions for Client: " + c);
//Set the values of the textfields for the client page.
acctIDText.setText("" + c.clientID);
fnameText.setText(c.firstName);
snameText.setText(c.lastName);
phoneText.setText(c.phone);
emailText.setText(c.email);
dobText.setText(c.dob);
addressText.setText(c.address);
miscInfoText.setText(c.miscInfo);
//Refill transaction table with new client.
infoTable.removeAll();
int max = processor.getNumTransactions();
for (int i = 0; i < max; i++) {
Transaction current = processor.getTransactionByIndex(i);
TableItem item = new TableItem(infoTable, SWT.NONE);
//Remove line breaks from description, and replaces with spaces.
StringBuffer descCopy = new StringBuffer(current.description);
int position;
while ((position = descCopy.indexOf("\n")) != -1)
descCopy.replace(position, position + 1, " ");
while ((position = descCopy.indexOf("\r")) != -1)
descCopy.deleteCharAt(position);
item.setText(
new String[] {
"" + current.transactionID,
"" + current.dollarAmount(),
current.date,
descCopy.toString()});
}
//Reset the text fields on the transaction page to blank.
if (transactionMode == VIEW_MODE) {
idText.setText("");
amtText.setText("");
dateText.setText("");
descText.setText("");
}
//Set the total text field.
int total = processor.getTransTotal();
totalText.setText(toDollarAmt(total));
}
private void setClientMode(int mode) {
//Set the mode for the transaction pane. (Viewing, editing, or adding).
if (clientMode == mode)
return;
//Make the modes exclusive, ie. can't edit transactions and clients simultaneously
setTransMode(VIEW_MODE);
if (mode == ADD_MODE) {
//Empty the text fields
clientInfo.setText("Client: New Client");
clientList.deselectAll();
acctIDText.setText("");
acctIDText.setEditable(true);
addressText.setText("");
addressText.setEditable(true);
fnameText.setText("");
fnameText.setEditable(true);
snameText.setText("");
snameText.setEditable(true);
dobText.setText("YYYY-MM-DD");
dobText.setEditable(true);
phoneText.setText("");
phoneText.setEditable(true);
miscInfoText.setText("");
miscInfoText.setEditable(true);
emailText.setText("");
emailText.setEditable(true);
newClientButton.setEnabled(false);
delClientButton.setEnabled(false);
editClientButton.setEnabled(false);
saveClientButton.setEnabled(true);
cancelClientButton.setEnabled(true);
//Move the cursor to the ID field for convenience
acctIDText.setFocus();
}
else if (mode == EDIT_MODE) {
//refuse edit mode, if no entry is selected
acctIDText.setEditable(true);
addressText.setEditable(true);
fnameText.setEditable(true);
snameText.setEditable(true);
dobText.setEditable(true);
phoneText.setEditable(true);
miscInfoText.setEditable(true);
emailText.setEditable(true);
newClientButton.setEnabled(false);
delClientButton.setEnabled(false);
editClientButton.setEnabled(false);
saveClientButton.setEnabled(true);
cancelClientButton.setEnabled(true);
//move the cursor to the ID field
acctIDText.setFocus();
}
else if (mode == VIEW_MODE) {
acctIDText.setEditable(false);
addressText.setEditable(false);
fnameText.setEditable(false);
snameText.setEditable(false);
dobText.setEditable(false);
phoneText.setEditable(false);
miscInfoText.setEditable(false);
emailText.setEditable(false);
newClientButton.setEnabled(true);
delClientButton.setEnabled(true);
editClientButton.setEnabled(true);
saveClientButton.setEnabled(false);
cancelClientButton.setEnabled(false);
}
clientMode = mode;
}
private void setTransMode(int mode) {
//Set the mode for the transaction pane. (Viewing, editing, or adding).
if (transactionMode == mode)
return;
setClientMode(VIEW_MODE);
if (mode == ADD_MODE) {
//Empty the text fields
idText.setText("");
idText.setEditable(true);
amtText.setText("");
amtText.setEditable(true);
descText.setText("");
descText.setEditable(true);
//Set the date field to the current date
java.util.Date currentDate = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = sdf.format(currentDate);
dateText.setText(dateString);
dateText.setEditable(true);
newTransButton.setEnabled(false);
delTransButton.setEnabled(false);
editTransButton.setEnabled(false);
saveTransButton.setEnabled(true);
cancelTransButton.setEnabled(true);
//Move the cursor to the ID field for convenience
idText.setFocus();
}
else if (mode == EDIT_MODE) {
//refuse edit mode, if no entry is selected
if (infoTable.getSelectionIndex() < 0) {
MessageBox msgBox =
new MessageBox(shell, SWT.APPLICATION_MODAL | SWT.OK);
msgBox.setText("Input Error");
msgBox.setMessage(
"In order to edit a transaction, one must be selected.");
msgBox.open();
return;
}
idText.setEditable(true);
amtText.setEditable(true);
descText.setEditable(true);
dateText.setEditable(true);
newTransButton.setEnabled(false);
delTransButton.setEnabled(false);
editTransButton.setEnabled(false);
saveTransButton.setEnabled(true);
cancelTransButton.setEnabled(true);
//move the cursor to the ID field
idText.setFocus();
}
else if (mode == VIEW_MODE) {
int index;
if ((index = infoTable.getSelectionIndex()) != -1) {
int id = Integer.parseInt(infoTable.getItem(index).getText(0));
Transaction current = processor.getTransactionByID(id);
idText.setText("" + current.transactionID);
dateText.setText(current.date);
descText.setText(current.description);
amtText.setText(current.dollarAmount());
}
else {
idText.setText("");
amtText.setText("");
dateText.setText("");
descText.setText("");
}
idText.setEditable(false);
amtText.setEditable(false);
descText.setEditable(false);
dateText.setEditable(false);
//update the transaction total
int total = processor.getTransTotal();
totalText.setText(toDollarAmt(total));
newTransButton.setEnabled(true);
delTransButton.setEnabled(true);
editTransButton.setEnabled(true);
saveTransButton.setEnabled(false);
cancelTransButton.setEnabled(false);
}
transactionMode = mode;
}
private void setupClientList() {
//Create client list (consistent for both tabs).
clientListComp = new Group(folder, SWT.NONE);
clientListComp.setText("Clients");
//Form layout data for composite
FormData formData = new FormData();
formData.top = new FormAttachment(5, 6);
formData.left = new FormAttachment(0, 10);
formData.bottom = new FormAttachment(100, -10);
formData.right = new FormAttachment(20, -10);
clientListComp.setLayoutData(formData);
//Composite has a grid layout
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
clientListComp.setLayout(gridLayout);
GridData data = new GridData();
data.horizontalSpan = 2;
clientList =
new List(
clientListComp,
SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
data = new GridData(GridData.FILL_BOTH);
data.horizontalSpan = 2;
clientList.setLayoutData(data);
clientList.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
setTransMode(VIEW_MODE);
setClientMode(VIEW_MODE);
processor.setActiveClient(clientList.getSelectionIndex());
refreshData();
}
});
//Create the filter text field and its listeners.
Label filterLabel = new Label(clientListComp, SWT.NONE);
filterLabel.setText("Filter:");
filterText = new Text(clientListComp, SWT.BORDER);
GridData data1 = new GridData();
data1.widthHint = 26;
filterLabel.setLayoutData(data1);
GridData data2 = new GridData();
data2.widthHint = 48;
filterText.setLayoutData(data2);
filterText.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent arg0) {
processor.setFilter(filterText.getText());
refreshData();
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -