Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Questions about the SWT Browser(Looking to mimic common browser functionality in the SWT Browser)
Questions about the SWT Browser [message #1753911] Sun, 12 February 2017 09:09
Tim Fielder is currently offline Tim FielderFriend
Messages: 1
Registered: February 2017
Junior Member
I'm trying to build a browser as part of a larger project I'm working on, and I'm having trouble replicating some standard modern browser functionality, and some less-standard functionality. Specifically, I am trying to do the following four things:

1) When I middle-click on a page that has scrolling enabled, it shows the drag-scrolling widget. I want to turn this feature off, but there doesn't appear to be any way to get or change the state of the drag-scrolling feature in the Browser class. This is the most critical one. If I can do this, the others I can deal with.

2) I would like to be able to get a list of all linked pages (all hrefs, at least) and maybe all linked objects. If I need to, I can just pull the html from getText() when the ProgressListener fires the completed event, but I thought I'd ask if this is built-in before I build an HTML parser.

3) I actually got the browser to open a new tab when you middle-click on a link, like other browsers do, but there's a feature that is found in certain addons that allows opening multiple links at once using a drag (Linkclump for Chrome, Snap Links for Firefox). Since so little of the Browser is exposed, I doubt that there's any reasonable way to get the collection of links within an area, but maybe there's a way to do it using BrowserFunction?

4) Is there a way that I can turn mouse wheel scrolling on and off?

Thanks in advance.

Full code (yes I need to clean it up because that one method is a monster, my apologies):

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Browser_Test {
	
	//private final String gBlankPage = "<html><head></head><body></body></html>";
	private String gStatusText;
	
	public Browser_Test()
	{
		final Display display = new Display();
		
		final Shell shell = new Shell(display);
		shell.setLayout(new FormLayout());
		shell.setMaximized(true);
		
		FormData data;
		
		final Label label = new Label(shell, SWT.NONE);
		data = new FormData();
		data.top = new FormAttachment(98, 0);
		data.bottom = new FormAttachment(100, 0);
		data.left = new FormAttachment(3, 0);
		data.right = new FormAttachment(75, 0);
		label.setLayoutData(data);
		
		final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
		data = new FormData();
		data.top = new FormAttachment(98, 0);
		data.bottom = new FormAttachment(99, 0);
		data.left = new FormAttachment(75, 0);
		data.right = new FormAttachment(97, 0);
		bar.setLayoutData(data);
		
		final TabFolder tabs = new TabFolder(shell, SWT.NONE);
		data = new FormData();
		data.top = new FormAttachment(3, 0);
		data.bottom = new FormAttachment(97, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 0);
		tabs.setLayoutData(data);
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
		fileMenuItem.setText("&File");
		
		Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
		fileMenuItem.setMenu(fileMenu);
		
		MenuItem closeWindowMenuItem = new MenuItem(fileMenu, SWT.PUSH);
		closeWindowMenuItem.setText("Close Tab\tCtrl+W");
		closeWindowMenuItem.setAccelerator(SWT.MOD1 + 'W');
		closeWindowMenuItem.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event event)
			{
				display.dispose();
			}
		});
		
		MenuItem navMenuItem = new MenuItem(menu, SWT.CASCADE);
		navMenuItem.setText("&Navigation");
		
		Menu navMenu = new Menu(shell, SWT.DROP_DOWN);
		navMenuItem.setMenu(navMenu);
		
		MenuItem backMenuItem = new MenuItem(navMenu, SWT.PUSH);
		backMenuItem.setText("Back\tBackspace");
		backMenuItem.setAccelerator(SWT.BS);
		
		MenuItem forwardMenuItem = new MenuItem(navMenu, SWT.PUSH);
		forwardMenuItem.setText("Forward\tShift+Backspace");
		forwardMenuItem.setAccelerator(SWT.MOD2 + SWT.BS);
		
		MenuItem newTabMenuItem = new MenuItem(navMenu, SWT.PUSH);
		newTabMenuItem.setText("New Tab\tCtrl+T");
		newTabMenuItem.setAccelerator(SWT.MOD1 + 'T');
		
		MenuItem closeTabMenuItem = new MenuItem(navMenu, SWT.PUSH);
		closeTabMenuItem.setText("Close Tab\tCtrl+W");
		closeTabMenuItem.setAccelerator(SWT.MOD1 + 'W');
		closeTabMenuItem.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event event)
			{
				tabs.getItems()[tabs.getSelectionIndex()].dispose();
				
				if (tabs.getItemCount() == 0)
				{
					display.dispose();
				}
			}
		});

		Button button  = new Button(shell, SWT.NONE);
		data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(3, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(5, 0);
		button.setLayoutData(data);
		button.setText("Back");
		button.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				if (b.isBackEnabled())
				{
					b.back();
				}
			}
			public void widgetSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				if (b.isBackEnabled())
				{
					b.back();
				}
			}
		});
		
		button  = new Button(shell, SWT.NONE);
		data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(3, 0);
		data.left = new FormAttachment(5, 0);
		data.right = new FormAttachment(10, 0);
		button.setLayoutData(data);
		button.setText("Forward");
		button.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				if (b.isForwardEnabled())
				{
					b.forward();
				}
			}
			public void widgetSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				if (b.isForwardEnabled())
				{
					b.forward();
				}
			}
		});
		
		button  = new Button(shell, SWT.NONE);
		data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(3, 0);
		data.left = new FormAttachment(10, 0);
		data.right = new FormAttachment(15, 0);
		button.setLayoutData(data);
		button.setText("Home");
		button.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				b.setUrl("http://www.google.com");
			}
			public void widgetSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				b.setUrl("http://www.google.com");
			}
		});
		
		button  = new Button(shell, SWT.NONE);
		data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(3, 0);
		data.left = new FormAttachment(15, 0);
		data.right = new FormAttachment(20, 0);
		button.setLayoutData(data);
		button.setText("Reload");
		button.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				b.refresh();
			}
			public void widgetSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				b.refresh();
			}
		});
		
		button  = new Button(shell, SWT.NONE);
		data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(3, 0);
		data.left = new FormAttachment(20, 0);
		data.right = new FormAttachment(25, 0);
		button.setLayoutData(data);
		button.setText("Stop");
		button.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				b.stop();
			}
			public void widgetSelected(SelectionEvent event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				b.stop();
			}
		});

		final Text address = new Text(shell, SWT.BORDER);
		data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(3, 0);
		data.left = new FormAttachment(25, 0);
		data.right = new FormAttachment(100, 0);
		address.setLayoutData(data);
		address.setText("http://www.google.com");
		address.addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent event)
			{
				if(event.keyCode == SWT.CR || event.keyCode == SWT.LF)
				{
					((Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl()).setUrl(((Text)event.widget).getText());
				}
			}
			@Override
			public void keyReleased(KeyEvent event)
			{
				
			}
		});
		
		newTabMenuItem.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event event)
			{
				newTab(tabs, label, bar, address, "http://www.google.com");
			}
		});
		backMenuItem.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				if (b.isBackEnabled())
				{
					b.back();
				}
			}
		});
		forwardMenuItem.addListener(SWT.Selection, new Listener()
		{
			public void handleEvent(Event event)
			{
				Browser b = (Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl();
				if (b.isForwardEnabled())
				{
					b.forward();
				}
			}
		});
		
		tabs.addMouseListener(new MouseListener()
		{
			public void mouseDoubleClick(MouseEvent event)
			{
				if (event.button == 1)
				{
					newTab(tabs, label, bar, address, "http://www.google.com");
				}
			}
			public void mouseDown(MouseEvent event)
			{
				if (event.button == 2)
				{
					boolean closed = false;
					for (int i = 0; i < tabs.getItems().length; i++)
					{
						Rectangle rect = tabs.getItems()[i].getBounds();
						if (event.x > rect.x && event.x < rect.x + rect.width)
						{
							tabs.getItems()[i].dispose();
							closed = true;
							break;
						}
					}
					
					if (tabs.getItemCount() == 0)
					{
						display.dispose();
					}
					
					if (!closed)
					{
						newTab(tabs, label, bar, address, "http://www.google.com");
					}
				}
			}
			public void mouseUp(MouseEvent event)
			{
			}
		});
		tabs.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				if ((Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl() != null)
				{
					address.setText(((Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl()).getUrl());
				}
			}
			public void widgetSelected(SelectionEvent event)
			{
				if ((Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl() != null)
				{
					address.setText(((Browser)tabs.getItems()[tabs.getSelectionIndex()].getControl()).getUrl());
				}
			}
		});
		
		newTab(tabs, label, bar, address, "http://www.google.com");

		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	private Browser newTab(final TabFolder tabs, final Label label, final ProgressBar bar, final Text address, final String url)
	{
		bar.setVisible(true);
		final TabItem tab = new TabItem(tabs, SWT.NONE);
		final Browser browser = new Browser(tabs, SWT.NONE);
		tab.setControl(browser);
		browser.setUrl(url);
		browser.addTitleListener(new TitleListener()
		{
			@Override
			public void changed(TitleEvent event)
			{
				tab.setText(event.title);
			}
		});
		browser.addStatusTextListener(new StatusTextListener()
		{
			public void changed(StatusTextEvent event)
			{
				if (tabs.getItems()[tabs.getSelectionIndex()] == tab)
				{
					label.setText(event.text);
					gStatusText = event.text;
				}
			}
		});
		browser.addProgressListener(new ProgressListener()
		{
			public void changed(ProgressEvent event)
			{
				if (tabs.getItems()[tabs.getSelectionIndex()] == tab)
				{
					bar.setMinimum(0);
					bar.setMaximum(event.total);
					bar.setState(event.current);
				}
			}
			public void completed(ProgressEvent event)
			{
				if (tabs.getItems()[tabs.getSelectionIndex()] == tab)
				{
					bar.setVisible(false);
				}
				
				System.out.println(browser.getText());
			}
		});
		browser.addLocationListener(new LocationListener()
		{
			public void changed(LocationEvent event)
			{
				address.setText(browser.getUrl());
			}
			public void changing(LocationEvent event)
			{
				address.setText(browser.getUrl());
			}
		});
		browser.addMouseListener(new MouseListener()
		{
			public void mouseDoubleClick(MouseEvent event)
			{
			}
			public void mouseDown(MouseEvent event)
			{
				//System.out.println(gStatusText);
				if (event.button == 2 && gStatusText.toLowerCase().startsWith("http"))
				{
					newTab(tabs, label, bar, address, gStatusText);
				}
			}
			public void mouseUp(MouseEvent event)
			{
			}
		});
		
		return browser;
	}
	
	public static void main(String[] args)
	{
		new Browser_Test();
	}
	
}
Previous Topic:Dragging Tabs - don't work at 4k? (linux)
Next Topic:nebula nattable vs swt table
Goto Forum:
  


Current Time: Fri Dec 15 18:12:19 GMT 2017

Powered by FUDForum. Page generated in 0.01299 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software