Did this blog help you solve a problem? Tip me with Bitcoins at: 17dQktcAmU4urXz7tGk2sbuiCqykm3WLs6

Tuesday, December 16, 2008

Enceladus - Sixth Largest Moon of Saturn


Saturn, the sixth planet from the sun and one of the gas giants in our solar system has 7 moons in addition to a prominent system of rings. One of these moons, Enceladus, is especially interesting to us humans because it is made mostly of water-ice and could possibly support life. It's diameter is roughly 500 km (15% of the moon's) and has a mass of about 0.2% of the moon's mass. Certain areas of the moon's surface lack craters indicating a dynamic surface similar to the floating ice sheets in the Earth's arctic regions. Water vapor has also been observed spewing from the surface. I found the image here and it is licensed under a Creative Commons liscence.

Optical Illusion - "Shifting Almonds"

I found this image in cyberspace here. It's just a plain ol' PNG, and one of the coolest optical illusions I've ever seen.

See also: Home-made and Bio-inspired Wind Power

Verify User Intent Before Closing an Eclipse RCP Application

Have you ever accidentally closed an application only to lose all the data you were working with or abort some long running calculation half way through? On one particular application I was working on, it would have been devastating to accidentally close the application so I needed to add a dialog box that asked the user to verify that s/he really wanted to close the application. I've also used some applications before with this feature, and found it annoying when I was asked every single time whether or not I was really sure I wanted to close the application. Use it sparingly. The best solution would be to add a preference to the application where the user could decide if s/he wanted the reminder or not. I'll add a tutorial on how to do that soon. Anyway, It took me a while to figure out how to add a confirmation dialog box, but it's actually quite easy. Here's how...



Step 0: Create a Hello World Application.

Step 1: Add code to create the dialog box when the close application action is triggered. In the ApplicationWorkbenchAdvisor class that was automatically generated when you created a hello world application using the new RCP application wizard in Eclipse, you need to override the preShutdown() method. The method returns a boolean where, if true, the application will shut down, and if false, the application will continue running. You need to add code in that method that opens up a dialog box, gets user input, and returns what the user selected. Eclipse's JFace library contains a nice little class called MessageDialog where we can easily make a dialog box pop up, ask a question, and get the answer. We use the MessageDialog.openQuestion() method and pass it three parameters: a Shell, the dialog box's title, and the question we want to ask.


package com.blogspot.obscuredclarity;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

private static final String PERSPECTIVE_ID = "com.timmolter.helloWorld.perspective";

public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
return new ApplicationWorkbenchWindowAdvisor(configurer);
}

public String getInitialWindowPerspectiveId() {
return PERSPECTIVE_ID;
}

public boolean preShutdown(){

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
String dialogBoxTitle = "Question";
String question = "Are you sure you want to close this application?";
return MessageDialog.openQuestion(shell, dialogBoxTitle, question);

}
}




Step 2: Run the application and test if everything worked. Your application should now have a dialog box that verifies user intent when the application is closed and look something like this:


Piece of Cake!

Next ---> Add Nested Menu Items and Custom Actions to an Eclipse RCP Application
<--- Previous - Add a Menu to an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents

Thursday, December 11, 2008

Add a Menu to an Eclipse RCP Application

As shown in the article Hello World with Eclipse RCP - Your First Application, it is quite simple to create and run your own very basic Java RCP application using Eclipse. In this article I will show you how to add a "Menu" to your application. In particular, I will add the "Exit" action to a "File" menu, and it is very simple. It only requires a few lines of code in one class. The application's menu is used to provide the user with a logical and organized interface to access all the global functions of the application. For example, almost all applications have a File--->Exit or a Help---> About menu. An Eclipse RCP application menu can be as simple or elaborate as needed, and it can contain icons, dividers, and expanding submenus. The following steps demonstrate how to add the Exit action to a File menu to an Eclipse RCP application.



Step 1: Add code to create the File menu and the Exit action. When you created a hello world application using the new RCP application wizard in Eclipse (which I always do to create a skeleton RCP project to build off of for a new application), Eclipse created a ApplicationActionBarAdvisor class that extends ActionBarAdvisor. You need to override 2 of the methods to configure the application's menu to suit the needs of the particular application. During certain strategic points in the workbench's lifecycle, these methods are called and your complete menu is created. In makeActions() the Exit action is created and registered. In fillMenuBar() the File menu is created, the Exit action is added to the File menu, and the File menu is added to the application's menu bar.

package com.blogspot.obscuredcalrity;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

IWorkbenchAction exitAction;

public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}

protected void makeActions(IWorkbenchWindow window) {

exitAction = ActionFactory.QUIT.create(window); //the ActionFactory defines a set of common actions and can be used in the application.
register(exitAction);  //register the action so they are deleted when the Workbench window is closed

}

protected void fillMenuBar(IMenuManager menuBar) {

MenuManager fileMenu = new MenuManager("&File", "file"); //create a menuManager to take care of all submenus in "File"
fileMenu.add(exitAction); //Add the "exit" action
menuBar.add(fileMenu);//Add the "File" menu to the menuBar

}

}




Step 2: Run the application and test if everything worked. Your application should now have a File menu with an Exit action and look something like this:



Piece of Cake!

Next ---> Verify User Intent Before Closing an Eclipse RCP Application
<--- Previous - Add an Icon to an Eclipse RCP Application View
Also see: Eclipse RCP Tutorial Table of Contents

Wednesday, December 3, 2008

Add an Icon to an Eclipse RCP Application View

In the last Java Eclipse RCP tutorial, Adding a View to an Eclipse RCP Application, I demonstrated how to add a view to a simple RCP application. In RCP applications, views are where you put things like tables, buttons, combo boxes, labels, charts, etc. - all the graphical interfaces that a user will interact with when using your application. In this article I will show where to get icons and how to add an icon to a view, which is placed on a tab corresponding to that view. I will also show how to label that tab with some custom text.



Step 1: Get some icons. Mark James at www.famfamfam.com has graciously created and made available over 1000 icons suitable for Eclipse RCP applications. His work is licensed under a Creative Commons Attribution 2.5 License, so you can use the icons any way you like. Download the icons, browse through them, and choose one that you'd like to use for your view.





Step 2: Add the icon to your RCP application. With Eclipse open and the Package Explorer showing the contents of your project, drag and drop an icon into the "icons" folder. For this example, I put the bomb.png icon into the icons folder.



Step 3: Add the icon to the view. Make sure the View is highlighted in the All Extensions list. To the right are the properties of the view. To the right of the icon property, click on the "Browse..." button. In the dialog box that pops up, choose the icon that you just added. The icon should show up next to the view's name in the "All Extensions" list.




Step 4: Specify that the view's title should be shown. Now that the icon is added to the application project and associated with the view, some code is needed to tie everything together. In Perspective.java set the boolean flag for the second argument in the "addStandAloneView" method to true. This is the "showTitle" flag which tells the RCP application to show a tab for the view containing the icon and a title for the view.

package com.blogspot.obscuredclarity.addicon;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

 public void createInitialLayout(IPageLayout layout) {

  layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f,   layout.getEditorArea());
  layout.setEditorAreaVisible(false); //hide the editor in the perspective

 }
}


Step 5: Give the view's tab a name. In the view's class, add the line "this.setPartName("Hello!");" to the createPartControl method (line 16). Change the "Hello!" String to whatever you want.

package com.blogspot.obscuredclarity.addicon;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {

 public static final String ID = "com.blogspot.eclipsercptutorials.addicon.mainView"; //the ID needs to match the id set in the view's properties

 public MainView() {    }

 public void createPartControl(Composite parent) {

  this.setPartName("Hello!");

  Label label = new Label(parent, SWT.None); //new up a Label widget
  label.setText("  All this program does is display this text in a view."); //set the label's text

 }

 public void setFocus() { }

}


Step 6: Run the application and test if everything worked. Your application should now look something like this:



Step 7: If you want the view to be non-closeable, add layout.getViewLayout(MainView.ID).setCloseable(false);" to the createInitialLayout" method (line 12). This will hide the little "X" on the tab and not allow the tab to be closed, which you probably want to do if your program only has one view.

package com.blogspot.obscuredclarity.addicon;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

 public void createInitialLayout(IPageLayout layout) {

  layout.addStandaloneView(MainView.ID, true, IPageLayout.LEFT, 1.0f,   layout.getEditorArea());
  layout.setEditorAreaVisible(false); //hide the editor in the perspective
  layout.getViewLayout(MainView.ID).setCloseable(false);
 }
}




Piece of cake!

Next ---> Add a Menu to an Eclipse RCP Application
<--- Previous - Adding a View to an Eclipse RCP Application
Also see: Eclipse RCP Tutorial Table of Contents