Thursday, June 30, 2011

"Hassle-free" life of a software engineer

Out of nowhere, I have this urge to learn development on rails now... since everyone thinks that it's so awesome and most importantly it's free and has shitload of free reference material online. I thought this would be a good idea to "start" (literally) with web development, when I should really be packing my bag for a flight to Chicago tomorrow.
Okay, so I have "THE" Mac OS X 10.6.7 and obviously since Apple think it's so damn awesome, it shipped the laptop with ruby pre-installed... Awesome right? WRONG!!
To go by the tutorials, I want to install rails 3.0.1... which by the way, works with ruby 1.9.x. So, all I need to do now is uninstall the ruby 1.8.6 that my macbook shipped with and install ruby1.9.x. Easy right?? WRONG again. Here's why:
1. To uninstall ruby, I will have to force remove the installation folder manually -- the idea with which I've never been and probably never will be comfortable, since I have messed up such a big time in the past.
2. I know a good way, which will get me started with a clean slate. Yes, I am talking about the rvm (the ruby version manager). The problem is, rvm needs cUrl (a command line utility that lets you download stuff from command line) and cUrl website is DOWN!!
.
.
.
You might as well tie my neck to the tail of the flight tomorrow and take me to Chicago. I bet that'll be way less irritating.
This is certainly one of the situations when I feel that windows is way better!!

Update: My bad! Apparently, MacBook also comes with the curl installed... now that's awesome. Just installed rvm, but rvm notes for snow leopard do insist on updating the xcode, which is what I have to do now :(

Saturday, June 25, 2011

Ubuntu on VirtualBox

In case you're wondering why your Ubuntu installation gets stuck while you're trying to actually install it from the disk that you mounted, it's probably because you have checked the download updates installation in the beginning. Try starting over with that option unchecked to get the downloads out of your way, you can have ubuntu do that separately later when you're sleeping with your headphones on or something (like I do). If that doesn't work, use the try ubuntu option to run it from the cd-rom without installing it and then install it by double clicking the install icon on the desktop. This worked for me on both - mac and windows!
On a personal note, VmWare in my experience, is much more feature rich as compared to Oracle's VirtualBox (and I swear there's nothing more painful than calling it Oracle's VirtualBox instead of Sun's VirtualBox... but I'll get over it someday), but seriously...who buys a software these days? Also, there's a developer kit for VirtualBox... so someday you happen to crave for some opensource development, you can dive into that and I guess it's worth getting used to it now rather than later.

Monday, June 20, 2011

Murphy's Law

Found this line in Linux Kernel Module Programming Guide this morning and aptly so:

"You'll need to compile your code using various header files from the Linux kernel. Murphy's Law states that the headers that are missing are exactly the ones that you'll need for your module work."

lol

Sunday, June 19, 2011

A Note to myself - JTable.setRowSorter(null)

Never, ever use a line of code from a random forum without completely knowing what it can do to your code. I happened to use JTable.setRowSorter(null)
because I wanted to prevent users from clicking the column header and accidentally sorting the row. While I did that, I didn't know that it would actually end up disabling all the filters I might add later. Murphy's law - it did exactly the same and I wasted hours trying to figure out why aren't my filters working when the sorters are returning correct values. Google doesn't really help a lot when it comes to debugging your code. Unfortunately, I did not mention disabling the rowsorter in the changelist when I committed the changes. Thanks to my memory though, it reminded me of that as a possibility and after removing that line of code, everything is golden!

Saturday, June 18, 2011

Creating a Wizard in Java Swings

Getting started:
I was recently asked to create a windows style wizard (step by step execution) in Java.
Creating a wizard essentially means multiple panels should share the same display area. As a beginner, I had a thought that overlapping panels one over the other might work just fine. But you know what, it doesn't! With a lot of pain, you might even get it to work, but here's a thing: java has something called as CardLayout which is perfect for creating a wizard. Here's the tutorial on basic use of card layout. If you are highly impatient, lazy and have a short attention span like me or you think that you don't need to go into much detail of it, skip it.

A good design:
If you care for more than just getting the job done somehow, like I did after stupidly coding everything myself, which by the way was a bad design, now that I know of a better way, you should really think about sticking to some pattern. And by some pattern, I mean MVC which is an obvious (and as far as I know, the only) choice. Here's a very nice and straightforward article on what are your choices and considerations in terms of the architecture.

Ready to eat stuff:
I found out that OpenSwing framework, that I have used for pivot tables has good APIs for creating a wizard. It follows the pattern suggested on the Oracle/Java website mentioned previously and has a straightforward tutorial. Although, I think it would've been nice to have some more ready-made Intro panels (like licence panel) in the miscellaneous package, but you can always create your custom wizard inner panels and add it to the main wizard frame and you should be fine. If you read the Demo14 source, it's not that hard to make out what's happening in 10 minutes or so. Still, I'll brief some steps:

1. Create a client application -- This class will more likely contain the main method which eventually calls the WizardFrame class, and that's all it does.
2. WizardFrame -- This class creates the actual visible window. This class is a good place to add the inner panels on the wizard frame. By inner panels, I mean the screens that appear when you navigate
3. WizardController -- This class embeds the navigation logic of the wizard. Let's say you made a particular choice that requires 3rd screen to be displayed after the first (and not go to second screen at all), you can define that logic here.
4. Intro/First/Second Panels and so on... : These are the screens that are displayed when you navigate. These are extended from the WizardInnerPanel.
5. Execution Engine: Contains the execution logic. And mostly covers what happens when you click a cancel button or a finish button. In the demo application, finish and cancel button are referred to by the same method (getCurrentVisiblePanel.getCancelButton) which is kind of confusing at first, but 5 minutes of debugging will just do the trick.

Tip: Within 15 minutes of use, I found out that when you add WizardInnerPanel to the main wizard frame, the controls on the main wizard frame are reset. e.g. Say you have two radio buttons on the First Panel and initially none of them are selected and unless one of them is selected, you don't want user to go to next screen so you disable the Next button. However, if you've added all the necessary inner panels already, that next button might get re-enabled, if so, you might need to explicitly disable the next button in the init() function of the FirstPanel, like I did.

Thursday, June 9, 2011

Zlib buffer while installing/updating ruby gems

I struggled with Zlib error for hours now and internet world seems to be convinced with the

gem update --system

solution.
Although, the aforementioned is indeed a solution, in my case I got the Zlib error even for

gem update --system

command.

Turns out that you need to update the rubygems-update by:

gem install rubygems-update

If the above command doesn't work as it is, try fetching the rubygems-update to the local repository first by:

gem fetch rubygems-update -v 1.4.0

In my situation, even gem fetch rubygems-update -v 1.4.0
did not work, because it requires the latest build of ruby installed (1.8.7) and I had 1.8.6p26. The trick is to manually download rubygems-update gem, then go to the download directory and install the gem.
After you're done, do:

gem update --system

and proceed with whatever gems you need to install.
Hope this works for a clueless googler like me :)

Oh! While we're on it... I also had an "Application failed to initialize properly" windows error with cl.exe (Cl.exe is some Microsoft Visual Studio executable by the way.) while trying to do:

gem install win32-api -v

Apparently, this requires native C library compilation and I am thinking, because of visual studio installation, my native C library pointed to some dll accessed by cl.exe to which ruby didn't have access and so, the windows error.... As a naive windows user, I uninstalled the Visual studio and the error was gone :)
This reminds me of this girl colleague I went out for lunch with. A guy called her and asked her what to do if CPU usage is 70% even if no application is running. She told him to restart the PC and if that doesn't fix the problem... format it.
I laughed at it and she hates me now (I guess) :P

Cheers!

Vital update:
As it turns out, your life can be made much more easier if you are on a non-windows computer and you use ruby version manager instead of manual ruby installation. rvm will take care of all the version compatibilities etc that you need. RVM also needs a utility that allows users to download stuff on command line. It's called as cUrl and comes pre-installed with a Mac (and I am guessing with linux distributions too).

Another update:
This morning, a colleague came upto me with a similar problem. Manual download of rubygems 1.4.0 worked as expected, but apparently gem update --system ended up installing newer version of watir and few other gems. If there are multiple versions of same gems installed, a require statement in your ruby script might get confused with the paths and might throw errors like require_all not found or package custom_require not found. You should uninstall the unwanted ruby gem versions in such a case!