Browse Source
Merge in DESIGN/design from ~ZHENG/c-design:feature/big-screen to feature/big-screen * commit '752e500a649ad3b10633acd4ffdf6d3dd2402943': test CHART-18790 design new template namefeature/big-screen
zheng
4 years ago
4 changed files with 82 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; |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package com.fr.design.mainframe; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
/** |
||||
* @author shine |
||||
* @version 10.0 |
||||
* Created by shine on 2021/5/8 |
||||
*/ |
||||
public class JTemplateNameHelperTest extends TestCase { |
||||
|
||||
public void testNewTemplateNameByIndex() { |
||||
|
||||
String name = JTemplateNameHelper.newTemplateNameByIndex("TEST"); |
||||
|
||||
assertEquals("TEST1", name); |
||||
|
||||
String name1 = JTemplateNameHelper.newTemplateNameByIndex("TEST"); |
||||
|
||||
assertEquals("TEST2", name1); |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue