?? brokerviewimpl.java
字號:
package trader;
import trader.gui.BrokerGui;
import java.util.*;
import java.io.Serializable;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BrokerViewImpl implements BrokerView, Serializable{
private transient BrokerGui gui;
private BrokerModel brokerModel;
private ArrayList brokerControllers = new ArrayList(10);
// Important note - For mod05_view exercises you are supplied
// with the BrokerGui class. You should treat the GUI as a
// blackbox. You should focus on the services it provides and
// not on how it provides the services. For the API (public
// methods) of the BrokerGui class, refer to Module 5 of
// the student guide.
public BrokerViewImpl(BrokerModel model) {
System.out.println("Creating BrokerViewImpl");
try {
//** 1 Assign model to the attribute brokerModel
brokerModel = model;
//** 2 Invoke the addChangeListener method with this
// instance of BrokerViewImpl as the input parameter
model.addChangeListener(this);
} catch (Exception e) {
System.out.println("BrokerViewImpl constructor " + e);
}
// Create and assign a BrokerGui object to gui
gui = new BrokerGui();
// Pass the array selectionPanelListeners containing the
// event listener objects that handle button clicks on
// the selection panel of the gui. The array
// selectionPanelListeners is declared later in this class.
// The buttons on the selection panel of the gui are
// "Customer Details", "Portfolio", "All Customers" and
// "Stocks".
gui.addSelectionPanelListeners(selectionPanelListeners);
// Pass the array custPanelListeners containing the
// event listener objects that handle button clicks on
// the customer details panel of the gui. The array
// custPanelListeners is declared later in this class.
// The buttons on the customer details panel of the gui are
// "Get Customer", "Add Customer", "Update Customer" and
// "Delete Customer".
gui.addCustPanelListeners(custPanelListeners);
//modified by ourteam 051228
//begin
//gui.addPortPanelListeners(portPanelListeners);
gui.addPortPanelListeners(portPanelListeners);
//end
//modified by ourteam 051228
//begin
gui.addStockPanelListeners(stockPanelListeners);
//end
}
//user gesture listener registration methods
/* ---------------------------------------------------------------
* adds requester to the list of objects to be notified of user
* gestures entered through a user interface such as a GUI.
* User getsures for the customer segment are add, delete, update
* get and getAll customers. There are similar user gestures for
* portfolio and stock segments
*/
public void addUserGestureListener(BrokerController b)
throws BrokerException {
System.out.println("BrokerViewImpl.addUserGestureListener " +b);
//** 1 add b to brokerControllers using the add method
brokerControllers.add(b);
}
//display selection request service methods
/* ------------------------------------------------------------
* shows the display page specified by the broker controller
*/
public void showDisplay(Object display) throws BrokerException {
System.out.println("BrokerViewImpl.showDisplay " + display);
// General hints: Use the instanceof operator to determine
// the display parameter variable's class type.
//** 1 If display variable's the class type is Customer then
//** 1.1 Invoke refreshCustPan method on the gui passing
//** display as the invoked methods input parameter.
//** Note a cast of display to Customer type may be
//** required.
//** 1.2 Invoke the showCard method on the gui passing the
//** string "customer" as the invoked methods input
//** parameter.
if (display instanceof Customer) {
gui.refreshCustPan((Customer) display);
gui.showCard("customer");
}
//** 2 If display variable's the class type is Customer[] then
//** 2.1 Invoke refreshAllCustPan method on the gui passing
//** display as the invoked methods input parameter.
//** Note a cast of display to Customer[] type may be
//** required.
//** 2.2 Invoke the showCard method on the gui passing the
//** string "allcustomers" as the invoked methods input
//** parameter.
if (display instanceof Customer[]) {
gui.refreshAllCustPan((Customer[])display);
gui.showCard("allcustomers");
}
//modified by ourteam 051228
//begin
if (display instanceof Portfolio) {
//TBD
gui.refreshPortPan((Portfolio) display);
gui.showCard("portfolio");
}
//end
//modified by ourteam 051228
//begin
if (display instanceof Stock[]) {
//TBD
gui.refreshAllStockPan((Stock[])display);
gui.showCard("allstocks");
}
if (display instanceof Stock) {
//if (true) {
//TBD
gui.refreshStockPan((Stock)display);
gui.showCard("stock");
}
//end
//** 3 If display variable's the class type is Exception then
//** 3.1 Invoke updateLog method on the gui passing
//** display.toString() as the invoked methods input
//** parameter.
if (display instanceof Exception) {
// One way to show exceptions
gui.updateLog(display.toString());
}
}
// iteration 1 Customer segment broker view methods
/* ---------------------------------------------------------------
* callback method to handle customer state change notification
* from the broker model
*/
public void handleCustomerChange(Customer cust)
throws BrokerException{
System.out.println("BrokerViewImpl.processCustomer " + cust);
// get the cust id currently on the customer details display
String cId;
//** 1 Assign cId to customer id on the gui's customer details
//** panel. Use the gui's getCustIdOnCustPan() method.
cId = gui.getCustIdOnCustPan();
// refresh customer details panel if required
//** 2 Use an if statement to test if cust.getId().equals(cId)
//** is true.
//** 2.1 If true:
//** Invoke refreshCustPan method on the gui passing
//** cust as the invoked methods input parameter.
if (cust.getId().equals(cId)) {
gui.refreshCustPan(cust);
}
// refresh all customers Panel
try {
Customer custs[];
//** 1 Assign custs with the Customer[] returned by invoking
//** the getAllCustomers() on the attribute brokerModel.
custs = brokerModel.getAllCustomers();
//** 2 Invoke the refreshAllCustPan on gui with custs as
//** the refreshAllCustPan methods input parameter.
gui.refreshAllCustPan(custs);
} catch (Exception e) {
System.out.println("BrokerViewImpl processCustomer " + e);
}
}
//event handler methods--------------------------------------
// Following is an anonymous inner class declaration
// The attribute custGetHandler is registered as the action
// event listener with the "Get Customer" button of the gui,
// by the constructor of this class.
transient ActionListener custGetHandler =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// This method is called when the "Get Customer" button
// is clicked by the user
System.out.println("BrokerViewImpl: custGetHandler");
BrokerController bc;
String custId;
custId = gui.getCustIdOnCustPan();
//modified by ourteam 051228
//begin
String custName;
custName = gui.getCustNameOnCustPan();
//end
//modified by ourteam 051228
//begin
String custAddr;
custAddr = gui.getCustAddrOnCustPan();
//end
// Assign custId with the value of the customer id on
// the gui. Use the getCustIdOnCustPan() method of gui
//custId = gui.getCustIdOnCustPan();
// Create a for loop:
// For every object in the ArrayList brokerControllers:
// -Use get method to get the object and assign to bc.
// -Invoke the handleGetCustomerGesture method on bc.
for (int i=0; i<brokerControllers.size(); i++) {
bc = (BrokerController) brokerControllers.get(i);
//modified by ourteam 051228
//begin
// bc.handleGetCustomerGesture(custId);
bc.handleGetCustomerGesture(custId,custName,custAddr);
}
}
};
// Following is an anonymous inner class declaration
// The attribute custAddHandler is registered as the action
// event listener with the "Add Customer" button of the gui,
// by the constructor of this class.
transient ActionListener custAddHandler =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// This method is called when the "Add Customer" button
// is clicked by the user
System.out.println("BrokerViewImpl: custAddHandler");
BrokerController bc;
Customer cust;
//** 1 Assign to cust a customer object that represents
//** the customer information on the gui. To get this
//** object use the getCustomerOnCustPan() method of gui
cust = gui.getCustomerOnCustPan();
//** 2 Create a for loop:
//** Hint: See actionPerformed method of the
//** custGetHandler object.
//** Invoke the handleAddCustomerGesture method on
//** every object in the ArrayList brokerControllers.
for (int i=0; i<brokerControllers.size(); i++) {
bc = (BrokerController) brokerControllers.get(i);
bc.handleAddCustomerGesture(cust);
}
}
};
// Following is an anonymous inner class declaration
// The attribute custDeleteHandler is registered as the action
// event listener with the "Delete Customer" button of the
// gui, by the constructor of this class.
transient ActionListener custDeleteHandler =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// This method is called when the "Delete Customer"
// button is clicked by the user
System.out.println("BrokerViewImpl: custDeleteHandler");
BrokerController bc;
Customer cust;
//** 1 Assign to cust a customer object that represents
//** the customer information on the gui. To get this
//** object Use the getCustomerOnCustPan() method of gui.
cust = gui.getCustomerOnCustPan();
//** 2 Create a for loop:
//** Hint: See actionPerformed method of the
//** custGetHandler object.
//** Invoke the handleDeleteCustomerGesture method on
//** every object in the ArrayList brokerControllers.
for (int i=0; i<brokerControllers.size(); i++) {
bc = (BrokerController) brokerControllers.get(i);
bc.handleDeleteCustomerGesture(cust);
}
}
};
// Following is an anonymous inner class declaration
// The attribute custUpdateHandler is registered as the action
// event listener with the "Update Customer" button of the
// gui, by the constructor of this class.
transient ActionListener custUpdateHandler =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("BrokerViewImpl: custUpdateHandler");
BrokerController bc;
Customer cust;
//** 1 Assign to cust a customer object that represents
//** the customer information on the gui. To get this
//** object Use the getCustOnCustPan() method of gui.
cust = gui.getCustomerOnCustPan();
//** 2 Create a for loop:
//** Hint: See actionPerformed method of the
//** custGetHandler object.
//** Invoke the handleUpdateCustomerGesture method on
//** every object in the ArrayList brokerControllers.
for (int i=0; i<brokerControllers.size(); i++) {
bc = (BrokerController) brokerControllers.get(i);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -