📄 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.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class BrokerViewImpl extends UnicastRemoteObject
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) throws RemoteException{
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);
}
//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");
}
if (display instanceof Portfolio) {
//TBD
}
if (display instanceof Stock[]) {
//TBD
}
//** 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);
}
// refresh portfolio panel if required
// c = gui.getCustOnPortPan();
// if (cust.equals(c)) {
// Portfolio p = model.getPortfolio(cust);
// gui.refreshPortfolioPan(p);
// }
}
//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;
// 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -