Posts Tagged ‘How-to’

Re-using the Eclipse proxy preference settings in your Eclipse RCP application

Tuesday, December 8th, 2009

The following demonstrates how to re-use the Eclipse proxy preferences in an Eclipse RCP application.

Create a project “de.vogella.rcp.net.proxy” and select the “RCP application with a view” as template.

Add the preferences command to your application. See Eclipse Preferences Tutorial for details.

Add the plugin “org.eclipse.ui.net” and “org.eclipse.core.net” as dependency to your plugin.

Run your plugin and open preferences.

proxy_settings

Fantastic! You already have the preference dialog.

Now change your view to the following.


package de.vogella.rcp.net.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.FrameworkUtil;
import org.osgi.util.tracker.ServiceTracker;

public class View extends ViewPart {
	public static final String ID = "de.vogella.rcp.net.proxy.view";
	private final ServiceTracker proxyTracker;

	public View() {
		proxyTracker = new ServiceTracker(FrameworkUtil.getBundle(
				this.getClass()).getBundleContext(), IProxyService.class
				.getName(), null);
		proxyTracker.open();
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		StyledText text = new StyledText(parent, SWT.NONE);
		text.setText(readWebpage());
	}

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

	private String readWebpage() {
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer();

		try {
			URI uri = new URI("http://www.vogella.de");
			IProxyService proxyService = getProxyService();
			IProxyData[] proxyDataForHost = proxyService.select(uri);

			for (IProxyData data : proxyDataForHost) {
				if (data.getHost() != null) {
					System.setProperty("http.proxySet", "true");
					System.setProperty("http.proxyHost", data.getHost());
				}
				if (data.getHost() != null) {
					System.setProperty("http.proxyPort", String.valueOf(data
							.getPort()));
				}
			}
			// Close the service and close the service tracker
			proxyService = null;

			URL url;

			url = uri.toURL();

			in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				// Process each line.
				sb.append(inputLine + "\n");
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
		return sb.toString();

	}

	public IProxyService getProxyService() {
		return (IProxyService) proxyTracker.getService();
	}

	@Override
	public void dispose() {
		proxyTracker.close();
		super.dispose();
	}

}

If you run your application behind a firewall and if you have maintained the proxy correctly then the View should display the HTML code.

Hope this helps!

 

Defining menu entries (commands) at runtime in Eclipse RCP

Thursday, December 3rd, 2009

A common question I receive is how menu entries can be defined at runtime in an RCP application. The following gives an example how this can be done.

Create the RCP project “de.vogella.rcp.commands.runtimecommands” using the “Hello RCP” template.

Define a menu contribution. Maintain the class “de.vogella.rcp.commands.runtimecommands.DefineCommands” in this menu contribution.


<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         id="application"
         point="org.eclipse.core.runtime.applications">
      <application>
         <run
               class="de.vogella.rcp.commands.runtimecommands.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="RCP Perspective"
            class="de.vogella.rcp.commands.runtimecommands.Perspective"
            id="de.vogella.rcp.commands.runtimecommands.perspective">
      </perspective>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            class="de.vogella.rcp.commands.runtimecommands.DefineCommands"
            locationURI="menu:org.eclipse.ui.main.menu">
      </menuContribution>
   </extension>

</plugin>

Create the following class.

package de.vogella.rcp.commands.runtimecommands;

import org.eclipse.swt.SWT;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.ExtensionContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.services.IServiceLocator;

public class DefineCommands extends ExtensionContributionFactory {

	@Override
	public void createContributionItems(IServiceLocator serviceLocator,
			IContributionRoot additions) {
		CommandContributionItemParameter p = new CommandContributionItemParameter(
				serviceLocator, "",
				"org.eclipse.ui.file.exit",
				SWT.PUSH);
		p.label = "Exit the application";
		p.icon = Activator.getImageDescriptor("icons/alt_window_16.gif");

		CommandContributionItem item = new CommandContributionItem(p);
		item.setVisible(true);
		additions.addContributionItem(item, null);
	}

}

Run the example, your application should have the Exit command in the menu.

Thanks to Robert Einsle for the tip.

This description has also be added to my Eclipse command tutorial.

 

Profiling Eclipse RCP applications with Eclipse TPTP

Wednesday, November 18th, 2009

I believe approx. one or two years ago I tried to profile an Eclipse RCP application with the Eclipse TPTP project. I believe at this point in time profiling an RCP application with TPTP was not possible.

I learned from Eugene Chan that the TPTP release which is part of Eclipse Galileo allows to profile Eclipse RCP applications.

I suggest you give it a try, it is as easy as profiling a standard ;-) Java application.

You find an updated description here: Eclipse TPTP Tutorial.

Thanks to Eugene Chan, Paul Slauenwhite and Kathy Chan from the TPTP team for feedback on the article.

 

Using Fast Views in Eclipse RCP

Tuesday, September 15th, 2009

In Adding the error view to RCP application one commenter asked how he could add a view as a fast view to an Eclipse RCP application.

To add views as fast view to your RCP application you have to do two things:

1.) Add your view as “fast” via the perspective extension point

fastview10

2.) Activate the FastViewBar via the ApplicationWorkbenchWindow Advisor (configurer.setShowFastViewBars(true);)


package de.vogella.test;

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);
    }

    @Override
	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }

    @Override
	public void preWindowOpen() {
    	IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

    	configurer.setInitialSize(new Point(1024, 800));
		configurer.setShowStatusLine(true);
		configurer.setTitle("Network Analyser");
		configurer.setShowFastViewBars(true);
    }

}

This should be sufficient to show your view in your RCP application in the fast view pane.

 

Eclipse e4 – Using the modeled user interface editor

Wednesday, August 26th, 2009

Update This description has been updated in the following tutorial: Eclipse e4 tutorial.

In the Eclipse e4 webseminar I learned about the e4 UI editor which allow you to change the user interface on the fly.

As you most likely know the Eclipse UI is modelled as an Eclipse EMF model. This model can be changed during directly in Eclipse via the editor or via the EMF API (I have not yet tested this) and the UI will change immeadiately according to the change.

To following my example you need the E4 0.9 download from Eclipse E4 0.9 download.

Start Eclipse e4 tutorial. Select Window -> UI Editor

e4modelledui10

Open the WorkbenchWindow

e4modelledui20

You can now modify the model, e.g. you can drag the package explorer on the same level as the Java perspective.

e4modelledui30

I believe the modeled UI concept is really powerful as it exposes the internal state of the Eclipse based apllication via a well-defined model with a clear API.

Kudos to the Eclipse e4 team!

 

Eclipse e4 – Styling your UI with css

Tuesday, August 18th, 2009

Update This description has been updated in the following tutorial: Eclipse e4 tutorial.

Eclipse e4 allows to style your applications via css like stylesheets. The following gives a short example how to get started with css stylecheets.

Create a new e4 product “de.vogella.e4.css” similar to the description Your first Eclipse e4 application. Of course you can also copy “de.vogella.e4.first” and edit the package, Application.xmi and the product to fit the new project “de.vogella.e4.css” or just extend “de.vogella.e4.first” with the usage of stylesheets.

I like to have my examples separate therefore I use “de.vogella.e4.css”.

First lets extend our UI from last time a little bit. Change View1.java to the following.


package de.vogella.e4.css.views;

import org.eclipse.e4.core.services.context.IEclipseContext;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
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.eclipse.ui.services.IDisposable;

public class View1 implements IDisposable {

	public View1(Composite parent, final IEclipseContext outputContext) {
		GridLayout gridLayout = new GridLayout(2, true);
		Label label = new Label(parent, SWT.NONE);
		label.setText("E4 is new");
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalIndent = 20;
		label.setLayoutData(gridData);
		Text text = new Text(parent, SWT.NONE);
		text.setText("and different");

		label = new Label(parent, SWT.NONE);
		label.setText("This is a button");
		new Button(parent, SWT.PUSH).setText("My button");

		GridLayoutFactory.createFrom(gridLayout).generateLayout(parent);
	}

	public void dispose() {

	}
}

Run your application and you get the following:

e10css10

This is obvious an application which does not yet use css styleing.

Add now the folder css to your application and create the following file myfirststylesheet.css:


Label {
	font: Verdana 20px;
	color:rgb(178,34,34);
	background-color:rgb(255,255,255) rgb(0,0,0) 60%;
}

Button {
	font: Verdana 15px;
	background-color:rgb(255,255,255) rgb(0,0,0);
}

Text {
	font: Verdana 15px;
	background-color:radial rgb(255,255,255) rgb(0,0,0);
}

Add the property “applicationCSS” which points to the css file to your product:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.e4.workbench.parts">
          <part
            class="de.vogella.e4.first.views.View1"
            label="My first E4 View"
            parentId="org.eclipse.e4.ui.tags.navigation">
      </part>
   </extension>
   <extension
         id="product1"
         point="org.eclipse.core.runtime.products">
      <product
            application="org.eclipse.e4.ui.workbench.swt.application"
            name="vogella">
         <property
               name="appName"
               value="vogella">
         </property>
         <property
               name="applicationXMI"
               value="de.vogella.e4.css/Application.xmi">
         </property>
         <property
               name="applicationCSS"
               value="platform:/plugin/de.vogella.e4.css/css/myfirststylesheet.css">
         </property>
      </product>
   </extension>

</plugin>

Run your Eclipse e4 application and you should receive this amazing and beautiful UI .

e10css20

Arguably the beauty lies in the eye of the beholder. ;-)

I hope this get you started in playing around with the css styling. Check the existing cvs examples (org.eclipse.e4.demo.contacts and org.eclipse.e4.ui.examples.css) to learn more about. You find these in the Eclipse cvs trunk under E4->org.eclipse.e4.ui. The usage of cvs is described Source Code Guide of Eclipse 3.5 (Galileo) – Using cvs

I’m sure you can conjure a nicer UI then I did in my example. :-)

Good luck in your E4 journey!