Previous | Next |
DpmGUIHandler.java
DpmGUIHandler sets up and maintains the customized portion of the DPM component user interface, including extended menus and tool buttons. DpmGUIHandler creates the extended interfaces and provides handling for actions performed on those items.
DpmGUIHandler also creates and manages the dialogs that allow the user to specify information for portfolio transactions. It implements SecurityDialogProcessor, which defines the methods called to manage the dialog flow.
Create a new DpmGUIHandler | public class DpmGUIHandler extends GUIHandler implements SecurityDialogProcessor { public DpmGUIHandler(ComponentController controller) { super(controller); } protected void handleNew() { handleConfirmNewModel(); } |
Build the menu of portfolio transactions and set it up to handle events | protected void handleCreateMenus(MenuBar menubar) { ResourceBundle resources = getComponentController().getResourceBundle(); buyMenuItem = new MenuItem(resources.getString("Buy")); sellMenuItem = new MenuItem(resources.getString("Sell")); setPriceMenuItem= new MenuItem(resources.getString("SetPrice")); depositMenuItem = new MenuItem(resources.getString("Deposit")); withdrawMenuItem= new MenuItem(resources.getString("Withdraw")); buyMenuItem.addActionListener(this); sellMenuItem.addActionListener(this); setPriceMenuItem.addActionListener(this); depositMenuItem.addActionListener(this); withdrawMenuItem.addActionListener(this); Menu securityMenu = new Menu(resources.getString("PortMenu"), true); securityMenu.add(buyMenuItem); securityMenu.add(sellMenuItem); securityMenu.add(setPriceMenuItem); securityMenu.add(depositMenuItem); securityMenu.add(withdrawMenuItem); menubar.add(securityMenu); } |
Build the extended tool bar for portfolio transactions and set it up to handle events | protected void handleCreateToolButtons(TToolBar toolbar) { ResourceBundle resources = getComponentController().getResourceBundle(); buyToolButton = new TToolButton(getClass(), "buy.gif", resources.getString("Buy"), resources.getString("BuyText")); sellToolButton = new TToolButton(getClass(), "sell.gif", resources.getString("Sell"), resources.getString("SellText")); setPriceToolButton = new TToolButton(getClass(), "price.gif", resources.getString("SetPrice"), resources.getString("SetPriceText")); depositToolButton = new TToolButton(getClass(), "withdraw.gif", resources.getString("Withdraw"), resources.getString("WithdrawText")); withdrawToolButton = new TToolButton(getClass(), "deposit.gif", resources.getString("Deposit"), resources.getString("DepositText")); buyToolButton.addActionListener(this); sellToolButton.addActionListener(this); setPriceToolButton.addActionListener(this); depositToolButton.addActionListener(this); withdrawToolButton.addActionListener(this); } |
Handle actions performed against the portfolio transactions menu or tool bar | public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof Button){ if( e.getActionCommand().equals("Buy")) doTransaction( kBuy); else if( e.getActionCommand().equals("Sell")) doTransaction( kSell); else if( e.getActionCommand().equals("Set Price")) doTransaction( kSetPrice); else if( e.getActionCommand().equals("Deposit")) doTransaction( kDeposit); else if( e.getActionCommand().equals("Withdraw")) doTransaction( kWithdraw); else if( e.getActionCommand().equals("Risk%")) doTransaction( kRiskView); else if( e.getActionCommand().equals("Security%")) doTransaction( kSecurityView); else super.actionPerformed(e); } if (e.getSource() instanceof MenuItem) { MenuItem mi = (MenuItem)e.getSource(); if( mi == buyMenuItem) doTransaction( kBuy); else if( mi == sellMenuItem) doTransaction( kSell); else if( mi == setPriceMenuItem) doTransaction( kSetPrice); else if( mi == depositMenuItem) doTransaction( kDeposit); else if( mi == withdrawMenuItem) doTransaction( kWithdraw); else super.actionPerformed(e); } else if (e.getSource() instanceof TToolButton) { TToolButton tb = (TToolButton)e.getSource(); if( tb == buyToolButton) doTransaction( kBuy); else if( tb == sellToolButton) doTransaction( kSell); else if( tb == setPriceToolButton) doTransaction( kSetPrice); else if( tb == depositToolButton) doTransaction( kDeposit); else if( tb == withdrawToolButton) doTransaction( kWithdraw); else super.actionPerformed(e); } } |
Bring up the correct dialog and let the user carry out the requested transaction | void doTransaction(int transType) { SecurityInfo secInfo; DpmSelection curSel = (DpmSelection)getModelSelection(); if( curSel != null && curSel.isNotEmpty()) { Security sec = curSel.getSelectedPortRecord().getSecurity(); sec.setPrice( curSel.getSelectedPortRecord().getBasis()); int shares = curSel.getSelectedPortRecord().getShares(); secInfo = new SecurityInfo( sec, shares); } else { secInfo = new SecurityInfo( new SecurityImpl("INTC", Security.kStock, (float)120.00), 100); } SecurityDialog secDialog; switch (transType) { case kBuy: secDialog = new SecurityDialog( getComponentFrame(), "Buy", SecurityDialog.kBuyDialog, this, secInfo); secDialog.show(); break; case kSell: if( curSel == null || curSel.isEmpty() ) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Please select a Security to Sell", null); mbox.show(); } else { secDialog = new SecurityDialog( getComponentFrame(), "Sell", SecurityDialog.kSellDialog, this, secInfo); secDialog.show(); } break; case kSetPrice: if( curSel == null || curSel.isEmpty() ) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Please select a Security first", null); mbox.show(); } else { secDialog = new SecurityDialog( getComponentFrame(), "Set Price", SecurityDialog.kSetPriceDialog, this, secInfo); secDialog.show(); } break; case kDeposit: secDialog = new SecurityDialog( getComponentFrame(), "Deposit Cash", SecurityDialog.kDepositDialog, this, null); secDialog.getPrice().requestFocus(); secDialog.show(); break; case kWithdraw: secDialog = new SecurityDialog( getComponentFrame(), "Withdraw Cash", SecurityDialog.kWithdrawDialog, this, null); secDialog.getPrice().requestFocus(); secDialog.show(); break; case kRiskView: Chart riskView = ((DpmView)getView()).getRiskView(); Frame riskFrame = ((DpmView)getView()).getRiskChartFrame(); if( riskFrame.isVisible() == false) { riskView.setChartLabels( ((DpmView)getView()).getSecurityTypeLabels()); riskView.setChartData( ((DpmView)getView()).getSecurityTypeValues()); riskFrame.pack(); riskFrame.setVisible( true); } break; case kSecurityView: Chart securityView = ((DpmView)getView()).getSecurityView(); Frame securityFrame = ((DpmView)getView()).getSecurityChartFrame(); if( securityFrame.isVisible() == false) { securityView.setChartLabels( ((DpmView)getView()).getSecurityLabels()); securityView.setChartData( ((DpmView)getView()).getSecurityValues()); securityFrame.pack(); securityFrame.setVisible( true); } break; default: break; } } |
Handle an interaction with the Buy dialog | public void processBuyDialogResult( Dialog source, Object obj) { float cashAvailable = ((DpmModel)getModel()).getCashAvailable(); DpmSelection curSel = (DpmSelection)getModelSelection(); DpmSelection cmdSel = null; if( curSel != null ) cmdSel = (DpmSelection)curSel.clone(); if( source instanceof SecurityDialog) { SecurityInfo secInfo = (SecurityInfo)obj; Component nextFocus = ((SecurityDialog)source).getShares(); float price = secInfo.sec.getPrice(); float amount = price * secInfo.shares; if( price <= (float)0.0) { nextFocus = ((SecurityDialog)source).getPrice(); MessageDialog mbox = new MessageDialog( getComponentFrame(), "Price can't be -ve or zero!", nextFocus); mbox.show(); } else if( secInfo.shares <= 0) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Shares can't be -ve or zero!", nextFocus); mbox.show(); } else if( amount > cashAvailable) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Not enough cash available!", nextFocus); mbox.show(); } else { source.dispose(); addAndDo(new BuyCommand(cmdSel, secInfo.sec, secInfo.shares, secInfo.sec.getPrice())); } } } |
Handle an interaction with the Sell dialog | public void processSellDialogResult( Dialog source, Object obj) { // Verify if the user holds enough number of shares DpmSelection curSel = (DpmSelection)getModelSelection(); int sharesAvailable = curSel.getSelectedPortRecord().getShares(); DpmSelection cmdSel = null; cmdSel = (DpmSelection)curSel.clone(); if( source instanceof SecurityDialog) { SecurityInfo secInfo = (SecurityInfo)obj; Component nextFocus = ((SecurityDialog)source).getShares(); float price = secInfo.sec.getPrice(); float amount = price * secInfo.shares; if( price <= (float)0.0) { nextFocus = ((SecurityDialog)source).getPrice(); MessageDialog mbox = new MessageDialog( getComponentFrame(), "Price can't be -ve or zero!", nextFocus); mbox.show(); } else if( secInfo.shares <= 0) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Shares can't be -ve or zero!", nextFocus); mbox.show(); } else if( secInfo.shares > sharesAvailable) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Not enough shares available!", nextFocus); mbox.show(); } else { source.dispose(); addAndDo(new SellCommand(cmdSel, secInfo.sec, secInfo.shares, secInfo.sec.getPrice())); } } } |
Handle an interaction with the Set Price dialog | public void processSetPriceDialogResult( Dialog source, Object obj) { // Set the default security from selection, do not allow modifying security DpmSelection curSel = (DpmSelection)getModelSelection(); int sharesAvailable = curSel.getSelectedPortRecord().getShares(); DpmSelection cmdSel = null; cmdSel = (DpmSelection)curSel.clone(); if( source instanceof SecurityDialog) { SecurityInfo secInfo = (SecurityInfo)obj; Component nextFocus = ((SecurityDialog)source).getPrice(); float price = secInfo.sec.getPrice(); if( price <= (float)0.0) { nextFocus = ((SecurityDialog)source).getPrice(); MessageDialog mbox = new MessageDialog( getComponentFrame(), "Price can't be -ve or zero!", nextFocus); mbox.show(); } else { source.dispose(); addAndDo(new SetPriceCommand((DpmSelection)getModelSelection(), secInfo.sec.getPrice())); } } } |
Handle a Deposit transaction | public void processDepositDialogResult( Dialog source, Object obj) { if( source instanceof SecurityDialog) { SecurityInfo secInfo = (SecurityInfo)obj; Component nextFocus = ((SecurityDialog)source).getPrice(); float amount = secInfo.sec.getPrice(); if( amount < (float)0.0) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Amount can't be -ve!", nextFocus); mbox.show(); } else { source.dispose(); if( amount != (float)0.0) { addAndDo(new CashTransactionCommand((DpmSelection)getModelSelection(), CashTransactionCommand.kDeposit, amount)); } } } } |
Handle an interaction with the Withdraw dialog | public void processWithdrawDialogResult( Dialog source, Object obj) { if( source instanceof SecurityDialog) { SecurityInfo secInfo = (SecurityInfo)obj; Component nextFocus = ((SecurityDialog)source).getPrice(); float cashAvailable = ((DpmModel)getModel()).getCashAvailable(); float amount = secInfo.sec.getPrice(); if( amount < (float)0.0) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Amount can't be -ve!", nextFocus); mbox.show(); } else if( amount > cashAvailable) { MessageDialog mbox = new MessageDialog( getComponentFrame(), "Not enough cash available!", nextFocus); mbox.show(); } else { source.dispose(); if( amount != (float)0.0) { addAndDo(new CashTransactionCommand((DpmSelection)getModelSelection(), CashTransactionCommand.kWithdraw, amount)); } } } } public void processDialogResult( Dialog source, Object obj) { } |
Correctly handle the animated ticker tape when the window is opened and closed | public void windowIconified( WindowEvent e) { TickerTape tickerTape = ((DpmView)getView()).getTickerTape(); tickerTape.stopAnimation(); super.windowIconified(e ); } public void windowDeiconified( WindowEvent e) { super.windowDeiconified(e); TickerTape tickerTape = ((DpmView)getView()).getTickerTape(); tickerTape.startAnimation(); } public void handleClose() { TickerTape tickerTape = ((DpmView)getView()).getTickerTape(); tickerTape.stopAnimation(); super.handleClose(); } |
Handle undo and redo by calling through to the super class | void undo() { handleUndo(); } void redo() { handleRedo(); } |
Data members | public static final int kBuy = 0; public static final int kSell = 1; public static final int kSetPrice = 2; public static final int kDeposit = 3; public static final int kWithdraw = 4; public static final int kRiskView = 5; public static final int kSecurityView = 6; private MenuItem buyMenuItem; private MenuItem sellMenuItem; private MenuItem setPriceMenuItem; private MenuItem depositMenuItem; private MenuItem withdrawMenuItem; private TToolButton buyToolButton; private TToolButton sellToolButton; private TToolButton setPriceToolButton; private TToolButton depositToolButton; private TToolButton withdrawToolButton; } |
Previous | Next |
Copyright ©
Taligent, Inc. 1996 - 1997.
Copyright © IBM Corporation 1996 - 1997.
All Rights Reserved.