Browse Source

REPORT-38555 fix:数字控件中内容可以输入空格和减号

security/10.0
Destiny.Lin 2 years ago
parent
commit
878470d77d
  1. 17
      designer-base/src/main/java/com/fr/design/gui/ispinner/UISpinner.java
  2. 12
      designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java
  3. 25
      designer-base/src/main/java/com/fr/design/widget/component/NumberEditorValidatePane.java
  4. 44
      designer-base/src/test/java/com/fr/design/gui/itextfield/UINumberFieldTest.java

17
designer-base/src/main/java/com/fr/design/gui/ispinner/UISpinner.java

@ -22,19 +22,8 @@ import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ButtonUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.*;
import java.awt.event.*;
public class UISpinner extends JPanel implements UIObserver, GlobalNameObserver {
@ -72,7 +61,6 @@ public class UISpinner extends JPanel implements UIObserver, GlobalNameObserver
init(minValue, maxValue, dierta);
textField.setValue(defaultValue);
}
protected void init(double minValue, double maxValue, double dierta) {
this.minValue = minValue;
this.maxValue = maxValue;
@ -411,7 +399,6 @@ public class UISpinner extends JPanel implements UIObserver, GlobalNameObserver
public boolean shouldResponseNameListener() {
return true;
}
/**
* 程序入口 测试
*

12
designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java

@ -4,6 +4,8 @@ import com.fr.base.Utils;
import com.fr.general.ComparatorUtils;
import com.fr.stable.CoreConstants;
import com.fr.stable.StringUtils;
import org.gradle.internal.impldep.org.junit.Test;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
@ -164,10 +166,16 @@ public class UINumberField extends UITextField {
|| ComparatorUtils.equals(s, "D")
|| ComparatorUtils.equals(s, "d")
|| (ComparatorUtils.equals(str.trim(), "0") && !ComparatorUtils.equals(s.substring(0, 1), ".") && offset != 0)// 第一位是0时,第二位只能为小数点
|| (ComparatorUtils.equals(s, ".") && maxDecimalLength == 0));
|| (ComparatorUtils.equals(s, ".") && maxDecimalLength == 0))
|| s.contains(" ")//不允许空格
|| (ComparatorUtils.equals(s.substring(0, 1),"-") && offset != 0)//负号只允许出现在第一位
|| (ComparatorUtils.equals(s.substring(0, 1),".") && offset == 0)//小数点不能在第一位
|| s.contains("-.")
|| s.contains(".-")//不能包含-.或.-非法格式
|| (str.startsWith("-") && ComparatorUtils.equals(s.substring(0, 1), ".") && offset == 1 )//负号不能接小数点
|| (s.contains("-") && !ComparatorUtils.equals(s.substring(0,1), "-"));//输入的字符串如果包含负号,负号必须在第一位
}
public void insertString(int offset, String s, AttributeSet a) throws BadLocationException {
String str = getText(0, getLength());

25
designer-base/src/main/java/com/fr/design/widget/component/NumberEditorValidatePane.java

@ -21,11 +21,16 @@ import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by kerry on 2017/9/10.
@ -102,8 +107,20 @@ public class NumberEditorValidatePane extends JPanel {
int[][] rowCount = {{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}};
JPanel panel = TableLayoutHelper.createGapTableLayoutPane(components, rowSize, columnSize, rowCount, IntervalConstants.INTERVAL_L2, IntervalConstants.INTERVAL_L1);
this.add(panel);
decimalLength.getTextField().addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
// Digit only
if (isDigit(e)) {
} else {
e.consume();
}
}
});
}
private boolean isDigit(KeyEvent e){
return e.getKeyChar() >= KeyEvent.VK_0 && e.getKeyChar() <= KeyEvent.VK_9;
}
private void initErrorMsgPane() {
TextFieldAdapterProvider provider = ExtraDesignClassManager.getInstance().getSingle(TextFieldAdapterProvider.XML_TAG);
if (provider != null) {

44
designer-base/src/test/java/com/fr/design/gui/itextfield/UINumberFieldTest.java

@ -0,0 +1,44 @@
package com.fr.design.gui.itextfield;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Destiny.Lin
* @version 10.0
* created by Destiny.Lin on 2022-07-11
*/
public class UINumberFieldTest {
@Test
public void testUINumberFieldTest(){
UINumberField uiNumberField = new UINumberField();
uiNumberField.setFieldDocument();
//异常输入测试
uiNumberField.setText("-.1");
Assert.assertEquals("",uiNumberField.getText());
uiNumberField.setText(".-1");
Assert.assertEquals("",uiNumberField.getText());
uiNumberField.setText("1-");
Assert.assertEquals("",uiNumberField.getText());
uiNumberField.setText("1-1");
Assert.assertEquals("",uiNumberField.getText());
uiNumberField.setText("1 ");
Assert.assertEquals("",uiNumberField.getText());
uiNumberField.setText(".1");
Assert.assertEquals("",uiNumberField.getText());
uiNumberField.setText("1 -");
Assert.assertEquals("",uiNumberField.getText());
//正常输入测试
uiNumberField.setText("0.1");
Assert.assertEquals("0.1",uiNumberField.getText());
uiNumberField.setText("1");
Assert.assertEquals("1",uiNumberField.getText());
uiNumberField.setText("-1.5");
Assert.assertEquals("-1.5",uiNumberField.getText());
uiNumberField.setText("-123.123");
Assert.assertEquals("-123.123",uiNumberField.getText());
}
}
Loading…
Cancel
Save