Showing posts with label swt. Show all posts
Showing posts with label swt. Show all posts

May 3, 2008

Single column TableViewer and TableColumnLayout

If you have a single column TableViewer (which is commonly used because ListViewer won't show you the images), the single coloumn won't take the entire space. For example if you run the TableViewer snippet, the output is like this:


To get rid of the other spurious coloum that appears on the right, you have to use the TableColumnLayout. Modifying the code to use that:

public Snippet001TableViewer(Shell shell) {

Composite tableComposite = new Composite(shell, SWT.NONE);
final TableViewer v = new TableViewer(tableComposite);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(new MyContentProvider());
MyModel[] model = createModel();
v.setInput(model);
v.getTable().setLinesVisible(true);

TableColumn singleColumn = new TableColumn(v.getTable(), SWT.NONE);
TableColumnLayout tableColumnLayout = new TableColumnLayout();
tableColumnLayout.setColumnData(singleColumn, new ColumnWeightData(100));
tableComposite.setLayout(tableColumnLayout);

}

Remember, the TableColumnLayout should be applied on the composite that holds the Table. And the composite should contain only the table and nothing else. Here is the result:

Apr 1, 2008

Content type Specific File icons

Eclipse 3.4 M6 is out. It has lots of wonderful features like p2 and the installer. But one small change that is going to save a lot of developers time is the content type specific file icons. Earlier if I had to contribute an icon for a specific file, I've to write my own label decorator. Now Eclipse comes with a label decorator that does the same.

All you have to do is to define the content type and associate an editor with that content type. If you were wondering writing an editor is not a trivial task, don't worry, you can reuse the existing ones.

Assume that in my app, I have some files with extension 'config'. They are nothing but normal properties files. I would like to invoke the Properties editor on this extension and give a different icon. Here is how I create a new content type for the extension associated the Properties editor for that.

<extension point="org.eclipse.core.contenttype.contentTypes">
<content-type base-type="org.eclipse.core.runtime.properties"
file-extensions="config"

id="in.cypal.eclipse.myConfig"
name="My Config File"

priority="normal">

</content-type>

</extension
>
<extension point="org.eclipse.ui.editors">
<editor
class="org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileEditor"
default="false"

extensions="config"

icon="icons/sample.gif"

id="in.cypal.eclipse.editors.myConfigEditor"

name="My Config Editor">
<contentTypeBinding contentTypeId="in.cypal.eclipse.myConfig">
</contentTypeBinding>
</editor>
</extension>





Content types are not just based on file names/extensions. If you have an xml file, you can use the XMLContentDescriber to find the content type based on the root element; create an editor extension; specify the Text Editor/XML Editor class; associate the content id and add the icon to the editor. You are done. All of this without writing a single line of code!

Mar 6, 2008

Eclipse CVS

The CVS Repositories view in Eclipse allows you to paste a CVS URL in the "New Repository Location" Wizard. (If you are on 3.3, you can directly paste on the view without the Wizard) In case you want to browse thru the Eclipse CVS repository, here are the URLs you can copy and paste into the wizard.

Platform, SWT, JFace, Equinox, JDT, launcher, UI, update, search, debug, team, etc) are available here:
:pserver:anonymous@dev.eclipse.org:/cvsroot/eclipse
Mylyn, GEF, CDT, COBOL, VE:
:pserver:anonymous@dev.eclipse.org:/cvsroot/tools
AspectJ, Nebula, EPP:
:pserver:anonymous@dev.eclipse.org:/cvsroot/technology
EMF, GMF, MDT:
:pserver:anonymous@dev.eclipse.org:/cvsroot/modeling
WTP:
:pserver:anonymous@dev.eclipse.org:/cvsroot/webtools
Data Tools:
:pserver:anonymous@dev.eclipse.org:/cvsroot/datatools


If you want to browse thru the CVS, you can use the web based viewer also: http://dev.eclipse.org/viewcvs/
More info on connecting to Eclipse CVS: http://wiki.eclipse.org/index.php/CVS_Howto

Related:
Searching Eclipse Sources

Feb 14, 2008

FullScreen mode in SWT/RCP Application

If you didn't know the Eclipse Summer of Code gave us a nice addition - Full Screen mode for SWT (From Eclipse 3.4 only. Won't work with 3.3 or earlier). No tricky API, just a getter/setter. Shell.setFullScreen(true) Go play!

Related:
How to add TrayItem in Eclipse RCP application?
How to add a status bar in an RCP app?

Feb 11, 2008

Eclipse Icons - follow up post

Follow up 1:
Ben had a nice script for fetching all the icons from the cvs. I'm adding it here:

#!/bin/sh

CVSROOT=:pserver:anonymous@dev.eclipse.org:/cvsroot/eclipse
export CVSROOT

mkdir -p eclipse
cd eclipse
cvs -q co org.eclipse.debug.ui/icons
cvs -q co org.eclipse.pde.ui/icons
cvs -q co org.eclipse.jdt.ui/icons
cvs -q co org.eclipse.vcm.ui/icons
cvs -q co org.eclipse.team.ui/icons
cvs -q co org.eclipse.ant.ui/icons
cvs -q co org.eclipse.help.ui/icons
cvs -q co org.eclipse.ui/icons
cvs -q co org.eclipse.ui.views/icons
cvs -q co org.eclipse.ui.console/icons
cd ..
rm -f ~/public_html/eclipse-icons.zip
find eclipse -name "*.gif" -print | zip ~/public_html/eclipse-icons.zip -@

Follow up 2:
I remember Ben having a HTML page which lists all the icons. But due to bandwidth issues, he moved it to a zipped set of icons. Now that is also gone from the page. I've a back up of those icons. So if you want the complete set of icons, instead of the most commonly used ones, download them here.

Follow up 3:
Phillipus left a comment in the post saying how to access the icons from other plugins without bundling them in your plugin:

AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipse.ui", "$nl$/icons/full/etool16/import_wiz.gif");

This is cool but raises an interesting question. What happens during an Eclipse upgrade? Will they be retained in the same name? Or rather a straight question: Are these icons part of API? I didn't know the answer. So posting the question here. Those who know please throw some light on this.

Related:

Eclipse Icons - original post
Searching for Eclipse - source, blogs, images ...
Accessing CVS of Eclipse.org

Feb 5, 2008

Eclipse on Mac OS Leopard

In case you are experiencing frequent crashes of Eclipse on Mac OS Leopard, here is the simple solution: update your SWT plugins from the latest stream build. Looks like it still has few issues, so make sure to back up the originals before replacing (Does Time Machine help?). More details are available here.

PS: I'm a happy Tiger user still searching for solid reasons to spend for $129 upgrade

Jan 27, 2008

How to add TrayItem in Eclipse RCP application?

In an application like Yahoo Messenger or Skype, you don't want to quit the application when the main window is closed. Most of these applications will move themselves to System Tray when the main window is closed and clicking on the Tray icon would bring back the window. Lets try to see how to do it in an Eclipse RCP Application.

When the window is closed, the WorkbenchWindowAdvisor.preWindowShellClose() method is called. If this returns false, then the close request will not be honored. So all we need to do is to override this method; create a TrayItem; hide the window and return false.


trayItem = new TrayItem(Display.getDefault().getSystemTray(), SWT.NONE);
trayItem.setText("My RCP App");
trayItem.setImage(getAppIcon());
trayItem.addSelectionListener(getSelectionListener());
getWindowConfigurer().getWindow().getShell().setVisible(false);
return false;

In the SelectionListener, we can add code to bring up the window:

Shell workbenchWindowShell = getWindowConfigurer().getWindow().getShell();
workbenchWindowShell.setVisible(true);
workbenchWindowShell.setActive();
workbenchWindowShell.setFocus();
workbenchWindowShell.setMinimized(false);
trayItem.dispose();


Simple :-)

Jan 18, 2008

How to add a status bar in an RCP app?

If you are writing an RCP application, you may want to add a status message in the status bar. Adding it is very simple.

By all chance, you already have a class extending org.eclipse.ui.application.ActionBarAdvisor in your RCP app (If not, Google around to find out how to add one to your RCP app). Just override the fillStatusLine method.

private StatusLineContributionItem statusItem;

@Override
protected void fillStatusLine(IStatusLineManager statusLine) {

statusItem = new StatusLineContributionItem("LoggedInStatus");
statusItem.setText("Logged in");
statusLine.add(statusItem);
}

Thats it :-)



Note 1: You can add any number of status items
Note 2: In Eclipse 3.3, the class StatusLineContributionItem is in org.eclipse.ui.internal.util package. This has been moved to org.eclipse.jface.action package in Eclipse 3.4

Oct 11, 2007

Removing Widgets from a composite

One of the frequent questions from Swing-to-SWT developers is: What is the equivalent of the Container.remove method in SWT?

AFAIK, there is none. The Composite doesn't have any method to remove its children. But then we have a workaround. We can dispose the child widget!

Let me give a simple code to explain. The problem statement is simple: Have a Combo box for different UI elements and fill a composite based on the selection. Hope the code is clear and doesn't require any clarifications.

composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));

Label label = new Label(composite, SWT.None);
label.setText("Select Control:");

combo = new Combo(composite, SWT.READ_ONLY | SWT.DROP_DOWN);
combo.add("Text Box");
combo.add("Combo");
combo.add("Radio Button");

combo.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {

if (control != null)
control.dispose();


switch (combo.getSelectionIndex()) {
case 0:
control = new Text(composite, SWT.BORDER);
break;
case 1:
control = new Combo(composite, SWT.DROP_DOWN);
break;
case 2:
control = new Button(composite, SWT.RADIO);
break;
}

GridData data = new GridData(SWT.FILL, SWT.NONE, false, false);
data.horizontalSpan = 2;
control.setLayoutData(data);

composite.layout(true);
}
});

Here is the result: