Eclipse SWT的简单使用记录

原创
2017/02/02 12:56
阅读数 203

SWT使用的版本为 org.eclipse.swt.win32.win32.x86_3.105.2.v20161122-0613.jar 是eclipse neon中的jar

火狐内核运行时 xulrunner-26.0b8.en-US.win32.zip

https://archive.mozilla.org/pub/xulrunner/releases/26.0b8/runtimes/xulrunner-26.0b8.en-US.win32.zip

git:https://git.oschina.net/iproject/ToyBrowser.git

定义了一个GeckoBrowser类继承Browser对一些操作实现了一些封装实现了浏览器的简单操作

package com.taomus.browser;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class GeckoBrowser extends Browser{
	private static String basePath = System.getProperty("user.dir");
	private static String xulpath= basePath+"/xulrunner";
	private static String LASTLOC = "";
	private static Browser browser;
	private static String defualUrl = "about:blank";
	private static boolean jsenabled = true;
	static{
		ResourceBundle rb = ResourceBundle.getBundle("config");
		defualUrl = rb.getString("url");
		String xulpath1 = rb.getString("xulpath");
		if(xulpath1 != null && !xulpath1.equals("")){
			xulpath = xulpath1;
		}
		System.setProperty("org.eclipse.swt.browser.XULRunnerPath", xulpath);
	}
	
	private GeckoBrowser(Composite parent, int style) {
		super(parent, style);
	}
	
	public GeckoBrowser(Composite parent){
		super(parent, SWT.MOZILLA);
		Shell shell = (Shell)parent;
		shell.setImage(SWTResourceManager.getImage(shell.getClass(), "/logo.png"));
		browser = this;
		browser.setJavascriptEnabled(jsenabled);
		Menu menu = new Menu(parent);
		MenuItem mitem = new MenuItem(menu, SWT.PUSH);
		mitem.setText("刷新");
		mitem.addListener(SWT.Selection, event->{
			MenuItem mi = (MenuItem)event.widget;
			if(mi.getText().trim().equals("刷新")){
				this.refresh();
			}
		});
		this.setMenu(menu);
		init();
	}

	
	private void init(){
		this.setUrl(defualUrl);
		this.addCloseWindowListener(event -> {
			Browser browser1 = (Browser) event.widget;
			Shell shell = browser1.getShell();
			shell.close();
		});
		this.addTitleListener(event->{
			Browser browser1 = (Browser) event.widget;
			browser1.getShell().setText(event.title);
		});
		this.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent arg0) {}
			
			@Override
			public void mouseDown(MouseEvent arg0) {
				if(arg0.button==1&&(LASTLOC.startsWith("http://")||LASTLOC.startsWith("https://"))){
					browser.setUrl(LASTLOC);
				}
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {}
		});
	}
	
	public void setLeftInfo(Label label){
		this.addStatusTextListener(new StatusTextListener() {
			@Override
			public void changed(StatusTextEvent arg0) {
				String info = arg0.text;
				label.setText(info);
				LASTLOC = info;
			}
		});
	}
	
	public void go(Button btn,Scrollable dest){
		btn.setToolTipText("转到");
		btn.setText("→");
		if(dest instanceof Text){
			Text text = (Text)dest;
			this.addLocationListener(new LocationListener() {
				
				@Override
				public void changing(LocationEvent arg0) {}
				
				@Override
				public void changed(LocationEvent arg0) {
					text.setText(arg0.location);
				}
			});
			btn.addListener(SWT.Selection,event->{
				this.setUrl(text.getText());
			});
		
		}
		
	}
	
	private void browserButton(Button btn) {
		btn.addListener(SWT.Selection,event->{
			Button btn1 = (Button)event.widget;
			switch(btn1.getData().toString()){
			case "back":
				this.back();
				break;
			case "forward":
				this.forward();
				break;
			case "refresh":
				this.refresh();
				break;
			case "stop":
				this.stop();
				break;
			}
		});
	}
	
	public void back(Button btn){
		btn.setData("back");
		btn.setText("◀");
		btn.setToolTipText("上一页");
		browserButton(btn);
	}

	public void forward(Button btn){
		btn.setData("forward");
		btn.setText("▶");
		btn.setToolTipText("下一页");
		browserButton(btn);
	}
	
	public void refresh(Button btn){
		btn.setData("refresh");
		btn.setText("刷新");
		btn.setToolTipText("刷新");
		browserButton(btn);
	}
	
	public void stop(Button btn){
		btn.setData("stop");
		btn.setText("×");
		btn.setToolTipText("停止");
		browserButton(btn);
	}
	
	@Override
    protected void checkSubclass() { }
}

     

package com.taomus.browser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.wb.swt.SWTResourceManager;

public class BrowserShell extends Shell {
	private Text location;
	private Map<String, Object> objMap = new HashMap<String, Object>();

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public BrowserShell(Display display) {
		super(display, SWT.SHELL_TRIM | SWT.BORDER);
		setLayout(new FormLayout());
		GeckoBrowser browser = new GeckoBrowser(this);
		FormData fd_browser = new FormData();
		fd_browser.bottom = new FormAttachment(100, -23);
		fd_browser.top = new FormAttachment(0, 33);
		fd_browser.right = new FormAttachment(100);
		fd_browser.left = new FormAttachment(0);
		browser.setLayoutData(fd_browser);

		Button backBtn = new Button(this, SWT.NONE);
		FormData fdBackBtn = new FormData();
		fdBackBtn.top = new FormAttachment(0);
		fdBackBtn.left = new FormAttachment(0);
		backBtn.setLayoutData(fdBackBtn);
		backBtn.setText("<");
		browser.back(backBtn);

		Button forwardBtn = new Button(this, SWT.NONE);
		FormData fdForwordBtn = new FormData();
		fdForwordBtn.top = new FormAttachment(backBtn, 0, SWT.TOP);
		fdForwordBtn.left = new FormAttachment(backBtn, 6);
		forwardBtn.setLayoutData(fdForwordBtn);
		forwardBtn.setText(">");
		browser.forward(forwardBtn);

		Button btnRefresh = new Button(this, SWT.NONE);
		FormData fdBtnRefresh = new FormData();
		fdBtnRefresh.left = new FormAttachment(backBtn, 33);
		fdBtnRefresh.top = new FormAttachment(backBtn, 0, SWT.TOP);
		btnRefresh.setLayoutData(fdBtnRefresh);
		browser.refresh(btnRefresh);

		location = new Text(this, SWT.BORDER);
		FormData fdLocation = new FormData();
		fdLocation.right = new FormAttachment(100, -50);
		fdLocation.top = new FormAttachment(0, 4);
		fdLocation.left = new FormAttachment(0, 157);
		location.setLayoutData(fdLocation);
		location.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == SWT.CR) {
					browser.setUrl(location.getText());
				}
			}
		});

		Button btnGo = new Button(this, SWT.NONE);
		FormData fdBtnGo = new FormData();
		fdBtnGo.left = new FormAttachment(100, -45);
		fdBtnGo.top = new FormAttachment(0, 2);
		fdBtnGo.right = new FormAttachment(100, -10);
		btnGo.setLayoutData(fdBtnGo);
		btnGo.setText("go");
		browser.go(btnGo, location);

		Button btnStop = new Button(this, SWT.NONE);
		FormData fdBtnStop = new FormData();
		fdBtnStop.top = new FormAttachment(backBtn, 0, SWT.TOP);
		fdBtnStop.left = new FormAttachment(btnRefresh, 6);
		btnStop.setLayoutData(fdBtnStop);
		btnStop.setText("✘");
		browser.stop(btnStop);

		Label lblloc = new Label(this, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.bottom = new FormAttachment(100);
		fd_lblNewLabel.right = new FormAttachment(100, -120);
		fd_lblNewLabel.left = new FormAttachment(0);
		lblloc.setLayoutData(fd_lblNewLabel);
		lblloc.setText("info");
	    browser.setLeftInfo(lblloc);

		Label lblhr = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		FormData fd_label_2 = new FormData();
		fd_label_2.left = new FormAttachment(0);
		fd_label_2.right = new FormAttachment(100);
		fd_label_2.bottom = new FormAttachment(100, -17);
		lblhr.setLayoutData(fd_label_2);
		this.thislistener(this);
		createContents();
	}

	private void thislistener(Shell shell) {
		this.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				MessageBox mb = new MessageBox(shell,SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
				mb.setText("提示");
				mb.setMessage("确定要关闭吗?");
				int rc = mb.open();
				if (e.doit == (rc == SWT.OK)) {
					System.exit(0);
				} else if (e.doit == (rc == SWT.CANCEL)) {
					Display display = shell.getDisplay();
					shell.open();
					shell.layout();
					while (!shell.isDisposed()) {
						if (!display.readAndDispatch()) {
							display.sleep();
						}
					}
					shell.close();
				}
			}
		});
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("MyBrowser");
		setSize(800, 600);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}

      

package com.taomus.browser;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

public class Main {
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			BrowserShell shell = new BrowserShell(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
			shell.close();
			//display.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

end

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部