Posts Tagged ‘RCP’

Eclipse 3.6M4 – Tiny nice things

Saturday, December 12th, 2009

I personally believe that little things make a difference. Eclipse 3.6 is going to be great for new (and perhaps also for experienced) plugin and RCP developers.

Especially I like in the Eclipse 3.6M4 release:

-consoleLog included per default in a new launch configuration
RCP with a View

I believe the solution of Bug for -consoleLog will solve the most common problem with getting started for new Plugin / RCP developers. Also the update of the PDE RCP templates will hopefully guide new developers in using commands instead of actions.

The change in Bug for -consoleLog was also a community decision, which is a good example that Eclipse does consider user feedback.

Thanks to all involved parties and the Eclipse community for these changes. :-)

Update Unfortunately Mail Template was not part of M4.

 

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.

 

Poll – Default launch configuration for Eclipse plugin / RCP development

Friday, November 20th, 2009

Eclipse shows errors during a launch via the log views in both the host and the target system and on the file system. In addition the developer can specify the flag “-consoleLog” in the launch configuration so see potential error messages in the console view.

Bug 284704 had been opened asking if the “-consoleLog” flag could be included by default in a new launch configuration.

What do you think? If you are a plugin or RCP developer please participate in the following survey:

Link to Survey

——————————
Please note that this is my first attempt using SurveyMonkey, I hope this works well. I believe the survey will close after the first 100 answers. If this number has not reached I’m planning to close the survey next week 27. Nov. 2009.

 

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.

 

Eclipse RCP – Removing the minimize and maximize buttons from Views

Friday, November 13th, 2009

I got the question how someone could remove the maximize and minimize buttons from a view in an Eclipse RCP application.

To archive this I know two ways.

Either set the layout to fixed in initialLayout() in Perspective.java


package de.vogella.intro.rcp.fixedview;

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(View.ID,  false, IPageLayout.LEFT, 1.0f, editorArea);
	}

}

Or use the Perspective.java to add a standalone view.


package de.vogella.intro.rcp.fixedview;

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(View.ID,  false, IPageLayout.LEFT, 1.0f, editorArea);
	}

}

If I add a standalone view via the extension “org.eclipse.ui.perspectiveExtensions” then the minimize and maximize buttons are still there.

<?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.intro.rcp.fixedview.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="Perspective"
            class="de.vogella.intro.rcp.fixedview.Perspective"
            id="de.vogella.intro.rcp.fixedview.perspective">
      </perspective>
   </extension>
   <extension
         point="org.eclipse.ui.views">
      <view
            name="View"
            class="de.vogella.intro.rcp.fixedview.View"
            id="de.vogella.intro.rcp.fixedview.view">
      </view>
   </extension>
   <extension
         point="org.eclipse.ui.perspectiveExtensions">
      <perspectiveExtension
            targetID="*">
         <view
               id="de.vogella.intro.rcp.fixedview.view"
               minimized="false"
               ratio="1.0f"
               relationship="left"
               relative="org.eclipse.ui.editorss"
               standalone="true"
               visible="true">
         </view>
      </perspectiveExtension>
   </extension>

</plugin>

The behavior seems inconsistent. If I use coding to add a standalone view the minimize / maximize buttons are not there, if I use extension points they are still there.

Does anymore know if I’m missing something? Or is this a bug? If you know please comment on this blog post or contact me via twitter.

Update: Projekt attached.

FixedView.zip

 

Eclipse PDE templates – Your action is my command

Wednesday, September 23rd, 2009

The Eclipse PDE templates are fantastic to help people to learn the platform and to get oven the first initial resistence to start with Eclipse RCP or Eclipse Plugin development.

Templates indicates to their consumers that they represent best development practices. This puts a certain burden on the quality to the templates. Unfortunately the PDE RCP templates in Eclipse 3.5 were all based on actions which I believe are superseded by Eclipse Commands.

I have seen in the past lots of questions regarding actions in the Eclipse RCP and PDE newsgroups. I believe that at least some of these questions are based on the fact that the PDE templates still promote actions.

I’m therefore happy to see the changes of Bug 265231 applied for the “RCP with a view” template for the upcoming Eclipse 3.6. Special thanks for this to Chris Aniszczyk.

It also looks good for the mail example. It seems that work is happening in Bug 253105

Actions are (a little bit more) dead. Long live the command! ;-)

 

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.

 

Add the error log view to your Eclipse RCP application

Monday, August 17th, 2009

The Eclipse platform provide error message in case something goes wrong.

During development I use the launch parameter -consoleLog to see if any error has occured. I assume most plugin developer use -consoleLog that is why I opened Bug for making -consoleLog a default in a new launch configuration.

Obviously you don’t want your users to check the console for errors. To make errors visible to the user you can add the exiting error log to your RCP application.

Create a new Eclipse RCP project “de.vogella.rcp.intro.errorview” for this purpose. Use the “Hello RCP” as a template.

Select the plugin.xml and add the dependency to org.eclipse.ui.views.log.

Ddd the standard Eclipse command “org.eclipse.ui.views.showView” to your menu under the Entry “Admin”. See Eclipse Commands how to do this.

This will result in the following plugin.xml


<?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.intro.errorview.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="RCP Perspective"
            class="de.vogella.rcp.intro.errorview.Perspective"
            id="de.vogella.rcp.intro.errorview.perspective">
      </perspective>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               label="Admin">
            <command
                  commandId="org.eclipse.ui.views.showView"
                  label="Show Error View"
                  style="push">
               <parameter
                     name="org.eclipse.ui.views.showView.viewId"
                     value="org.eclipse.pde.runtime.LogView">
               </parameter>
            </command>
         </menu>
      </menuContribution>
   </extension>

</plugin>

Run your new application. You should have a menu entry “Admin” with an entry “Show Error View”. If you select it the error view should open.

errorLogView10

 

Create your first Eclipse e4 application

Wednesday, August 12th, 2009

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

Eclipse released on the 29 July 2009 the tech preview version 0.9 of Eclipse e4, which can be downloaded here.

I wasn’t able to find a description how to create an new application based on E4. Starting from the CVS example is possible but I wanted to start from scratch. After several failed attempts to create an E4 application I believe I have found a way to create an E4 application. I assume others will have similar problems so I wanted to share my experience.

To follow this description you need to have knowledge how to develop Eclipse Plugins in Eclipse 3.5 or earlier. Check out this Eclipse Plugin Development Tutorial.

So lets get started with e4.

Download e4. Extract it and start it is similar to older Eclipse versions.

Currently there is no wizard which creates a working scaffold of an E4 application. We will therefore use the exsting plugin templates.

Create a new Plugin project “de.vogella.e4.first”. Do not select “Generate an activator”, nor “This plug-in will make contributions to the UI” nor “Would you like to create a rich client application”. Do not select a template. Press finish.

e410

This will open the Plugin Perspective, switch here to the Package Explorer to see your new project.

Create the package “de.vogella.e4.first”. The result should look like the following.

e412

Select your MANIFEST.MF and add the following dependencies to your plugin.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: First
Bundle-SymbolicName: de.vogella.e4.first;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.e4.ui.workbench;bundle-version="0.9.0",
 org.eclipse.e4.ui.workbench.renderers.swt;bundle-version="0.9.0",
 org.eclipse.e4.ui.workbench.renderers.swt.contributions;bundle-version="0.9.0",
 org.eclipse.e4.ui.workbench.swt;bundle-version="0.9.0",
 org.eclipse.e4.ui.services;bundle-version="0.9.0",
 org.eclipse.e4.core.services;bundle-version="0.9.0"

You need (some of) these plugins for the usage of E4 features.

Add the following extension points. I noticed that for some of the features you have to modify the plugin.xml directly as the UI does not support them. But you have to create the plugin.xml via the MANIFEST.MF file; as I created it directly as a new file I received a strange system behavior.


<?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="product"
         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.first/Application.xmi">
         </property>
      </product>

   </extension>

</plugin>

“org.eclipse.e4.workbench.parts” is a new extension point by which you can define your view parts, formally known as views and editors.

The extension point “org.eclipse.core.runtime.products” has a new parameter “applicationXMI”. This is a parameter to the model of your UI. We came to this later. You also do not define your own application; we use “org.eclipse.e4.ui.workbench.swt.application”.

Create a product configuration called “vogella.product”. This is similar to Eclipse 3.5; see here for a description: Eclipse Product Configuration.

e420

Make sure you select “org.eclipse.e4.ui.workbench.swt.application”.

e430

Add as dependency to your product the plugin “org.eclipse.e4.ui.workbench”. Add also the plugin “de.vogella.e4.first”.
Press “add required plugins”.

Create the following two classes:


package de.vogella.e4.first.handlers;

import org.eclipse.e4.workbench.ui.IWorkbench;

public class ExitHandler {
	public void execute(IWorkbench workbench) {
		workbench.close();
	}
}

and

package de.vogella.e4.first.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.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) {
		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");
		GridLayoutFactory.fillDefaults().generateLayout(parent);
	}

	public void dispose() {

	}
}

You need a model for your application. In the parameter “applicationXMI” we earlier defined that this model will be “de.vogella.e4.first/Application.xmi”. Create the file Application.xmi with the following content.


<?xml version="1.0" encoding="ASCII"?>
<application:MApplication xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:application="http://www.eclipse.org/ui/2008/Application">
  <windows name="Main" x="100" y="100" width="800" height="600">
    <menu>
      <items xsi:type="application:MMenuItem" name="File">
        <menu>
          <items xsi:type="application:MMenuItem" id="" name="Exit" command="//@command.0"/>
        </menu>
      </items>
    </menu>
    <children policy="VerticalComposite">
      <children xsi:type="application:MSashForm" policy="HorizontalSash">
        <children xsi:type="application:MStack">
          <children xsi:type="application:MContributedPart" iconURI="" name="My View" tooltip="My first View" URI="platform:/plugin/de.vogella.e4.first/de.vogella.e4.first.views.View1"/>
        </children>
        <weights>30</weights>
        <weights>70</weights>
      </children>
    </children>
    <handlers id="" URI="platform:/plugin/de.vogella.e4.first/de.vogella.e4.first.handlers.ExitHandler" persistedState="" command="//@command.0"/>
  </windows>
  <command id="application.exit" name="Exit"/>
</application:MApplication>

This is the EMF model which defines your UI. Eclipse should display this model in a EMF editor so that you can investigate it. In this model you find your view and command definition. The content can be viewed via the properities view.

e450

Now start your application from your vogella.product. Starting it directly from the plugin.xml will not work (easily).

If you receive the error “org.eclipse.core.runtime.AssertionFailedException: null argument:-applicationXMI argument missing” check you plugin.xml. E4 created in several cases twice the products entry.

You should be rewarded ;-) with this user interface:

e460

With this first application I hope you can started using the E4 advanced features, e.g. CSS styling, etc.

My final thoughts: Eclipse e4 has already amazing capabilities for a tech review release. The new modeled UI removes the need for a lot of boilerplate code.

Try it out!