📄 builderutils.java
字号:
package com.cownew.uidesigner.builder;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import com.cownew.ctk.io.ResourceUtils;
import com.cownew.uidesigner.Activator;
import com.cownew.uidesigner.common.CodeGenUtils;
import com.cownew.uidesigner.model.Form;
public class BuilderUtils
{
/**
* 构建界面文件file
*
* @param file
* @param monitor
*/
public static void buildUI(IFile file, IProgressMonitor monitor)
{
IJavaElement javaElement = JavaCore.create(file.getParent());
// 当第二次构建的时候bin目录中的ui也会被构建一次
// 这样就造成javaElement为null了,所以需要判断一下
if ((javaElement instanceof IPackageFragment) == false)
{
return;
}
IPackageFragment pckFragment = (IPackageFragment) javaElement;
// 得到短文件名
String fileName = file.getName();
int extLen = file.getFileExtension().length() + 1;
// 去掉扩展名就得到文件对应的类名
String className = fileName.substring(0, fileName.length() - extLen);
// 构建参数
ArgInfo argInfo = new ArgInfo();
argInfo.setClassName(className);
argInfo.setPackageName(pckFragment.getElementName());
InputStream instream = null;
try
{
instream = file.getContents();
// 得到界面文件的模型对象
Form form = Form.makeFromStream(instream);
argInfo.setForm(form);
JavaCodeGenerator codeGen = new JavaCodeGenerator();
// 生成界面文件对应的代码
String code = codeGen.generate(argInfo);
// 得到界面文件对应的Java源代码文件名
IFile javaPath = uiFileToJavaFile(file);
// 将代码保存到Java源代码文件
CodeGenUtils.saveToFile(javaPath, code, monitor);
} catch (CoreException e)
{
Activator.logException(e);
} catch (IOException e)
{
Activator.logException(e);
} catch (ClassNotFoundException e)
{
Activator.logException(e);
} finally
{
ResourceUtils.close(instream);
}
}
private static IFile uiFileToJavaFile(IFile file)
{
String uiPathName = file.getName();
// 将ui后缀替换为java后缀就得到源码文件名了
// 有点不严谨,有待改进
String javaName = uiPathName.replace(".ui", ".java");
IFile pyPath = file.getParent().getFile(new Path(javaName));
return pyPath;
}
/**
* 删除界面文件file对应的Java源码文件
*
* @param file
* @param monitor
*/
public static void deleteJava(IFile file, IProgressMonitor monitor)
{
// 得到界面文件对应的Java文件
IFile javaPath = uiFileToJavaFile(file);
try
{
// 删除对应的Java文件
javaPath.delete(true, monitor);
} catch (CoreException e)
{
Activator.logException(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -