4521e7e0561b462f32bc8db9cad0758d352452e0
[jalviewjs.git] / site / j2s / swingjs / test / FormattedTextFieldDemo.js
1 Clazz.declarePackage ("swingjs.test");
2 Clazz.load (["java.awt.event.ActionListener", "$.FocusListener", "java.beans.PropertyChangeListener", "javax.swing.JApplet", "$.JFormattedTextField"], "swingjs.test.FormattedTextFieldDemo", ["java.awt.BorderLayout", "$.Color", "$.GridLayout", "java.lang.Boolean", "$.Double", "java.text.NumberFormat", "javax.swing.JFrame", "$.JLabel", "$.JPanel", "$.SwingUtilities", "$.UIManager"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.amount = 100000;
5 this.rate = 7.5;
6 this.numPeriods = 30;
7 this.amountLabel = null;
8 this.rateLabel = null;
9 this.numPeriodsLabel = null;
10 this.paymentLabel = null;
11 this.amountField = null;
12 this.rateField = null;
13 this.numPeriodsField = null;
14 this.paymentField = null;
15 this.amountFormat = null;
16 this.percentFormat = null;
17 this.paymentFormat = null;
18 if (!Clazz.isClassDefined ("swingjs.test.FormattedTextFieldDemo.SwingJSValidatedNumberField")) {
19 swingjs.test.FormattedTextFieldDemo.$FormattedTextFieldDemo$SwingJSValidatedNumberField$ ();
20 }
21 Clazz.instantialize (this, arguments);
22 }, swingjs.test, "FormattedTextFieldDemo", javax.swing.JApplet, [java.awt.event.ActionListener, java.beans.PropertyChangeListener, java.awt.event.FocusListener]);
23 Clazz.overrideMethod (c$, "init", 
24 function () {
25 this.setLayout ( new java.awt.BorderLayout ());
26 this.setUpFormats ();
27 var payment = this.computePayment (this.amount, this.rate, this.numPeriods);
28 this.amountLabel =  new javax.swing.JLabel (swingjs.test.FormattedTextFieldDemo.amountString);
29 this.rateLabel =  new javax.swing.JLabel (swingjs.test.FormattedTextFieldDemo.rateString);
30 this.numPeriodsLabel =  new javax.swing.JLabel (swingjs.test.FormattedTextFieldDemo.numPeriodsString);
31 this.paymentLabel =  new javax.swing.JLabel (swingjs.test.FormattedTextFieldDemo.paymentString);
32 this.amountField = Clazz.innerTypeInstance (swingjs.test.FormattedTextFieldDemo.SwingJSValidatedNumberField, this, null, this.amountFormat);
33 this.amountField.setValue ( new Double (this.amount));
34 this.amountField.setColumns (10);
35 this.amountField.addPropertyChangeListener ("value", this);
36 this.amountField.addActionListener (this);
37 this.rateField = Clazz.innerTypeInstance (swingjs.test.FormattedTextFieldDemo.SwingJSValidatedNumberField, this, null, this.percentFormat);
38 this.rateField.setValue ( new Double (this.rate));
39 this.rateField.setColumns (10);
40 this.rateField.addPropertyChangeListener ("value", this);
41 this.rateField.addActionListener (this);
42 this.rateField.addActionListener (this);
43 this.numPeriodsField = Clazz.innerTypeInstance (swingjs.test.FormattedTextFieldDemo.SwingJSValidatedNumberField, this, null);
44 this.numPeriodsField.setValue ( new Integer (this.numPeriods));
45 this.numPeriodsField.setColumns (10);
46 this.numPeriodsField.addPropertyChangeListener ("value", this);
47 this.numPeriodsField.addActionListener (this);
48 this.paymentField =  new javax.swing.JFormattedTextField (this.paymentFormat);
49 this.paymentField.setValue ( new Double (payment));
50 this.paymentField.setColumns (10);
51 this.paymentField.setEditable (false);
52 this.paymentField.setForeground (java.awt.Color.red);
53 this.amountLabel.setLabelFor (this.amountField);
54 this.rateLabel.setLabelFor (this.rateField);
55 this.numPeriodsLabel.setLabelFor (this.numPeriodsField);
56 this.paymentLabel.setLabelFor (this.paymentField);
57 var labelPane =  new javax.swing.JPanel ( new java.awt.GridLayout (0, 1));
58 labelPane.add (this.amountLabel);
59 labelPane.add (this.rateLabel);
60 labelPane.add (this.numPeriodsLabel);
61 labelPane.add (this.paymentLabel);
62 var fieldPane =  new javax.swing.JPanel ( new java.awt.GridLayout (0, 1));
63 fieldPane.add (this.amountField);
64 fieldPane.add (this.rateField);
65 fieldPane.add (this.numPeriodsField);
66 fieldPane.add (this.paymentField);
67 this.add (labelPane, "Center");
68 this.add (fieldPane, "After");
69 this.resize (270, 100);
70 });
71 Clazz.overrideMethod (c$, "propertyChange", 
72 function (e) {
73 var source = e.getSource ();
74 System.out.println ("propertyChange " + e);
75 if (source === this.amountField) {
76 this.amount = (this.amountField.getValue ()).doubleValue ();
77 } else if (source === this.rateField) {
78 this.rate = (this.rateField.getValue ()).doubleValue ();
79 } else if (source === this.numPeriodsField) {
80 this.numPeriods = (this.numPeriodsField.getValue ()).intValue ();
81 }var payment = this.computePayment (this.amount, this.rate, this.numPeriods);
82 this.paymentField.setValue ( new Double (payment));
83 }, "java.beans.PropertyChangeEvent");
84 c$.createAndShowGUI = Clazz.defineMethod (c$, "createAndShowGUI", 
85  function () {
86 var frame =  new javax.swing.JFrame ("FormattedTextFieldDemo");
87 frame.setDefaultCloseOperation (3);
88 frame.add ( new swingjs.test.FormattedTextFieldDemo ());
89 frame.pack ();
90 frame.setVisible (true);
91 });
92 c$.main = Clazz.defineMethod (c$, "main", 
93 function (args) {
94 javax.swing.SwingUtilities.invokeLater (((Clazz.isClassDefined ("swingjs.test.FormattedTextFieldDemo$1") ? 0 : swingjs.test.FormattedTextFieldDemo.$FormattedTextFieldDemo$1$ ()), Clazz.innerTypeInstance (swingjs.test.FormattedTextFieldDemo$1, this, null)));
95 }, "~A");
96 Clazz.defineMethod (c$, "computePayment", 
97 function (loanAmt, rate, numPeriods) {
98 var I;
99 var partial1;
100 var denominator;
101 var answer;
102 numPeriods *= 12;
103 if (rate > 0.01) {
104 I = rate / 100.0 / 12.0;
105 partial1 = Math.pow ((1 + I), (0.0 - numPeriods));
106 denominator = (1 - partial1) / I;
107 } else {
108 denominator = numPeriods;
109 }answer = (-1 * loanAmt) / denominator;
110 return answer;
111 }, "~N,~N,~N");
112 Clazz.defineMethod (c$, "setUpFormats", 
113  function () {
114 this.amountFormat = java.text.NumberFormat.getNumberInstance ();
115 this.percentFormat = java.text.NumberFormat.getNumberInstance ();
116 this.percentFormat.setMinimumFractionDigits (3);
117 this.paymentFormat = java.text.NumberFormat.getCurrencyInstance ();
118 });
119 Clazz.overrideMethod (c$, "focusGained", 
120 function (e) {
121 System.out.println ("focusGained " + (e.getSource ()).getText () + " " + (e.getSource ()).getValue ());
122 }, "java.awt.event.FocusEvent");
123 Clazz.overrideMethod (c$, "focusLost", 
124 function (e) {
125 System.out.println ("focusLost " + (e.getSource ()).getText () + " " + (e.getSource ()).getValue ());
126 }, "java.awt.event.FocusEvent");
127 Clazz.overrideMethod (c$, "actionPerformed", 
128 function (e) {
129 System.out.println ("actionEvent " + e);
130 (e.getSource ()).validateNumber ();
131 }, "java.awt.event.ActionEvent");
132 c$.$FormattedTextFieldDemo$SwingJSValidatedNumberField$ = function () {
133 Clazz.pu$h(self.c$);
134 c$ = Clazz.decorateAsClass (function () {
135 Clazz.prepareCallback (this, arguments);
136 Clazz.instantialize (this, arguments);
137 }, swingjs.test.FormattedTextFieldDemo, "SwingJSValidatedNumberField", javax.swing.JFormattedTextField);
138 Clazz.overrideMethod (c$, "invalidEdit", 
139 function () {
140 this.validateNumber ();
141 });
142 Clazz.defineMethod (c$, "validateNumber", 
143 function () {
144 try {
145 this.setText (this.getFormatter ().valueToString (this.getValue ()));
146 } catch (e1) {
147 if (Clazz.exceptionOf (e1, java.text.ParseException)) {
148 } else {
149 throw e1;
150 }
151 }
152 });
153 c$ = Clazz.p0p ();
154 };
155 c$.$FormattedTextFieldDemo$1$ = function () {
156 Clazz.pu$h(self.c$);
157 c$ = Clazz.declareAnonymous (swingjs.test, "FormattedTextFieldDemo$1", null, Runnable);
158 Clazz.overrideMethod (c$, "run", 
159 function () {
160 javax.swing.UIManager.put ("swing.boldMetal", Boolean.FALSE);
161 swingjs.test.FormattedTextFieldDemo.createAndShowGUI ();
162 });
163 c$ = Clazz.p0p ();
164 };
165 Clazz.defineStatics (c$,
166 "amountString", "Loan Amount: ",
167 "rateString", "APR (%): ",
168 "numPeriodsString", "Years: ",
169 "paymentString", "Monthly Payment: ");
170 });