📄 originalclientbillingui.txt
字号:
//Use absolute positioning of controls to define the client billing interface
import java.text.SimpleDateFormat;
import java.util.StringTokenizer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
//TODO Clean up the ClientBillingUI class.
public class ClientBillingUI {
private static final String VERSION = "V0.1";
//Size constants
private static final int SHELL_HEIGHT = 480;
private static final int SHELL_WIDTH = 640;
private static final int VIEW_MODE = 0;
private static final int EDIT_MODE = 1;
private static final int ADD_MODE = 2;
private Display display;
private Shell shell;
private Label activeClientLabel;
private List clientList;
private Text filterText;
private TabFolder folder;
private TabItem clientsItem, transItem;
private Composite clientsPane, transPane;
//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;
//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 newButton;
private Button editButton;
private Button delButton;
private Button saveButton;
private Button cancelButton;
//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 (for transaction page)
private int activeMode = VIEW_MODE;
ClientBillingUI() {
try {
processor = new ClientBillingProc();
}
catch (Exception e) {
System.err.println("Couldn't open a connection to the database.");
//e.printStackTrace();
}
processor.setClientView(ClientBillingProc.VIEW_BY_NAME);
display = new Display();
shell = new Shell(display);
shell.setSize(SHELL_WIDTH, SHELL_HEIGHT);
shell.setText("Client Billing Application " +VERSION);
Image image1 = new Image(null, "splash.jpg");
shell.setImage(image1);
setupFolders();
setupFileMenu();
}
public void run() {
refreshData();
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
processor.close(); //terminate the database connection
display.dispose();
}
private void setupFolders() {
setupClientList();
activeClientLabel = new Label(shell, SWT.NONE);
activeClientLabel.setLocation(155, 30);
folder = new TabFolder(shell, SWT.NONE);
Rectangle r = shell.getClientArea();
folder.setLocation(0, 0);
folder.setSize(r.width, r.height);
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();
activeClientLabel.setText("Active Client: " + c);
activeClientLabel.pack();
//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.getTransaction(i);
TableItem item = new TableItem(infoTable, SWT.NONE);
item.setText(
new String[] {
"" + current.transactionID,
"" + current.dollarAmount(),
current.date,
current.description });
}
//Reset the text fields on the transaction page to blank.
if (activeMode == 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 setMode(int mode) {
//Set the mode for the transaction pane. (Viewing, editing, or adding).
if (activeMode == mode)
return;
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);
newButton.setEnabled(false);
delButton.setEnabled(false);
editButton.setEnabled(false);
saveButton.setEnabled(true);
cancelButton.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)
return;
idText.setEditable(true);
amtText.setEditable(true);
descText.setEditable(true);
dateText.setEditable(true);
newButton.setEnabled(false);
delButton.setEnabled(false);
editButton.setEnabled(false);
saveButton.setEnabled(true);
cancelButton.setEnabled(true);
//move the cursor to the ID field
idText.setFocus();
}
else if (mode == VIEW_MODE) {
int id = infoTable.getSelectionIndex();
if (id >= 0) {
Transaction current = processor.getTransaction(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));
newButton.setEnabled(true);
delButton.setEnabled(true);
editButton.setEnabled(true);
saveButton.setEnabled(false);
cancelButton.setEnabled(false);
}
activeMode = mode;
}
private void setupClientList() {
//Create client list (consistent for both tabs).
Label clientsLabel = new Label(shell, SWT.NONE);
clientsLabel.setText("Current Clients:");
clientsLabel.pack();
clientList =
new List(
shell,
SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
clientList.setSize(120, 260);
clientList.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
setMode(VIEW_MODE);
processor.setActiveClient(clientList.getSelectionIndex());
refreshData();
}
});
//Create the filter text field and its listeners.
Label filterLabel = new Label(shell, SWT.NONE);
filterLabel.setText("Filter:");
filterLabel.pack();
filterText = new Text(shell, SWT.BORDER);
filterText.setSize(120, 20);
filterText.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent arg0) {
processor.setFilter(filterText.getText());
refreshData();
}
});
filterText.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) { //check for "enter" key
processor.setFilter(filterText.getText());
refreshData();
}
});
//Create radio buttons to switch viewing mode.
Label numLabel = new Label(shell, SWT.NONE);
Label nameLabel = new Label(shell, SWT.NONE);
numLabel.setText("View by ID");
nameLabel.setText("View by name");
nameLabel.pack();
numLabel.pack();
Button numRadio = new Button(shell, SWT.RADIO);
Button nameRadio = new Button(shell, SWT.RADIO);
nameRadio.pack();
numRadio.pack();
nameRadio.setSelection(true);
nameRadio.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
processor.setClientView(ClientBillingProc.VIEW_BY_NAME);
refreshData();
}
});
numRadio.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
processor.setClientView(ClientBillingProc.VIEW_BY_ID);
refreshData();
}
});
//Position the various elements.
clientsLabel.setLocation(10, 30);
clientList.setLocation(10, 50);
nameRadio.setLocation(10, 315);
nameLabel.setLocation(30, 315);
numRadio.setLocation(10, 335);
numLabel.setLocation(30, 335);
filterLabel.setLocation(10, 360);
filterText.setLocation(10, 380);
}
private void setupClientsPane() {
clientsItem = new TabItem(folder, SWT.NONE);
clientsItem.setText("&Clients");
clientsPane = new Composite(folder, SWT.NONE);
clientsItem.setControl(clientsPane);
Group clientInfo = new Group(clientsPane, SWT.NONE);
clientInfo.setText("Client Information:");
clientInfo.setSize(450, 350);
Label acctIDLabel = new Label(clientInfo, SWT.NONE);
acctIDLabel.setText("Account ID:");
acctIDLabel.pack();
Label fnameLabel = new Label(clientInfo, SWT.NONE);
fnameLabel.setText("First Name:");
fnameLabel.pack();
Label snameLabel = new Label(clientInfo, SWT.NONE);
snameLabel.setText("Surname:");
snameLabel.pack();
Label dobLabel = new Label(clientInfo, SWT.NONE);
dobLabel.setText("Date of birth:");
dobLabel.pack();
Label phoneLabel = new Label(clientInfo, SWT.NONE);
phoneLabel.setText("Phone number:");
phoneLabel.pack();
Label emailLabel = new Label(clientInfo, SWT.NONE);
emailLabel.setText("Email address:");
emailLabel.pack();
Label addressLabel = new Label(clientInfo, SWT.NONE);
addressLabel.setText("Address:");
addressLabel.pack();
Label miscInfoLabel = new Label(clientInfo, SWT.NONE);
miscInfoLabel.setText("Miscellaneous Information:");
miscInfoLabel.pack();
final int TEXT_FIELD_WIDTH = 180;
final int TEXT_FIELD_HEIGHT = 20;
acctIDText = new Text(clientInfo, SWT.BORDER);
acctIDText.setSize(TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);
acctIDText.setEditable(false);
fnameText = new Text(clientInfo, SWT.BORDER);
fnameText.setSize(TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);
fnameText.setEditable(false);
snameText = new Text(clientInfo, SWT.BORDER);
snameText.setSize(TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);
snameText.setEditable(false);
phoneText = new Text(clientInfo, SWT.BORDER);
phoneText.setSize(TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);
phoneText.setEditable(false);
emailText = new Text(clientInfo, SWT.BORDER);
emailText.setSize(TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);
emailText.setEditable(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -