Search Engine
Sunday, July 4, 2010
Thank you
I maintain this tutorial in my private time. If you like the information please help me by donating or by recommending this tutorial to other people.
Saturday, July 3, 2010
Eclipse Forms
1.1. Overview
Eclipse supports a web-like user interface style, called Eclipse Forms. Eclipse Forms is an plugin based on SWT and JFace that provides the support for creating portable web-style user interfaces across all Eclipse UI categories.
The Eclipse forms functionality is contained in the plugin "org.eclipse.ui.forms".
The class FormToolkit serves as a factory for the creation of the required user interface elements. This factory adjust the look and and feel of the standard SWT and JFace elements to the Forms API.
Tip
Existing UI elements can be enhanced to the form API via the method adapt(Composite).Example
Create a new project "de.vogella.rcp.intro.forms" (see Eclipse RCP Tutorials for details). Use the "RCP Application with a view" as a template.
Add a dependency to "org.eclipse.ui.forms" in plugin.xml. You do this via selecting the tab "Dependencies". Press the "Add" button and type in the name of "org.eclipse.ui.forms".
Tip
Adding a dependency to an existing plug-in automatically updates the classpath of your project.
Change the View.java to the following. This will create a re-sizable table in a form with a button on the right side of it.
package de.vogella.rcp.intro.forms;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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.Table;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.ViewPart;
public class View extends ViewPart {
public static final String ID = "de.vogella.rcp.intro.forms.view";
private TableViewer viewer;
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
return new String[] { "One", "Two", "Three" };
}
}
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);
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
FormToolkit toolkit = new FormToolkit(parent.getDisplay());
// Lets make a layout for the first section of the screen
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginWidth = 2;
layout.marginHeight = 2;
// Creating the Screen
Section section = toolkit.createSection(parent, Section.DESCRIPTION
| Section.TITLE_BAR);
section.setText("Section 1 for demonstration"); //$NON-NLS-1$
section.setDescription("This demonstrates the usage of section");
// Composite for storing the data
Composite client = toolkit.createComposite(section, SWT.WRAP);
layout = new GridLayout();
layout.numColumns = 2;
layout.marginWidth = 2;
layout.marginHeight = 2;
client.setLayout(layout);
Table t = toolkit.createTable(client, SWT.NULL);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 20;
gd.widthHint = 100;
t.setLayoutData(gd);
toolkit.paintBordersFor(client);
Button b = toolkit.createButton(client, "Do something", SWT.PUSH); //$NON-NLS-1$
gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
b.setLayoutData(gd);
section.setClient(client);
viewer = new TableViewer(t);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(getViewSite());
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
}
The result should look like the following:
Thank you
Thank you for practicing with this tutorial.
I maintain this tutorial in my private time. If you like the information please help me by donating or byFriday, July 2, 2010
Eclipse Drag and Drop
Eclipse supports Drag and Drop based on transfer types defined as a subclass "org.eclipse.swt.dnd.Transfer". Every UI element can define if it provides a certain transfer type as source and if it accepts a certain transfer type as destination.
Eclipse provides several pre-defined transfer types, to define your own transfer type it is recommended to subclass "org.eclipse.swt.dnd.ByteArrayTransfer".
The following will demonstrate the usage of drag and drop for your own domain object between two JFace viewers (table and tree). The example in this article will just transfer Text. For a comprehensive description of drag and drop please check the articles in the appendix.
Project
Create a new Eclipse RCP project "de.vogella.dnd.jface" with two views.
See Eclipse RCP For information on how to create Eclipse RCP applications and views.
Model
Create the following model class.
package de.vogella.dnd.jface.model;
public class Todo {
private String shortDescription;
private String longDescription;
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getLongDescription() {
return longDescription;
}
public void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
}
Create the following content providers (Singletons).
package de.vogella.dnd.jface.model;
import java.util.ArrayList;
import java.util.List;
public enum ContentProvider {
INSTANCE;
public ListgetModel(){
Listlist = new ArrayList ();
Todo todo = new Todo("Java", "Learn the Closure proposal");
list.add(todo);
todo = new Todo("Eclipse", "Learn more about the RCP platform");
list.add(todo);
return list;
}
}
package de.vogella.dnd.jface.model;
import java.util.ArrayList;
import java.util.List;
public enum ContentProviderTree {
INSTANCE;
Listlist = new ArrayList ();
private ContentProviderTree() {
list.add("Branch1");
list.add("Branch1");
}
public ListgetModel(){
return list;
}
}