Добавлены методы для JButton и JTextField

This commit is contained in:
Victor 2019-10-06 20:18:23 +03:00
parent 0614d0fa53
commit 7775b264cb
6 changed files with 124 additions and 35 deletions

View File

@ -0,0 +1,60 @@
package com.annimon.ownlang.modules.forms;
import com.annimon.ownlang.lib.Arguments;
import static com.annimon.ownlang.lib.Converters.*;
import com.annimon.ownlang.lib.Function;
import com.annimon.ownlang.lib.FunctionValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.ValueUtils;
import javax.swing.AbstractButton;
public class AbstractButtonValue extends JComponentValue {
final AbstractButton abstractButton;
public AbstractButtonValue(int functionsCount, AbstractButton abstractButton) {
super(functionsCount + 2, abstractButton);
this.abstractButton = abstractButton;
init();
}
private void init() {
set("onClick", new FunctionValue(this::addActionListener));
set("addActionListener", new FunctionValue(this::addActionListener));
set("onChange", new FunctionValue(this::addChangeListener));
set("addChangeListener", new FunctionValue(this::addChangeListener));
set("doClick", intOptToVoid(abstractButton::doClick, abstractButton::doClick));
set("getActionCommand", voidToString(abstractButton::getActionCommand));
set("getDisplayedMnemonicIndex", voidToInt(abstractButton::getDisplayedMnemonicIndex));
set("getHideActionText", voidToBoolean(abstractButton::getHideActionText));
set("getHorizontalAlignment", voidToInt(abstractButton::getHorizontalAlignment));
set("getHorizontalTextPosition", voidToInt(abstractButton::getHorizontalTextPosition));
set("getIconTextGap", voidToInt(abstractButton::getIconTextGap));
set("getText", voidToString(abstractButton::getText));
set("getVerticalAlignment", voidToInt(abstractButton::getVerticalAlignment));
set("getVerticalTextPosition", voidToInt(abstractButton::getVerticalTextPosition));
set("isSelected", voidToBoolean(abstractButton::isSelected));
set("setActionCommand", stringToVoid(abstractButton::setActionCommand));
set("setHorizontalAlignment", intToVoid(abstractButton::setHorizontalAlignment));
set("setHorizontalTextPosition", intToVoid(abstractButton::setHorizontalTextPosition));
set("setSelected", booleanToVoid(abstractButton::setSelected));
set("setText", stringToVoid(abstractButton::setText));
set("setVerticalAlignment", intToVoid(abstractButton::setVerticalAlignment));
set("setVerticalTextPosition", intToVoid(abstractButton::setVerticalTextPosition));
}
private Value addActionListener(Value[] args) {
Arguments.check(1, args.length);
final Function action = ValueUtils.consumeFunction(args[0], 0);
abstractButton.addActionListener(e -> action.execute());
return NumberValue.ZERO;
}
private Value addChangeListener(Value[] args) {
Arguments.check(1, args.length);
final Function action = ValueUtils.consumeFunction(args[0], 0);
abstractButton.addChangeListener(e -> action.execute());
return NumberValue.ZERO;
}
}

View File

@ -77,7 +77,7 @@ public abstract class ComponentValue extends MapValue {
set("validate", voidToVoid(component::validate)); set("validate", voidToVoid(component::validate));
} }
private Value addKeyListener(Value... args) { private Value addKeyListener(Value[] args) {
Arguments.check(1, args.length); Arguments.check(1, args.length);
final Function action = ValueUtils.consumeFunction(args[0], 0); final Function action = ValueUtils.consumeFunction(args[0], 0);
component.addKeyListener(new KeyListener() { component.addKeyListener(new KeyListener() {
@ -118,7 +118,7 @@ public abstract class ComponentValue extends MapValue {
return NumberValue.ZERO; return NumberValue.ZERO;
} }
private Value getLocation(Value... args) { private Value getLocation(Value[] args) {
final Point location = component.getLocation(); final Point location = component.getLocation();
final ArrayValue result = new ArrayValue(2); final ArrayValue result = new ArrayValue(2);
result.set(0, NumberValue.of(location.x)); result.set(0, NumberValue.of(location.x));
@ -126,7 +126,7 @@ public abstract class ComponentValue extends MapValue {
return result; return result;
} }
private Value getLocationOnScreen(Value... args) { private Value getLocationOnScreen(Value[] args) {
final Point location = component.getLocationOnScreen(); final Point location = component.getLocationOnScreen();
final ArrayValue result = new ArrayValue(2); final ArrayValue result = new ArrayValue(2);
result.set(0, NumberValue.of(location.x)); result.set(0, NumberValue.of(location.x));
@ -134,7 +134,7 @@ public abstract class ComponentValue extends MapValue {
return result; return result;
} }
private Value setLocation(Value... args) { private Value setLocation(Value[] args) {
Arguments.check(2, args.length); Arguments.check(2, args.length);
component.setLocation(args[0].asInt(), args[1].asInt()); component.setLocation(args[0].asInt(), args[1].asInt());
return NumberValue.ZERO; return NumberValue.ZERO;

View File

@ -1,32 +1,17 @@
package com.annimon.ownlang.modules.forms; package com.annimon.ownlang.modules.forms;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Function;
import com.annimon.ownlang.lib.FunctionValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.ValueUtils;
import javax.swing.JButton; import javax.swing.JButton;
public class JButtonValue extends JComponentValue { public class JButtonValue extends AbstractButtonValue {
final JButton button; final JButton jButton;
public JButtonValue(JButton button) { public JButtonValue(JButton jButton) {
super(2, button); super(0, jButton);
this.button = button; this.jButton = jButton;
init(); init();
} }
private void init() { private void init() {
set("onClick", new FunctionValue(this::addActionListener));
set("addActionListener", new FunctionValue(this::addActionListener));
}
private Value addActionListener(Value... args) {
Arguments.check(1, args.length);
final Function action = ValueUtils.consumeFunction(args[0], 0);
button.addActionListener(e -> action.execute());
return NumberValue.ZERO;
} }
} }

View File

@ -8,12 +8,24 @@ public abstract class JComponentValue extends ContainerValue {
final JComponent jComponent; final JComponent jComponent;
public JComponentValue(int functionsCount, JComponent jComponent) { public JComponentValue(int functionsCount, JComponent jComponent) {
super(functionsCount + 2, jComponent); super(functionsCount + 14, jComponent);
this.jComponent = jComponent; this.jComponent = jComponent;
init(); init();
} }
private void init() { private void init() {
set("getAutoscrolls", voidToBoolean(jComponent::getAutoscrolls));
set("setAutoscrolls", booleanToVoid(jComponent::setAutoscrolls));
set("isDoubleBuffered", voidToBoolean(jComponent::isDoubleBuffered));
set("setDoubleBuffered", booleanToVoid(jComponent::setDoubleBuffered));
set("getInheritsPopupMenu", voidToBoolean(jComponent::getInheritsPopupMenu));
set("setInheritsPopupMenu", booleanToVoid(jComponent::setInheritsPopupMenu));
set("isOpaque", voidToBoolean(jComponent::isOpaque));
set("setOpaque", booleanToVoid(jComponent::setOpaque));
set("isRequestFocusEnabled", voidToBoolean(jComponent::isRequestFocusEnabled));
set("setRequestFocusEnabled", booleanToVoid(jComponent::setRequestFocusEnabled));
set("getVerifyInputWhenFocusTarget", voidToBoolean(jComponent::getVerifyInputWhenFocusTarget));
set("setVerifyInputWhenFocusTarget", booleanToVoid(jComponent::setVerifyInputWhenFocusTarget));
set("getToolTipText", voidToString(jComponent::getToolTipText)); set("getToolTipText", voidToString(jComponent::getToolTipText));
set("setToolTipText", stringToVoid(jComponent::setToolTipText)); set("setToolTipText", stringToVoid(jComponent::setToolTipText));
} }

View File

@ -0,0 +1,38 @@
package com.annimon.ownlang.modules.forms;
import static com.annimon.ownlang.lib.Converters.*;
import javax.swing.text.JTextComponent;
public class JTextComponentValue extends JComponentValue {
private final JTextComponent textComponent;
public JTextComponentValue(int functionsCount, JTextComponent textComponent) {
super(functionsCount + 20, textComponent);
this.textComponent = textComponent;
init();
}
private void init() {
set("copy", voidToVoid(textComponent::copy));
set("cut", voidToVoid(textComponent::cut));
set("getCaretPosition", voidToInt(textComponent::getCaretPosition));
set("getDragEnabled", voidToBoolean(textComponent::getDragEnabled));
set("getSelectedText", voidToString(textComponent::getSelectedText));
set("getSelectionStart", voidToInt(textComponent::getSelectionStart));
set("getSelectionEnd", voidToInt(textComponent::getSelectionEnd));
set("getText", voidToString(textComponent::getText));
set("isEditable", voidToBoolean(textComponent::isEditable));
set("moveCaretPosition", intToVoid(textComponent::moveCaretPosition));
set("paste", voidToVoid(textComponent::paste));
set("replaceSelection", stringToVoid(textComponent::replaceSelection));
set("select", int2ToVoid(textComponent::select));
set("selectAll", voidToVoid(textComponent::selectAll));
set("setCaretPosition", intToVoid(textComponent::setCaretPosition));
set("setDragEnabled", booleanToVoid(textComponent::setDragEnabled));
set("setEditable", booleanToVoid(textComponent::setEditable));
set("setSelectionStart", intToVoid(textComponent::setSelectionStart));
set("setSelectionEnd", intToVoid(textComponent::setSelectionEnd));
set("setText", stringToVoid(textComponent::setText));
}
}

View File

@ -9,12 +9,12 @@ import com.annimon.ownlang.lib.Value;
import javax.swing.JTextField; import javax.swing.JTextField;
import static com.annimon.ownlang.lib.ValueUtils.consumeFunction; import static com.annimon.ownlang.lib.ValueUtils.consumeFunction;
public class JTextFieldValue extends JComponentValue { public class JTextFieldValue extends JTextComponentValue {
private final JTextField textField; private final JTextField textField;
public JTextFieldValue(JTextField textField) { public JTextFieldValue(JTextField textField) {
super(16, textField); super(10, textField);
this.textField = textField; this.textField = textField;
init(); init();
} }
@ -22,20 +22,14 @@ public class JTextFieldValue extends JComponentValue {
private void init() { private void init() {
set("onAction", new FunctionValue(this::addActionListener)); set("onAction", new FunctionValue(this::addActionListener));
set("addActionListener", new FunctionValue(this::addActionListener)); set("addActionListener", new FunctionValue(this::addActionListener));
set("getCaretPosition", voidToInt(textField::getCaretPosition));
set("getColumns", voidToInt(textField::getColumns)); set("getColumns", voidToInt(textField::getColumns));
set("getHorizontalAlignment", voidToInt(textField::getHorizontalAlignment)); set("getHorizontalAlignment", voidToInt(textField::getHorizontalAlignment));
set("getSelectionEnd", voidToInt(textField::getSelectionEnd));
set("getSelectionStart", voidToInt(textField::getSelectionStart));
set("getScrollOffset", voidToInt(textField::getScrollOffset)); set("getScrollOffset", voidToInt(textField::getScrollOffset));
set("getText", voidToString(textField::getText)); set("postActionEvent", voidToVoid(textField::postActionEvent));
set("setCaretPosition", intToVoid(textField::setCaretPosition)); set("setActionCommand", stringToVoid(textField::setActionCommand));
set("setColumns", intToVoid(textField::setColumns)); set("setColumns", intToVoid(textField::setColumns));
set("setHorizontalAlignment", intToVoid(textField::setHorizontalAlignment)); set("setHorizontalAlignment", intToVoid(textField::setHorizontalAlignment));
set("setScrollOffset", intToVoid(textField::setScrollOffset)); set("setScrollOffset", intToVoid(textField::setScrollOffset));
set("setSelectionEnd", intToVoid(textField::setSelectionEnd));
set("setSelectionStart", intToVoid(textField::setSelectionStart));
set("setText", stringToVoid(textField::setText));
} }
private Value addActionListener(Value... args) { private Value addActionListener(Value... args) {