Browse Source

Pull request #8547: REPORT-68277 日期控件专项优化

Merge in DESIGN/design from ~LUCIAN.CHEN/design:feature/x to feature/x

* commit '3237ffbb37022d462715174389cf4f959d215327':
  REPORT-68277 日期控件专项优化
feature/x
Lucian.Chen 3 years ago
parent
commit
8d79bf8e1d
  1. 13
      designer-form/src/main/java/com/fr/design/widget/ui/designer/DateEditorDefinePane.java
  2. 26
      designer-form/src/main/java/com/fr/design/widget/ui/designer/date/DateFormatCheck.java
  3. 34
      designer-form/src/main/java/com/fr/design/widget/ui/designer/date/DateFormatCheckManager.java
  4. 43
      designer-form/src/main/java/com/fr/design/widget/ui/designer/date/DateFormatCheckResult.java
  5. 35
      designer-form/src/main/java/com/fr/design/widget/ui/designer/date/check/DateFormatCustomCheck.java
  6. 95
      designer-form/src/main/java/com/fr/design/widget/ui/designer/date/check/DateFormatFrontCheck.java
  7. 13
      designer-realize/src/main/java/com/fr/design/widget/ui/DateEditorDefinePane.java

13
designer-form/src/main/java/com/fr/design/widget/ui/designer/DateEditorDefinePane.java

@ -15,6 +15,8 @@ import com.fr.design.layout.TableLayoutHelper;
import com.fr.design.widget.component.DateValuePane; import com.fr.design.widget.component.DateValuePane;
import com.fr.design.widget.component.UIComboBoxNoArrow; import com.fr.design.widget.component.UIComboBoxNoArrow;
import com.fr.design.widget.ui.designer.component.FormWidgetValuePane; import com.fr.design.widget.ui.designer.component.FormWidgetValuePane;
import com.fr.design.widget.ui.designer.date.DateFormatCheckManager;
import com.fr.design.widget.ui.designer.date.DateFormatCheckResult;
import com.fr.form.ui.DateEditor; import com.fr.form.ui.DateEditor;
import com.fr.stable.ArrayUtils; import com.fr.stable.ArrayUtils;
@ -172,13 +174,9 @@ public class DateEditorDefinePane extends DirectWriteEditorDefinePane<DateEditor
try { try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(text); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(text);
String sample = simpleDateFormat.format(new Date()); String sample = simpleDateFormat.format(new Date());
Color c = Color.black; DateFormatCheckResult result = DateFormatCheckManager.check(sample, text);
if (!ArrayUtils.contains(FormatField.getInstance().getDateFormatArray(), text)) { currentSamplelabel.setText(result.getSample());
sample += " " + com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_DateFormat_Custom_Warning"); currentSamplelabel.setForeground(result.getColor());
c = Color.red;
}
currentSamplelabel.setText(sample);
currentSamplelabel.setForeground(c);
} catch (Exception exp) { } catch (Exception exp) {
currentSamplelabel.setForeground(Color.red); currentSamplelabel.setForeground(Color.red);
currentSamplelabel.setText(exp.getMessage()); currentSamplelabel.setText(exp.getMessage());
@ -221,7 +219,6 @@ public class DateEditorDefinePane extends DirectWriteEditorDefinePane<DateEditor
if (text != null && text.length() > 0) { if (text != null && text.length() > 0) {
try { try {
simpleDateFormat = new SimpleDateFormat(text); simpleDateFormat = new SimpleDateFormat(text);
this.currentSamplelabel.setText(simpleDateFormat.format(new Date()));
} catch (Exception exp) { } catch (Exception exp) {
simpleDateFormat = new SimpleDateFormat(""); simpleDateFormat = new SimpleDateFormat("");
} }

26
designer-form/src/main/java/com/fr/design/widget/ui/designer/date/DateFormatCheck.java

@ -0,0 +1,26 @@
package com.fr.design.widget.ui.designer.date;
/**
* 日期控件格式检测接口
*
* @author Lucian.Chen
* @version 11.0
* Created by Lucian.Chen on 2022/4/8
*/
public interface DateFormatCheck {
/**
* 校验日期格式
* @param date 日期
* @param format 格式
* @return 是否通过
*/
boolean accept(String date, String format);
/**
* 校验结果
* @param sample 提示
* @return 校验结果
*/
DateFormatCheckResult result(String sample);
}

34
designer-form/src/main/java/com/fr/design/widget/ui/designer/date/DateFormatCheckManager.java

@ -0,0 +1,34 @@
package com.fr.design.widget.ui.designer.date;
import com.fr.design.widget.ui.designer.date.check.DateFormatCustomCheck;
import com.fr.design.widget.ui.designer.date.check.DateFormatFrontCheck;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lucian.Chen
* @version 11.0
* Created by Lucian.Chen on 2022/4/8
*/
public class DateFormatCheckManager {
private static final List<DateFormatCheck> CHECKS = new ArrayList<>();
static {
// 顺序执行
CHECKS.add(DateFormatFrontCheck.KEY);
CHECKS.add(DateFormatCustomCheck.KEY);
}
@NotNull
public static DateFormatCheckResult check(String date, String format) {
for (DateFormatCheck check : CHECKS) {
if (!check.accept(date, format)) {
return check.result(date);
}
}
return DateFormatCheckResult.create(date);
}
}

43
designer-form/src/main/java/com/fr/design/widget/ui/designer/date/DateFormatCheckResult.java

@ -0,0 +1,43 @@
package com.fr.design.widget.ui.designer.date;
import java.awt.Color;
/**
* @author Lucian.Chen
* @version 11.0
* Created by Lucian.Chen on 2022/4/8
*/
public class DateFormatCheckResult {
private String sample;
private Color color;
public static DateFormatCheckResult create(String sample) {
return create(sample, Color.BLACK);
}
public static DateFormatCheckResult create(String sample, Color color) {
return new DateFormatCheckResult(sample, color);
}
DateFormatCheckResult(String sample, Color color) {
this.sample = sample;
this.color = color;
}
public String getSample() {
return sample;
}
public void setSample(String sample) {
this.sample = sample;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}

35
designer-form/src/main/java/com/fr/design/widget/ui/designer/date/check/DateFormatCustomCheck.java

@ -0,0 +1,35 @@
package com.fr.design.widget.ui.designer.date.check;
import com.fr.data.core.FormatField;
import com.fr.design.i18n.Toolkit;
import com.fr.design.widget.ui.designer.date.DateFormatCheck;
import com.fr.design.widget.ui.designer.date.DateFormatCheckResult;
import com.fr.stable.ArrayUtils;
import java.awt.Color;
/**
* 自定义格式校验
* @author Lucian.Chen
* @version 11.0
* Created by Lucian.Chen on 2022/4/8
*/
public class DateFormatCustomCheck implements DateFormatCheck {
public static final DateFormatCustomCheck KEY = new DateFormatCustomCheck();
/**
* 不是自定义格式
*/
@Override
public boolean accept(String date, String format) {
return ArrayUtils.contains(FormatField.getInstance().getDateFormatArray(), format);
}
@Override
public DateFormatCheckResult result(String sample) {
sample += " " + Toolkit.i18nText("Fine-Design_Basic_DateFormat_Custom_Warning");
return DateFormatCheckResult.create(sample, Color.red);
}
}

95
designer-form/src/main/java/com/fr/design/widget/ui/designer/date/check/DateFormatFrontCheck.java

@ -0,0 +1,95 @@
package com.fr.design.widget.ui.designer.date.check;
import com.fr.design.i18n.Toolkit;
import com.fr.design.widget.ui.designer.date.DateFormatCheck;
import com.fr.design.widget.ui.designer.date.DateFormatCheckResult;
import com.fr.stable.AssistUtils;
import com.fr.stable.StringUtils;
import java.awt.Color;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 前端的解析方案检测
* FR.str2Date
* @author Lucian.Chen
* @version 11.0
* Created by Lucian.Chen on 2022/4/7
*/
public class DateFormatFrontCheck implements DateFormatCheck {
private static final Pattern YEAR_PATTERN_1 = Pattern.compile("y{4,}"); //yyyy
private static final Pattern YEAR_PATTERN_2 = Pattern.compile("y{2,}"); //yy
private static final Pattern MONTH_PATTERN_1 = Pattern.compile("M{4,}"); //MMMM
private static final Pattern MONTH_PATTERN_2 = Pattern.compile("M{3}"); //MMM
private static final Pattern MONTH_PATTERN_3 = Pattern.compile("M{2}"); //MM
private static final Pattern MONTH_PATTERN_4 = Pattern.compile("M"); //M
private static final Pattern MONTH_PATTERN_5 = Pattern.compile("a");
private static final Pattern DAY_PATTERN_1 = Pattern.compile("d{2,}"); //dd
private static final Pattern DAY_PATTERN_2 = Pattern.compile("d"); //d
private static final Pattern HOUR_PATTERN_1 = Pattern.compile("h+"); //hh、h
private static final Pattern HOUR_PATTERN_2 = Pattern.compile("H+"); //HH、H
private static final Pattern MINUTE_PATTERN = Pattern.compile("m{2,}"); //mm
private static final Pattern SECOND_PATTERN = Pattern.compile("s{2,}"); //ss
private static final Pattern FMT_PATTERN = Pattern.compile("%.");
public static final DateFormatFrontCheck KEY = new DateFormatFrontCheck();
/**
* 前端支持的格式
*/
@Override
public boolean accept(String date, String format) {
if (StringUtils.isBlank(date) || StringUtils.isBlank(format)) {
return false;
}
String fmt = parseFmt(format);
int fmtCount = 0;
Matcher matcher = FMT_PATTERN.matcher(fmt);
while (matcher.find()) {
fmtCount ++;
}
String[] dates = date.split("\\W+");
return dates.length == fmtCount || isSpecialFmt(fmt);
}
@Override
public DateFormatCheckResult result(String sample) {
return DateFormatCheckResult.create(Toolkit.i18nText("Fine-Design_Basic_DateFormat_Not_Support"), Color.red);
}
private boolean isSpecialFmt(String fmt) {
String lowerFmt = fmt.toLowerCase();
return AssistUtils.equals(lowerFmt, "%y%x") || AssistUtils.equals(lowerFmt, "%y%x%d");
}
private String parseFmt(String fmt) {
//年
fmt = YEAR_PATTERN_1.matcher(fmt).replaceAll("%Y");
fmt = YEAR_PATTERN_2.matcher(fmt).replaceAll("%y");
//月
fmt = MONTH_PATTERN_1.matcher(fmt).replaceAll("%b");
fmt = MONTH_PATTERN_2.matcher(fmt).replaceAll("%B");
fmt = MONTH_PATTERN_3.matcher(fmt).replaceAll("%X");
fmt = MONTH_PATTERN_4.matcher(fmt).replaceAll("%x");
fmt = MONTH_PATTERN_5.matcher(fmt).replaceAll("%p");
//天
Matcher dayMatcher = DAY_PATTERN_1.matcher(fmt);
if (dayMatcher.find()) {
fmt = dayMatcher.replaceAll("%d");
} else {
fmt = DAY_PATTERN_2.matcher(fmt).replaceAll("%e");
}
//时
fmt = HOUR_PATTERN_1.matcher(fmt).replaceAll("%I");
fmt = HOUR_PATTERN_2.matcher(fmt).replaceAll("%H");
//分
fmt = MINUTE_PATTERN.matcher(fmt).replaceAll("%M");
//秒
fmt = SECOND_PATTERN.matcher(fmt).replaceAll("%S");
return fmt;
}
}

13
designer-realize/src/main/java/com/fr/design/widget/ui/DateEditorDefinePane.java

@ -13,6 +13,8 @@ import com.fr.design.layout.TableLayout;
import com.fr.design.layout.TableLayoutHelper; import com.fr.design.layout.TableLayoutHelper;
import com.fr.design.widget.component.DateValuePane; import com.fr.design.widget.component.DateValuePane;
import com.fr.design.widget.component.UIComboBoxNoArrow; import com.fr.design.widget.component.UIComboBoxNoArrow;
import com.fr.design.widget.ui.designer.date.DateFormatCheckManager;
import com.fr.design.widget.ui.designer.date.DateFormatCheckResult;
import com.fr.form.ui.DateEditor; import com.fr.form.ui.DateEditor;
import com.fr.stable.ArrayUtils; import com.fr.stable.ArrayUtils;
@ -164,13 +166,9 @@ public class DateEditorDefinePane extends DirectWriteEditorDefinePane<DateEditor
try { try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(text); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(text);
String sample = simpleDateFormat.format(new Date()); String sample = simpleDateFormat.format(new Date());
Color c = Color.black; DateFormatCheckResult result = DateFormatCheckManager.check(sample, text);
if (!ArrayUtils.contains(FormatField.getInstance().getDateFormatArray(), text)) { currentSamplelabel.setText(result.getSample());
sample += " " + com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_DateFormat_Custom_Warning"); currentSamplelabel.setForeground(result.getColor());
c = Color.red;
}
currentSamplelabel.setText(sample);
currentSamplelabel.setForeground(c);
} catch (Exception exp) { } catch (Exception exp) {
currentSamplelabel.setForeground(Color.red); currentSamplelabel.setForeground(Color.red);
currentSamplelabel.setText(exp.getMessage()); currentSamplelabel.setText(exp.getMessage());
@ -222,7 +220,6 @@ public class DateEditorDefinePane extends DirectWriteEditorDefinePane<DateEditor
if (text != null && text.length() > 0) { if (text != null && text.length() > 0) {
try { try {
simpleDateFormat = new SimpleDateFormat(text); simpleDateFormat = new SimpleDateFormat(text);
this.currentSamplelabel.setText(simpleDateFormat.format(new Date()));
} catch (Exception exp) { } catch (Exception exp) {
simpleDateFormat = new SimpleDateFormat(""); simpleDateFormat = new SimpleDateFormat("");
} }

Loading…
Cancel
Save