RCP作为Rich client platform胖客户端,SWT是界面常用的图形包,所以学好RCP要学好SWT
下面做一个簡單的Combo取值問題的DEMO
package com.swtdesigner;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
/**
* @author jeantian
*/
public class hellow {
/**
* @param args
*/
public static void main(String[] args) {
final Display display=Display.getDefault() ;
final Shell shell=new Shell();
shell.setSize( 327,253);
shell.setText( "myswt");
final Combo combo = new Combo(shell, SWT.READ_ONLY); //定义一个只读的下拉框
combo.setBounds(16, 11, 100, 25);
//设值按钮
final Button button1 = new Button(shell, SWT.NONE);
button1.setBounds(17, 65, 100, 25);
button1.setText("设值");
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
combo.removeAll(); //先清空combo,以防"设值"按钮多次按下时出BUG
for (int i = 1; i <= 10; i++)
//循环,赋值
combo.add("第" + i + "个字符串"); //在combo中显示的字符串
combo.select(0); //设置第一项为当前项
}
});
//取值按钮
final Button button2 = new Button(shell, SWT.NONE);
button2.setBounds(136, 66, 100, 25);
button2.setText("取值");
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// combo.getText()得到combo中当前显示的字符串
MessageDialog.openInformation(shell, null, combo.getText());
}
});
shell.layout() ;
shell.open() ;
while(!shell.isDisposed() ){
if(!display .readAndDispatch() )
display.sleep();
}
}
}
2、关于MessageDialog.java
package com.swtdesigner;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
public class MessageDialog {
// public MessageDialog(Shell parent) {
// super(parent);
// // TODO 1111111111
// }
// protected Control createDialogArea(Shell parent){
//
// MessageDialog dialog=new MessageDialog(shell);
//
// return parent;
//
// }
public static void main(String[] args) {
Display display=new Display().getDefault() ;
final Shell shell=new Shell();
shell.setSize( 500,400);
final Button button1=new Button(shell,SWT.NONE);
button1.setText( "ok");
button1.setBounds(20,30,25,25);
button1.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent e) {
// MessageBox msgbox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
// msgbox.setMessage("确认删除吗?");
// int rc = msgbox.open();
// if (rc == SWT.YES)
// System.out.println("您单击了“是”按钮");
// if (rc == SWT.NO)
// System.out.println("您单击了“否”按钮");
//
//
}
public void widgetDefaultSelected(SelectionEvent e) {
// TODO this is jean
}
});
shell.layout() ;
shell.open() ;
while(!shell.isDisposed() ){
if(!display.readAndDispatch() )
display.sleep() ;
}
}
}
3、ButtonCheck.java
package com.swtdesigner;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.MessageDialog;
public class ButtonCheck {
public static void main(String[] args) {
ButtonCheck window=new ButtonCheck();
window.open() ;
}
public void open(){
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("SWT ButtonCheck");
final Button button1 = new Button(shell, SWT.CHECK);
final Button button2 = new Button(shell, SWT.CHECK | SWT.FLAT);
final Button button3 = new Button(shell, SWT.BORDER);
final Button button4=new Button(shell,SWT.BOLD);
button1.setBounds(20, 30, 50, 50);
button1.setText("Damage");
button2.setBounds(20, 80, 50, 50);
button2.setText("Sound");
button3.setBounds(20, 150, 60, 60);
button3.setText("confirm ");
button4.setBounds(200, 150, 100, 60);
button4.setText( "Validate input TEXT");
Label label1=new Label(shell,SWT.NONE );
label1.setBounds(20, 220, 60, 60);
label1.setText( "please input:");
final Text text1=new Text(shell,SWT.BORDER);
text1.setBounds(80, 220, 60, 20);
text1.setText( "");
text1.setFocus() ;
button4.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
doValidateFormat();
}
} );
button3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (button1.getSelection() && button2.getSelection()) {
MessageDialog.openInformation(null, "", "you have selected"
+ button1.getText() + " " + button2.getText()
+" "+ "option");
} else if (button1.getSelection()) {
MessageDialog.openInformation(null, "", "you have selected"
+ button1.getText() + "option");
} else if (button2.getSelection()) {
MessageDialog.openInformation(null, "", "you have selected"
+ button2.getText() +" "+ "option");
}
else {
MessageDialog.openInformation(null, "", "you haven't selected");
}
}
4、进度表ProgressMonitor.java
package com.swtdesigner;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ProgressMonitor {
public static void main(String[] args) {
try {
ProgressMonitor window = new ProgressMonitor();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
shell.setLayout(new RowLayout());
Button button = new Button(shell, SWT.BORDER);
button.setText(" GO ");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//第一步:创建一个进度条对话框对象
ProgressMonitorDialog pmd = new ProgressMonitorDialog(shell);
//第二步:创建进度条对话框的处理过程对象
IRunnableWithProgress rwp = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("开始执行......", 10);
//10次循环,每次间隔一秒
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (Throwable e2) {
} //间隔一秒
monitor.setTaskName("完成" + (i + 1) + "%");
monitor.worked(1);//进度条前进一步
}
monitor.done();//进度条前进到底
}
};
try {
//第三步:运行对话框,第二个参数为false表示对话框上的取消按钮不可用
pmd.run(false, false, rwp);
} catch (Exception e3) {
e3.printStackTrace();
}
}
});
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
});
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
5、GridLayoutExample关于布局
package com.swtdesigner;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
public class GridLayoutExample {
public static void main(String[] args) {
final Display display=Display.getDefault() ;
final Shell shell=new Shell();
shell.setSize( 327,253);
shell.setText( "myswt");
GridLayout gridlayout=new GridLayout();
shell.setLayout(gridlayout);
new Button(shell, SWT.NONE).setText("b1");
new Button(shell, SWT.NONE).setText("button2");
Button button = new Button(shell, SWT.NONE);
GridData gridData = new GridData();//定义一个GridData对象
gridData.horizontalSpan = 2;//让button抢占两个列的空间
button.setLayoutData(gridData);
button.setText("b3");
new Button(shell, SWT.NONE).setText("button4");
new Button(shell, SWT.NONE).setText("button5");
new Combo(shell,SWT.READ_ONLY);
shell.layout() ;
shell.open() ;
while(!shell.isDisposed() ){
if(!display .readAndDispatch() )
display.sleep();
}
}
}
6、button.java
package com.swtdesigner;
import java.util.Random;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class button {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("SWT Application");
//------------------新插入的界面核心代码------------------------
//事件代码里要访问button,所以加一个final
// Random random=new Random();
// for(int i = 0; i<5;i++){
// int k = random.nextInt();
// int j = Math.abs(k % 10) ;
// System.out.print(j) ;
// }
final Text text=new Text(shell,SWT.BORDER);
final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() { //加一个选择监听器
public void widgetSelected(SelectionEvent e) {
// Random random=new Random();
// for(int i; i<5;i++){
// int k = random.nextInt();
// int j = Math.abs(k % 5) ;
// System.out.println(j) ;
// }//显示小于5的数
MessageDialog.openInformation(null, "哈吓不倒", "你单击了" + button.getText() + "按钮");
}
});
button.setBounds(50, 51, 100, 25); //设置按钮位置
button.setText("确定");//设置按钮上的文字
button.setForeground(new Color(Display.getCurrent(), 255, 255,128));
button.addMouseMoveListener(new MouseMoveListener(){
public void mouseMove(MouseEvent e) {
// Control [] children = button.getParent().getChildren();
// for (int i=0; i// Control child = children [i];
// if (child != button &&
// child instanceof Button && (child.getStyle () & SWT.NONE) != 0) {
// ((Button)child).setSelection (false);
// }
// }
// button.setSelection(true);
// button.computeSize(23,30,true);
// }
// TODO this is jean
if(button.getSelection()){
button.getStyle();
System.out.println(button.getStyle());
//button.setText("aaa");
}
}
});
//------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
.............................................................有空再写。。。。。。。
RCP
1.application.java
package jean;
import org.eclipse.core.runtime.IPlatformRunnable;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
/**
* This class controls all aspects of the application's execution
*/
public class Application implements IPlatformRunnable {
/* (non-Javadoc)
* @see org.eclipse.core.runtime.IPlatformRunnable#run(java.lang.Object)
*/
public Object run(Object args) throws Exception {
Display display = PlatformUI.createDisplay();
try {
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART) {
return IPlatformRunnable.EXIT_RESTART;
}
return IPlatformRunnable.EXIT_OK;
} finally {
display.dispose();
}
}
}
2 ApplicationWorkbenchAdvisor.java
package jean;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
private static final String PERSPECTIVE_ID = "jean.perspective";
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
return new ApplicationWorkbenchWindowAdvisor(configurer);
}
public String getInitialWindowPerspectiveId() {
return PERSPECTIVE_ID;
}
}
3 JeanPlugin.java
package jean;
import org.eclipse.ui.plugin.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;
/**
* The main plugin class to be used in the desktop.
*/
public class JeanPlugin extends AbstractUIPlugin {
//The shared instance.
private static JeanPlugin plugin;
/**
* The constructor.
*/
public JeanPlugin() {
plugin = this;
}
/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
}
/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}
/**
* Returns the shared instance.
*/
public static JeanPlugin getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path.
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("jean", path);
}
}
4 Perspective.java
package jean;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);
layout.setFixed(true);
layout.addStandaloneView(Jean.ID, false, IPageLayout.LEFT, 1.0f, editorArea);
}
}
5 ApplicationWorkbenchWindowAdvisor.java
package jean;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(180,180));
configurer.setShowCoolBar(false);
configurer.setShowStatusLine(false);
configurer.setTitle("User Login");
}
}
7 SWT I18
package jean;
import java.util.Calendar;
import java.util.Locale;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Tian extends Composite {
// private static final Locale DEFAULT_LOCALE = null;
private Text txtid;
private Text txtpwd;
private ComboViewer cmbLang;
private Button okPressed;
// public Locale locale=DEFAULT_LOCALE;
protected String baseName = "i18n.message";
public Tian(Composite parent, int style) {
super(parent, style);
initialize();
}
public void initialize() {
GridLayout layout=new GridLayout();
layout.numColumns =2;
this.setLayout( layout);
Label label=new Label(this,SWT.BORDER);
label.setText( "username");
label.setLayoutData( new GridData(GridData.CENTER));
txtid=new Text(this ,SWT.BORDER);
txtid.setLayoutData( new GridData(GridData.CENTER));
Label label2=new Label(this,SWT.BORDER);
label2.setText( "password");
label2.setLayoutData( new GridData(GridData.CENTER));
txtpwd=new Text(this,SWT.PASSWORD|SWT.BORDER);
txtpwd.setLayoutData( new GridData(GridData.CENTER));
Label label3=new Label(this,SWT.NONE);
label3.setText( "language");
/*以下是jface提供的几个类
* ComboViewer 以及它的一些方法setContentProviderr setInput setSelection setLabelProvider
* */
cmbLang=new ComboViewer(this,SWT.BORDER|SWT.READ_ONLY);
//ComboViewer设置提供的内容
cmbLang.setContentProvider(new ArrayContentProvider());
cmbLang.setInput(new Object[]{Locale.US, Locale.SIMPLIFIED_CHINESE});
// cmbLang.setInput( new Object[]{Locale.US ,Locale.TRADITIONAL_CHINESE });
cmbLang.setSelection( new StructuredSelection(Locale.US));
// 设置comboViewer 的Label 从中获得Text对象文本
cmbLang.setLabelProvider( new LabelProvider(){
public String getText(Object element){
if(element instanceof Locale){
Locale locale=(Locale) element;
String lang=locale.getLanguage();
if(lang.equals( "zh")){
lang="Chinese";
}
if(lang.equals( "en"))
{
lang="English";
}
// else{
// lang="English";
//
// }
return lang;
}
return super.getText(element);
}
});
okPressed = new Button(this,SWT.BUTTON1);
okPressed.setText( " OK ");
okPressed.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent e) {
// TODO this is jean
//
// ApplicationContext ctx=new FileSystemXmlApplicationContext("i18nContext.xml");
// System.out.println("sssssssss");
// Object[] arg = new Object[]{ "Erica", Calendar.getInstance().getTime() };
// String msg=ctx.getMessage("userinfo",arg,Locale.CHINA);
// System.out.println("Message is ===> "+msg);
Locale locale=(Locale) ((IStructuredSelection)cmbLang.getSelection()).getFirstElement();
//getFirstElement() Returns an iterator over the elements of this selection.
//Locale.setDefault(locale);//设置默认国家
Resp r = new Resp();
// System.out.println(r.getLog() .toString()) ;
String aa=locale.getLanguage() ;
String bb=locale.getCountry();
System.out.println(aa+" "+bb);
Locale localec=new Locale(aa,bb);
String str=r.getResource(localec).getString( "convert");
System.out.println(str) ;
}
public void widgetDefaultSelected(SelectionEvent e) {
// TODO this is jean
}
});
this.setSize(180, 180);
}
}
package jean;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Resp {
protected Map bundles=new HashMap();
public Locale locale = null;
protected String baseName = "i18n.message";
public Log log=LogFactory.getLog( this.getClass() );
public Log getLog(){
return log;
}
public ResourceBundle getResource(Locale locale)
{
ResourceBundle bundle;
String localeId=locale.toString() ;//toString的方法将获得的语言和国家前加上_线。如zh_CN
System.out.println("localeId="+localeId);
System.out.println("Locale.getDefault()="+Locale.getDefault().toString());
synchronized(bundles){
bundle=((ResourceBundle) bundles.get( localeId)) ;
if(bundle==null){
try{
if(locale.equals( Locale.getDefault())){
bundle=ResourceBundle.getBundle( baseName);
}
else
{
bundle=ResourceBundle.getBundle( baseName,locale);
}
//设置当前的locale值
this.locale =locale;
} catch (MissingResourceException rme)
{
bundle = ResourceBundle.getBundle(baseName);
localeId = Locale.getDefault().toString();
}
//bundles.put(localeId, bundle);Object Key and object value;
//return 是一个Object对象,Map
}
}
return bundle;
}
}