summaryrefslogtreecommitdiffstats
path: root/web-gui/calculator/source/class/calculator/Application.js
blob: 3041beabf1691ef1c872386b8c631a9fb01eaba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* ************************************************************************

   Copyright:

   License:

   Authors:

************************************************************************ */

/* ************************************************************************

#asset(calculator/*)

************************************************************************ */

/**
 * This is the main application class of your custom application "calculator"
 */
qx.Class.define("calculator.Application",
{
  extend : qx.application.Standalone,



  /*
  *****************************************************************************
     MEMBERS
  *****************************************************************************
  */

  members :
  {
    __edit: null,
    __win: null,
    __last: null,
    __lastCmd: null,
    __clear: false,
    __rpc: null,

    /**
     * This method contains the initial application code and gets called 
     * during startup of the application
     * 
     * @lint ignoreDeprecated(alert)
     */
    main : function()
    {
      // Call super class
      this.base(arguments);

      // Enable logging in debug variant
      if (qx.core.Variant.isSet("qx.debug", "on"))
      {
        // support native logging capabilities, e.g. Firebug for Firefox
        qx.log.appender.Native;
        // support additional cross-browser console. Press F7 to toggle visibility
        qx.log.appender.Console;
      }

      /*
      -------------------------------------------------------------------------
        Below is your actual application code...
      -------------------------------------------------------------------------
      */

      // Document is the application root
      var doc = this.getRoot();
      doc.setBackgroundColor("#ffffff");

      var back = new qx.ui.basic.Image("calculator/back.png");
      back.setScale(true);
      doc.add(back, {left:0, top: 0});

      var win = new qx.ui.window.Window("Calculator");
      this.__win = win;
      doc.add(win, {left: 50, top: 50});

      var winLayout = new qx.ui.layout.Grid();
      winLayout.setSpacing(10);
      win.setLayout(winLayout);
      win.setShowStatusbar(true);
      win.open();

      this.__edit = new qx.ui.form.TextField();
      this.__edit.setAppearance("calc-edit");
      win.add(this.__edit, {row: 0, column: 0, colSpan: 5 });
      this.__edit.setReadOnly(true);

      var app = this;
      var makeButton = function(text, r, c)
      {
        var button = new qx.ui.form.Button(text);
        win.add(button, {row: r, column: c });
        button.addListener("execute", function(e) {
                            this.__doKey(text);
                           }, app);
        button.setAppearance("calc-button");
      }
      makeButton("1", 4, 0);
      makeButton("2", 4, 1);
      makeButton("3", 4, 2);
      makeButton("4", 3, 0);
      makeButton("5", 3, 1);
      makeButton("6", 3, 2);
      makeButton("7", 2, 0);
      makeButton("8", 2, 1);
      makeButton("9", 2, 2);
      makeButton("0", 5, 0);

      var clearButton = new qx.ui.form.Button("C");
      win.add(clearButton, {row: 1, column: 0});
      clearButton.addListener("execute", this.__clearAll, this);
      clearButton.setAppearance("calc-button");

      var makeOpp = function(text, func, r, c, span)
      {
        if (span == null)
          span = 1;
        var button = new qx.ui.form.Button(text);
        win.add(button, {row: r, column: c, colSpan: span });
        button.addListener("execute", function(e) {
                            app.__doExec(func);
                           }, app);
        button.setAppearance("calc-button");
      }

      makeOpp("=", null, 1, 4);
      makeOpp("+", "Add", 2, 4);
      makeOpp("-", "Sub", 3, 4);
      makeOpp("*", "Multiply", 4, 4);
      makeOpp("/", "Divide", 5, 4);

      this.__rpc = new qx.io.remote.Rpc("http://localhost:8080/rpc", "com.pengutronix.jdb.Calculator|/");

    },
    __doKey : function(key)
    {
      var value;
      this.__win.setStatus("");
      if (this.__clear) {
        value = "";
        this.__clear = false;
      }
      else {
        value = this.__edit.getValue();
        if (value == null)
          value = "";
      }
      this.__edit.setValue(value + key);
    },
    __doExec : function(func)
    {
      this.__win.setStatus("");
      var current = parseInt(this.__edit.getValue());
      if (current == null)
        current = 0;
      if ((this.__last == null) || (this.__lastCmd == null)) {
        this.__last = current;
      }
      else {
        try {
          this.__last = this.__rpc.callSync("com.pengutronix.jdb.Calculator." + this.__lastCmd,
                                            "ii", this.__last, current);
          this.__edit.setValue("" + this.__last);
        }
        catch (error) {
          this.__clearAll();
          this.__win.setStatus(error);
          return;
        }
      }
      this.__lastCmd = func;
      this.__clear = true;
    },
    __clearAll : function()
    {
      this.__last = null;
      this.__lastCmd = null;
      this.__edit.setValue("");
    }
  }
});