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