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