Who’s On Phirst

Official blog of Phurnace Software.

Tag >> Eclipse

Posted by: Shawn Spiars on

The Eclipse Resources Plug-in (org.eclipse.core.resources) is one of the handiest Eclipse plug-ins that no one ever talks about. The Resources Plug-in is rarely talked about because it works behind-the-scenes to enable other plug-ins and APIs to function well. For example, it is the Resources Plug-in that enables the Resource Navigator view to expose a tree hierarchy of all the projects, folders, and files in your Eclipse workspace.

Before we get into the details of the Resources Plug-in it's important to understand the function of the Eclipse Workspace. The Eclipse Workspace is where the Eclipse IDE stores all your development artifacts (Projects/Folders/Files). The Eclipse Workspace's resource model is very similar to a file system. All Workspace resources are backed by a real file or directory in the native file system.

The Resources Plug-in, or API, contains many interfaces to help developers access and manipulate the artifacts within the workspace. Here are a few definitions of the more common interface classes.

 

  • IResource – all artifacts in the workspace are resources
  • IWorkspace – the basis for the Eclipse Platform resource management
  • IWorkspaceRoot – a root resource that represents the top of the workspace hierarchy
  • IContainer – resources which may contain other resources (projects and folders)
  • IProject – a type of resource which groups resources into buildable, reusable units
  • IFolder – may be leaf or non-leaf resources, and may contain files or other folders
  • IFile – files are leaf resources which contain data

If you are a java developer you are probably familiar with the java.io.* class library. The java.io.File class is what java developers normally use when reading and writing files and directories. When you are working with files and directories within the Eclipse workspace you should use the Eclipse Resources API, rather than the java.io.File class. If you use the standard java.io.File class to modify files or directories within the workspace you will need to force a refresh in your viewer so the changes made to the model (resources) are visible to the user.

The screenshot below shows a Package Explorer view exposing the contents of an Eclipse Workspace. This workspace contains a single project named “My Project”. The project contains a single folder named “My Folder”, and the folder contains a single file named “deleteMe.txt”.

 

The following code snippet demonstrates how to use the Resources API to access the project, folder, and file in this workspace example. After getting a handle to the workspace file “deleteMe.txt” we use the IFile delete method to delete the file from the folder.


try {

IWorkspace workspace = ResourcesPlugin.getWorkspace();

IWorkspaceRoot workspaceRoot = workspace.getRoot();

IProject myProject = workspaceRoot.getProject("MyProject");

if (myProject.exists()) {
IFolder myFolder = myProject.getFolder("My Folder");

if (myFolder.exists()) {
IFile myFile = myFolder.getFile("deleteMe.txt");

if (myFile.exists()) {
myFile.delete(true, true, null);
}

}

}

} catch (CoreException e) {
}

This is a very simple demonstration of how to use the Resources API to access and manipulate artifacts within the Eclipse Workspace. For more information on the Resources Plug-in and the Eclipse Workspace, check out these links:

Eclipse Wiki - Resources
Eclipse Workspace Team
Eclipse.org

In Eclipse
Comment (0) Read More...


Posted by: Shawn Spiars on

From time to time I visit the Eclipse news groups to find a solution to a problem I am having with a particular area of Eclipse. Since Eclipse is such a huge platform with many projects there are many different news groups, each designed to cover a particular project or API (Application Programming Interface).

Some of my favorite Eclipse news groups are:

eclipse.platform
eclipse.platform.rcp
eclipse.platform.swt

Often while I am looking for answers in a news group I run across questions that I have already conquered and so I try to give back and help others. Recently I responded to a question about how to perform in-line editing within the cell of an SWT (Standard Widget Toolkit) table. I have included my example java program below. If you are a Java developer and somewhat familiar with SWT/JFace then you should have no problems understanding the example code. If you are new to SWT/JFace I have provided a brief summary of the primary components used to create the example.

Looking at the java code below you will see that the program contains a main method to launch the java program. The primary UI component is an SWT Table which is added to an SWT Composite. Under the table I have added SWT buttons to “Add” and “Remove” rows from the table. The SelectionListeners on the buttons handle the add and remove actions. The model for the table is a POJO (Plain Old Java Object) which I have named “Row”. A TableContentProvider and TableLabelProvider provide the content and labels for the table and the TextCellEditor provides the ability to edit the table allowing a user to type data directly into the table’s cells.


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
* This is an example program showing how to use a TableCellEditor
* to allow direct editing of a table's cell.
*
* @author Shawn Spiars
*
*/

public class TableCellModifierExample {

private Table table;
private TableViewer tableViewer;

private final static String[] COLUMN_HEADINGS = {"Property",
"Value"}
;

public static void main(String [] args) {
new TableCellModifierExample();
}

public TableCellModifierExample() {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setText("TableCellModifier Example");
shell.setLayout(new FillLayout());

createContents(shell);

shell.setSize(400, 400);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}

protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
layout.verticalSpacing = 10;
composite.setLayout(layout);
GridData data = new GridData(GridData.FILL_BOTH);
data.grabExcessHorizontalSpace = true;
composite.setLayoutData(data);

table = new Table(composite, SWT.BORDER | SWT.V_SCROLL
| SWT.MULTI | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
data = new GridData(SWT.FILL, SWT.FILL, true, false);
data.heightHint = 300;
table.setLayoutData(data);

TableLayout tableLayout = new TableLayout();
table.setLayout(tableLayout);

tableLayout.addColumnData(new ColumnWeightData(10, 100,
true));
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(COLUMN_HEADINGS[0]);
column.setAlignment(SWT.LEFT);

tableLayout.addColumnData(new ColumnWeightData(15, 200,
true));
column = new TableColumn(table, SWT.NONE);
column.setText(COLUMN_HEADINGS[1]);
column.setAlignment(SWT.LEFT);

tableViewer = new TableViewer(table);
tableViewer.setColumnProperties(COLUMN_HEADINGS);
tableViewer.setContentProvider(new
TableContentProvider());
tableViewer.setLabelProvider(new TableLabelProvider());

CellEditor[] editors = new CellEditor[2];
editors[0] = new TextCellEditor(table);
editors[1] = new TextCellEditor(table);
tableViewer.setCellEditors(editors);
tableViewer.setCellModifier(new TableCellModifier());

Composite buttonComposite = new Composite(composite,
SWT.NONE);
FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
fillLayout.spacing = 10;
buttonComposite.setLayout(fillLayout);

Button addButton = new Button(buttonComposite,
SWT.PUSH);
addButton.setText("Add");
addButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Row row = new Row("", "");
tableViewer.add(row);
table.setTopIndex(table.getItemCount());
table.select(table.getItemCount()-1);
tableViewer.editElement(row, 0);
}
});

Button removeButton = new Button(buttonComposite,
SWT.PUSH);
removeButton.setText("Remove");
removeButton.addSelectionListener(new
SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ISelection selection =
tableViewer.getSelection();
if (selection instanceof
IStructuredSelection) {
Iterator iterator =
((IStructuredSelection)selection).iterator();
while(iterator.hasNext()) {
Object obj = iterator.next();
tableViewer.remove(obj);
}
}
}
});

return composite;
}

public class TableLabelProvider extends LabelProvider
implements
ITableLabelProvider {
public Image getColumnImage(Object element, int
columnIndex) {
return null;
}

public String getColumnText(Object element,
int columnIndex) {
Row row = (Row) element;

switch
(columnIndex) {
case 0:
return row.getKey();
case 1:
return row.getValue();
}

return null;
}

}

public class TableContentProvider implements
IStructuredContentProvider {
public Object[] getElements(Object parent) {
List results = new ArrayList();
if (parent instanceof ArrayList) {
results = (ArrayList) parent;
}
return results.toArray();
}

public void dispose() {
}

public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
}

}

class TableCellModifier implements ICellModifier {

public boolean canModify(Object element,
String property) {
return true;
}

public Object getValue(Object element,
String property) {
Object result = null;

Row row = (Row) element;

List list = Arrays.asList(COLUMN_HEADINGS);
int columnIndex = list.indexOf(property);

switch (columnIndex) {
case 0:
result = row.getKey();
break;
case 1:
result = row.getValue();
break;
}

return result;
}

public void modify(Object element, String property,
Object value) {
List list = Arrays.asList(COLUMN_HEADINGS);
int columnIndex = list.indexOf(property);

TableItem tableItem = (TableItem) element;
Row row = (Row) tableItem.getData();

switch (columnIndex) {
case 0:
String key = (String) value;
if (key.length() > 0) {
row.setKey(key);
}
break;

case 1:
String v = (String) value;
if (v.length() > 0) {
row.setValue(v);
}
break;
}
tableViewer.update(row, null);
}

}

private class Row {
private String key;
private String value;

public Row(String key, String value) {
setKey(key);
setValue(value);
}

public void setKey(String key) {
this.key = key
}

public String getKey() {
return key;
}

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

}

In SWTJFaceSWTStandard Widget ToolkitEclipse
Comment (0) Read More...


Posted by: Alexander Bibighaus on

Switching IDE's has always been something I feel is important to keep an open mind about. I learned this early on when I was an emacs fanatic. I started software development in a Unix/C environment where emacs and vi were the two editors of choice (or debate). Despite all of the vi/emacs wars , my experience was that both came in handy, but at different times.

I recently feel this way about Netbeans vs Eclipse in the Java IDE world. Both IDE's contribute different tools to a Java developer that are extremely useful.

Let's take Eclipse for example. I like Eclipse's efficiency. Everything takes less clicks! In addition, the SWT toolkit still feels and performs better than any Java application out there. Netbeans continues to have small quirks about it that cause discomfort for the user because the application does not behave as a normal native application. Something as simple as managing the cursor in the code does not feel natural to me. So, the “Everyday IDE” still goes to Eclipse.

On the flip side, Netbeans has far more integrated tools and wizards than Eclipse. Netbeans has a rich set of wizards and tools for mobility, J2EE, Web development etc. Moreover, the Java Profiler is excellent! The best part about Netbeans is that it is one coherent tool from a single vendor. The “Most Complete IDE” goes to NetBeans.

Netbeans Visual Development tools are the first visual tools for Java I can say I enjoyed. The best part is the ability to integrate manually written code with the visual designer and vice-versa. In addition, the visual designer generates simple code. The generated code is straightforward Java that anyone unfamiliar with the visual tools would understand. The “Most Innovative IDE” goes to NetBeans for finally giving Java a set of useful visual tools.

Eclipse has a one big leg up on Netbeans. Eclipse RCP is a fantastic development framework. It is rich in features and has a unifying effect on product development. Anyone thinking about developing a desktop application should seriously consider Eclipse RCP. The “Best Application Development IDE” goes to Eclipse because you should be developing RCP applications in that case.

In conclusion, just as vi and emacs both brought different ideas as an editor, it is worth the effort to learn both. You will find that rather wasting your time arguing for one or the other, you just have more tools in you tool chest since you tried them both!

In NetbeansIDEEclipse
Comment (0) Read More...


Posted by: Shawn Spiars on

It seems like there are more visual GUI builder products available on the market today than ever before. Visual GUI builders propose an easier way to design and create user interface components. They usually consist of a palette from which you select and drag controls or widgets onto a window, frame, or dialog. Then they provide a table or list where you can manipulate the properties of your controls (size, font, color, coordinates, etc) without requiring the user to know the subtleties’ of the syntax or layout managers. Here is a really good link if you are interested in Java GUI Builders.

Call me old fashion, but I always prefer a language editor (HTML, XML, Java, etc…) over a visual editor because I want to understand the commands I write, and I want to format and organize the code in a way that is meaningful to me so I can easily edit the code later. A good language editor will have syntax highlighting, syntax checking, command completion and various other features to assist you in developing the code, but it won’t generate the substance of the code for you.

Another type of code generation tool falls into the Model Driven Development (MDA) category. With MDD, logical models are used to capture design decisions and generate code, and sometimes the generated code is user interface code. The problem I have with user interfaces generated from models is that there is no consideration by the machine for who the user is, their technical aptitude, or the way they approach a task (workflow). The entire human experience is missing and the user interface has been reduced to a bunch of domain objects and business rules.

For example, the Eclipse Modeling Framework (EMF) can use a model to quickly generate a cool looking prototype consisting of a relational tree, object listeners, pop-up menus, and a properties view, but this in no way resembles a complex, well designed, user friendly application. And don’t be fooled into thinking you can easily tweak the generated code to get it to behave the way you want it to. I’ve spent hours and hours digging thru layers and layers of cryptic machine generated code trying to find the one line that needs to be changed to get the desired behavior.

There are plenty of MDD advocates out there who will disagree with me. Most of them are experts in modeling, but I’ve never met one who is an expert in user interface design. Since most project leads and architects today are very familiar with UML and modeling tools they often buy into the promise that MDD will save them time on their UI development. This has not been my experience.

If you do choose to use a modeling tool to generate UI code – I recommend you decouple the UI model from the back-end model. Just imagine a project where every time the back-end architect decides to change a relationship in the model the UI mysteriously breaks with no warnings.

“And that’s all I have to say about that.”

-Shawn

In Java GUIEclipse
Comment (0) Read More...


Posted by: Shawn Spiars on

When I started working with Eclipse in 2003 I would just download the plug-ins I needed from the Eclipse website, unzip them into my plugins directory, and restart Eclipse. Sometime later, the Eclipse Update Manager was introduced to help you configure your Eclipse development environment providing the ability to update your existing features and plug-ins and search for new features. I have always found the Update Manager dialogs difficult to understand and use. Customizing the Update Manager to work with my Rich Client Platform (RCP) applications has also been very difficult.

When I heard about the new Equinox p2 provisioning system at EclipseCon this year I was excited that the Update Manager was finally being replaced. However, one thing I have learned when working with Eclipse is to take a “wait and see” approach before adopting the newest and improved APIs and methodologies. So, I have been reading various blogs to see what experiences developers are having implementing p2 in their products. Here are a couple of postings that make me think that p2 may not be quite ready for prime time:

Why Eclipse Equinox P2 Update Manager Sucks

Why Eclipse Equinox P2 Update Manager is not good enough for me yet #2

If you have had a positive experience replacing Update Manager functionality with p2 in your software product I would love to hear about it.

-Shawn

 

In Eclipse
Comment (0) Read More...


Posted by: Shawn Spiars on

 Last week I attended the EclipseCon 2008 conference in Santa Clara, CA.  This was my third EclipseCon and by far my best experience.  ThisEclipseCon year there were a lot more technical sessions to choose from and overall the presentations seemed more professional than in the previous years.  I also liked that the Monday tutorials were included in the overall conference registration fee, rather than an extra charge.

I think the highlight of the conference was the key note speech of Dan Lyon (Fake Steve Jobs).   Here's a quote about Dan from the EclipseCon website.

"At his blog, fakesteve.blogspot.com, Lyons has captured the Zeitgeist, from perhaps the one place it is clearest-the point of view of Steve Jobs. In the tradition of Jonathan Swift and The Onion, he uses a pitch-perfect satirical style to deliver trenchant social commentary, reflecting on everything from the Cult of Steve and the rise of Apple ("Dude, I invented the friggin' iPhone. Perhaps you've heard of it!") to the ubiquitous influence of the tech industry on our everyday lives."

Another key note speaker was Sam Ramji, Director of Platform Strategy at Microsoft.   I think of Microsoft as the Borg and their presence at EclipseCon as another strategy to assimilate the Eclipse Community and open source.

The sessions that I found most interesting were those discussing the Rich AJAX Platform (RAP), The Rich Client Platform (RCP), and the Web Tools Platform (WTP).  I particularly enjoyed the "GWT vs RAP" presentation by Mark Russell and Dan Rubel, discussing the differences between Google Web Toolkit (GWT) and Eclipse Rich AJAX Platform (RAP) development.

If you are a software developer I would highly recommend you check out next year's EclipseCon.                 

In Open SourceEclipse
Comment (1) Read More...