001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package calc.step1;
022:
023: import java.awt.Dimension;
024: import java.awt.GridBagConstraints;
025: import java.awt.GridBagLayout;
026: import java.awt.Insets;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.WindowAdapter;
030: import java.awt.event.WindowEvent;
031: import calc.CalcLogic;
032: import com.javujavu.javux.wings.WingButton;
033: import com.javujavu.javux.wings.WingFrame;
034: import com.javujavu.javux.wings.WingPanel;
035: import com.javujavu.javux.wings.WingSkin;
036: import com.javujavu.javux.wings.WingTextField;
037:
038: /**
039: * step 1 just a simple calculator
040: */
041: public class Calc extends WingPanel implements ActionListener {
042: public static void main(String[] args) {
043: // at first load skin !
044: loadCalcSkin();
045:
046: // and show calc in frame
047: final WingFrame frame = new WingFrame();
048: frame.setContentPane(new Calc());
049:
050: frame.setTitle("WingCalc step1");
051:
052: frame.addWindowListener(new WindowAdapter() {
053: public void windowClosing(WindowEvent e) {
054: System.exit(0);
055: }
056: });
057:
058: frame.pack();
059: frame.setLocation(100, 100);
060: frame.setVisible(true);
061: }
062:
063: protected static void loadCalcSkin() {
064: // from directory skin/green relative to class file of Calc
065: WingSkin.loadSkin("/skin/green", true, Calc.class);
066: }
067:
068: private WingTextField tfDisplay;
069: private WingButton[] bFunc;
070:
071: private CalcLogic logic = new CalcLogic();
072:
073: public Calc() {
074: // build content panel
075: WingPanel panel = this ;
076: panel.setLayout(new GridBagLayout());
077: bFunc = new WingButton[CalcLogic.FUNC_COUNT];
078: GridBagConstraints c = new GridBagConstraints();
079: c.fill = GridBagConstraints.BOTH;
080: c.weightx = 1.0;
081: c.weighty = 1.0;
082: c.insets = new Insets(6, 3, 7, 3);
083: c.gridx = 0;
084: c.gridy = 0;
085: c.gridheight = 1;
086: c.gridwidth = 5;
087: // with display
088: panel.add(tfDisplay = new WingTextField(), c);
089: tfDisplay.setEditable(false);
090: tfDisplay.setWingFocusable(false);
091: tfDisplay.setHorizontalAlignment(RIGHT);
092: tfDisplay.setText(logic.getDisplayText());
093:
094: // and buttons
095: int x, y;
096: y = 1;
097: x = 0;
098: addButton(x++, y, 1, 1, "%", CalcLogic.FUNC_PERCENT);
099: addButton(x++, y, 1, 1, "Sqrt", CalcLogic.FUNC_SQRT);
100: addButton(x++, y, 1, 1, "BkSp", CalcLogic.FUNC_BACKSPACE);
101: addButton(x++, y, 1, 1, "C", CalcLogic.FUNC_C);
102: addButton(x++, y, 1, 1, "CE", CalcLogic.FUNC_CE);
103: y++;
104: x = 0;
105: addButton(x++, y, 1, 1, "7", CalcLogic.FUNC_7);
106: addButton(x++, y, 1, 1, "8", CalcLogic.FUNC_8);
107: addButton(x++, y, 1, 1, "9", CalcLogic.FUNC_9);
108: addButton(x++, y, 1, 2, "-", CalcLogic.FUNC_MINUS);
109: addButton(x++, y, 1, 1, "+/-", CalcLogic.FUNC_SIGN);
110: y++;
111: x = 0;
112: addButton(x++, y, 1, 1, "4", CalcLogic.FUNC_4);
113: addButton(x++, y, 1, 1, "5", CalcLogic.FUNC_5);
114: addButton(x++, y, 1, 1, "6", CalcLogic.FUNC_6);
115: x++;
116: addButton(x++, y, 1, 1, "1/x", CalcLogic.FUNC_RECIPROCAL);
117: y++;
118: x = 0;
119: addButton(x++, y, 1, 1, "1", CalcLogic.FUNC_1);
120: addButton(x++, y, 1, 1, "2", CalcLogic.FUNC_2);
121: addButton(x++, y, 1, 1, "3", CalcLogic.FUNC_3);
122: addButton(x++, y, 1, 2, "+", CalcLogic.FUNC_PLUS);
123: addButton(x++, y, 1, 1, "/", CalcLogic.FUNC_DIVIDE);
124: y++;
125: x = 0;
126: addButton(x++, y, 2, 1, "0", CalcLogic.FUNC_0);
127: x++;
128: addButton(x++, y, 1, 1, ".", CalcLogic.FUNC_PERIOD);
129: x++;
130: addButton(x++, y, 1, 1, "*", CalcLogic.FUNC_MULTIPLY);
131: y++;
132: x = 0;
133: addButton(x, y, 5, 1, "=", CalcLogic.FUNC_EQUAL);
134:
135: }
136:
137: private void addButton(int gridx, int gridy, int gridwidth,
138: int gridheight, String text, int func) {
139: // prepare GridBagConstraints describing size and location of the button
140: GridBagConstraints c = new GridBagConstraints();
141: c.gridx = gridx;
142: c.gridy = gridy;
143: c.gridwidth = gridwidth;
144: c.gridheight = gridheight;
145: c.insets = new Insets(0, (gridx == 0) ? 3 : 0, 3, 3);
146: c.weightx = c.weighty = 1;
147: c.fill = GridBagConstraints.BOTH;
148:
149: //create the button
150: WingButton b = new WingButton(text);
151:
152: // add to button array
153: bFunc[func] = b;
154:
155: // add to panel
156: this .add(b, c);
157:
158: // add action listener
159: b.addActionListener(this );
160:
161: // make the button a big rectangle
162: b.setPreferredSize(new Dimension(40, 32));
163:
164: }
165:
166: public void actionPerformed(ActionEvent e) {
167: Object src = e.getSource();
168: // browse button array to find the pressed button
169: for (int f = 0; f < bFunc.length; f++) {
170: if (src == bFunc[f]) {
171: // call logic to execute function f
172: logic.buttonAction(f);
173: // display result
174: tfDisplay.setText(logic.getDisplayText());
175:
176: // update button enabled state
177: for (int n = 0; n < bFunc.length; n++) {
178: if (bFunc[n] != null)
179: bFunc[n].setEnabled(logic.isEnabled(n));
180: }
181: return;
182: }
183: }
184: }
185: }
|