KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > calculator > listeners > Calculator_li


1 /*******************************************************************************
2  * Copyright (c) 2004, Dirk von der Weiden.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * Dirk von der Weiden - initial API and implementation
10  *
11  * Created on 14.06.2004
12  *
13  * date: 14.06.2004
14  * project: WiSer-Calculator
15  *
16  * *******************************************************************************/

17
18 package calculator.listeners;
19
20 import Jmc.baseGui.*;
21 import Jmc.baseTools.*;
22 import Jmc.commonGui.*;
23
24 /**
25  * @author Dirk
26  *
27  * date: 14.06.2004
28  *
29  * <p>
30  * Purpose: This listener handles all events from the calculator
31  * </p>
32  */

33 public class Calculator_li implements base_guiListener
34 {
35   class CHelper
36   {
37     private int cnt = 0;
38      
39     protected double v[] = {0,0,0};
40     protected char o[] = { ' ', ' '};
41     
42     public void pcmf_c ()
43     {
44       this.cnt = 0;
45     };
46
47     public void pcmf_ce ()
48     {
49       if (this.cnt >= 1)
50         this.cnt--;
51     };
52     
53     public void pcmf_setVal(double xVal)
54     {
55       v[cnt++] = xVal;
56     };
57     
58     public double pcmf_setOp (char xOp)
59     {
60       if (cnt == 0)
61         return (v[cnt]);
62         
63       if (xOp == '=')
64         return (this.pcmf_setOp('+'));
65
66       if (cnt > 1 && (o[cnt-2] == '/'))
67       {
68         v[cnt-2] = v[cnt-2]/v[cnt-1];
69         cnt = cnt-1;
70         o[cnt-1] = xOp;
71         return (this.pcmf_setOp(xOp));
72       }
73       if (cnt > 1 && (o[cnt-2] == '*'))
74       {
75         v[cnt-2] = v[cnt-2]*v[cnt-1];
76         cnt = cnt-1;
77         o[cnt-1] = xOp;
78         return (this.pcmf_setOp(xOp));
79       }
80       if (cnt > 1 && (o[cnt-2] == '+') && (xOp == '+' || xOp == '-'))
81       {
82         v[cnt-2] = v[cnt-2]+v[cnt-1];
83         cnt = cnt-1;
84         o[cnt-1] = xOp;
85         return (this.pcmf_setOp(xOp));
86       }
87       else
88       if (cnt > 1 && (o[cnt-2] == '-') && (xOp == '+' || xOp == '-'))
89       {
90         v[cnt-2] = v[cnt-2]-v[cnt-1];
91         cnt = cnt-1;
92         o[cnt-1] = xOp;
93         
94         return (this.pcmf_setOp(xOp));
95       }
96       
97       o[cnt-1] = xOp;
98       return (v[cnt-1]);
99     }
100   }
101   private String JavaDoc pem_disp = "";
102   private CHelper pem_calc = new CHelper();
103     /**
104          * <p>
105          * This method is called whenever the button is pressed.
106          * </p><p>
107          *
108          * </p><p>
109          * @param xParam the widget which generates the event
110          * </p>
111          */

112     public void pcmf_execListener(base_guiObj xParam) throws Exception JavaDoc
113     {
114     // get the widget from the registry
115
base_guiWidget_if l_disp = (base_guiWidget_if)base_registredObject.pcmf_getObjByName("display");
116
117     // get the value of the pressed button
118
String JavaDoc l_value = (String JavaDoc)xParam.pcmf_getValue();
119     
120     if (xParam instanceof base_eventChannel_if)
121       l_value = l_value.substring(l_value.length()-1);
122     
123     // calculate
124
if (l_value.equals("+"))
125     {
126       this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp));
127       
128       // set the value of the calculators display
129
l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('+')));
130       this.pem_disp = "0";
131     }
132     else
133     if (l_value.equals("-"))
134     {
135       this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp));
136
137       // set the value of the calculators display
138
l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('-')));
139       this.pem_disp = "0";
140     }
141     else
142     if (l_value.equals("*"))
143     {
144       this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp));
145
146       // set the value of the calculators display
147
l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('*')));
148       this.pem_disp = "0";
149     }
150     else
151     if (l_value.equals("/"))
152     {
153       this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp));
154
155       // set the value of the calculators display
156
l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('/')));
157       this.pem_disp = "0";
158     }
159     else
160     if (l_value.equals("C"))
161     {
162       l_disp.pcmf_setValue("0");
163       this.pem_disp = "0";
164       this.pem_calc.pcmf_c();
165     }
166     else
167     if (l_value.equals("CE"))
168     {
169       l_disp.pcmf_setValue("0");
170       this.pem_disp = "0";
171       this.pem_calc.pcmf_ce();
172     }
173     else
174     if (l_value.equals("="))
175     {
176       this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp));
177
178       // set the value of the calculators display
179
l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('=')));
180       this.pem_disp = l_disp.pcmf_getValue().toString();
181       this.pem_calc.pcmf_c();
182     }
183     else
184     {
185       if (l_value.equals(".")==false && l_disp.pcmf_getValue().equals("0") || this.pem_disp.equals("0"))
186         this.pem_disp = l_value;
187       else
188         this.pem_disp += l_value;
189       
190       l_disp.pcmf_setValue(this.pem_disp);
191     }
192     
193     // repaint the calculators display
194
l_disp.pcmf_repaint();
195     
196     return;
197     }
198 }
Popular Tags