root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / nonSpatial / calculator / CalculatorKeysPanel.java @ 59
History | View | Annotate | Download (5.46 KB)
1 | 59 | nbrodin | package es.unex.sextante.nonSpatial.calculator; |
---|---|---|---|
2 | |||
3 | import info.clearthought.layout.TableLayout; |
||
4 | import info.clearthought.layout.TableLayoutConstants; |
||
5 | |||
6 | import java.awt.event.ActionEvent; |
||
7 | import java.awt.event.ActionListener; |
||
8 | |||
9 | import javax.swing.JButton; |
||
10 | import javax.swing.JPanel; |
||
11 | import javax.swing.JTextArea; |
||
12 | |||
13 | public class CalculatorKeysPanel |
||
14 | extends
|
||
15 | JPanel {
|
||
16 | |||
17 | private JTextArea m_TextExpression = null; |
||
18 | private JButton jButtonMinus; |
||
19 | private JButton jButtonDivide; |
||
20 | private JButton jButton2; |
||
21 | private JButton jButtonDot; |
||
22 | private JButton jButtonBrackets; |
||
23 | private JButton jButton0; |
||
24 | private JButton jButton9; |
||
25 | private JButton jButton8; |
||
26 | private JButton jButton7; |
||
27 | private JButton jButton6; |
||
28 | private JButton jButton5; |
||
29 | private JButton jButton4; |
||
30 | private JButton jButton3; |
||
31 | private JButton jButton1; |
||
32 | private JButton jButtonMultiply; |
||
33 | private JButton jButtonPlus; |
||
34 | |||
35 | |||
36 | public CalculatorKeysPanel(final JTextArea textExpression) { |
||
37 | |||
38 | super();
|
||
39 | |||
40 | m_TextExpression = textExpression; |
||
41 | |||
42 | initialize(); |
||
43 | |||
44 | } |
||
45 | |||
46 | |||
47 | private void initialize() { |
||
48 | |||
49 | final ActionListener listener = new ActionListener() { |
||
50 | public void actionPerformed(ActionEvent evt) { |
||
51 | addText(evt.getSource()); |
||
52 | } |
||
53 | }; |
||
54 | |||
55 | final ActionListener listenerBrackets = new ActionListener() { |
||
56 | public void actionPerformed(ActionEvent evt) { |
||
57 | m_TextExpression.insert(" ()", m_TextExpression.getCaretPosition());
|
||
58 | m_TextExpression.setCaretPosition(m_TextExpression.getCaretPosition() - 1);
|
||
59 | } |
||
60 | }; |
||
61 | |||
62 | final TableLayout thisLayout = new TableLayout( |
||
63 | new double[][] { |
||
64 | { TableLayoutConstants.FILL, 10.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL,
|
||
65 | TableLayoutConstants.FILL }, |
||
66 | { TableLayoutConstants.FILL, TableLayoutConstants.FILL, TableLayoutConstants.FILL, |
||
67 | TableLayoutConstants.FILL } }); |
||
68 | thisLayout.setHGap(5);
|
||
69 | thisLayout.setVGap(5);
|
||
70 | this.setLayout(thisLayout);
|
||
71 | this.setPreferredSize(new java.awt.Dimension(284, 239)); |
||
72 | { |
||
73 | jButtonPlus = new JButton(); |
||
74 | this.add(jButtonPlus, "0, 0"); |
||
75 | jButtonPlus.setText("+");
|
||
76 | jButtonPlus.addActionListener(listener); |
||
77 | } |
||
78 | { |
||
79 | jButtonMinus = new JButton(); |
||
80 | this.add(jButtonMinus, "0, 1"); |
||
81 | jButtonMinus.setText("-");
|
||
82 | jButtonMinus.addActionListener(listener); |
||
83 | } |
||
84 | { |
||
85 | jButtonMultiply = new JButton(); |
||
86 | this.add(jButtonMultiply, "0, 2"); |
||
87 | jButtonMultiply.setText("*");
|
||
88 | jButtonMultiply.addActionListener(listener); |
||
89 | } |
||
90 | { |
||
91 | jButtonDivide = new JButton(); |
||
92 | this.add(jButtonDivide, "0, 3"); |
||
93 | jButtonDivide.setText("/");
|
||
94 | jButtonDivide.addActionListener(listener); |
||
95 | } |
||
96 | { |
||
97 | jButton1 = new JButton(); |
||
98 | this.add(jButton1, "2, 2"); |
||
99 | jButton1.setText("1");
|
||
100 | jButton1.addActionListener(listener); |
||
101 | } |
||
102 | { |
||
103 | jButton2 = new JButton(); |
||
104 | this.add(jButton2, "3, 2"); |
||
105 | jButton2.setText("2");
|
||
106 | jButton2.addActionListener(listener); |
||
107 | } |
||
108 | { |
||
109 | jButton3 = new JButton(); |
||
110 | this.add(jButton3, "4, 2"); |
||
111 | jButton3.setText("3");
|
||
112 | jButton3.addActionListener(listener); |
||
113 | } |
||
114 | { |
||
115 | jButton4 = new JButton(); |
||
116 | this.add(jButton4, "2, 1"); |
||
117 | jButton4.setText("4");
|
||
118 | jButton4.addActionListener(listener); |
||
119 | } |
||
120 | { |
||
121 | jButton5 = new JButton(); |
||
122 | this.add(jButton5, "3, 1"); |
||
123 | jButton5.setText("5");
|
||
124 | jButton5.addActionListener(listener); |
||
125 | } |
||
126 | { |
||
127 | jButton6 = new JButton(); |
||
128 | this.add(jButton6, "4, 1"); |
||
129 | jButton6.setText("6");
|
||
130 | jButton6.addActionListener(listener); |
||
131 | } |
||
132 | { |
||
133 | jButton7 = new JButton(); |
||
134 | this.add(jButton7, "2, 0"); |
||
135 | jButton7.setText("7");
|
||
136 | jButton7.addActionListener(listener); |
||
137 | } |
||
138 | { |
||
139 | jButton8 = new JButton(); |
||
140 | this.add(jButton8, "3, 0"); |
||
141 | jButton8.setText("8");
|
||
142 | jButton8.addActionListener(listener); |
||
143 | } |
||
144 | { |
||
145 | jButton9 = new JButton(); |
||
146 | this.add(jButton9, "4, 0"); |
||
147 | jButton9.setText("9");
|
||
148 | jButton9.addActionListener(listener); |
||
149 | } |
||
150 | { |
||
151 | jButton0 = new JButton(); |
||
152 | this.add(jButton0, "2, 3"); |
||
153 | jButton0.setText("0");
|
||
154 | jButton0.addActionListener(listener); |
||
155 | } |
||
156 | { |
||
157 | jButtonDot = new JButton(); |
||
158 | this.add(jButtonDot, "4, 3"); |
||
159 | jButtonDot.setText(".");
|
||
160 | jButtonDot.addActionListener(listener); |
||
161 | } |
||
162 | { |
||
163 | jButtonBrackets = new JButton(); |
||
164 | this.add(jButtonBrackets, "3, 3"); |
||
165 | jButtonBrackets.setText("( )");
|
||
166 | jButtonBrackets.addActionListener(listenerBrackets); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | |||
171 | private void addText(final Object source) { |
||
172 | |||
173 | if (source instanceof JButton) { |
||
174 | String s = ((JButton) source).getText(); |
||
175 | try {
|
||
176 | final int i = Integer.parseInt(s); |
||
177 | } |
||
178 | catch (final NumberFormatException e) { |
||
179 | s = " " + s + " "; |
||
180 | } |
||
181 | m_TextExpression.insert(s, m_TextExpression.getCaretPosition()); |
||
182 | } |
||
183 | |||
184 | } |
||
185 | |||
186 | } |