From 434f4a0f5622140dab6dd1c2d5d50335ecf0011c Mon Sep 17 00:00:00 2001 From: kuangshuai Date: Mon, 15 Mar 2021 16:37:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?CHART-18080=20=E4=BF=AE=E5=A4=8D=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E8=BE=93=E5=85=A5=E6=A1=86=E8=BE=93=E5=85=A5=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E6=97=B6=E8=87=AA=E5=8A=A8=E5=88=A0=E9=99=A4=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../design/gui/itextfield/UINumberField.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java b/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java index f4437a9c4a..8176905fec 100644 --- a/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java +++ b/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java @@ -5,11 +5,15 @@ import com.fr.general.ComparatorUtils; import com.fr.stable.CoreConstants; import com.fr.stable.StringUtils; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; import java.awt.Dimension; import java.awt.Toolkit; +import java.awt.event.InputMethodEvent; +import java.awt.event.InputMethodListener; /** * Number Field. @@ -35,6 +39,8 @@ public class UINumberField extends UITextField { private boolean isContentChanged = false; private boolean fillNegativeNumber = true; + private boolean isInputMethodChange = false; + private int insertOffset = 0; public UINumberField() { this(MAX_INTEGERLENGTH_32, MAX_DECIMALLENGTH); @@ -55,6 +61,7 @@ public class UINumberField extends UITextField { this.minValue = minValue; this.maxValue = maxValue; setFieldDocument(); + initFieldListener(); } public void setFieldDocument() { @@ -62,6 +69,31 @@ public class UINumberField extends UITextField { initListener(); } + private void initFieldListener() { + addInputMethodListener(new InputMethodListener() { + @Override + public void inputMethodTextChanged(InputMethodEvent event) { + isInputMethodChange = true; + } + + @Override + public void caretPositionChanged(InputMethodEvent event) { + } + }); + + addCaretListener(new CaretListener() { + @Override + public void caretUpdate(CaretEvent e) { + if (isInputMethodChange) { + isInputMethodChange = false; + setCaretPosition(insertOffset); + } else { + insertOffset = e.getDot(); + } + } + }); + } + public void canFillNegativeNumber(boolean fillNegativeNumber) { this.fillNegativeNumber = fillNegativeNumber; } @@ -186,6 +218,13 @@ public class UINumberField extends UITextField { super.insertString(offset, s, a); } + public void remove(int offs, int len) throws BadLocationException { + if (isInputMethodChange) { + return; + } + super.remove(offs, len); + } + // kunsnat: 这种限制输入 有个不好的地方, 比如删除时: 10.1 最大值限定100, 那么就删除中间的小数点之后变为101, 超出了100. // 但是直接限制不能删除中间类似小数点, 那么也可能遇到: 最小值10 , 从100变化到其中的19, 就很难.. private boolean notChange(String strNew) { From e329e432ff304be65ff69f0cf06f487462b20eaf Mon Sep 17 00:00:00 2001 From: kuangshuai Date: Wed, 24 Mar 2021 17:37:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?CHART-18080=20=E5=8F=98=E9=87=8F=E5=90=8D?= =?UTF-8?q?=E6=9B=B4=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/gui/itextfield/UINumberField.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java b/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java index 8176905fec..62d24c56a4 100644 --- a/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java +++ b/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java @@ -39,7 +39,10 @@ public class UINumberField extends UITextField { private boolean isContentChanged = false; private boolean fillNegativeNumber = true; - private boolean isInputMethodChange = false; + /** + * 输入法输入完成一个字符输入过程的标记。开始输入的时候是true,输入完矫正光标后为false + */ + private boolean isInputMethodOnceInserting = false; private int insertOffset = 0; public UINumberField() { @@ -73,7 +76,7 @@ public class UINumberField extends UITextField { addInputMethodListener(new InputMethodListener() { @Override public void inputMethodTextChanged(InputMethodEvent event) { - isInputMethodChange = true; + isInputMethodOnceInserting = true; } @Override @@ -84,8 +87,8 @@ public class UINumberField extends UITextField { addCaretListener(new CaretListener() { @Override public void caretUpdate(CaretEvent e) { - if (isInputMethodChange) { - isInputMethodChange = false; + if (isInputMethodOnceInserting) { + isInputMethodOnceInserting = false; setCaretPosition(insertOffset); } else { insertOffset = e.getDot(); @@ -219,7 +222,7 @@ public class UINumberField extends UITextField { } public void remove(int offs, int len) throws BadLocationException { - if (isInputMethodChange) { + if (isInputMethodOnceInserting) { return; } super.remove(offs, len);