* You are viewing the archive for the ‘Eclipse’ Category

Vista glass in SWT application - continuation

In my previous post about Vista glass I’ve introduced very simple way to enable DWM glass in SWT. Since that time, I did a lot of experiments how one can improve that experience, and I’ve found solutions to almost all issues with DWM/Glass/Vista in SWT. This post became much bigger than I’ve anticipated, therefore it’s divided into “text after more…” :-)

First, let’s see some screenshot of application in work:

Vista native look for SWT - glass

Continue Reading »

Combo Box with Images in SWT

For past few days I’ve been working on new SWT widget called ComboBoxEx. This widget is capable of displaying items in Combo along with assigned images. At current state it’s more or less completed for Windows (win32) SWT. Code base for this widget is derived from SWT Combo class. Windows widget used in this class has class name “COMBOBOXEX32″ and was introduced long time ago by Internet Explorer 4.0 and is currently available for all Windows versions.

Currently widget is only available on Windows, but I’m planning to provide separate classes for use in other major operating systems (GTK and MacOs) that will encapsulate Combo widget with same interface, e.g. ComboItem instead of String based item.

Here’s the widget example as it is drawn by operating system on Windows XP.

ComboBoxEx32 on Windows XP

Similar image will be posted for Windows Vista, later today. Here’s an image from Windows Vista.

ComboBoxEx32 on Windows Vista

Code for example shown above:

ComboBoxEx combo = new ComboBoxEx(shell, SWT.READ_ONLY);
combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new ComboItem(combo, "Question Icon 32×32", display.getSystemImage(SWT.ICON_QUESTION), 0);
new ComboItem(combo, "Error Icon 32×32", display.getSystemImage(SWT.ICON_ERROR), 0);
combo.select(0);

Unfortunately this widget has a dependency on Java Native Access library from Sun. Let me explain. OS class that has native method declared doesn’t provide COMBOBOXEXITEM structure and appropriate SendMessage method that takes this structure as a LWORD parameter. Without this changes we’re not able to send COMBOBOXEXITEM structure to underlying widget.

I’ll try to release this widget within 2 weeks first for Windows, after that for other operating systems.

Using Vista’s UAC in SWT

UAC allows users to launch specific applications with elevated privileges, e.g. Changing some system settings, or launching an application that needs to have access to files not in your profile (e.g. C:/Program Files/ directory). To accomplish that we need to do two things.

First is to launch application with elevated privileges, it’s quite easy to do, we need to use OS.ShellExecuteEx and SHELLEXECUTEINFO in which there’s a field called lpVerb. This field needs to have runas string. Here’s the example code (it’s modified version of launch(String) method from Program class.

/**
 * Launches the operating system executable associated with the file or
 * URL (http:// or https://) with elevated privileges.  If the file is an
 * executable then the executable is launched.  Note that a
 * <code>Display</code> must already exist to guarantee that this method
 * returns an appropriate result. This method waits on OS.ShellExecuteEx
 * until user close UAC warning. This works only with Windows Vista and
 * higher. On lower versions of Windows there will be no visible difference.
 *
 * @param fileName the file or program name or URL (http:// or https://)
 * @return <code>true</code> if the file is launched, otherwise
 *         <code>false</code>
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT when fileName is null</li>
 * </ul>
 */

public static boolean launchElevated (String fileName) {
    if (fileName == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
   
    /* Use the character encoding for the default locale */
    int /*long*/ hHeap = OS.GetProcessHeap ();

    TCHAR buffer = new TCHAR (0, fileName, true);
    int byteCount = buffer.length () * TCHAR.sizeof;
    int /*long*/ lpFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
    OS.MoveMemory (lpFile, buffer, byteCount);

    TCHAR verbBuffer = new TCHAR (0, "runas", true);
    int verbByteCount = verbBuffer.length () * TCHAR.sizeof;
    int /*long*/ lpVerb = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, verbByteCount);
    OS.MoveMemory (lpVerb, verbBuffer, verbByteCount);

    SHELLEXECUTEINFO info = new SHELLEXECUTEINFO ();
    info.cbSize = SHELLEXECUTEINFO.sizeof;
    info.lpFile = lpFile;
    info.lpVerb = lpVerb;
    info.nShow = OS.SW_SHOW;
    boolean result = OS.ShellExecuteEx (info);

    if (lpFile != 0) OS.HeapFree (hHeap, 0, lpFile);
    if (lpVerb != 0) OS.HeapFree (hHeap, 0, lpVerb);

    return result;
}

Second thing is visual approach. All Vista’s applications that tries to elevate privileges have shield icon as button image. We don’t need to provide this icon by ourselves, we just simply send a message BCM_SETSHIELD to button widget with lParam 1 (true) or 0 (false).

OS.SendMessageW(button.handle, BCM_SETSHIELD, 0, 1);

Since BCM_SETSHIELD is not available in OS class (I wonder why?) we need to declare it:

public static final int BCM_SETSHIELD = OS.BCM_FIRST + 0xc;

Here’s obligatory screenshot of BCM_SETSHIELD message in use. In this screenshot there’s an example of retrieving icon from my previous post.

Launch notepad.exe elevated

Hope it will help, it did me :-)

Extracting executable file icon under SWT

Recently one of my colleagues at work found out that in pure SWT you’re not able to extract icon from an executable file, what you can only do is to get an icon from a file that is associated with specific file type (extension). So, I decided to write a method of my own, and here is it.

public ImageData getExecutableFileIcon(String fileName, boolean large)
{
    int nIconIndex = 0;
    ImageData imageData = null;

    TCHAR lpszFile = new TCHAR(0, fileName, true);
    int /*long*/ [] phiconSmall = new int /*long*/[1];
    int /*long*/ [] phiconLarge = new int /*long*/[1];
    OS.ExtractIconEx(lpszFile, nIconIndex, phiconLarge, phiconSmall, 1);

    if (phiconSmall[0] != 0 && !large)
    {
        Image image = Image.win32_new(null, SWT.ICON, phiconSmall[0]);
        imageData = image.getImageData();
        image.dispose();
    }

    if (phiconLarge[0] != 0 && large)
    {
        Image image = Image.win32_new(null, SWT.ICON, phiconLarge[0]);
        imageData = image.getImageData();
        image.dispose();
    }

    return imageData;
}

At this moment I don’t really know whether we should call OS.ExtractIconEx for both images, or depending on the flag ask for one of them, dunno.

Although, I have a question for Eclipse community, why similar functionality isn’t available in SWT? Or if it is, where to find it?

Vista glass in SWT application

Desktop Java applications by definition are/should be portable to multiple operating systems. Writing with SWT refines set of available operating systems, but it also gives you some opportunities to make application aware of operating system and blends it visually. With Windows Vista’s new Desktop Window Manager we are able to create visually more stunning and eye catching applications. Look at the Chrome screenshot below, notice the decoration of window, it’s transparent and blurred:

Chrome

You can actually do similar thing in SWT (under Windows Vista only) and here’s a small example screenshot:

Vista glass in SWT

So, what do we need to do to accomplish that? It’s quite easy, as a matter of fact, but requires nasty access to internal OS class which makes it SWT windows only!

/**
 * Applies vista glass frame - works only on Vista with Areo.
 */

private void applyVistaGlassFrame()
{
    int glassHeight = 100;

    MARGINS margins = new MARGINS();
    margins.cyTopHeight = glassHeight;

    OS.DwmExtendFrameIntoClientArea(shell.handle, margins);
}

Example above will add 100 pixels of glass area at top of the dialog.

But that’s not enough, we need also to cover glass area with black color, for this we can add Composite widget that will have background color set to SWT.COLOR_BLACK.

Unfortunately this functionality is not 100% covered by SWT. Additional checks against Vista needs to be done. We also need to know if Desktop Window Manager’s Compositions are enabled. Unfortunately we don’t have access to DwmIsCompositionEnabled (it’s not declared in OS class).

While writing this article, I’ve came up with idea how this can be used in Eclipse - about dialog. Recent changes to about dialog are great, but what if go a little bit further and make top of the dialog similar to this, but under Windows Vista with composition instead of white background use glass?

Icons in preferences dialog in Eclipse

When you’re working on custom RCP based applications you’re sometimes forced to change visual aspects of parts of the application that are provided by Eclipse RCP itself. Preferences is a good example of that visual aspect. Since we want to reuse code from RCP as much as possible we decided to use JFace preferences dialog.

After implementing first pages we were pointed out that items should have icons next to label. I thought I would have to patch JFace plug-in to achieve that, but I was wrong! Preferences dialog (and therefore Properties dialog) have undocumented functionality of showing image next to item on the tree view and above content composite. No coding is required to achieve that!

Here’s how preference page (in this case wklej.org plug-in’s page) is shown

Preferences without icons

After adding attribute icon to extension point we’re presented with nice image

Preferences with icons

And here’s modified extension point entry in your plugin.xml (or fragment.xml):

<extension point="org.eclipse.ui.preferencePages">
    <page
        class="org.example.plugin.preferences.ExamplePreferencePage"
        id="org.example.preferencepage" name="Preference page with image"
        icon="images/example.png">

    </page>
</extension>

Notice that Eclipse will show you a warning that icon element is illegal. Fortunately it just a warning, and extension point org.eclipse.ui.preferencePages does support images (icons) but it just doesn’t expose this publicly.

As you can see on the screenshots above, placing image in a plug-in that will be used in standard Eclipse IDE just looks wrong, I agree. But for a custom applications, when you’re providing all pages, it looks great and helps non-developers to find what they’re looking for.

On the other note, I would like to welcome everyone at Eclipse Planet. It’s a pleasure to be here :-)

Introducing wklej.org integration for Eclipse JDT plug-in

Although I’ve wrote dozens of Eclipse plug-ins, all of them (except those for Eclipse DemoCamp) are during my day job. This time, I’ve managed to find enough free time and wrote simple yet useful plug-in for Eclipse JDT.

wklej.org icon wklej.org integration for Eclipse JDT

Main purpose of this plug-in is to provide convenient way of sharing pieces of code through wklej.org service.
wklej.org QuickFix

You’re able to send selection to wklej.org privately or publicly. To do that, select part of the code you want to share and press Ctrl+1 (Quick Fix context menu item). After successful send plug-in will open browser window/tab with result page from wklej.org. Plug-in also contribute preference page to set up your name displayed on the result page.

Download it here:
org.khrone.eclipse.jdt.wklej_0.1.0 (14.71 KB)
org.khrone.eclipse.jdt.wklej_0.1.0 source code (13.26 KB)

Plug-in requires Eclipse 3.4 Ganymede. First version supports only JDT (Java class files). Future version will support more environments, e.g. PDT, CDT.

Plug-in is published under Eclipse Public License (EPL). Read up on it here.

For a quick installation, drop plug-in file (org.khrone.eclipse.jdt.wklej_0.1.0.200902100044.jar) to dropins folder.

Enjoy and leave comments if you found the plug-in useful.

Example plug-ins from Eclipse DemoCamp are now available

It took a while to find some spare time to write JavaDoc comments (with the exception of org.khrone.example.app.rssview plug-in) but I’ve finally managed to do that and here it is. Simple application that I’ve presented during my presentation.

Download it here:
1.0.0 - org.khrone.example.app (426.08 KB)

RssView plug-in includes software developed by the JDOM Project and Project ROME

Eclipse DemoCamp podsumowanie

Pierwszy wrocławski Eclipse DemoCamp za nami. Jak wynika z blogu Grześka udało nam się osiągnąć zamierzony sukces. W obecnej chwili nie mam zbyt wiele czasu aby napisać troche więcej na temat spotkania, ale postaram się w ciągu paru dni zamieścić kod źródłowy aplikacji które demonstrowałem podczas swojej prezentacji.

Eclipse DemoCamp we Wrocławiu

Eclise & Java

Na sobotę, 25 Listopada, zaplanowane jest pierwsze we Wrocławiu Eclipse DemoCamp. To będzie już moja druga impreza eclipsowa, pierwsza zaś na którym będę pełnił rolę prezentera.

Temat dość obszerny, a mianowicie: “Tworzenie aplikacji w oparciu o Eclipse RCP”.

Planuję swoją prezentacją zainteresować innych programistów do tworzenia aplikacji okienkowych w oparciu o już istniejące fundamenty jakimi właśnie jest Eclipse Rich Client Platform. Postaram się przybliżyć podstawy budowania takiego typu aplikacji, od czego zacząć, co dostajemy gratis, co musimy dopisać samemu, itp.

Ponadto prezentować będą:

  • Jacek Pospychała, IBM Eclipse Support Center - Eclipse RAP
  • Grzegorz Białek, Sygnity - Eclipse Modeling (DSL)

Godzina i miejsce spotkania: 11:00, lokalizacja nie została jeszcze potwierdzona ul. Robotnicza 46 ul. Strzegomska 140.

Zapraszam wszystkich chętnych, a tych co nie mogą, to komunikuje, że wszelkie prezentacje będą dostępne na Wiki Eclipse.