shine
4 years ago
3 changed files with 59 additions and 42 deletions
@ -0,0 +1,55 @@ |
|||||||
|
package com.fr.design.mainframe; |
||||||
|
|
||||||
|
import com.fr.design.file.TemplateTreePane; |
||||||
|
import com.fr.design.gui.itree.filetree.TemplateFileTree; |
||||||
|
import com.fr.general.ComparatorUtils; |
||||||
|
|
||||||
|
import javax.swing.tree.DefaultMutableTreeNode; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author shine |
||||||
|
* @version 10.0 |
||||||
|
* Created by shine on 2021/5/7 |
||||||
|
*/ |
||||||
|
public class JTemplateNameHelper { |
||||||
|
|
||||||
|
private static final int PREFIX_NUM = 3000; |
||||||
|
|
||||||
|
private static short currentIndex = 0;// 此变量用于多次新建模板时,让名字不重复
|
||||||
|
|
||||||
|
public static String newTemplateNameByIndex(String prefix) { |
||||||
|
// 用于获取左侧模板的文件名,如左侧已包含"WorkBook1.cpt, WorkBook12.cpt, WorkBook177.cpt"
|
||||||
|
// 那么新建的文件名将被命名为"WorkBook178.cpt",即取最大数+1
|
||||||
|
TemplateFileTree tt = TemplateTreePane.getInstance().getTemplateFileTree(); |
||||||
|
DefaultMutableTreeNode gen = (DefaultMutableTreeNode) tt.getModel().getRoot(); |
||||||
|
String[] str = new String[gen.getChildCount()]; |
||||||
|
ArrayList<String> al = new ArrayList<String>(); |
||||||
|
for (int j = 0; j < gen.getChildCount(); j++) { |
||||||
|
str[j] = gen.getChildAt(j).toString(); |
||||||
|
if (str[j].contains(prefix) && str[j].contains(".")) { |
||||||
|
for (int i = 0; i < PREFIX_NUM; i++) { |
||||||
|
if (ComparatorUtils.equals(str[j].split("[.]")[0], (prefix + i))) { |
||||||
|
al.add(str[j]); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
int[] reportNum = new int[al.size()]; |
||||||
|
for (int i = 0; i < al.size(); i++) { |
||||||
|
Pattern pattern = Pattern.compile("[" + prefix + ".]+"); |
||||||
|
String[] strs = pattern.split(al.get(i).toString()); |
||||||
|
reportNum[i] = Integer.parseInt(strs[1]); |
||||||
|
} |
||||||
|
|
||||||
|
Arrays.sort(reportNum); |
||||||
|
int idx = reportNum.length > 0 ? reportNum[reportNum.length - 1] + 1 : 1; |
||||||
|
idx = idx + currentIndex; |
||||||
|
currentIndex++; |
||||||
|
return prefix + idx; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue