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.step2;
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.KeyEvent;
030: import java.awt.event.WindowAdapter;
031: import java.awt.event.WindowEvent;
032: import calc.CalcLogic;
033: import com.javujavu.javux.wings.WingButton;
034: import com.javujavu.javux.wings.WingFrame;
035: import com.javujavu.javux.wings.WingPanel;
036: import com.javujavu.javux.wings.WingSkin;
037: import com.javujavu.javux.wings.WingTextField;
038:
039: /**
040: * step 2 tooltips and shortcuts
041: */
042: public class Calc extends WingPanel implements ActionListener {
043: public static void main(String[] args) {
044: // at first load skin !
045: loadCalcSkin();
046:
047: // and show calc in frame
048: final WingFrame frame = new WingFrame();
049: frame.setContentPane(new Calc());
050:
051: frame.setTitle("WingCalc step2");
052:
053: frame.addWindowListener(new WindowAdapter() {
054: public void windowClosing(WindowEvent e) {
055: System.exit(0);
056: }
057: });
058:
059: frame.pack();
060: frame.setLocation(100, 100);
061: frame.setVisible(true);
062: }
063:
064: protected static void loadCalcSkin() {
065: // from directory skin/green relative to class file of Calc
066: WingSkin.loadSkin("/skin/green", true, Calc.class);
067: }
068:
069: private WingTextField tfDisplay;
070: private WingButton[] bFunc;
071:
072: private CalcLogic logic = new CalcLogic();
073:
074: public Calc() {
075: // build content panel
076: WingPanel panel = this ;
077: panel.setLayout(new GridBagLayout());
078: bFunc = new WingButton[CalcLogic.FUNC_COUNT];
079: GridBagConstraints c = new GridBagConstraints();
080: c.fill = GridBagConstraints.BOTH;
081: c.weightx = 1.0;
082: c.weighty = 1.0;
083: c.insets = new Insets(6, 3, 7, 3);
084: c.gridx = 0;
085: c.gridy = 0;
086: c.gridheight = 1;
087: c.gridwidth = 5;
088: // with display
089: panel.add(tfDisplay = new WingTextField(), c);
090: tfDisplay.setEditable(false);
091: tfDisplay.setWingFocusable(false);
092: tfDisplay.setHorizontalAlignment(RIGHT);
093: tfDisplay.setText(logic.getDisplayText());
094:
095: // and buttons
096: int x, y;
097: y = 1;
098: x = 0;
099: addButton(x++, y, 1, 1, "%", CalcLogic.FUNC_PERCENT,
100: "Percent operator\nShortcut: %", KeyEvent.VK_5,
101: KeyEvent.SHIFT_MASK);
102: addButton(x++, y, 1, 1, "Sqrt", CalcLogic.FUNC_SQRT,
103: "Square root operator\nShortcut: Shift+S",
104: KeyEvent.VK_S, KeyEvent.SHIFT_MASK);
105: addButton(x++, y, 1, 1, "BkSp", CalcLogic.FUNC_BACKSPACE,
106: "Backspace\nShortcut: Backspace",
107: KeyEvent.VK_BACK_SPACE, 0);
108: addButton(x++, y, 1, 1, "C", CalcLogic.FUNC_C,
109: "Clear\nShortcut: Escape", KeyEvent.VK_ESCAPE, 0);
110: addButton(x++, y, 1, 1, "CE", CalcLogic.FUNC_CE,
111: "Clear entry\nShortcut: Delete", KeyEvent.VK_DELETE, 0);
112: y++;
113: x = 0;
114: addButton(x++, y, 1, 1, "7", CalcLogic.FUNC_7,
115: "Digit 7\nShortcut: numpad 7", KeyEvent.VK_NUMPAD7, 0);
116: addButton(x++, y, 1, 1, "8", CalcLogic.FUNC_8,
117: "Digit 8\nShortcut: numpad 8", KeyEvent.VK_NUMPAD8, 0);
118: addButton(x++, y, 1, 1, "9", CalcLogic.FUNC_9,
119: "Digit 9\nShortcut: numpad 9", KeyEvent.VK_NUMPAD9, 0);
120: addButton(x++, y, 1, 2, "-", CalcLogic.FUNC_MINUS,
121: "Subtraction operator\nShortcut: numpad -",
122: KeyEvent.VK_SUBTRACT, 0);
123: addButton(x++, y, 1, 1, "+/-", CalcLogic.FUNC_SIGN,
124: "Sign button\nShortcut: F9", KeyEvent.VK_F9, 0);
125: y++;
126: x = 0;
127: addButton(x++, y, 1, 1, "4", CalcLogic.FUNC_4,
128: "Digit 4\nShortcut: numpad 4", KeyEvent.VK_NUMPAD4, 0);
129: addButton(x++, y, 1, 1, "5", CalcLogic.FUNC_5,
130: "Digit 5\nShortcut: numpad 5", KeyEvent.VK_NUMPAD5, 0);
131: addButton(x++, y, 1, 1, "6", CalcLogic.FUNC_6,
132: "Digit 6\nShortcut: numpad 6", KeyEvent.VK_NUMPAD6, 0);
133: x++;
134: addButton(x++, y, 1, 1, "1/x", CalcLogic.FUNC_RECIPROCAL,
135: "Reciprocal button\nShortcut: Shift+R", KeyEvent.VK_R,
136: KeyEvent.SHIFT_MASK);
137: y++;
138: x = 0;
139: addButton(x++, y, 1, 1, "1", CalcLogic.FUNC_1,
140: "Digit 1\nShortcut: numpad 1", KeyEvent.VK_NUMPAD1, 0);
141: addButton(x++, y, 1, 1, "2", CalcLogic.FUNC_2,
142: "Digit 2\nShortcut: numpad 2", KeyEvent.VK_NUMPAD2, 0);
143: addButton(x++, y, 1, 1, "3", CalcLogic.FUNC_3,
144: "Digit 3\nShortcut: numpad 3", KeyEvent.VK_NUMPAD3, 0);
145: addButton(x++, y, 1, 2, "+", CalcLogic.FUNC_PLUS,
146: "Addition operator\nShortcut: numpad +",
147: KeyEvent.VK_ADD, 0);
148: addButton(x++, y, 1, 1, "/", CalcLogic.FUNC_DIVIDE,
149: "Division operator\nShortcut: numpad /",
150: KeyEvent.VK_DIVIDE, 0);
151: y++;
152: x = 0;
153: addButton(x++, y, 2, 1, "0", CalcLogic.FUNC_0,
154: "Digit 0\nShortcut: numpad 0", KeyEvent.VK_NUMPAD0, 0);
155: x++;
156: addButton(x++, y, 1, 1, ".", CalcLogic.FUNC_PERIOD,
157: "Decimal point button\nShortcut: numpad .",
158: KeyEvent.VK_DECIMAL, 0);
159: x++;
160: addButton(x++, y, 1, 1, "*", CalcLogic.FUNC_MULTIPLY,
161: "Multiplication operator\nShortcut: numpad *",
162: KeyEvent.VK_MULTIPLY, 0);
163: y++;
164: x = 0;
165: addButton(x, y, 5, 1, "=", CalcLogic.FUNC_EQUAL,
166: "Equal button\nShortcut: Enter", KeyEvent.VK_ENTER, 0);
167:
168: }
169:
170: private void addButton(int gridx, int gridy, int gridwidth,
171: int gridheight, String text, int func, String tooltipText,
172: int keyCode, int keyModifiers) {
173: // prepare GridBagConstraints describing size and location of the button
174: GridBagConstraints c = new GridBagConstraints();
175: c.gridx = gridx;
176: c.gridy = gridy;
177: c.gridwidth = gridwidth;
178: c.gridheight = gridheight;
179: c.insets = new Insets(0, (gridx == 0) ? 3 : 0, 3, 3);
180: c.weightx = c.weighty = 1;
181: c.fill = GridBagConstraints.BOTH;
182:
183: //create the button
184: WingButton b = new WingButton(text);
185:
186: // add to button array
187: bFunc[func] = b;
188:
189: // add to panel
190: this .add(b, c);
191:
192: // add action listener
193: b.addActionListener(this );
194:
195: // make the button a big rectangle
196: b.setPreferredSize(new Dimension(40, 32));
197:
198: // set tooltip for the button
199: b.setTooltip(tooltipText);
200:
201: // set keyboard shortcut
202: b.setShortcut(keyCode, keyModifiers);
203:
204: }
205:
206: public void actionPerformed(ActionEvent e) {
207: Object src = e.getSource();
208: // browse button array to find the pressed button
209: for (int f = 0; f < bFunc.length; f++) {
210: if (src == bFunc[f]) {
211: // call logic to execute function f
212: logic.buttonAction(f);
213: // display result
214: tfDisplay.setText(logic.getDisplayText());
215:
216: // update button enabled state
217: for (int n = 0; n < bFunc.length; n++) {
218: if (bFunc[n] != null)
219: bFunc[n].setEnabled(logic.isEnabled(n));
220: }
221: return;
222: }
223: }
224: }
225: }
|