poniedziałek, 19 lipca 2010

Build SWT application with maven

There are couple of issues when trying to use SWT with application using Maven for building and deploying. The most fundamental is that you have to deliver separate application per architecture and windowing system. Another one is the fact there are no up-to-date releases of SWT in common Maven repositories. And finally when trying one platform independent binary all handles platforms must be include particular SWT implementations, but here another issue appears: how to put appropriate SWT on class path.

First lets populate our local maven repository with SWT libraries. The first step is to obtain desirable swt implementation for os/ws/arch triples. Lets assume that for needs of this example we limit supported triples to linux/gtk/x86_64, win32/win32/x86, win32/win32/x86_64.

Best can be use p2 director to grab libraries. There is not easy way for importing all swt implementations into one location therefore here you are somewhat weird workaround - each impl needs to be grabbed into separate location.

director/director -consolelog -r http://download.eclipse.org/releases/helios 
-d SWT_GTK -i org.eclipse.swt.gtk.linux.x86
director/director -consolelog -r http://download.eclipse.org/releases/helios 
-d SWT_GTK64 -i org.eclipse.swt.gtk.linux.x86_64
director/director -consolelog -r http://download.eclipse.org/releases/helios 
-d SWT_WIN -i org.eclipse.swt.win32.win32.x86 -p2.os win32 -p2.ws win32 -p2.arch x86
director/director -consolelog -r http://download.eclipse.org/releases/helios 
-d SWT_WIN64 -i org.eclipse.swt.win32.win32.x86_64 -p2.os win32 -p2.ws win32 -p2.arch x86_64

Note that specifying profile details is needed for non-native implementations.

In order to use eclipse:to-maven target we need to mimic eclipse installation with plugin folder. For Linux users:

/tmp/SWTLIBS$ find SWT* -name "org.eclipse.swt.*.jar" | xargs -I{} cp {} SWTLIBS/plugins/
/tmp/SWTLIBS$ ls SWTLIBS/plugins
org.eclipse.swt.gtk.linux.x86_3.6.0.v3650b.jar     org.eclipse.swt.win32.win32.x86_3.6.0.v3650b.jar
org.eclipse.swt.gtk.linux.x86_64_3.6.0.v3650b.jar  org.eclipse.swt.win32.win32.x86_64_3.6.0.v3650b.jar

Once done install jars to the maven repository:
mvn eclipse:to-maven -DdeployTo=eclipse.org::default::file:///home/krma/.m2/repository 
-DeclipseDir=. -DstripQualifier=true

After that we can receive following dependency entries:

 <groupid>org.eclipse.swt.gtk.linux</groupId>
 <artifactid>x86_64</artifactId>
 <version>3.6.0</version>
</dependency>
<dependency>
 <groupid>org.eclipse.swt.win32.win32</groupId>
 <artifactid>x86_64</artifactId>
 <version>3.6.0</version>
</dependency>
<dependency>
 <groupid>org.eclipse.swt.win32.win32</groupId>
 <artifactid>x86</artifactId>
 <version>3.6.0</version>
</dependency>
<dependency>
 <groupid>org.eclipse.swt.win32.win32</groupId>
 <artifactid>x86_64</artifactId>
 <version>3.6.0</version>
</dependency>

But with such configuration there will be one problem. First of all all the SWT implementation will be included to our build. But this is actually not really true as our artifact names are duplicated.

The solution is to force other naming for artifactID by manual installing each jar into local maven repository
mvn install:install-file -Dfile=org.eclipse.swt.gtk.linux.x86_3.6.0.v3650b.jar -DgroupId=org.eclipse -DartifactId=swt.gtk.linux.x86 -Dversion=3.6.0 -Dpackaging=jar -DgeneratePom=true -Dmaven.repo.local=/path/to/mvnrepo

Now the following can be used:
<dependency>
 <groupid>org.eclipse</groupId>
 <artifactid>swt.gtk.linux.x86</artifactId>
 <version>3.6.0</version>
</dependency>
<dependency>
 <groupid>org.eclipse</groupId>
 <artifactid>swt.gtk.win32.win32</artifactId>
 <version>3.6.0</version>
</dependency>

piątek, 28 maja 2010

How to setup Trac for LDAP authentication

First all of I'd like mentioned i'm not Apache master thus do not take responsibility for potential damages whilst following this tutor. But don't be afraid nothing wrong should happen ;).

But to the point. When we decide to install Trac as Apache/httpd application it comes with some basic authentication based on unix password. The following is typical configuration:

<virtualHost *:80>

  ServerName trac.example.com

  <location />
    SetHandler mod_python
    PythonHandler trac.web.modpython_frontend
    PythonOption TracEnv /var/www/trac
    PythonOption TracUriRoot /
  </location>
  <location "/login">
    AuthType Basic
    AuthName "trac"
    AuthUserFile /var/www/trac/auth-file
    Require valid-user
  </location>

</virtualHost>

For switching to LDAP authentication use following:

<virtualhost *:80 >

 ServerName trac.example.com

 <location />
  SetHandler mod_python
  PythonHandler trac.web.modpython_frontend
  PythonOption TracEnv /var/www/trac
  PythonOption TracUriRoot /
 </location>

 <location "/login" >
   AuthType Basic
   AuthBasicProvider ldap
   AuthzLDAPAuthoritative off
   AuthLDAPBindDN "DOMAIN\\BINDUSER"
   AuthLDAPBindPassword PASSWORD
   AuthLDAPUrl LDAPUTL
   AuthName "Authorization required"
   Require valid-user
 </location>

</VirtualHost>


BindDN - the Distinguished Name binddn to bind to the LDAP directory

DOMAIN - name of the domain
BINDUSER - special bind user for accessing non-public data
PASSWORD - password for BINDUSER
LDAPURL - ldap://HOST:389/SEARCHBASE?sAMAccountName?sub

SEARCHBASE - the starting point for the search

The following command can be used to test your LDAP connection:

ldapsearch -h HOST -b "SEARCHBASE" -D "DOMAIN\\BINDUSER" -s sub -x -w PASSWORD "(givenName=K*)"

piątek, 9 kwietnia 2010

IHandler - How to obtain current selection based on ExecutionEvent object

The most common method of implementing custom handler is to subclass AbstractHandler. This very handy class reduce our work to fill just one method: execute(ExecutionEvent event). The entire stub looks as following:

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

public class MyCustomHandler extends AbstractHandler {

 @Override
 public Object execute(ExecutionEvent event) throws ExecutionException {
  // TODO Auto-generated method stub
  return null;
 }
}

The question is how to obtain selection? Actually it is fairly simple and needs employ HandlerUtil class. This class is particularity interesting as delivers couple of utility method for working with ExecutionEvent object.

Two exemplary methods for obtaining current selection looks as below:

// the most straightforward ;>
ISelection selection = HandlerUtil.getCurrentSelection(event);

// and another more elaborate
ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();

ExecutionEvent javadoc:

The data object to pass to the command (and its handler) as it executes. This
carries information about the current state of the application, and the
application context in which the command was executed.

An execution event carries three blocks of data: the parameters, the trigger,
and the application context. How these blocks are used is application
dependent. In the Eclipse workbench, the trigger is an SWT event, and the
application context contains information about the selection and active part.

more..

And someshing more about handlers: org.eclipse.ui.handlers

piątek, 2 kwietnia 2010

How to force line numbering when opening a file with TextEditor?

Generally Eclipse workbench use global setting for line-numbering feature. Thus one need to perform following code to on the numbering.


IPreferenceStore store = EditorsUI.getPreferenceStore();
store.setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER, true);

But remember: DO not happing others by force ;). Thus if for some reason you decide your editor absolutely needs the numbering, take care to reset the setting then close the editor.

static private String NUMBERING = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER;
private boolean numbering;

...

IPreferenceStore store = EditorsUI.getPreferenceStore();
numbering = store.getBoolean(NUMBERING);
store.setValue(NUMBERING, true);

...

// when dispose
IPreferenceStore store= EditorsUI.getPreferenceStore();
store.setValue(NUMBERING, numbering);
  
super.dispose();


Eclipse - Workspace Cannot Be Locked (former: Workspace in use or cannot be created)

Full error message:
Could not launch the application because the associated workspace is currently use by another Eclipse application.

Basically the following should do the job:
rm $WORKSPACE_DIR/.metadata/.lock

Quick explanation:
Each time you run Eclipse with particular workspace it is locked. Mechanism of that is quite simple. It is just a .lock file in .metadata directory. Thus the only one need to do in order to unlock workspace is to delete .lock file. Usually such happens when Eclipse hangs and you need restart your machine.

środa, 31 marca 2010

How to work with Selection Service and make your WorkbenchPart more cooperative

The whole power of Eclipse platform comes from its cooperation features among of all it part. We used to see all views and editors work together seamlessly. But when comes to develop our own view or editor we found our part not so eager cooperate with environment. Many of the users found this as big disadvantage of your contribution and will not feel comfortable using it. To avoid you part an alien please let me share some common issues I faced during my advantage with Eclipse plug-in development.


1). Make your view open to cooperate with other parts and share own selection events.

Assuming your XXViewer implements ISelectionProvider interface (common to all objects that provide a selection).

public interface ISelectionProvider {
    public void addSelectionChangedListener(ISelectionChangedListener listener);
    public ISelection getSelection();
    public void removeSelectionChangedListener(ISelectionChangedListener listener);
    public void setSelection(ISelection selection);
}

You should set your view (more precisely your XXViewer) as a Selection Provider. Thus any other part listening on selection service can hear your selection events.

public void createPartControl(Composite parent) {
  viewer = new XXViewer(..);
  getSite().setSelectionProvider(viewer);
}

2). Make your view be aware of the others selection events. Don't let your contribution to be ignorant - listen others.

To do so you need to register your workbench part with the selection service.

private ISelectionListener listener = new ISelectionListener() {
  public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
   doSomeActionHere(IWorkbenchPart sourcepart, ISelection selection);
  }
 };

Also please remember about removing listener when part's dispose. It's really important!!

public void dispose() {
 getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(listener);
 super.dispose();
}

Ok, now you can be absolutely sure that your contribution will be full-rights member of Eclipse community.

sobota, 18 lipca 2009

Eclipse PDT "No completions available"

At the beginning make sure you have successfully added appropriate libraries directory in Project Properties (right click on project root -> Properties -> PHP Include Path (Libraries)). Eg. in case of Zend Framework assuming you extract archive to zf directory, the actual libraries(those in Zend directory) are in "library" subdirectory, thus that is exactly what we need to add to Libraries pane (should be displayed as follows: [path_toextracted_archive]/zf/library).

Sometimes even though you have properly added all libraries, auto-completion still seems does not work. What is the reason? I found that auto-completion tool is quite smart, and basically analyse the context the cursor is in. Context describes where precisely we are (inside class, inside method, just inside tags, etc. The completion engine works in computed context delivered to the user only available in that very context proposals.

Another important issue is that PHP as dynamic language is taught to deliver completion mechanism, especially for code written in "more dynamic" way. In many cases type of the expression can be known at runtime, thus not possible for IDE to know the type without deep analysis of entire application. PDT comes with support from PHPDoc blocks, where user can be told of the this particular method return type.