@ -3,8 +3,10 @@ package com.fr.design.mainframe;
import com.fr.design.file.TemplateTreePane ;
import com.fr.design.file.TemplateTreePane ;
import com.fr.design.gui.itree.filetree.TemplateFileTree ;
import com.fr.design.gui.itree.filetree.TemplateFileTree ;
import com.fr.stable.StringUtils ;
import com.fr.stable.StringUtils ;
import org.jetbrains.annotations.Nullable ;
import javax.swing.tree.DefaultMutableTreeNode ;
import javax.swing.tree.DefaultMutableTreeNode ;
import java.math.BigInteger ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.List ;
import java.util.List ;
@ -27,19 +29,18 @@ public class JTemplateNameHelper {
DefaultMutableTreeNode gen = ( DefaultMutableTreeNode ) tt . getModel ( ) . getRoot ( ) ;
DefaultMutableTreeNode gen = ( DefaultMutableTreeNode ) tt . getModel ( ) . getRoot ( ) ;
String [ ] str = new String [ gen . getChildCount ( ) ] ;
String [ ] str = new String [ gen . getChildCount ( ) ] ;
List < Integer > reportNum = new ArrayList < > ( ) ;
List < Big Integer> reportNum = new ArrayList < > ( ) ;
for ( int j = 0 ; j < gen . getChildCount ( ) ; j + + ) {
for ( int j = 0 ; j < gen . getChildCount ( ) ; j + + ) {
str [ j ] = gen . getChildAt ( j ) . toString ( ) ;
str [ j ] = gen . getChildAt ( j ) . toString ( ) ;
//返回文件名中的index(算法中没有再匹配文件后缀了,因为DefaultMutableTreeNode中已经匹配过了)
//返回文件名中的index(算法中没有再匹配文件后缀了,因为DefaultMutableTreeNode中已经匹配过了)
Integer index = getFileNameIndex ( prefix , str [ j ] ) ;
Big Integer index = getFileNameIndex ( prefix , str [ j ] ) ;
if ( index ! = null ) {
if ( index ! = null ) {
reportNum . add ( index ) ;
reportNum . add ( index ) ;
}
}
}
}
Collections . sort ( reportNum ) ;
Collections . sort ( reportNum ) ;
int idx = reportNum . size ( ) > 0 ? reportNum . get ( reportNum . size ( ) - 1 ) + 1 : 1 ;
BigInteger idx = reportNum . size ( ) > 0 ? reportNum . get ( reportNum . size ( ) - 1 ) . add ( BigInteger . valueOf ( 1 ) ) : BigInteger . valueOf ( 1 ) ;
idx = idx . add ( BigInteger . valueOf ( currentIndex ) ) ;
idx = idx + currentIndex ;
currentIndex + + ;
currentIndex + + ;
return prefix + idx ;
return prefix + idx ;
}
}
@ -52,35 +53,58 @@ public class JTemplateNameHelper {
* @Author Henry . Wang
* @Author Henry . Wang
* @Date 2021 / 4 / 9 11 : 13
* @Date 2021 / 4 / 9 11 : 13
* * /
* * /
private static Integer getFileNameIndex ( String prefix , String fileName ) {
@Nullable
if ( fileName . length ( ) < = prefix . length ( ) ) {
private static BigInteger getFileNameIndex ( String prefix , String fileName ) {
//如果文件名长度小于等于前缀长度或者匹配前缀失败,直接返回就可以了
if ( ( prefix . length ( ) > = fileName . length ( ) ) | | ( ! StringUtils . equals ( prefix , fileName . substring ( 0 , prefix . length ( ) ) ) ) ) {
return null ;
return null ;
}
}
char [ ] chars = new char [ fileName . length ( ) ] ;
BigInteger integer = null ;
int i = 0 ;
integer = matchFileNameIndex ( prefix , fileName ) ;
for ( ; i < fileName . length ( ) ; i + + ) {
return integer ;
char c = fileName . charAt ( i ) ;
//匹配前缀
if ( i < prefix . length ( ) ) {
if ( c ! = prefix . charAt ( i ) ) {
return null ;
}
}
} else {
if ( c = = '.' ) {
/ * *
* 匹配文件名称的数字后缀Index
* @param prefix 前缀
* @param fileName 文件名称全名
* @return 返回对应的数字后缀Index
* /
@Nullable
private static BigInteger matchFileNameIndex ( String prefix , String fileName ) {
StringBuilder result = new StringBuilder ( ) ;
for ( int i = prefix . length ( ) ; i < fileName . length ( ) ; i + + ) {
char c = fileName . charAt ( i ) ;
if ( isDot ( c ) ) {
break ;
break ;
} else {
} else {
//匹配0~9
if ( isNotNumber ( c ) ) {
if ( c < 48 | | c > 57 ) {
return null ;
return null ;
}
}
chars [ i - prefix . length ( ) ] = c ;
result . append ( c ) ;
}
}
}
}
}
String s = new String ( chars ) . substring ( 0 , i - prefix . length ( ) ) ;
if ( StringUtils . isBlank ( result . toString ( ) ) ) {
if ( StringUtils . isBlank ( s ) ) {
return null ;
return null ;
}
}
return Integer . valueOf ( s ) ;
return new BigInteger ( result . toString ( ) , 10 ) ;
}
/ * *
* 是否不属于数字0 - 9
* @param c 用于判断的char
* @return 返回对应判断结果
* /
private static boolean isNotNumber ( char c ) {
return c < 48 | | c > 57 ;
}
/ * *
* 是否属于 '.'
* @param c 用于判断的char
* @return 返回对应判断结果
* /
private static boolean isDot ( char c ) {
return c = = '.' ;
}
}
}
}