当前页面: 开发资料首页 → Eclipse 专题 → XSWT for Eclipse form layout
摘要: XSWT for Eclipse form layout
XSWT is an XML-based GUI-description language for SWT. The XSWT engine uses Java reflection to actually construct a user interface, so it is automatically compatible with current and future SWT controls. An eclipse editor for XSWT files is included.
现在开发的产品中用户界面也是用XML定义的,但是界面定义很不灵活。XSWT网站上介绍说XSWT是和SWT元素一一对应的,应该有很大的灵活性。
xswt的下载地址是:http://xswt.sourceforge.net/cgi-bin/xswt/download
也可以使用Eclipse的Update Manager从http://xswt.sourceforge.net/updates 在线更新。
下载了一个试用了一下,总体感觉还好。但是好像项目还没有开发完,文档没有。而且官方网站上说支持Groovy脚本语言的功能好像还没有提供。期待中。。。。
以下是一个我写的示例代码,演示了在XSWT中如何处理事件。
/*******************************************************************************
* $Header$
* $Revision$
* $Date$
*
*==============================================================================
*
* Copyright (c) 2001-2004 XYZ Technologies, Ltd.
* All rights reserved.
*
* Created on 2004-11-27
*******************************************************************************/
import java.io.InputStream;
import java.util.Map;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.swtworkbench.community.xswt.XSWT;
/**
* TODO此处填写 class 信息
*
* @author Yanfei (mailto:yanfei@hotmail.com)
*/
/*
* 修改历史
* $Log$
*/
public class XSWTDialogTest {
/**
*
*/
public XSWTDialogTest() {
super();
}
public static void main(String[] args) throws Exception{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
InputStream in = XSWTDialogTest.class.getResource("JavaEditorPreferencePage.xswt").openStream();
Map controls = XSWT.create(shell, in);
Button button1 = (Button) controls.get("button1");
button1.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
MessageDialog.openInformation(shell, "Message", "button1 selected");
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.setSize(600, 600);
shell.open();
while(!shell.isDisposed())
if(!display.readAndDispatch())
display.sleep();
display.dispose();
}
}
以下是定义界面的XSWT文件:
<?xml version="1.0" encoding="UTF-8"?>
以下付上XSWT邮件列表中关于event handling的对话:
<table border=0>
Hi all, I just now downloaded XSWT and took a quick look around. One thing that is not obvious to me is how to handle events. For instance, how do I handle a button selection? TIA, ted stockwell</td></tr>
On Tue, 16 Nov 2004 07:07:46 -0600 "ted stockwell"wrote: > Hi all, > > I just now downloaded XSWT and took a quick look around. > One thing that is not obvious to me is how to handle > events. > For instance, how do I handle a button selection? Ted, This is not _really_ part of XSWT, but it"s relatively easy to set up. One of the many nice features in XSWT is that you can do:
<table border=0>
<tr> <td noWrap bgColor=#dddddd>From: David J. OrmeNice to see you here, Ted! On Tuesday 16 November 2004 07:07 am, ted stockwell wrote: > I just now downloaded XSWT and took a quick look around. > One thing that is not obvious to me is how to handle events. > For instance, how do I handle a button selection? Right now, here"s how this works: In the XSWT file: In your Java code: InputStream in = getClass().getResource("bounds_example.xswt").openStream(); Map controls = XSWT.create(shell, in); Button aButton = (Button) controls.get("buttonName"); aButton.addSelectionListener(new MySelectionListener()); In other words, any SWT control with an x:id will be in the Map that XSWT.create() returns, indexed by the ID string. Then you can do whatever you want with it. This is very workable, if a bit cumbersome. There have been several proposals floated to improve this. 1) Have XSWT generate an interface with getter methods corresponding to every x:ID attribute. Have a version of XSWT.create() that returns a dynamic proxy implementing this interface that looks at the name of the method, does the Map lookup for you, and returns the appropriate object. This would eliminate the typecast and would enable the compiler to catch coding errors more effectively. 2) Something along the lines of what Jan mentioned (originally suggested by Ed Burnette in the XSWT bug report), where you have an engine that looks up event names based on the x:id or possibly based on an extended attribute initialized using x:id.eventhandlername="value" and automatically sets the event handler using Java reflection. So you could write: Map controls = XSWT.create(shell, in); EventManager.hookup(controls, aControllerObject); and have it all just done for you. 3) And then you have the XSWT to Java compiler, which provides even more interesting possibilities for event hookup because you can just generate code that does it and bypass Java reflection entirely... Again, welcome to XSWT, Ted! Best regards, Dave Orme -- Got Java? Use SWTworkbench! " target=_NEW>http://www.swtworkbench.com> PGP Public Key (for confidential communications): http://www.coconut-palm-software.com/~djo/public_key.txt</td></tr>