Skip to content

Commit 7775b26

Browse files
committed
Добавлены методы для JButton и JTextField
1 parent 0614d0f commit 7775b26

File tree

6 files changed

+124
-35
lines changed

6 files changed

+124
-35
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.annimon.ownlang.modules.forms;
2+
3+
import com.annimon.ownlang.lib.Arguments;
4+
import static com.annimon.ownlang.lib.Converters.*;
5+
import com.annimon.ownlang.lib.Function;
6+
import com.annimon.ownlang.lib.FunctionValue;
7+
import com.annimon.ownlang.lib.NumberValue;
8+
import com.annimon.ownlang.lib.Value;
9+
import com.annimon.ownlang.lib.ValueUtils;
10+
import javax.swing.AbstractButton;
11+
12+
public class AbstractButtonValue extends JComponentValue {
13+
14+
final AbstractButton abstractButton;
15+
16+
public AbstractButtonValue(int functionsCount, AbstractButton abstractButton) {
17+
super(functionsCount + 2, abstractButton);
18+
this.abstractButton = abstractButton;
19+
init();
20+
}
21+
22+
private void init() {
23+
set("onClick", new FunctionValue(this::addActionListener));
24+
set("addActionListener", new FunctionValue(this::addActionListener));
25+
set("onChange", new FunctionValue(this::addChangeListener));
26+
set("addChangeListener", new FunctionValue(this::addChangeListener));
27+
set("doClick", intOptToVoid(abstractButton::doClick, abstractButton::doClick));
28+
set("getActionCommand", voidToString(abstractButton::getActionCommand));
29+
set("getDisplayedMnemonicIndex", voidToInt(abstractButton::getDisplayedMnemonicIndex));
30+
set("getHideActionText", voidToBoolean(abstractButton::getHideActionText));
31+
set("getHorizontalAlignment", voidToInt(abstractButton::getHorizontalAlignment));
32+
set("getHorizontalTextPosition", voidToInt(abstractButton::getHorizontalTextPosition));
33+
set("getIconTextGap", voidToInt(abstractButton::getIconTextGap));
34+
set("getText", voidToString(abstractButton::getText));
35+
set("getVerticalAlignment", voidToInt(abstractButton::getVerticalAlignment));
36+
set("getVerticalTextPosition", voidToInt(abstractButton::getVerticalTextPosition));
37+
set("isSelected", voidToBoolean(abstractButton::isSelected));
38+
set("setActionCommand", stringToVoid(abstractButton::setActionCommand));
39+
set("setHorizontalAlignment", intToVoid(abstractButton::setHorizontalAlignment));
40+
set("setHorizontalTextPosition", intToVoid(abstractButton::setHorizontalTextPosition));
41+
set("setSelected", booleanToVoid(abstractButton::setSelected));
42+
set("setText", stringToVoid(abstractButton::setText));
43+
set("setVerticalAlignment", intToVoid(abstractButton::setVerticalAlignment));
44+
set("setVerticalTextPosition", intToVoid(abstractButton::setVerticalTextPosition));
45+
}
46+
47+
private Value addActionListener(Value[] args) {
48+
Arguments.check(1, args.length);
49+
final Function action = ValueUtils.consumeFunction(args[0], 0);
50+
abstractButton.addActionListener(e -> action.execute());
51+
return NumberValue.ZERO;
52+
}
53+
54+
private Value addChangeListener(Value[] args) {
55+
Arguments.check(1, args.length);
56+
final Function action = ValueUtils.consumeFunction(args[0], 0);
57+
abstractButton.addChangeListener(e -> action.execute());
58+
return NumberValue.ZERO;
59+
}
60+
}

src/main/java/com/annimon/ownlang/modules/forms/ComponentValue.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void init() {
7777
set("validate", voidToVoid(component::validate));
7878
}
7979

80-
private Value addKeyListener(Value... args) {
80+
private Value addKeyListener(Value[] args) {
8181
Arguments.check(1, args.length);
8282
final Function action = ValueUtils.consumeFunction(args[0], 0);
8383
component.addKeyListener(new KeyListener() {
@@ -118,23 +118,23 @@ private void handleKeyEvent(String type, final KeyEvent e) {
118118
return NumberValue.ZERO;
119119
}
120120

121-
private Value getLocation(Value... args) {
121+
private Value getLocation(Value[] args) {
122122
final Point location = component.getLocation();
123123
final ArrayValue result = new ArrayValue(2);
124124
result.set(0, NumberValue.of(location.x));
125125
result.set(1, NumberValue.of(location.y));
126126
return result;
127127
}
128128

129-
private Value getLocationOnScreen(Value... args) {
129+
private Value getLocationOnScreen(Value[] args) {
130130
final Point location = component.getLocationOnScreen();
131131
final ArrayValue result = new ArrayValue(2);
132132
result.set(0, NumberValue.of(location.x));
133133
result.set(1, NumberValue.of(location.y));
134134
return result;
135135
}
136136

137-
private Value setLocation(Value... args) {
137+
private Value setLocation(Value[] args) {
138138
Arguments.check(2, args.length);
139139
component.setLocation(args[0].asInt(), args[1].asInt());
140140
return NumberValue.ZERO;
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
package com.annimon.ownlang.modules.forms;
22

3-
import com.annimon.ownlang.lib.Arguments;
4-
import com.annimon.ownlang.lib.Function;
5-
import com.annimon.ownlang.lib.FunctionValue;
6-
import com.annimon.ownlang.lib.NumberValue;
7-
import com.annimon.ownlang.lib.Value;
8-
import com.annimon.ownlang.lib.ValueUtils;
93
import javax.swing.JButton;
104

11-
public class JButtonValue extends JComponentValue {
5+
public class JButtonValue extends AbstractButtonValue {
126

13-
final JButton button;
7+
final JButton jButton;
148

15-
public JButtonValue(JButton button) {
16-
super(2, button);
17-
this.button = button;
9+
public JButtonValue(JButton jButton) {
10+
super(0, jButton);
11+
this.jButton = jButton;
1812
init();
1913
}
2014

2115
private void init() {
22-
set("onClick", new FunctionValue(this::addActionListener));
23-
set("addActionListener", new FunctionValue(this::addActionListener));
24-
}
25-
26-
private Value addActionListener(Value... args) {
27-
Arguments.check(1, args.length);
28-
final Function action = ValueUtils.consumeFunction(args[0], 0);
29-
button.addActionListener(e -> action.execute());
30-
return NumberValue.ZERO;
3116
}
3217
}

src/main/java/com/annimon/ownlang/modules/forms/JComponentValue.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@ public abstract class JComponentValue extends ContainerValue {
88
final JComponent jComponent;
99

1010
public JComponentValue(int functionsCount, JComponent jComponent) {
11-
super(functionsCount + 2, jComponent);
11+
super(functionsCount + 14, jComponent);
1212
this.jComponent = jComponent;
1313
init();
1414
}
1515

1616
private void init() {
17+
set("getAutoscrolls", voidToBoolean(jComponent::getAutoscrolls));
18+
set("setAutoscrolls", booleanToVoid(jComponent::setAutoscrolls));
19+
set("isDoubleBuffered", voidToBoolean(jComponent::isDoubleBuffered));
20+
set("setDoubleBuffered", booleanToVoid(jComponent::setDoubleBuffered));
21+
set("getInheritsPopupMenu", voidToBoolean(jComponent::getInheritsPopupMenu));
22+
set("setInheritsPopupMenu", booleanToVoid(jComponent::setInheritsPopupMenu));
23+
set("isOpaque", voidToBoolean(jComponent::isOpaque));
24+
set("setOpaque", booleanToVoid(jComponent::setOpaque));
25+
set("isRequestFocusEnabled", voidToBoolean(jComponent::isRequestFocusEnabled));
26+
set("setRequestFocusEnabled", booleanToVoid(jComponent::setRequestFocusEnabled));
27+
set("getVerifyInputWhenFocusTarget", voidToBoolean(jComponent::getVerifyInputWhenFocusTarget));
28+
set("setVerifyInputWhenFocusTarget", booleanToVoid(jComponent::setVerifyInputWhenFocusTarget));
1729
set("getToolTipText", voidToString(jComponent::getToolTipText));
1830
set("setToolTipText", stringToVoid(jComponent::setToolTipText));
1931
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.annimon.ownlang.modules.forms;
2+
3+
import static com.annimon.ownlang.lib.Converters.*;
4+
import javax.swing.text.JTextComponent;
5+
6+
public class JTextComponentValue extends JComponentValue {
7+
8+
private final JTextComponent textComponent;
9+
10+
public JTextComponentValue(int functionsCount, JTextComponent textComponent) {
11+
super(functionsCount + 20, textComponent);
12+
this.textComponent = textComponent;
13+
init();
14+
}
15+
16+
private void init() {
17+
set("copy", voidToVoid(textComponent::copy));
18+
set("cut", voidToVoid(textComponent::cut));
19+
set("getCaretPosition", voidToInt(textComponent::getCaretPosition));
20+
set("getDragEnabled", voidToBoolean(textComponent::getDragEnabled));
21+
set("getSelectedText", voidToString(textComponent::getSelectedText));
22+
set("getSelectionStart", voidToInt(textComponent::getSelectionStart));
23+
set("getSelectionEnd", voidToInt(textComponent::getSelectionEnd));
24+
set("getText", voidToString(textComponent::getText));
25+
set("isEditable", voidToBoolean(textComponent::isEditable));
26+
set("moveCaretPosition", intToVoid(textComponent::moveCaretPosition));
27+
set("paste", voidToVoid(textComponent::paste));
28+
set("replaceSelection", stringToVoid(textComponent::replaceSelection));
29+
set("select", int2ToVoid(textComponent::select));
30+
set("selectAll", voidToVoid(textComponent::selectAll));
31+
set("setCaretPosition", intToVoid(textComponent::setCaretPosition));
32+
set("setDragEnabled", booleanToVoid(textComponent::setDragEnabled));
33+
set("setEditable", booleanToVoid(textComponent::setEditable));
34+
set("setSelectionStart", intToVoid(textComponent::setSelectionStart));
35+
set("setSelectionEnd", intToVoid(textComponent::setSelectionEnd));
36+
set("setText", stringToVoid(textComponent::setText));
37+
}
38+
}

src/main/java/com/annimon/ownlang/modules/forms/JTextFieldValue.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,27 @@
99
import javax.swing.JTextField;
1010
import static com.annimon.ownlang.lib.ValueUtils.consumeFunction;
1111

12-
public class JTextFieldValue extends JComponentValue {
12+
public class JTextFieldValue extends JTextComponentValue {
1313

1414
private final JTextField textField;
1515

1616
public JTextFieldValue(JTextField textField) {
17-
super(16, textField);
17+
super(10, textField);
1818
this.textField = textField;
1919
init();
2020
}
2121

2222
private void init() {
2323
set("onAction", new FunctionValue(this::addActionListener));
2424
set("addActionListener", new FunctionValue(this::addActionListener));
25-
set("getCaretPosition", voidToInt(textField::getCaretPosition));
2625
set("getColumns", voidToInt(textField::getColumns));
2726
set("getHorizontalAlignment", voidToInt(textField::getHorizontalAlignment));
28-
set("getSelectionEnd", voidToInt(textField::getSelectionEnd));
29-
set("getSelectionStart", voidToInt(textField::getSelectionStart));
3027
set("getScrollOffset", voidToInt(textField::getScrollOffset));
31-
set("getText", voidToString(textField::getText));
32-
set("setCaretPosition", intToVoid(textField::setCaretPosition));
28+
set("postActionEvent", voidToVoid(textField::postActionEvent));
29+
set("setActionCommand", stringToVoid(textField::setActionCommand));
3330
set("setColumns", intToVoid(textField::setColumns));
3431
set("setHorizontalAlignment", intToVoid(textField::setHorizontalAlignment));
3532
set("setScrollOffset", intToVoid(textField::setScrollOffset));
36-
set("setSelectionEnd", intToVoid(textField::setSelectionEnd));
37-
set("setSelectionStart", intToVoid(textField::setSelectionStart));
38-
set("setText", stringToVoid(textField::setText));
3933
}
4034

4135
private Value addActionListener(Value... args) {

0 commit comments

Comments
 (0)