Browse Source

REPORT-72634 设计器卡顿优化一期

feature/x
John.Ying 2 years ago
parent
commit
4367e08557
  1. 123
      designer-base/src/main/java/com/fr/design/gui/date/JDateDocument.java
  2. 2
      designer-base/src/main/java/com/fr/design/gui/date/UIDatePicker.java

123
designer-base/src/main/java/com/fr/design/gui/date/JDateDocument.java

@ -10,9 +10,8 @@ import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument; import javax.swing.text.PlainDocument;
/** /**
*
*/ */
public class JDateDocument extends PlainDocument { public class JDateDocument extends PlainDocument{
private JTextComponent textComponent; //日期输入文本框 private JTextComponent textComponent; //日期输入文本框
private SimpleDateFormat dateFormat; private SimpleDateFormat dateFormat;
@ -36,60 +35,38 @@ public class JDateDocument extends PlainDocument {
SimpleDateFormat dateFormat, SimpleDateFormat dateFormat,
String initDateTime) throws String initDateTime) throws
UnsupportedOperationException { UnsupportedOperationException {
this(tc, dateFormat, initDateTime, false);
}
/**
*
* @param tc
* @param dateFormat
* @param initDateTime
* @param initTime 判断是不是要通过insertString设置默认值
* @throws UnsupportedOperationException
*/
public JDateDocument(JTextComponent tc,
SimpleDateFormat dateFormat,
String initDateTime,
boolean initTime) throws
UnsupportedOperationException {
//设置当前日期格式 //设置当前日期格式
setDateFormat(dateFormat); setDateFormat(dateFormat);
//保存操作的文本框 //保存操作的文本框
textComponent = tc; textComponent = tc;
//设置显示为当前日期,同时完成显示的格式化,如果emptyDateTime为true就设时间为空 try {
if (!initTime) { insertString(0, initDateTime, null);
try { } catch (BadLocationException ex) {
insertString(0, initDateTime, null); throw new UnsupportedOperationException(ex.getMessage());
} catch (BadLocationException ex) {
throw new UnsupportedOperationException(ex.getMessage());
}
} }
} }
/** /**
* 设置当前日期格式 * 设置当前日期格式
*
* @param dateFormat SimpleDateFormat * @param dateFormat SimpleDateFormat
*/ */
public void setDateFormat(SimpleDateFormat dateFormat) { public void setDateFormat(SimpleDateFormat dateFormat){
this.dateFormat = dateFormat; this.dateFormat = dateFormat;
} }
/** /**
* 取得当前日期格式 * 取得当前日期格式
*
* @return SimpleDateFormat * @return SimpleDateFormat
*/ */
public SimpleDateFormat getDateFormat() { public SimpleDateFormat getDateFormat(){
return dateFormat; return dateFormat;
} }
/** /**
* 取得当前系统日时 * 取得当前系统日时
*
* @return String * @return String
*/ */
public static String getCurrentDate(SimpleDateFormat smFormat) { public static String getCurrentDate(SimpleDateFormat smFormat){
return smFormat.format(new Date()); return smFormat.format(new Date());
} }
@ -104,15 +81,15 @@ public class JDateDocument extends PlainDocument {
** 调用者类JDateDocument ** 调用者类JDateDocument
*******************************************************************/ *******************************************************************/
public void insertString(int offset, String s, public void insertString(int offset, String s,
AttributeSet attributeSet) throws BadLocationException { AttributeSet attributeSet) throws BadLocationException{
String toTest; //用于测试输入合法性的字符串 String toTest; //用于测试输入合法性的字符串
//判断插入字符串长度 //判断插入字符串长度
if (s.length() == 1) { if(s.length() == 1){
//长度为1 //长度为1
try { try{
//限制输入为整数 //限制输入为整数
Integer.parseInt(s); Integer.parseInt(s);
} catch (Exception ex) { } catch(Exception ex){
//错误则提示并返回 //错误则提示并返回
Toolkit.getDefaultToolkit().beep(); Toolkit.getDefaultToolkit().beep();
return; return;
@ -120,14 +97,14 @@ public class JDateDocument extends PlainDocument {
//取得原始插入位置 //取得原始插入位置
int newOffset = offset; int newOffset = offset;
//如果插入位置为"/"," ","-"符号的前面,则移动到其后面插入(改变newOffset的值) //如果插入位置为"/"," ","-"符号的前面,则移动到其后面插入(改变newOffset的值)
if (offset == 4 || offset == 7 || if(offset == 4 || offset == 7 ||
offset == 10 || offset == 13 || offset == 10 || offset == 13 ||
offset == 16) { offset == 16){
newOffset++; newOffset++;
textComponent.setCaretPosition(newOffset); textComponent.setCaretPosition(newOffset);
} }
//如果插入位置为最后,则不插入 //如果插入位置为最后,则不插入
if (offset == dateFormat.toPattern().length()) { if(offset == dateFormat.toPattern().length()){
return; return;
} }
//取得显示的时间,处理后得到要显示的字符串 //取得显示的时间,处理后得到要显示的字符串
@ -136,7 +113,7 @@ public class JDateDocument extends PlainDocument {
toTest.substring(newOffset + 1); toTest.substring(newOffset + 1);
//如果要显示的字符串合法,则显示,否则给出提示并退出 //如果要显示的字符串合法,则显示,否则给出提示并退出
boolean isValid = isValidDate(toTest); boolean isValid = isValidDate(toTest);
if (!isValid) { if(!isValid){
Toolkit.getDefaultToolkit().beep(); Toolkit.getDefaultToolkit().beep();
return; return;
} }
@ -145,9 +122,9 @@ public class JDateDocument extends PlainDocument {
super.insertString(newOffset, s, attributeSet); super.insertString(newOffset, s, attributeSet);
} }
//如果插入长度10 //如果插入长度10
else if (s.length() == 10 || s.length() == 19) { else if(s.length() == 10 || s.length() == 19){
//合法则显示,否则给出提示退出 //合法则显示,否则给出提示退出
if (!isValidDate(s)) { if(!isValidDate(s)){
Toolkit.getDefaultToolkit().beep(); Toolkit.getDefaultToolkit().beep();
return; return;
} }
@ -165,10 +142,10 @@ public class JDateDocument extends PlainDocument {
** 返回值 ** 返回值
** 调用者insertString(int, String,AttributeSet) ** 调用者insertString(int, String,AttributeSet)
***********************************************************************************/ ***********************************************************************************/
public void remove(int offset, int length) throws BadLocationException { public void remove(int offset, int length) throws BadLocationException{
//如果插入位置在"-"前,则回退一个光标位置 //如果插入位置在"-"前,则回退一个光标位置
//yyyy-MM-dd HH:mm:ss //yyyy-MM-dd HH:mm:ss
if (offset == 4 || offset == 7 || if(offset == 4 || offset == 7 ||
offset == 10 || offset == 13 || offset == 10 || offset == 13 ||
offset == 16) offset == 16)
textComponent.setCaretPosition(offset - 1); textComponent.setCaretPosition(offset - 1);
@ -185,32 +162,32 @@ public class JDateDocument extends PlainDocument {
** 返回值boolean型,,表示是合法的,,表示不合法 ** 返回值boolean型,,表示是合法的,,表示不合法
** 调用者insertString(int, String,AttributeSet) ** 调用者insertString(int, String,AttributeSet)
***********************************************************************************/ ***********************************************************************************/
private boolean isValidDate(String strDate) { private boolean isValidDate(String strDate){
int intY, intM, intD; //年,月,日,时,分,秒的值 int intY, intM, intD; //年,月,日,时,分,秒的值
int intH = 0, intMi = 0, intS = 0; int intH = 0, intMi = 0, intS = 0;
int iCaretPosition; //光标位置 int iCaretPosition; //光标位置
int iPatternLen = getDateFormat().toPattern().length(); int iPatternLen = getDateFormat().toPattern().length();
//获取字符串 //获取字符串
if (strDate == null) { if(strDate == null){
return false; return false;
} }
strDate = strDate.trim(); strDate = strDate.trim();
//如果为空,长度不对,则为非法,返回false //如果为空,长度不对,则为非法,返回false
if (strDate.length() != iPatternLen) { if(strDate.length() != iPatternLen){
return false; return false;
} }
//如果是全角字符,则返回false //如果是全角字符,则返回false
for (int i = 0; i < 10; i++) { for(int i = 0; i < 10; i++){
if (((int) strDate.charAt(i)) > 255) { if(((int)strDate.charAt(i)) > 255){
return false; return false;
} }
} }
//取年,月,日的值 //取年,月,日的值
try { try{
intY = Integer.parseInt(strDate.substring(0, 4)); intY = Integer.parseInt(strDate.substring(0, 4));
intM = Integer.parseInt(strDate.substring(5, 7)); intM = Integer.parseInt(strDate.substring(5, 7));
intD = Integer.parseInt(strDate.substring(8, 10)); intD = Integer.parseInt(strDate.substring(8, 10));
} catch (Exception e) { } catch(Exception e){
//失败则返回false //失败则返回false
return false; return false;
} }
@ -219,23 +196,23 @@ public class JDateDocument extends PlainDocument {
boolean isValid = true; boolean isValid = true;
//月越界 //月越界
if (intM > 12 || intM < 1) { if(intM > 12 || intM < 1){
intM = Math.min(12, Math.max(1, intM)); intM = Math.min(12, Math.max(1, intM));
isValid = false; isValid = false;
} }
//根据月份,判断日期输入,如越界,则修改 //根据月份,判断日期输入,如越界,则修改
if (intD < 1) { if(intD < 1){
intD = 1; intD = 1;
isValid = false; isValid = false;
} }
switch (intM) { switch(intM){
case 4: case 4:
case 6: case 6:
case 9: case 9:
case 11: //最大天数为30天 case 11: //最大天数为30天
//如果输入大于30,则修改为30 //如果输入大于30,则修改为30
if (intD > 30) { if(intD > 30){
intD = 30; intD = 30;
isValid = false; isValid = false;
} }
@ -243,15 +220,15 @@ public class JDateDocument extends PlainDocument {
case 2: //2月份 case 2: //2月份
//区别闰年 //区别闰年
if ((intY % 4 == 0 && intY % 100 != 0) || intY % 400 == 0) { if((intY % 4 == 0 && intY % 100 != 0) || intY % 400 == 0){
//如果输入大于29,则修改为29 //如果输入大于29,则修改为29
if (intD > 29) { if(intD > 29){
intD = 29; intD = 29;
isValid = false; isValid = false;
} }
} else { } else{
//如果输入大于28,则修改为28 //如果输入大于28,则修改为28
if (intD > 28) { if(intD > 28){
intD = 28; intD = 28;
isValid = false; isValid = false;
} }
@ -260,7 +237,7 @@ public class JDateDocument extends PlainDocument {
default: //最大天数为31天 default: //最大天数为31天
//如果输入大于31,则修改为31 //如果输入大于31,则修改为31
if (intD > 31) { if(intD > 31){
intD = 31; intD = 31;
isValid = false; isValid = false;
} }
@ -268,31 +245,31 @@ public class JDateDocument extends PlainDocument {
// System.err.println("out:intY="+intY+",intM="+intM+",intD="+intD); // System.err.println("out:intY="+intY+",intM="+intM+",intD="+intD);
//yyyy-MM-dd HH:mm:ss //yyyy-MM-dd HH:mm:ss
if (iPatternLen > 10) { if(iPatternLen > 10){
try { try{
intH = Integer.parseInt(strDate.substring(11, 13)); intH = Integer.parseInt(strDate.substring(11, 13));
intMi = Integer.parseInt(strDate.substring(14, 16)); intMi = Integer.parseInt(strDate.substring(14, 16));
intS = Integer.parseInt(strDate.substring(17)); intS = Integer.parseInt(strDate.substring(17));
} catch (Exception e) { } catch(Exception e){
return false; return false;
} }
//时越界 //时越界
if (intH > 23 || intH < 0) { if(intH > 23 || intH < 0){
intH = Math.min(23, Math.max(0, intH)); intH = Math.min(23, Math.max(0, intH));
isValid = false; isValid = false;
} }
//分越界 //分越界
if (intMi > 59 || intMi < 0) { if(intMi > 59 || intMi < 0){
intMi = Math.min(59, Math.max(0, intMi)); intMi = Math.min(59, Math.max(0, intMi));
isValid = false; isValid = false;
} }
//秒越界 //秒越界
if (intS > 59 || intS < 0) { if(intS > 59 || intS < 0){
intS = Math.min(59, Math.max(0, intS)); intS = Math.min(59, Math.max(0, intS));
isValid = false; isValid = false;
} }
} }
if (!isValid) { if(!isValid){
textComponent.setText(toDateString(intY, intM, intD, intH, intMi, textComponent.setText(toDateString(intY, intM, intD, intH, intMi,
intS)); intS));
textComponent.setCaretPosition(iCaretPosition + 1); textComponent.setCaretPosition(iCaretPosition + 1);
@ -300,11 +277,11 @@ public class JDateDocument extends PlainDocument {
return isValid; return isValid;
} }
private String toDateString(int y, int m, int d, int h, int mi, int s) { private String toDateString(int y, int m, int d, int h, int mi, int s){
m = Math.max(1, Math.min(12, m)); m = Math.max(1, Math.min(12, m));
//最大天数为31天 //最大天数为31天
d = Math.max(1, Math.min(31, d)); d = Math.max(1, Math.min(31, d));
switch (m) { switch(m){
case 4: case 4:
case 6: case 6:
case 9: case 9:
@ -314,9 +291,9 @@ public class JDateDocument extends PlainDocument {
case 2: case 2:
//润年 //润年
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) { if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0){
d = Math.min(29, d); //最大天数为29天 d = Math.min(29, d); //最大天数为29天
} else { } else{
d = Math.min(28, d); //最大天数为28天 d = Math.min(28, d); //最大天数为28天
} }
break; break;
@ -333,7 +310,7 @@ public class JDateDocument extends PlainDocument {
String strDate; String strDate;
strDate = strY + strPattern.substring(4, 5) strDate = strY + strPattern.substring(4, 5)
+ strM + strPattern.substring(7, 8) + strD; + strM + strPattern.substring(7, 8) + strD;
if (strPattern.length() == 19) { if(strPattern.length() == 19){
strDate += strPattern.substring(10, 11) strDate += strPattern.substring(10, 11)
+ rPad0(2, "" + h) + strPattern.substring(13, 14) + rPad0(2, "" + h) + strPattern.substring(13, 14)
+ rPad0(2, "" + mi) + strPattern.substring(16, 17) + rPad0(2, "" + mi) + strPattern.substring(16, 17)
@ -342,8 +319,8 @@ public class JDateDocument extends PlainDocument {
return strDate; return strDate;
} }
private String rPad0(int maxLen, String str) { private String rPad0(int maxLen, String str){
if (str.length() < maxLen) { if(str.length() < maxLen){
str = "0" + str; str = "0" + str;
} }
return str; return str;

2
designer-base/src/main/java/com/fr/design/gui/date/UIDatePicker.java

@ -92,7 +92,7 @@ public class UIDatePicker extends UIComboBox implements Serializable {
//设置编辑器属性(只能输入正确日期) //设置编辑器属性(只能输入正确日期)
JTextField textField = ((JTextField) getEditor().getEditorComponent()); JTextField textField = ((JTextField) getEditor().getEditorComponent());
if (feedbackToolboxDialog != null) { if (feedbackToolboxDialog != null) {
dateDocument = new JDateDocument(textField, this.dateFormat, StringUtils.EMPTY, false); dateDocument = new JDateDocument(textField, this.dateFormat, StringUtils.EMPTY);
this.feedbackToolboxDialog = feedbackToolboxDialog; this.feedbackToolboxDialog = feedbackToolboxDialog;
textField.setHorizontalAlignment(SwingConstants.LEFT); textField.setHorizontalAlignment(SwingConstants.LEFT);
//设置当前选择日期 //设置当前选择日期

Loading…
Cancel
Save