📄 templatefactory.java
字号:
/**
*
*/
package flow.graph.gui.graph.template;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.Vector;
import flow.graph.gui.graph.encoder.XMLEncoder;
/**
* @author Administrator
*
*/
public class TemplateFactory {
public final static String TEMPLATE_PATH = "template";
public final static String CACHE_PATH = "cache";
public final static String CACHE_FILE = "cache.xml";
public final static String FUNCTION_FILES = "function.xml";
public static Templates loadTemplate(){
System.out.println("加载模板库...");
checkCachePath();
Templates templates = new Templates();
if(!checkTemplatePath())
return templates;
String templatePath = System.getProperty("user.dir")+"/"+TEMPLATE_PATH;
File file = new File(templatePath);
if(file.exists()){//总目录
String[] files = file.list();
for(int i=0;i<files.length;i++){
String strFile = templatePath+"/"+files[i];
File tmpfile = new File(strFile);
if(tmpfile.isDirectory()){
Template template = new Template(files[i]);//创建模版
//查找子目录中的模板文件
String[] templateFile = tmpfile.list(new TemplateFileNameFilter("xml"));
for(int j=0;j<templateFile.length;j++){
//System.out.println(strFile+"/"+templateFile[j]);
int pos = templateFile[j].lastIndexOf(".");
String name = templateFile[j].substring(0, pos);
Vector v, m;
//GraphTemplate graphTemplate = new GraphTemplate(name, readGraphFile(strFile+"/"+templateFile[j]));
GraphTemplate graphTemplate = new GraphTemplate(name, strFile+"/"+templateFile[j]);
//PrintGraphCell(graphTemplate.getGraphTemplate().toArray());
template.addTemplate(graphTemplate);
}
templates.addTemplate(template);
}
}
}
return templates;
}
public static boolean checkTemplatePath(){
String templatePath = System.getProperty("user.dir")+"/"+TEMPLATE_PATH;
File file = new File(templatePath);
if(!file.exists()){
file.mkdir();
return false;
}
return true;
}
public static boolean checkCachePath(){
String templatePath = System.getProperty("user.dir")+"/"+CACHE_PATH;
File file = new File(templatePath);
if(!file.exists()){
file.mkdir();
return false;
}
return true;
}
public static Vector readGraphFile(String s){
try {
XMLDecoder dec = new XMLDecoder(new BufferedInputStream(new FileInputStream(s)));
Vector vec = (Vector)dec.readObject();
dec.close();
return vec;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static void PrintGraphCell(Object[] cell){
System.out.println("PrintGraphCell..........");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
XMLEncoder enc = new XMLEncoder(bout);
enc.writeObject(cell);
enc.close();
System.out.println(bout);
}
public static void main(String[] args){
String templatePath = System.getProperty("user.dir")+"/"+TEMPLATE_PATH;
File file = new File(templatePath);
if(file.exists()){
String[] files = file.list();
for(int i=0;i<files.length;i++){
String strFile = templatePath+"/"+files[i];
File tmpfile = new File(strFile);
if(tmpfile.isDirectory()){
//查找子目录中的模板文件
String[] templateFile = tmpfile.list(new TemplateFileNameFilter("xml"));
for(int j=0;j<templateFile.length;j++){
System.out.println(strFile+"/"+templateFile[j]);
}
}
}
}
}
public static class TemplateFileNameFilter implements FilenameFilter{
private String suffix;
public TemplateFileNameFilter(String suffix) {
this.suffix = suffix;
}
public boolean accept(File dir, String name) {
return name.endsWith(suffix) || dir.isDirectory();
}
}
public static class TemplateFileFilter implements FilenameFilter{
private String suffix;
public TemplateFileFilter(String suffix) {
this.suffix = suffix;
}
public boolean accept(File dir, String name) {
return name.endsWith(suffix) || dir.isDirectory();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -