Showing posts with label jface. Show all posts
Showing posts with label jface. Show all posts

SWT File Dialog

During the development of an Rich Client Platform (RCP) I needed to perform a file browse, here's how to show a file dialog in SWT:

import org.eclipse.swt.widgets.FileDialog;

FileDialog dialog = new FileDialog(this.getShell(), SWT.NULL);
dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
dialog.setFilterNames(new String[] { "Text files", "All files" });
String path = dialog.open();
if (path != null) {
File file = new File(path);
if (file.isFile()) {
System.out.println(file.toString());
}
}

The snippet above filters by text files (*.txt) and all files (*.*) only, and it can be easily applied to any button or file menu option click event.

./M6

JFace TreeView with Manual Expand of TreeItems

I have a FilteredTree with check items and I was trying to expand the tree whenever the user checks an item on it. By default, the tree does only expand a TreeItem when the user clicks on the + icon and retracts when the user clicks on the - icon.
Since this tree has check items, I wanted the tree to expand when the user clicks on the check box.

This proved to be a lot harder than I expected, partially because I was using the wrong object, partially because it is lazy, and partially because it was hard to find an answer for what I was doing wrong.
So, I'm posting here how to manually expand a TreeView, which is part of a FilteredTree.

To begin with, the TreeView requires the following classes to be defined:

1. Your tree node that will hold the data of each tree item:
private class Node {

private final Node parent;
private final String word;
private List children = new ArrayList();

Node(Node parent, String word) {
this.parent = parent;
this.word = word;
if(parent != null) {
parent.children.add(this);
}
}

public Node getParent() {
return parent;
}

public Collection getChildren() {
return new ArrayList(children);
}

public boolean hasChildren() {
return children.size() > 0;
}

public void setChildren(Collection newChildren) {
if(newChildren != null) {
children = new ArrayList(newChildren);
}
}

public String getWord() {
return word;
}

}
Please note that this class can be whatever class you require, this is only an example.

2. A tree content provider that will provide the content for the tree:
private class NodeContentProvider implements ITreeContentProvider {

@Override
public Object[] getChildren(Object element) {
return ((Node)element).getChildren().toArray();
}

@Override
public Object getParent(Object element) {
return ((Node)element).getParent();
}

@Override
public boolean hasChildren(Object element) {
return ((Node)element).hasChildren();
}

@Override
public Object[] getElements(Object inputElement) {
return (Object[]) inputElement;
}

@Override
public void dispose() {
// unused
}

@Override
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
// unused
}

}

3. The label provider that will provide the label for each item in the tree:
private class NodeLabelProvider extends LabelProvider {

private final TreeViewer viewer;

NodeLabelProvider(TreeViewer viewer) {
this.viewer = viewer;
}

@Override
public String getText(Object element) {
Node node = (Node) element;
return node.getWord();
}
}

I'll create a FilteredTree, but since it makes use of TreeView, using only a TreeView is trivial:

PatternFilter patternFilter = new PatternFilter();
FilteredTree filteredTree = new FilteredTree(cTree,
SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CHECK,
this.patternFilter);
filteredTree.setInitialText("type your filter text here");
TreeViewer treeViewer = filteredTree.getViewer();

// Assign the label provider
treeViewer.setLabelProvider(new NodeLabelProvider());

// Assign the content provider
treeViewer.setContentProvider(new NodeContentProvider());

// Populate the tree with elements
Node ex1= new Node(null, "Example 1");
new Node(ex1, "Example 1.1");
new Node(ex1, "Example 1.2");
new Node(ex1, "Example 1.3");
Node ex2= new Node(null, "Example 2");
new Node(ex2, "Example 2.1");
Node[] nodes = Node[] {ex1, ex2}
viewer.setInput(nodes);

// Event for item selection
treeViewer.getTree().addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
TreeItem item = (TreeItem) event.item;
Node node = (Node)item.getData();
// Manually Expand
treeViewer.expandToLevel(node, 1);
treeViewer.update(node, null);
}
});

Please note that the tree selection event always expands the selected node, you may wish to retract it or do another operation, like check all its descendants.
This is actually quite simple, but I was calling treeViewer.expandToLevel with the TreeItem instead of using the Node object, and that was the cause of my problems.

If you're looking for a similar example but using custom images, check Eclipse Nuggets.
./M6

Java by API examples

I was seeking information for the FilteredTree Jface object and I've found a great knowledge repository for Java, SWT, JFace EJB, and other technologies, like C#, C++, Python, SQL Server, and many more.

In the Java By API examples, everything is by example and it's hierarchically organized, which helps a lot when one's exploring Java packages, for instance.

You can take a look at it in http://www.java2s.com/.

./M6

RCP Message Dialog

It looks like every time I need a message dialog in an Rich Client Platform (RCP) application, it takes me too much time finding which JFace dialog class is better for me...

This time I decided to post it here so that the next time I won't waste time.
All the methods needed are statically available in the org.eclipse.jface.dialogs.MessageDialog class:
  • MessageDialog.openConfirm, for a confirmation dialog with an Ok/Cancel button set.
  • MessageDialog.openError, for an error dialog with an Ok button.
  • MessageDialog.openInformation, for an information dialog with an Ok button.
  • MessageDialog.openQuestion, for a question dialog with and Yes/No button set.
  • MessageDialog.openWarning, for warning dialog with an Ok button.
The org.eclipse.jface.dialogs.MessageDialogWithToggle is similar,but allows the user to adjust a toggle setting, like Yes Always/Yes/No or Yes/No/Never.

You can use org.eclipse.jface.dialogs.DialogSettings for a dialog setting, supporting loading and saving of properties in an XML file.

You can use org.eclipse.jface.dialogs.ProgressMonitorDialog to display progress during a long running operation.

And you can design your own dialog windows, just extend the org.eclipse.jface.dialogs.IconAndMessageDialog class.


Note: in RCP, you can get the shell using PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();.

./M6

i18n RCP applications

I'm developing an RCP application, also known as Eclipse Application, with a couple of friends and it is internationalized (i18n) for English, Portuguese and, currently non-officially and incomplete, Spanish.

Currently it is being developed on Europa (3.3.2) and after fetching the i18n Eclipse packages for Portuguese, I was unable to use the i18n files withing the application.
What this means is that the application specific stuff was i18n but the RCP core was not. For instance, the menus were in Portuguese but the welcome page navigation buttons, and all JFace stuff and alike, were in English.

I've digged for "awhile" in the official RCP documentation, FAQs, How-Tos, and non-official stuff like news groups, forums and blogs. I've made some posts in some-what official RCP fóruns/news groups but I haven't got a single answer.

After almost two weeks fighting with this problem, I've finally solved it!
It's actually quite easy and all it requires is a couple mouse clicks.

Here's what it takes to i18n the core of an RCP application:
  1. Download the language packages from the Babel project using the Eclipse install/update mechanism.
  2. Open the RCP application ".product" file.
  3. Go to the "Configuration" section.
  4. Press "Add Required Plug-ins" button.
This last step is the "magical step", it includes the i18n plugins to the RCP application.
You can identify the language plugins by their name. They come in the "<plugin>.nl_<language>" format. For instance, Portuguese JFace translation file is "org.eclipse.jface.nl_pt".

I sure hope this information helps others and save their time.

./M6