Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Open internal browser programatically
Open internal browser programatically [message #715577] Sun, 14 August 2011 16:07 Go to next message
Mads Nielsen is currently offline Mads Nielsen
Messages: 2
Registered: August 2011
Junior Member
Hello

I know this has probably been done 1000+ times before, and i can just go to <url> and donwload a plugin that does excactly what i am trying to do, but that is not going to teach me how to make Eclipse plugins Smile

I have this code:
package com.doc.ext.views;


import org.eclipse.swt.widgets.Composite;
//import org.eclipse.swt.widgets.MessageBox;
//import org.eclipse.swt.widgets.Shell;
//import org.eclipse.swt.widgets.List;
//import org.eclipse.ui.browser.IWebBrowser;
//import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.part.*;
import org.eclipse.swt.browser.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
//import org.eclipse.swt.SWTError;
//import org.eclipse.swt.custom.*;
//import org.eclipse.swt.layout.*;
//import org.eclipse.swt.widgets.*;
import java.io.*;
//import java.net.*;
//import java.util.Arrays;
//import java.net.URL;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class SampleView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */

	private TableViewer viewer;
	private Action action1;
	private Action action2;
	private Action doubleClickAction;

	
	[color=green]//Browser
	/*=========================================================================*/
	
	public class BrowserView extends ViewPart {
		/**
		 * Returns the ID that this browser is known as,
		 */
		public final String ID = BrowserView.class.getName();
		/**
		 * Stores the browser's instance, created in
		 */
		private Browser browser = null;
		/**
		 * Create the browser component by initialising the browser in the frame.
		 */
		public void createPartControl(Composite frame) {
			browser = new Browser(frame, SWT.NONE);
		}
		/**
		 * Disposes the internal browser widget.
		 */
		public void dispose() {
			browser.dispose();
			browser = null;
			super.dispose();
		}
		/**
		 * Directs the browser to the given URL.
		 */
		public void gotoURL(String url) {
			browser.setUrl(url);
		}
		/**
		 * Delegates focussing this view to the browser, so that it can handle
		 * mouse-clicks etc.
		 */
		public void setFocus() {
			browser.setFocus();
		}
	}
	
	/*=========================================================================*/[/color]
	
	/*
	 * The content provider class is responsible for
	 * providing objects to the view. It can wrap
	 * existing objects in adapters or simply return
	 * objects as-is. These objects may be sensitive
	 * to the current input of the view, or ignore
	 * it and always show the same content 
	 * (like Task List, for example).
	 */
	 
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		
		public Object[] getElements(Object parent) {
			
			File dir = new File("documentation");
			//String[] children = dir.list();

			return dir.list();
		}
	}
	
	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().
					getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}
	class NameSorter extends ViewerSorter {
	}

	/**
	 * The constructor.
	 */
	public SampleView() {
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setSorter(new NameSorter());
		viewer.setInput(getViewSite());

		// Create the help context id for the viewer's control
		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "com.doc.ext.viewer");
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				SampleView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(action1);
		manager.add(new Separator());
		manager.add(action2);
	}

	private void fillContextMenu(IMenuManager manager) {
		manager.add(action1);
		manager.add(action2);
		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(action1);
		manager.add(action2);
	}

	private void makeActions() {
		action1 = new Action() {
			public void run() {
				showMessage("Action 1 executed");
			}
		};
		action1.setText("Action 1");
		action1.setToolTipText("Action 1 tooltip");
		action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		
		action2 = new Action() {
			public void run() {
				showMessage("Action 2 executed");
			}
		};
		action2.setText("Action 2");
		action2.setToolTipText("Action 2 tooltip");
		action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		doubleClickAction = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				showMessage(" + obj.toString() + ");
			}
		};
	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});
	}
	private void showMessage(String message) {
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Documentation",
			message);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
}


How do i use this code to open a browser view when one of the items i double clicked ?

Right now it just makes a textbox with the name of the double clicked item.

The browser class should work. i just don't know how to display the browser view when a item from the list is double clicked.

/mads
Re: Open internal browser programatically [message #715579 is a reply to message #715577] Sun, 14 August 2011 16:25 Go to previous message
Lubos Pochman is currently offline Lubos Pochman
Messages: 29
Registered: July 2009
Junior Member
Check my Eclipse development notes at http://lubospeclipse.wordpress.com/eclipse-plugin-and-rcp-development-notes-2-2/ you will see some examples of using web browser.
Previous Topic:Finding code attached to UI
Next Topic:Detect User Activity in Mac OS X from SWT/RCP
Goto Forum:
  


Current Time: Mon Sep 22 22:31:07 GMT 2014

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

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