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.step4;
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 4 custom style sheet
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 step4");
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: // register calc style sheet
075: WingSkin.registerStyleSheet("wingcalc");
076: // load from directory skin/_common relative to class file of Calc
077: WingSkin.loadSkin("/skin/_common", true, Calc.class);
078: // load from directory skin/skinTheme relative to class file of Calc
079: WingSkin.loadSkin("/skin/" + skinTheme, true, Calc.class);
080: }
081:
082: private WingTextField tfDisplay;
083: private WingButton[] bFunc;
084:
085: private CalcLogic logic = new CalcLogic();
086: private WingCheckBox tbGreen;
087: private WingCheckBox tbOrange;
088: private WingCheckBox tbVista;
089:
090: public Calc() {
091: // build content panel
092: setLayout(new GridBagLayout());
093: bFunc = new WingButton[CalcLogic.FUNC_COUNT];
094: GridBagConstraints c = new GridBagConstraints();
095: c.fill = GridBagConstraints.BOTH;
096: c.weightx = 1.0;
097: c.weighty = 1.0;
098: c.insets = new Insets(0, 0, 0, 0);
099: c.gridx = 0;
100: c.gridy = 0;
101: c.gridheight = 1;
102: c.gridwidth = 5;
103:
104: // create panel with skin toggle buttons
105: WingPanel panel2 = new WingPanel(new GridLayout(1, 0));
106: panel2.add(tbGreen = new WingCheckBox("green", TOGGLE));
107: panel2.add(tbOrange = new WingCheckBox("orange", TOGGLE));
108: panel2.add(tbVista = new WingCheckBox("vista", TOGGLE));
109: // add buttons to radio group
110: RadioGroup g = new RadioGroup();
111: g.add(tbGreen);
112: g.add(tbOrange);
113: g.add(tbVista);
114: // select initial skin
115: tbGreen.setSelected(true);
116: //add to main panel
117: this .add(panel2, c);
118:
119: // add item listeners
120: tbGreen.addItemListener(this );
121: tbOrange.addItemListener(this );
122: tbVista.addItemListener(this );
123:
124: // add display below
125: c.insets = new Insets(6, 3, 7, 3);
126: c.gridy = 1;
127: this .add(tfDisplay = new WingTextField(), c);
128: tfDisplay.setEditable(false);
129: tfDisplay.setWingFocusable(false);
130: tfDisplay.setHorizontalAlignment(RIGHT);
131: tfDisplay.setText(logic.getDisplayText());
132: // set display style ID
133: tfDisplay.setStyleId("calc.display");
134:
135: // and buttons
136: int x, y;
137: y = 2;
138: x = 0;
139: addButton(x++, y, 1, 1, "%", "calc.small",
140: CalcLogic.FUNC_PERCENT,
141: "Percent operator\nShortcut: %", KeyEvent.VK_5,
142: KeyEvent.SHIFT_MASK);
143: addButton(x++, y, 1, 1, "Sqrt", "calc.small",
144: CalcLogic.FUNC_SQRT,
145: "Square root operator\nShortcut: Shift+S",
146: KeyEvent.VK_S, KeyEvent.SHIFT_MASK);
147: addButton(x++, y, 1, 1, "BkSp", "calc.small",
148: CalcLogic.FUNC_BACKSPACE,
149: "Backspace\nShortcut: Backspace",
150: KeyEvent.VK_BACK_SPACE, 0);
151: addButton(x++, y, 1, 1, "C", "calc", CalcLogic.FUNC_C,
152: "Clear\nShortcut: Escape", KeyEvent.VK_ESCAPE, 0);
153: addButton(x++, y, 1, 1, "CE", "calc", CalcLogic.FUNC_CE,
154: "Clear entry\nShortcut: Delete", KeyEvent.VK_DELETE, 0);
155: y++;
156: x = 0;
157: addButton(x++, y, 1, 1, "7", "calc", CalcLogic.FUNC_7,
158: "Digit 7\nShortcut: numpad 7", KeyEvent.VK_NUMPAD7, 0);
159: addButton(x++, y, 1, 1, "8", "calc", CalcLogic.FUNC_8,
160: "Digit 8\nShortcut: numpad 8", KeyEvent.VK_NUMPAD8, 0);
161: addButton(x++, y, 1, 1, "9", "calc", CalcLogic.FUNC_9,
162: "Digit 9\nShortcut: numpad 9", KeyEvent.VK_NUMPAD9, 0);
163: addButton(x++, y, 1, 2, "-", "calc", CalcLogic.FUNC_MINUS,
164: "Subtraction operator\nShortcut: numpad -",
165: KeyEvent.VK_SUBTRACT, 0);
166: addButton(x++, y, 1, 1, "+/-", "calc.small",
167: CalcLogic.FUNC_SIGN, "Sign button\nShortcut: F9",
168: KeyEvent.VK_F9, 0);
169: y++;
170: x = 0;
171: addButton(x++, y, 1, 1, "4", "calc", CalcLogic.FUNC_4,
172: "Digit 4\nShortcut: numpad 4", KeyEvent.VK_NUMPAD4, 0);
173: addButton(x++, y, 1, 1, "5", "calc", CalcLogic.FUNC_5,
174: "Digit 5\nShortcut: numpad 5", KeyEvent.VK_NUMPAD5, 0);
175: addButton(x++, y, 1, 1, "6", "calc", CalcLogic.FUNC_6,
176: "Digit 6\nShortcut: numpad 6", KeyEvent.VK_NUMPAD6, 0);
177: x++;
178: addButton(x++, y, 1, 1, "1/x", "calc.small",
179: CalcLogic.FUNC_RECIPROCAL,
180: "Reciprocal button\nShortcut: Shift+R", KeyEvent.VK_R,
181: KeyEvent.SHIFT_MASK);
182: y++;
183: x = 0;
184: addButton(x++, y, 1, 1, "1", "calc", CalcLogic.FUNC_1,
185: "Digit 1\nShortcut: numpad 1", KeyEvent.VK_NUMPAD1, 0);
186: addButton(x++, y, 1, 1, "2", "calc", CalcLogic.FUNC_2,
187: "Digit 2\nShortcut: numpad 2", KeyEvent.VK_NUMPAD2, 0);
188: addButton(x++, y, 1, 1, "3", "calc", CalcLogic.FUNC_3,
189: "Digit 3\nShortcut: numpad 3", KeyEvent.VK_NUMPAD3, 0);
190: addButton(x++, y, 1, 2, "+", "calc", CalcLogic.FUNC_PLUS,
191: "Addition operator\nShortcut: numpad +",
192: KeyEvent.VK_ADD, 0);
193: addButton(x++, y, 1, 1, "/", "calc", CalcLogic.FUNC_DIVIDE,
194: "Division operator\nShortcut: numpad /",
195: KeyEvent.VK_DIVIDE, 0);
196: y++;
197: x = 0;
198: addButton(x++, y, 2, 1, "0", "calc", CalcLogic.FUNC_0,
199: "Digit 0\nShortcut: numpad 0", KeyEvent.VK_NUMPAD0, 0);
200: x++;
201: addButton(x++, y, 1, 1, ".", "calc", CalcLogic.FUNC_PERIOD,
202: "Decimal point button\nShortcut: numpad .",
203: KeyEvent.VK_DECIMAL, 0);
204: x++;
205: addButton(x++, y, 1, 1, "*", "calc", CalcLogic.FUNC_MULTIPLY,
206: "Multiplication operator\nShortcut: numpad *",
207: KeyEvent.VK_MULTIPLY, 0);
208: y++;
209: x = 0;
210: addButton(x, y, 5, 1, "=", "calc", CalcLogic.FUNC_EQUAL,
211: "Equal button\nShortcut: Enter", KeyEvent.VK_ENTER, 0);
212:
213: }
214:
215: private void addButton(int gridx, int gridy, int gridwidth,
216: int gridheight, String text, String styleID, int func,
217: String tooltipText, int keyCode, int keyModifiers) {
218: // prepare GridBagConstraints describing size and location of the button
219: GridBagConstraints c = new GridBagConstraints();
220: c.gridx = gridx;
221: c.gridy = gridy;
222: c.gridwidth = gridwidth;
223: c.gridheight = gridheight;
224: c.insets = new Insets(0, (gridx == 0) ? 3 : 0, 3, 3);
225: c.weightx = c.weighty = 1;
226: c.fill = GridBagConstraints.BOTH;
227:
228: //create the button
229: WingButton b = new WingButton(text);
230:
231: //set button styleID
232: b.setStyleId(styleID);
233:
234: // add to button array
235: bFunc[func] = b;
236:
237: // add to panel
238: this .add(b, c);
239:
240: // add action listener
241: b.addActionListener(this );
242:
243: // make the button a big rectangle
244: b.setPreferredSize(new Dimension(40, 32));
245:
246: // set tooltip for the button
247: b.setTooltip(tooltipText);
248:
249: // set keyboard shortcut
250: b.setShortcut(keyCode, keyModifiers);
251: }
252:
253: public void actionPerformed(ActionEvent e) {
254: Object src = e.getSource();
255:
256: // browse button array to find the pressed button
257: for (int f = 0; f < bFunc.length; f++) {
258: if (src == bFunc[f]) {
259: // call logic to execute function f
260: logic.buttonAction(f);
261: // displa result
262: tfDisplay.setText(logic.getDisplayText());
263:
264: // update button enabled state
265: for (int n = 0; n < bFunc.length; n++) {
266: if (bFunc[n] != null)
267: bFunc[n].setEnabled(logic.isEnabled(n));
268: }
269: return;
270: }
271: }
272: }
273:
274: public void itemStateChanged(ItemEvent e) {
275: Object src = e.getSource();
276:
277: // check skin buttons
278: if (src == tbGreen || src == tbOrange || src == tbVista) {
279: loadCalcSkin((tbGreen.isSelected()) ? "green" : (tbOrange
280: .isSelected()) ? "orange" : "vista");
281: // update skin starting from the root pane
282: WingComponent.updateSkin(getRootPane());
283: }
284: }
285: }
|