RCP客户端实现保存不同用户的界面状态
2009-12-21 00:00:00 来源:WEB开发网具体实施:
Java代码
package rcptest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
/**
* This class controls all aspects of the application's execution
*/
public class Application implements IApplication
{
/*
* (non-Javadoc)
*
* @seeorg.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
* IApplicationContext)
*/
public Object start(IApplicationContext context) throws Exception
{
Display display = PlatformUI.createDisplay();
try
{
//默认配置文件名
String defaultName = "workbench.xml";
//配置文件所在路径,可以根据产品插件所在路径动态获取,为了方便硬编码实现
String workBenchPath = "D:/runtime-rcptest.application/.metadata/.plugins/org.eclipse.ui.workbench/";
//路径全名称
String absolutePath = workBenchPath + defaultName;
//登录的用户名,启动第一次后尝试更换名称看效果
String loginUser = "SystemManager";
//用户专有的配置文件名称
String userCfgPath = workBenchPath + loginUser +"_"+ defaultName;
File f = new File(userCfgPath);
if (f.isFile())
{
// 文件存在则将用户的配置文件中的数据,写入到默认的配置文件中
FileInputStream fis = new FileInputStream(f);
byte[] data = new byte[fis.available()];
fis.read(data);
FileOutputStream fos = new FileOutputStream(absolutePath);
fos.write(data);
fos.close();
}
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
//工程关闭后改变配置文件名
f = new File(absolutePath);
if(f.isFile())
{
//如果默认的配置文件存在(肯定是存在的,判断一下安全),将默认的配置文件数据写入到用户配置文件中
FileInputStream fis = new FileInputStream(f);
byte[] data = new byte[fis.available()];
fis.read(data);
FileOutputStream fos = new FileOutputStream(userCfgPath);
fos.write(data);
fos.close();
//删除默认的配置文件,如果不删除则当一个没有登录过系统的用户来说,第一次登录享用的是上一次用户配置的界面
f.deleteOnExit();
}
if (returnCode == PlatformUI.RETURN_RESTART)
return IApplication.EXIT_RESTART;
else
return IApplication.EXIT_OK;
}
finally
{
display.dispose();
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.equinox.app.IApplication#stop()
*/
public void stop()
{
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null)
return;
final Display display = workbench.getDisplay();
display.syncExec(new Runnable()
{
public void run()
{
if (!display.isDisposed())
workbench.close();
}
});
}
}
更多精彩
赞助商链接