home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / LoanCalc / Src / LCCtrlPnl.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  8.5 KB  |  340 lines

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import com.ms.ui.*;
  5. import com.ms.fx.*;
  6. import java.awt.Dimension;
  7.  
  8. public class LCCtrlPnl extends UIPanel implements LCConsts, LCCallbacks, Runnable
  9. {
  10.     private LCWizard wiz;
  11.     private WZIntro intro;
  12.     private WZValue value;
  13.     private WZInterest intrst;
  14.     private WZNumPeriods numper;
  15.     private WZPayment paymnt;
  16.     private WZFinal wfinal;
  17.     private LCParams lparms;
  18.     private LCInfoPnl info;
  19.     private LCDataPnl sched;
  20.     private int calc;
  21.     private UIMessageBox mbox;
  22.  
  23.     public LCCtrlPnl(UIApplet applet, UIFrame frame)
  24.     {
  25.         setLayout(new UISplitLayout(0, 167));
  26.  
  27.         // Initialize load on demand images
  28.         LCImages.init(applet, frame);
  29.  
  30.         // Set default values for loan parameters
  31.         lparms = new LCParams(INIT_PV, INIT_RATE, INIT_LEN, true, INIT_FREQ, 0);
  32.         calc = BL_PMNT; // --------------------------------------------------^
  33.         lparms.calc(BL_PMNT);
  34.  
  35.         // Create Loan parameter Display panel
  36.         add(info = new LCInfoPnl(lparms, this), "nw");
  37.  
  38.         // Create Viewer showing loan data(payment schedule)
  39.         add(sched = new LCDataPnl(lparms), "se");
  40.  
  41.         // Create wizard steps and wizard for specifying different loan parameters
  42.         intro = new WZIntro(); value = new WZValue();
  43.         intrst = new WZInterest(); numper = new WZNumPeriods();
  44.         paymnt = new WZPayment(); wfinal = new WZFinal();
  45.  
  46.         //  Need UIFrame for wizard, if we are run as applet passed in frame is null
  47.         if ( applet != null )
  48.             frame = FxToolkit.getHelperUIFrame();
  49.  
  50.         wiz = new LCWizard(frame, this, wfinal);
  51.         wiz.setForcedBounds("image", new Dimension(80, 80));
  52.         wiz.setForcedBounds("content", new Dimension(270, 150));
  53.         wiz.addStep(intro); wiz.addStep(value); wiz.addStep(intrst);
  54.         wiz.addStep(numper); wiz.addStep(paymnt); wiz.addStep(wfinal);
  55.  
  56.         // Create OK Message Box which will be used by the wizard to indicate
  57.         //  an error in entering a loan parameter
  58.         mbox = new UIMessageBox(frame, "Parameter Entry Error!", "", LCImages.get(INTRO),
  59.                                 new UIButtonBar(UIButtonBar.OK, UIButtonBar.RAISED));
  60.         mbox.setFont(new FxFont("Dialog", FxFont.PLAIN, 14));
  61.     }
  62.  
  63.     public void hideWizard()
  64.     {
  65.         if ( wiz != null )
  66.             wiz.setVisible(false);
  67.     }
  68.  
  69.     public void run()
  70.     {
  71.         invokeWizard();
  72.         // After first time, do not need Intro page
  73.         wiz.setFirstStep(value);
  74.     }
  75.  
  76.     public void setInitialFocus()
  77.     {
  78.         Thread th = new Thread(this);
  79.         th.start();
  80.     }
  81.  
  82.     public void invokeWizard()
  83.     {
  84.         long t_pv;
  85.         int t_i, t_len, t_fidx, t_pmnt, oldcalc = calc;
  86.  
  87.         // set parameter to calculate to NONE, the various validation
  88.         //  steps will set it accordingly
  89.         setCalc(BL_NONE);
  90.  
  91.         // Call wizard
  92.         if ( wiz.doModal() != null ) {
  93.             LCParams t_lp = getParamsFromWiz();
  94.  
  95.             // If nothing has changed don't recalculate
  96.             if ( (oldcalc != calc) ||
  97.                  ((calc != BL_PV) && (t_lp.pv != lparms.pv)) || 
  98.                  (t_lp.i != lparms.i) || 
  99.                  ((calc != BL_LEN) && (t_lp.len != lparms.len)) ||
  100.                  (t_lp.years != lparms.years) || (t_lp.hz_idx != lparms.hz_idx) ||
  101.                  ((calc != BL_PMNT) && (t_lp.pmnt != lparms.pmnt)) ) {
  102.                 lparms = t_lp;
  103.                 if ( lparms.calc(calc) )
  104.                     setWizParams(lparms, false);
  105.  
  106.                 // Display loan information
  107.                 info.setParams(lparms);
  108.                 //info.setValid(true);
  109.  
  110.                 // repaint panel after wizard disappears, putting up an hour glass
  111.                 //  would also be a good idea, as it may take a little time to
  112.                 //  calculate payment schedule.
  113.                 //setVisible(false); setValid(true); setVisible(true);
  114.  
  115.                 // Calculate and display payment schedule
  116.                 sched.fillList(lparms, false);
  117.             }
  118.         }
  119.         else { // user cancelled wizard reset any parameters user may have changed
  120.             setCalc(oldcalc);
  121.             setWizParams(lparms, true);
  122.         }
  123.     }
  124.  
  125.     // get/set parameter to be calculated
  126.     public int getCalc() { return calc; }
  127.     public void setCalc(int id) { calc = id; }
  128.  
  129.     public int getID(IUIWizardStep step)
  130.     {
  131.         if ( step instanceof WZValue ) return PVALUE;
  132.         else if ( step instanceof WZInterest ) return INTEREST;
  133.         else if ( step instanceof WZNumPeriods ) return NUMPERIODS;
  134.         else if ( step instanceof WZPayment ) return PAYMENT;
  135.         else return -1;
  136.     }
  137.  
  138.     // ******************************
  139.     // Validate Wizard steps section
  140.     // ******************************
  141.     public void processPV()
  142.     {
  143.         if ( getPVFromWiz() == 0 )
  144.             setCalc(BL_PV);
  145.         else
  146.             setCalc(BL_NONE); // Reset in case user uses Back to get here
  147.     }
  148.  
  149.     public boolean isValidNumper()
  150.     {
  151.         String error = new String("");
  152.         int nummonths = getLenFromWiz();
  153.  
  154.         if ( nummonths == 0 ) {
  155.             if ( calc != BL_PV )
  156.                 setCalc(BL_LEN);
  157.             else if ( calc == BL_PV )
  158.                 error = LEN_CALC_PV;
  159.         }
  160.         else {
  161.             if ( calc != BL_PV )
  162.                 setCalc(BL_NONE);
  163.             if ( numper.pnl.years )
  164.                 nummonths *= 12;
  165.             if ( nummonths > MAX_MONTHS )
  166.                 error = NUMPER_TOO_LARGE;
  167.         }
  168.  
  169.         if ( error.length() != 0 ) {
  170.             mbox.setText(error);
  171.             mbox.doModal();
  172.             return false;
  173.         }
  174.         else
  175.             return true;
  176.     }
  177.  
  178.     public boolean isValidPayment()
  179.     {
  180.         String error = "";
  181.  
  182.         // if we get here either pv or n is being calculated so the pmnt cannot be 0
  183.         LCParams lp = getParamsFromWiz();
  184.  
  185.         if ( lp.pmnt == 0 ) {
  186.             if ( calc == BL_PV )
  187.                 error = PMNT_CALC_PV;
  188.             else // must be calculating LEN
  189.                 error = PMNT_CALC_LEN;
  190.         }
  191.         else { // a payment has been entered
  192.             if ( calc == BL_LEN ) {
  193.                 if ( (lp.pmnt/100.0) <= (lp.pv * lp.ip) )
  194.                     error = PMNT_TOO_SMALL_INT + calcMinPmnt(lp);
  195.                 else {
  196.                     lp.calc(BL_LEN);
  197.                     int lmonths = (lp.years ? lp.len*12 : lp.len);
  198.                     if ( lmonths > MAX_MONTHS )
  199.                         error = PMNT_TOO_SMALL_LEN + calcMinPmnt(lp);
  200.                 }
  201.             }
  202.             else { // must be calculating PV
  203.                 lp.calc(BL_PV);
  204.                 if ( lp.pv > MAX_PV )
  205.                     error = PMNT_TOO_LARGE_PV;
  206.             }
  207.         }
  208.  
  209.         if ( error.length() != 0 ) {
  210.             mbox.setText(error);
  211.             mbox.doModal();
  212.             return false;
  213.         }
  214.         else
  215.             return true;
  216.     }
  217.  
  218.     private String calcMinPmnt(LCParams lp)
  219.     {
  220.         LCParams lcp = new LCParams(lp);
  221.         lcp.n = (MAX_MONTHS*FREQ_FACTOR[lcp.hz_idx])/12;
  222.         lcp.calc(BL_PMNT);
  223.         return "" + new DNC(lcp.pmnt+1) + ".";
  224.     }
  225.  
  226.     // ******************************
  227.     // Get Wizard Parameters section
  228.     // ******************************
  229.     private LCParams getParamsFromWiz()
  230.     {
  231.         return new LCParams(getPVFromWiz(), getIFromWiz(), getLenFromWiz(),
  232.                             numper.pnl.years, getFreqIdxFromWiz(), getPmntFromWiz());
  233.     }
  234.  
  235.  
  236.     private long getPVFromWiz()
  237.     {
  238.         String value_str = value.pnl.pv_e.getValueText();
  239.         if ( value_str.length() == 0 )
  240.             return 0;
  241.         else
  242.             return (new Integer(value_str)).longValue();
  243.     }
  244.  
  245.     private int getIFromWiz()
  246.     {
  247.         return (intrst.pnl.i_ec.getSelectedIndex() + 24);
  248.     }
  249.  
  250.     private int getLenFromWiz()
  251.     {
  252.         String value_str = numper.pnl.len_e.getValueText();
  253.         if ( value_str.length() == 0 )
  254.             return 0;
  255.         else
  256.             return (new Integer(value_str)).intValue();
  257.     }
  258.  
  259.     private int getFreqIdxFromWiz()
  260.     {
  261.         return numper.pnl.hz_c.getSelectedIndex();
  262.     }
  263.  
  264.     private int getPmntFromWiz()
  265.     { 
  266.         String value_str = paymnt.pnl.pmnt_e.getValueText();
  267.         if ( value_str.length() == 0 )
  268.             return 0;
  269.         else
  270.             return (int)((new Double(value_str)).doubleValue()*100.0);
  271.     }
  272.  
  273.     // ******************************
  274.     // Set Wizard Parameters section
  275.     // ******************************
  276.     private void setWizParams(LCParams lp, boolean doall)
  277.     {
  278.         if ( doall || (calc != BL_PV) )
  279.             setPV(lp.pv);
  280.         else
  281.             setPV(0);
  282.  
  283.         if ( doall || (calc != BL_LEN) ) {
  284.             setLen(lp.len);
  285.             setLenUnit(lp.years);
  286.         }
  287.         else
  288.             setLen(0);
  289.  
  290.         if ( doall || (calc != BL_PMNT) )
  291.             setPayment(lp.pmnt);
  292.         else
  293.             setPayment(0);
  294.  
  295.         setI(lp.i);
  296.         setHz(lp.hz_idx);
  297.     }
  298.  
  299.     private void setPV(long pv)
  300.     {
  301.         if ( pv == 0 )
  302.             value.pnl.pv_e.setValueText("");
  303.         else
  304.             value.pnl.pv_e.setValueText("" + pv);
  305.     }
  306.  
  307.     private void setLen(int len)
  308.     {
  309.         if ( len == 0 )
  310.             numper.pnl.len_e.setValueText("");
  311.         else
  312.             numper.pnl.len_e.setValueText("" + len);
  313.     }
  314.  
  315.     private void setLenUnit(boolean years)
  316.     {
  317.         if ( years ) {
  318.             numper.pnl.btn_y.setChecked(true);
  319.             numper.pnl.btn_m.setChecked(false);
  320.         }
  321.         else {
  322.             numper.pnl.btn_y.setChecked(false);
  323.             numper.pnl.btn_m.setChecked(true);
  324.         }
  325.     }
  326.  
  327.     // payment is stored as cents, so 100 = $1.00
  328.     private void setPayment(int pmnt)
  329.     {
  330.         if ( pmnt == 0 )
  331.             paymnt.pnl.pmnt_e.setValueText("");
  332.         else
  333.             paymnt.pnl.pmnt_e.setValueText("" + (pmnt/100) + "." + DLRS.mk2Digits(pmnt%100));
  334.     }
  335.  
  336.     private void setI(int i) { intrst.pnl.i_ec.setSelectedIndex(i - MIN_RATE); }
  337.  
  338.     private void setHz(int idx) { numper.pnl.hz_c.setSelectedIndex(idx); }
  339. }
  340.