Browse Source

REPORT-56906&REPORT-56926 自定义一个输入框,只支持输入整数

feature/10.0
方磊 3 years ago
parent
commit
31cf103296
  1. 4
      designer-base/src/main/java/com/fr/design/gui/ispinner/UnsignedIntUISpinner.java
  2. 59
      designer-base/src/main/java/com/fr/design/gui/itextfield/UIIntNumberField.java
  3. 61
      designer-base/src/main/java/com/fr/design/gui/itextfield/UIPositiveIntNumberField.java
  4. 4
      designer-base/src/main/java/com/fr/design/mainframe/mobile/ui/MobileCollapsedStyleExpandPane.java
  5. 4
      designer-realize/src/main/java/com/fr/design/report/freeze/RowSpinner.java
  6. 4
      designer-realize/src/main/java/com/fr/design/webattr/printsettings/GlobalNativePrintSettingPane.java

4
designer-base/src/main/java/com/fr/design/gui/ispinner/UnsignedIntUISpinner.java

@ -1,6 +1,6 @@
package com.fr.design.gui.ispinner;
import com.fr.design.gui.itextfield.UIIntNumberField;
import com.fr.design.gui.itextfield.UIPositiveIntNumberField;
import com.fr.design.gui.itextfield.UINumberField;
@ -29,7 +29,7 @@ public class UnsignedIntUISpinner extends UISpinner {
@Override
protected UINumberField initNumberField() {
return new UIIntNumberField() {
return new UIPositiveIntNumberField() {
public boolean shouldResponseChangeListener() {
return false;
}

59
designer-base/src/main/java/com/fr/design/gui/itextfield/UIIntNumberField.java

@ -1,38 +1,32 @@
package com.fr.design.gui.itextfield;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.Toolkit;
/**
* Created with IntelliJ IDEA.
* User: Lenovo
* Date: 13-3-29
* Time: 下午12:02
* To change this template use File | Settings | File Templates.
*/
public class UIIntNumberField extends UINumberField {
private static final long serialVersionUID = 4946379346015964509L;
public void setFieldDocument() {
setDocument(createNumberDocument());
}
public class NumberDocument extends PlainDocument {
private static final long serialVersionUID = 1024213269275179172L;
public NumberDocument() {
}
public void insertString(int offset, String s, AttributeSet a) throws BadLocationException {
String str = getText(0, getLength());
public boolean checkString(int offset, String s, String str) {
String strNew = str.substring(0, offset) + s + str.substring(offset, getLength());
return isMinusSignOnly(strNew) || (isIntNumber(strNew) && isInputIllegalNumber(strNew) && !isOverMaxOrMinValue(strNew));
if (!s.matches("^[0-9]+$")) {
Toolkit.getDefaultToolkit().beep();
return;
}
String strNew = str.substring(0, offset) + s + str.substring(offset, getLength());
if (isOverMaxOrMinValue(strNew) && !isContinueInsertWhenOverMaxOrMinValue()) {
public void insertString(int offset, String s, AttributeSet a) throws BadLocationException {
String str = getText(0, getLength());
if (!checkString(offset, s, str)) {
Toolkit.getDefaultToolkit().beep();
return;
}
@ -41,17 +35,38 @@ public class UIIntNumberField extends UINumberField {
super.insertString(offset, s, a);
}
private boolean isMinusSignOnly(String s) {
return s.contains("-") && s.length() == 1;
}
/**
* 是否继续插入输入的字符 - 当超出范围时
*
* @return true : 继续插入输入的字符
* 输入字符是否是不合法数字
* @param s 输入的字符串
* @return 是否不合法
*/
public boolean isContinueInsertWhenOverMaxOrMinValue() {
private boolean isInputIllegalNumber(String s) {
try {
Integer.parseInt(s);
} catch (Exception e) {
return false;
}
return true;
}
private boolean isIntNumber(String s) {
boolean result = true;
for (int i = 0; i < s.length(); i++) {
String ch = s.charAt(i) + "";
if (!ch.matches("^[0-9\\-]+$")) {
result = false;
}
}
return result;
}
private boolean isOverMaxOrMinValue( String strNew) {
return (Double.parseDouble(strNew)<getMinValue() || Double.parseDouble(strNew)>getMaxValue());
private boolean isOverMaxOrMinValue(String s) {
int value = Integer.parseInt(s);
return (value < getMinValue() || value > getMaxValue());
}
}

61
designer-base/src/main/java/com/fr/design/gui/itextfield/UIPositiveIntNumberField.java

@ -0,0 +1,61 @@
package com.fr.design.gui.itextfield;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.Toolkit;
/**
* Created with IntelliJ IDEA.
* User: Lenovo
* Date: 13-3-29
* Time: 下午12:02
* To change this template use File | Settings | File Templates.
*/
public class UIPositiveIntNumberField extends UINumberField {
public void setFieldDocument() {
setDocument(createNumberDocument());
}
public class NumberDocument extends PlainDocument {
public NumberDocument() {
}
public void insertString(int offset, String s, AttributeSet a) throws BadLocationException {
String str = getText(0, getLength());
if (!s.matches("^[0-9\\-]+$")) {
Toolkit.getDefaultToolkit().beep();
return;
}
String strNew = str.substring(0, offset) + s + str.substring(offset, getLength());
if (isOverMaxOrMinValue(strNew) && !isContinueInsertWhenOverMaxOrMinValue()) {
Toolkit.getDefaultToolkit().beep();
return;
}
setisContentChanged(true);
super.insertString(offset, s, a);
}
/**
* 是否继续插入输入的字符 - 当超出范围时
*
* @return true : 继续插入输入的字符
*/
public boolean isContinueInsertWhenOverMaxOrMinValue() {
return false;
}
private boolean isOverMaxOrMinValue( String strNew) {
return (Double.parseDouble(strNew)<getMinValue() || Double.parseDouble(strNew)>getMaxValue());
}
}
public NumberDocument createNumberDocument() {
return new NumberDocument();
}
}

4
designer-base/src/main/java/com/fr/design/mainframe/mobile/ui/MobileCollapsedStyleExpandPane.java

@ -2,7 +2,7 @@ package com.fr.design.mainframe.mobile.ui;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.ispinner.UISpinner;
import com.fr.design.gui.itextfield.UIIntNumberField;
import com.fr.design.gui.itextfield.UIPositiveIntNumberField;
import com.fr.design.gui.itextfield.UINumberField;
import com.fr.design.i18n.Toolkit;
import com.fr.design.layout.FRGUIPaneFactory;
@ -39,7 +39,7 @@ public class MobileCollapsedStyleExpandPane extends MobileCollapsedStylePane {
this.rowSpinner = new UISpinner(1, Integer.MAX_VALUE, 1, 1) {
@Override
protected UINumberField initNumberField(){
return new UIIntNumberField();
return new UIPositiveIntNumberField();
}
};
this.rowSpinner.setPreferredSize(new Dimension(62, COMPONENT_HEIGHT));

4
designer-realize/src/main/java/com/fr/design/report/freeze/RowSpinner.java

@ -1,7 +1,7 @@
package com.fr.design.report.freeze;
import com.fr.design.gui.ispinner.UISpinner;
import com.fr.design.gui.itextfield.UIIntNumberField;
import com.fr.design.gui.itextfield.UIPositiveIntNumberField;
import com.fr.design.gui.itextfield.UINumberField;
/**
@ -25,7 +25,7 @@ public class RowSpinner extends UISpinner {
@Override
protected UINumberField initNumberField(){
return new UIIntNumberField();
return new UIPositiveIntNumberField();
}

4
designer-realize/src/main/java/com/fr/design/webattr/printsettings/GlobalNativePrintSettingPane.java

@ -3,7 +3,7 @@ package com.fr.design.webattr.printsettings;
import com.fr.base.print.NativePrintAttr;
import com.fr.design.gui.icheckbox.UICheckBox;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.itextfield.UIIntNumberField;
import com.fr.design.gui.itextfield.UIPositiveIntNumberField;
import com.fr.design.gui.itextfield.UINumberField;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.design.layout.FRGUIPaneFactory;
@ -89,7 +89,7 @@ public class GlobalNativePrintSettingPane extends AbstractNativePrintSettingPane
}
private JPanel getPrintPortFieldPane() {
printPortField = new UIIntNumberField();
printPortField = new UIPositiveIntNumberField();
printPortField.setMaxValue(NativePrintAttr.MAX_PRINT_PORT_VALUE);
printPortField.setColumns(PRINT_PORT_FIELD_COLUMNS);
JPanel panel = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane();

Loading…
Cancel
Save