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

Java Developers Almanac

I was seeking JDBC information and I found the Java Developers Almanac.
It has a great collection of snippets, organized by Java packages, very useful, specially for Java new comers.
Here's an example for java.sql package.

./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

Find Java Class Path in the System

Many times one has to know where in the system some Java class is located, usually when one has to perform file operations relative to the class location.

In Java such task is not straight forward to accomplish, so I'm posting here a snippet that will return the full path of the class location.

import java.security.CodeSource;
import java.security.ProtectionDomain;

public class Utilities {
protected static final String getApplicationLocation() {

final ProtectionDomain pd = Utilities.class.getProtectionDomain();
final CodeSource cs = pd.getCodeSource();

return cs.getLocation().getPath().replaceFirst("/", "");
}
}

./M6

RCP Open and Save File Dialogs

I'm currently developing an Eclipse application, and since some of the usual functionalities is the open and save file, or project, I'm posting here a nice code snippet for using open and save dialogs in Rich Client Platform (RCP) applications.

Both the FileOpen and FileSave classes bellow are ready for usage as a command default handler for the extension org.eclipse.ui.commands in the plugin.xml file. Don't forget to create the menu entry to use this command.

Here's the open file snippet (don't forget to update the package)
package your.package.in.here;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
* Opens a file
*/
public class FileOpen extends AbstractHandler implements IHandler {

@Override
public Object execute(ExecutionEvent event)
throws ExecutionException {

Shell shell = PlatformUI.getWorkbench().
getActiveWorkbenchWindow().getShell();

FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterExtensions(new String[] {"*.txt", "*.*"});
dialog.setFilterNames(new String[] {"Text File", "All Files"});
String fileSelected = dialog.open();

if (fileSelected != null) {
// Perform Action, like open the file.
System.out.println("Selected file: " + fileSelected);
}
return null;
}
}


And here's the save file snippet (don't forget to update the package):
package your.package.in.here;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
* Save file as...
*/
public class ProjectFileAs extends AbstractHandler implements IHandler {

@Override
public Object execute(ExecutionEvent event)
throws ExecutionException {
Shell shell = PlatformUI.getWorkbench().
getActiveWorkbenchWindow().getShell();

FileDialog dialog = new FileDialog(shell, SWT.SAVE);
dialog.setFilterExtensions(new String[] {"*.txt", "*.*"});
dialog.setFilterNames(new String[] {"Text File", "All Files"});
String fileSelected = dialog.open();

if (fileSelected != null) {
// Perform Action, like save the file.
System.out.println("Selected file: " + fileSelected);
}
return null;
}
}

./M6

Wolfram, Computational Knowledge Engine

From time to time, I see something that is totally mind blowing.

The last time it was 6th Sense, a couple of months ago, and not it's Wolfram|Alfa, Computational Knowledge Engine.
It's a real knowledge engine, in the sense that it knows things, it knows a lot of stuff about things. And better yet, it uses the knowledge about things to enrich the answer, from simple useful information up to correlations.

This is truly amazing, one has to see it in order to see it in order to fully understand it.

I can hardly wait to use it.

./M6

Is IT Doing a Bad Job for Business?

I was following a LinkedIn discution about what areas of IT managment gives most problems and there was a very interesting oppinion about where all the listes problems really came from.


Robert Hossary said that
"departments have mostly lost the ability to understand business processes. They do not research the tools required to best suit a process or solve a problem. To many of these departments and their personnel the Operating System or specific program becomes a religion and therefore they cannot see beyond the brand.
[...]
If IT did their research and embraced the correct tools for the job rather than the best gadget for cool status then maybe they could solve issues like; resource allocation; project management; ability to sell change to management and train users; working with other departments; working with a small budget and so on."

I must say I tend to agree with this point of view.

I've already wrote about how a bad IT choice ruined and organization productivity. It was one of the worst cases I've remember, and suffered with.
The IT decided to go into full virtualization, this means every one used virtual machines.
Productivity fall, specially in the development teams and even the business managers saw that was ruining their timings and, in some cases, their budgets.
When confronted with that productivity problem, the IT manager said it was the best choice, and it was it. The best choice? For whom?
Not for the organization, that's for sure.
But virtualization was cool back then, so they've just had to have it.

On another occasion, I remember I was trying to persuade my manager to get us a requirement tool for the analysis phase. It was expensive so it was not easy to get approval. But one day we got lucky! We had the chance to get exactly the requirement tool we needed, almost for free! One of the holding companies, to which we were very close, decided not to use the requirement tool anymore, since they moved to something else. So, there was a good chance we could get it cheap, with a one year support service included and all. Sounds great, doesn't it? Well, not for my manager, that stated "if that's not good enough for them, then it's not good enough for us"!
Actually it was excellent for us, but with that "childish" behavior we ended up with no tool at all!

Everyone makes bad decisions. Sometimes those bad decisions come from a bad "home work" and sometimes it just proved not to work as expected, despite all the investigation and tests performed.
When things don't work, what matters is to act upon it in order to fix it and to learn from that. What went wrong and why? Answering those questions and avoiding similar mistakes will result on a much better IT management. This is actually true for any area, not just for IT.

If IT management actually did its job well, then it would be doing a good job for the business. It would have a lot more time to focus on important issues instead of "fighting" in the political arena to defend bad choices. I believe this is also true for any area, not just for IT.

./M6

Online Business, Giving a Hard Time to Customers

Yesterday I was browsing Playmobil web site to check their current offer, products and price.
But when it comes to online business, Playmobile, as many others, does not get it.
One has to go through a painful registration process, including giving your correct zip code and fiscal number, just to see the prices!
This dosen't make any sense.

And today, I was checking a LinkedIn question about displaying the products price to casual visitor. "Should a website display prices and quantity to casual visitors?" was the question.

Why do companies and marketeers keep insisting in give a hard time to customers?
Why do they think one will register in order to see a product price?

In such cases, people just leave and go make business somewhere else.
I know I do!

If you have an online business, actually any business at all, you should give as much information as possible to customers. You should make their life as easy as possible.
I bet those same organizations and marketeers all say they like to make the shopping experience a pleasant one, so that the customer is pleased and return for more business.
Well, where's that philosophy in cases such as the Playmobil web site?

Does the CEO, CFO and, specially, the shareholders know how much they are loosing for sending customers away?

They shoud read the The $300 Million Button article and seriously rethink they online business user interaction.

./M6

ATI Radeon on Linux (Really) Sucks

ATI drivers for Linux never worked well. I have an Mobility Radeon X700 and I've been struggling with it for over 4 years on several Linux distributions.

I'm using Kubuntu for several months now, and on the previous version I was able to use the ATI drivers without the 3D acceleration. The drivers had to be installed from the Hardware Drivers System application. If I tried the official driver downloaded from ATI, it simply crashed.

I've recently upgraded Kubuntu to 9.04, and the Xorg configuration and ATI driver being used, were gone. Once again, I've tried the official ATI driver, version 9.4, and, once again, it crashed the system in such a way that I had to reboot through hardware. I've uninstalled the driver and tried the same procedure that worked on the previous Kubuntu version. It didn't work and it also crashed.
The problem was, I was able to install and uninstall the driver through the Hardware Drivers System application.
So, for two day I've been fighting to make the ATI driver to work or to go back to the previous configuration and driver.
I was unable to make the driver to work, so I spend some hours trying to revert into the previous working configuration.

Finally, I found the answer in the Ubuntu Documentation site, under the Community Documentation section, in the RadeonDriver article. Everyone's having problems with Radeon graphic cards under Linux, specially Ubuntu/Kubuntu, should read it.

Here's the magic command line that made X work again:
apt-get remove --purge xorg-driver-fglrx
The command must be executed as root.

If you've been suffering with ATI bad Linux support and decided not to use the official drivers, but if you wish to keep fighting, check the Radeon article on X.Org Foundation wiki.

./M6