2009/09/29

Notepad++ a great quick editor for Windows

I've been looking for a free (as a beer) good text editor for Windows, specially for quick editing or viewing a file while navigating. Notepad is "free", but too simple (I know Notepad is not free, but once you have Windows installed you don't have to pay for it separately) and there are really wonderful text editors like UltraEdit but they're not free. JEdit is very powerful and free, and with all the plugins you can download for it you can have almost all the functionality you need, but its startup is a bit slow (at least with the minimal set of plugins that I use); if I must work with an ant file for some hours it pays off, but if I'm only navigating through a directory and making very small and fast modifications to a file, it is too much for me.

Searching through the Internet I found this super text editor called Notepad++. It is open source (GPL), it is blazingly fast and it has a great set of features, from which I would emphasize:
  • Syntax highlighting and syntax folding (syntax highlighting can also be user defined)
  • Autocompletion
  • Multidocument and multiview
  • Powerful search/replace, including regular expression search
  • Full drag and drop
  • Bookmarks
  • Macros (you can record sequences and play them back)
I've been using it for some weeks and I can say it rocks! It's my current Windows quick editor. It can even update quite frequently the view of a log file, not as smoothly as the Unix command "tail -f", but enough for my needs.

It is not such a discovery, though. Once I installed it I saw with surprise that some of my colleagues had been working with it for some time. So if you haven't heard about it and you need a quick editor for Windows, take a look at it!


2009/03/17

Dokan: Filesystem in userspace (FUSE) for Windows

As Wikipedia explains, a filesystem is a method for storing and organizing computer files and the data they contain to make it easy to find and access them.

There are many different filesystems, like ext3 (Linux) or NTFS (Windows), only to name a few. These filesystems allow the usual and necessary file operations we do every day: creating, copying, moving or deleting files or directories, listing directories or showing the content of files. We usually do these operations with "real" files and directories of a hard disk or a USB device, but nothing prevents us from doing them on other kind of systems which have not been prepared from the beginning to be used as filesystems in an operating system. An example of this system would be an FTP server. It clearly organizes like a filesystem, with directories, files, attributes of files and so on. So it would be possible to access an FTP server like a hard disk using your favourite file explorer if you were able to mount a "virtual" filesystem which connects automagically to the FTP server and lists the root files and directories when you go to the root of the filesystem, it deletes a file in the FTP server when you delete it in the filesystem and so on.

In fact, this is not new at all. In Linux, there exist currently many different "virtual" filesystems you can mount to ease the access of different types of resources, be them local or remote. What is more, Linux has a package called FUSE (Filesystem in Userspace), which allows mounting your own filesystem without having to make a device driver and compile it. FUSE is based on callback functions you have to implement that will be called when the different operations are invoked from the operating system. There exist a lot of language bindings for FUSE, so you can write your filesystem in Java, C#, Haskell, TCL, Python and many more (besides C or C++, which would be the expected languages for such a low level based feature). With FUSE, writing your own filesystem for Linux can be a much easier task.

But what about Windows ? A Windows port of FUSE has not existed for a long time and, from what I know, nothing similar was available as open source. But on May 2008 a project called Dokan was released as open source (LGPL). This project is the equivalent of FUSE for the Windows platform. You can download it from here. It has bindings for Ruby and .NET. In Yukoba's blog you can find very interesting contributions that enhance this project, like a Java binding (JDokan), MemoryFS (a filesystem on memory) or JSFlickerFS (a filesystem for managing Flicker albums, written in Javascript).

So if you need to write your own virtual filesystem in Windows, you have no excuse...give Dokan a try!

2009/02/01

HTML crosslinked documentation from source code files

I know that modern IDEs can navigate through source code files very easily. But...what if I want to browse through source code files in my Palm TX ? Netbeans, Eclipse and other IDEs are not available for it, but I can read HTML pages offline easily with Plucker. Maybe I simply don't want to open an IDE only to browse the source code, without actually coding.

There are tools that can generate HTML pages from source code, so you can browse through them easily. They can even beautify the code with syntax highlighting and (this is my favourite) provide links between the different elements of the documentation. So, when you browse class Foo and it extends from BaseFoo, you can simply click on the word BaseFoo and you navigate to the class BaseFoo, and so on.

I've been searching for tools of this kind and I found some of them.

I like Doxygen a lot, it can make a wonderful and complete documentation of the source code, including this crosslinking source code navigation. It can optionally use another tool called htags to include the source code with this crosslinking navigation (instead of its own system). And this little tool attracted my attention...htags. It is an extremely simple and fast yet powerful tool that can generate HTML crosslinked documentation from source code.

If you have a Linux (or other *nix), you can simply install htags with a package manager, the whole package where it belongs is global. If you use Windows, you can download global from this site. There are other binaries in the download page of global.

With htags you can go to the source code root directory, write:

htags -g

...and that's all! You have your browsable crosslinked HTML documentation in a directory called HTML. I have tested it with Java files, but it supports other languages as well (C, C++, Yacc, PHP and Assembler).

If you want more features (like class diagrams, Javadoc-style complementary documentation, more flexibility with configuration, etc.), you can use Doxygen. It also supports more languages (C++, C, Java, Objective-C, Python, IDL, Fortran, VHDL, PHP, C#, and to some extent D). But if you only need plain source code with syntax highlighting and crosslinked navigation, give htags a try!

2008/11/08

Calling Eclipse Ant builds from the command line

I usually use Eclipse for my software projects and I wanted to do Continuous Integration (CI), so after choosing a CI tool (I chose Hudson, a very powerful yet easy to use tool) I decided to make an Ant build file from my sample Eclipse project.

Even if you only want to make a very simple Ant task, like compiling and JUnit testing, a difficult problem will arise: the classpath. An Eclipse project can have a complex classpath, full of variable references (which are defined in the Preferences of your workspace), references to other projects, and so on. There are some Eclipse ant tasks in Eclipse itself that may be added, like projectBuild, and they take into account the classpath defined in the project, but when you try to add a JUnit task, you have to specify the classpath by hand in the task parameter. This classpath creation is tedious and time-consuming.

I thought it should be easier the creation of an Ant build from an Eclipse project, because the classpath information is in the .classpath project file. After googling a little, I found this great Eclipse Plugin, called eclipse2ant, which "tries to create a buildfile that matches the Eclipse project as close as possible". And it really works. I could very easily create an Ant build file from my Eclipse project, with the usual targets of compilation and it also exported my last JUnit runs! (the ones that you see in the "Run history..." menu of Eclipse).

All the classpath variables were imported in the Ant file, and I could execute the tests of my project without problems with Eclipse closed. Of course, you may want to tweak a little bit the Ant file to make it more portable or to organize better the variables, but with eclipse2ant you have a very good starting point that saves you a lot of time!

Happy continuous integration!

2008/07/14

EnvyNG saved my day

A week ago I upgraded my Ubuntu Linux from Gutsy (7.10) to Hardy (8.04.1). All seemed to work properly after the upgrade. But then I saw a message in my desktop that notified me the driver I was using was a free one that was not able to manage the 3D acceleration of my card. It suggested me to activate the binary one from NVidia if I wanted those advanced features, so I did it.

When I restarted the system, my monitor was not detected and I couldn't use a high resolution screen (only 800x600). I tried to revert the change but it was not possible. Then I googled a little and could find this interesting thread, where I found a magical solution: EnvyNG.

I installed its GTK+ package (envyng-gtk) with Synaptic, executed its GTK+ app, followed the instructions of its wizard (very easy to use) and after a restart...voilà! My desktop works like a charm again !!!

The description of the EnvyNG package is:
"EnvyNG is an application written in Python which will download the latest ATI or NVIDIA driver or the Legacy driver (for older cards) (according to the model of your card) from ATI or Nvidia's website and set it up for you handling dependencies (compilers, OpenGL, etc.) which are required in order to build and use the driver."

To this brilliant programmer called Alberto Milone: thank you very much for making this great and elegant solution!

2008/04/13

Unit testing mail code

If you ever need to write unit tests for code that includes sending or receiving e-mails, check this Java library, called GreenMail. It's easy to use and free (Apache 2.0 License) !

It supports POP3, SMTP and IMAP (even with SSL!!!). You can browse its website to see some examples, but this is a little sample to show you how easy it is to use it:

...

public void testSmtpCode() throws Exception {

GreenMail greenMail = new GreenMail();
greenMail.start();

// ... Your code that sends e-mails

assertEquals("body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]));
greenMail.stop();
}

public void testPop3Code() throws Exception {

GreenMail greenMail = new GreenMail();
greenMail.start();
MimeMessage message = ... // Build a javax.mail MIME Msg
User user = greenMail.setUser("to@localhost.com", "login-id", "password");
user.deliver(message);

// ...Your code that reads e-mails and processes them

// ... Your asserts with the processing

greenMail.stop();
}
...

The GreenMail server uses by default port numbers suitable for testing, not the usual ones in real sending/receiving, but you can change them if you want. The default port numbers are easy to rememeber, because they are the same as the standard ones prefixed by 3 (or 30 if the real port number has only 2 digits), so: the GreenMail SMTP server (standard port 25) uses a default port number 3025, the POP3 server (standard port 110) uses a default 3110, etc.

Happy testing !

2008/04/05

The New Builder Pattern

The idea

I like to create immutable objects, especially after reading Josh Bloch's excellent "Effective Java" book. If an object is immutable, it has only one possible state and it is a stable one, so once you successfully build an object, you don't need to care about state transitions that can make your object unstable or corrupted. And immutable objects can be shared even in a multithreaded application. There are many other pros of immutability (you can read some of them here).

There is a classical way of making immutable objects in Java which consists of making all fields final (and private, of course), using only constructors to modify them (so that the only moment when a field is modified is during its construction) and making the class final (to avoid adding "setter" methods to subclasses). When you only have a couple of fields, that's fine, but when you have many of them you end up with a constructor with many arguments, which is ugly and difficult to use. If you have optional parameters, you can have a constructor with all the parameters and some other shorter constructors that have the mandatory parameters and some optional ones, that invoke the big constructor, like this:


public class Foo {

private final String mandatoryOne;
private final String mandatoryTwo;
private final String optionalOne;
private final String optionalTwo;

public Foo(String mOne, String mTwo, String optOne, String optTwo){
this.mandatoryOne = mOne;
this.mandatoryTwo = mTwo;
this.optionalOne = optOne;
this.optionalTwo = optTwo;
}

public Foo(String mOne, String mTwo, String optOne){
this(mOne, mTwo, optOne, null);
}
...
}


This can be a bit messy when you add more optional parameters, you end up with a lot of constructors like these and it has a lot of boilerplate code.The use of setters for the optional parameters is not an option, because this leads to non immutable objects (some object can change the state of your object with one of those setter methods).
Some time ago, thinking about this problem, I thought a solution could be to use a Javabean object, with one setter per field (even for the mandatory ones), but with a kind of "seal" method, that would "mark" the object as built and since that moment, an IllegalStateException would be thrown if a setter was called. Nevertheless, I wasn't very satisfied with this approach, because the setter methods that sometimes can be called and sometimes not would be confusing for the caller.

Then I found the New Builder pattern, explained by Josh Bloch in this PDF presentation, which is different from the original GoF Builder pattern. This pattern uses a public inner static class as a builder. The constructor of the original class is made private, so the only way to build objects is with the inner builder class. The builder has a setter method for each optional parameter and uses a fluent idiom that allows chaining of these method calls. I like this pattern a lot, because it solves the problem elegantly and effectively.

The implementation

In Josh Bloch's presentation there wasn't a detailed implementation of the pattern, although it was very clear the idea and the intention so I have searched for it in the Internet.

In Richard Hansen's blog you can find an implementation that seems to be more close to what Josh explains: the builder is a static nested class of the class from which it has to make instances, the builder's constructor is public (so you invoke the builder with 'new'), and the builder has the same fields as its enclosing class. The 'build()' method copies the content of the builder's fields into a new instance of the enclosing class. What I don't like about this implementation is this duplication of fields (for each field in the original class you have a duplicate field in the builder).

In Robbie Vanbrabant's blog there is a variation of this pattern, which avoids the boilerplate code using a base class for the builder and some reflection to build the object from the builder. I don't like the use of an interface for the builder, because that way you can't add a new optional parameter without breaking existing code that uses the builder (if you change the signature of a public interface the classes that use it have to change their code to implement the new methods). Update: This is not a problem at all, as Robbie points out in a comment. Also, I don't like the use of reflection because it's slower than the normal access to fields, but I do like the way it avoids duplication of fields in the builder.

The implementation I like most is the one found in Mario Hochreiter's blog. The builder is a nested public static class, but it changes the fields of its enclosing class directly, it doesn't use duplicates. It doesn't use reflection and the builder is a class, not an interface. The only problem I see is that, in theory, with a reference to a builder, you can change the state of the object it built, so you don't have the guarantee that the object is immutable. So I would add a check before each "setter" of the builder that would throw an IllegalStateException if the object has been already built and a check before the 'build' method itself to ensure the object is not built more than once. Also, I would make the mandatory parameters final. Update: I have made volatile the non final fields of the class in order to avoid problems of visibility if the reference returned by the method build() is passed to code to be executed in another thread, as Niklas points out in his comment.

So, with the example of Mario, I would implement this pattern this way:


public class ID3Tag {

private final String title;
private final String artist;
private volatile String album;
private volatile int albumTrack;
private volatile String comment;

public static class Builder {

private boolean isBuilt = false;
private ID3Tag id3tag;

public Builder(String title, String artist) {
id3tag = new ID3Tag(title, artist);
}

public Builder album(String val) {
if (isBuilt){
throw new IllegalStateException("The object cannot be modified after built");
}
id3tag.album = val;
return this;
}

public Builder albumTrack(int val) {
if (isBuilt){
throw new IllegalStateException("The object cannot be modified after built");
}
id3tag.albumTrack = val;
return this;
}

public Builder comment(String val) {
if (isBuilt){
throw new IllegalStateException("The object cannot be modified after built");
}
id3tag.comment = val;
return this;
}
// ... a lot more optional parameters

public ID3Tag build() {
if (isBuilt){
throw new IllegalStateException("The object cannot be built twice");
}
isBuilt = true;
return id3tag;
}
}

private ID3Tag(String title, String artist) {
this.title = title;
this.artist = artist;
}
}


The usage of this class would be:

ID3Tag tag = new ID3Tag.Builder("My Title", "My author")
.comment("Great song").build();


I have found a similar pattern, called the Essence pattern, described here and here by Dr Herbie. This pattern uses direct access to the fields of the builder (like in a C++ structure) instead of using "setter" methods and it doesn't use "chaining" of modifications like in the New Builder Pattern ("...builder.option1(value1).option2(value2)...").


2008/04/03

Freemind 0.8.1 now works with Java 6

Freemind, this excellent mind-mapping open source application, has released a new version: 0.8.1. This is a bugfix release. The most important bug I would remark is that now Freemind works with the latest version of Java, Java 6 (before this release, you could use it with Java 6, but some options didn't work, like the Preferences dialog). I have also seen performance improvements in this release.

2008/01/19

A very easy way to view dependencies: GraphViz

There is a wonderful and powerful tool called GraphViz that is able to draw graphs very easily. When I visited its website for the first time I thought it was very difficult to use, and this is completely false, so I will show how easy it can be.

This tool can do very powerful and complex graphs, but for the impatients (like me), that want to understand quickly how it works and be able to use it in another context (e.g. to show JAR dependencies in a Java application), I will show a very minimal example. Let's go.

First, you must download and install GraphViz. It is very easy to install. For Windows you have an installer, and for Linux you can use your favourite installer (at least Graphviz it's present in the Ubuntu Gutsy repositories, I don't know the other distributions).

Now, we will write a minimal text file with the dependencies we want to draw. Use your favourite editor and write a text file called 'sample.dot' with this content:

digraph g {
node1 -> node2
node2 -> node3
node2 -> node4
}

Now, you simply have to call the executable dot, from the GraphViz tool:

dot -Tpng -osample.png sample.dot

Pretty simple, isn't it ? We say we want to generate a PNG image (-T is the type of output file) and write it to an output file 'sample.png' from our graph file 'sample.dot'.

And the result is this:


Now, you can take a look at the documentation to know how to customize your graphs: you can specify colours, widths and shapes of your nodes, etc.

And one last advice: if the names of your nodes have spaces or special characters (like ':' or '\'), double quote them in the '.dot' file or you will get errors. So our last file would look like this:

digraph g {
"node1" -> "node2"
"node2" -> "node3"
"node2" -> "node4"
}

That's all. Enjoy making images of your dependencies!

2007/10/21

Lossless digital zoom in digital cameras

These last two weeks I've been on my honeymoon holidays...really really nice. I have been quite disconnected from computing or technological news, but I learned a useful feature of my digital camera that was absolutely unknown to me: the lossless digital zoom. Let me explain a little bit...

I have had several digital cameras and many of them had digital zoom. As this nice article explains, the digital zoom makes the illusion of having a longer optical zoom by making larger a small part of the original image the optical zoom gets (through interpolation). The result is a softened image (with less definition), not so clear as a normal one and not very useful in general. That's why I always disabled the digital zoom of my cameras. But, these holidays I discovered a great trick some modern cameras use to be able to enhance your optical zoom with a special kind of digital zoom that has no degradation...Is that magic ? No, but it's amazing anyway.

The trick is called "Smart zoom" by Sony and "Safe zoom" by Canon, but it's implemented by other cameras and it's pretty simple to implement. If you have a camera with many megapixels but you make a picture with less resolution than the maximum, your camera can actually make a picture with the maximum resolution and then crop it to the small part you have selected (by zooming with the digital zoom). In other words, the camera traditionally has had to make a large image from the small part selected by the digital zoom, and it had to "invent" the information it didn't have by interpolation; but now, it doesn't have to "invent" this information, because it actually has them (just selecting a larger resolution for the image). This article explains it much better than me, so read it (specially the "Smart zoom" part). This special zoom has limitations, it only can work if the image resolution you select is smaller than the maximum and the factor of zoom without degradation is not unlimited (in my camera I can "only" double the optical zoom range when I select a 1600x1200 resolution)...but it's enough for me.

The technique is equivalent to make a picture with a maximum resolution with the optical zoom and crop it yourself at home to the size of image you really wanted from the beginning, but it has some advantages:
  • You don't need to do this process at home (the camera does it for you), so this saves your time
  • You save space in your camera, because what is saved is the cropped image, not the lareg one (at maximum resolution)
  • You can choose the composition of the picture while you do it (I prefer it)
So...I have enabled the digital zoom in my camera again, it's worth it.



Powered by ScribeFire.

2007/09/21

Personal Wiki (TiddlyWiki)

This weeek I have discovered a great tool to organize my ideas or little tricks in an easy but powerful way: TiddlyWiki. It's a personal Wiki page, written in Javascript...you don't need any web server to publish it. You can add short "tiddlers" (short entries with a title), associate tags to them (in order to classify them), and that's all. You can search your tiddlers easily, open all the tiddlers with a given tag, arrange them in the page the way you want, etc.

I use Freemind a lot, specially to write documents, because I can organize and move my ideas very easily while writing them, but when I need to store tips or solutions to computing problems I prefer this TiddlyWiki tool, it searches very well and tags are great to classify easily with less effort.

Try it out!



Powered by ScribeFire.

2007/08/03

Linux in your Palm


Yes !!! You don't need to buy a new Palm, to wait for the new model that uses Linux as its OS...you can simply download some binaries, follow some easy steps and....woohooooo...you have Linux in your Palm !!!!

I have a Palm TX and it works really good, it doesn't have Wifi support yet but overall it's great and promising.

Try this out ! (but make a backup of your data before, just in case).

2007/02/27

Some small nice utilities for Windows

At my work I have a Lotus Notes account, but not the Lotus Notes client, so I must have the browser open all the time, just to be notified of e-mail or calendar events. As I don't like having it open (it eats resurces) and it's annoying to click on a dialog each time the browser session is about to expire, I use some useful utilities.

For the e-mail events, I have POP Peeper. It is a small application that notifies you when new e-mails arrive. It can handle more than one account and you can choose the kind of alert signal you prefer: a flag in the system tray, a dialog, etc.

For the calendar events, I use Personal Reminder. It's a very simple application that allows you to insert entries to remind, when you want to be reminded and the repetition of notification if you need it (to remind you every day, every week, etc.).

I love these kind of small apps: simple to install, small size, simple and intuitive to use, ...

Good intro to Grub

If you need to use Grub and understand its basics, there's an excellent introduction called "Grub from the ground up". I never understood the syntax of Grub, and when I was face to face with the Grub shell I didn't know what to do and its help information was a bit hard to understand at first sight.

This article introduces you to Grub easily, teaching the very basic information you should know to be able to boot a PC with Grub and understand how a 'menu.lst' file is oganized.