Saturday, August 9, 2008

Communication between Views in the Eclipse IDE

One of the head-scratchers that come up after a bit of programming for Eclipse using SWT or programming your own app using Eclipse as a backbone is that you have to communicate between views. I recently bumped into this while developing my own software, so I thought I'd share how it's done very simply without singletons or any other gimmicks.

public class InfoView extends ViewPart {
private TransactionsView transactionsView;
public void createPartControl(Composite parent) {
getSite().getWorkbenchWindow().getPartService().addPartListener(partListener);
}
private IPartListener partListener = new IPartListener() {
public void partOpened(IWorkbenchPart part) {
if (part instanceof TransactionsView) {
transactionsView = (TransactionsView)part;
}
}
}
private void myOwnFunction() {
if (transactionsView != null) {
transactionsView.getTransactions();
}
}
}

That's all there is to it. When your view is created in createPartControl() tell Eclipse to add a listener that gets notified whenever a part-related event occurs and you'll know when the TransactionsView is opened. Some other part events you can listen to are:

public void partClosed(IWorkbenchPart part){}
public void partActivated(IWorkbenchPart part) {}
public void partBroughtToTop(IWorkbenchPart part) {}
public void partDeactivated(IWorkbenchPart part) {}

You'll have to make sure InfoView gets added to the perspective before TransactionsView otherwise the TransactionsView will be created before you add the listener and you won't get the notification.

Keywords for beloved Google:
eclipse swt
communicate betweeen views
call functions in another view
reference to another view
access another view's members
access another view's variables

Tuesday, May 6, 2008

Disassembling the Canon Powershot A650 Digital Camera

My Christmas present to myself in 2007 was the Canon Powershot A650 digital camera. I like this particular line of cameras, the A620, 640, 650. Mainly because of the AA batteries. I use rechargable batteries and they last much much longer than other cameras' custom lithium ion batteries do. But that's besides the point. The point is that I dropped the camera three days before my trip to St. Maarten (or Martin, if you like the French more). I dropped this camera once before and the only damage was a little dent in one of the corners. The second time around though, I ruined it. The lens did not have the full range of motion anymore and the camera refused to work because of the lens. Looking around on the web, I find that the lens is the weak point when dropping a camera. (To make sure this is indeed the case, when I have a bit more money to waste, I'll buy a statistically significant number of cameras and drop them.)

Seeing the camera is busted and I have nothing further to lose, I took it apart.

The process is pretty straightforward. I looked at the parts of the camera and how they come together, took the screws out, wiggled it to see what else is still holding it together, unplugged a few connectors, and got to the lens. The tricky part is making sure you remove as few screws as necessary so that it's won't be any more complicated than it needs to be.

Turns out the flimsy plastic that is used to guide the lens broke when I dropped the camera. I glued the piece and put the camera back together. Keeping track of the screws is difficult and I am sure I used the wrong screws in some places but I did put the camera back together. Except for two screws that were left over. Nonetheless, the camera looked solid, so I ignored the two screws.

I held my breath and powered the camera. The only problem was that even though the lens moved fine, it made a terrible noise. Also, I soon found out that the camera did not focus well in video mode anymore.

The LCD started going white a few days later. St. Maarten has a world-famous airport with a runway that ends right next to a beach, so planes land very very low--to everybody's delight. To make it even more exciting, they also land 747's on this runway! I looked up the flight schedules hoping a 747 would land during the four days I was in St. Maarten and sure enough there was one scheduled for the second day. There I was on the beach with my girlfriend admiring all the small and medium planes landing that afternoon as the weather alternated between sunshine and small showers when the big one finally showed up on the horizon! Everybody scrambled for what they considered the best photo spot and got ready. Literally three seconds before I wanted to press the shutter, the LCD went white! I fumbled with it for a second, gave up, looked through the viewfinder that was half blocked by the filter adapter and instinctively pressed the shutter three times as the plane approached the beach and passed over it. I followed it with my gaze as it landed and slowed down to taxiing speed. I got back to my camera and realized it's time to go St. Maarten's famous shopping strip and buy a new camera--pronto! Later, I found the LCD worked when undocked a bit but whenever I clicked it into the holder, the LCD turned white. For all the bad timing, I did get the shot! http://www.panoramio.com/photo/8273379. Then I got a replacement camera.


I took the camera apart a second time after I returned home from vacation. This time I managed to break one of the two ribbon cables that go into the lens assembly. Fate sealed! I reassembled the camera and this time I found myself holding ten screws I forgot to put back in, but the camera was still holding together well!

What follows is my guide in words and pictures to taking this camera apart. Read it over before starting the disassembly.

Tools needed:
- Set of small screwdrivers. The screws have Philips heads. A thin flathead screwdriver is useful for prying apart the plastic latches in the camera.
- Can of compressed air.

Take off the filter adapter silver ring around the lens by pressing the button next to it. Look at the camera from every angle and take out all the screws you can find. You should come up with a system to track where each screw came from because the screws that you will remove have several types of threads and several lengths. For each screws or group of screws, I ripped a small piece of paper and wrote on it where the screws belong. I formed it into a "container" by creasing it with my thumbs and my index fingers. The problem is that halfway through, you don't know how to name the locations anymore. Probably the easiest thing is to print the images on this site and number the screw holes in the pictures as you remove each screw. This will also make sure you put all the screws back in when you re-assemble the camera.





Next, pry off the back half of the case. Notice it's held in place by three latches, so you have to raise the top a little:


This is how the back looks without the case:


and the front (without the lens assembly):


Next, remove the piece that has the shutter and the zoom controls. The main obstacle is the latch and the ribbon cable:




To remove the LCD, unscrew the two screws shown below and unplug the yellow and the blue connectors:


While you're at it, also remove the ribbon cable that you see in the corner next to the yellow and the blue connectors.

To remove the LCD, there's also this screw and one or two others:


Look at the metal case and remove the screws that hold it together. When you get the flash unit loose, short-circuit the capacitor to discharge it so you won't accidentally electrocute yourself. I used a pair of scissors WITH PLASTIC HANDLES. Touch the two points shown below to discharge the capacitor and to see a decent-sized spark in the process:

NOTE: This is the bad way to discharge a capacitor because it creates a very large current for a brief period of time, possibly destroying the capacitor. You're better off using a resistor to drain the capacitor over ten seconds or so.

Take out the screws that hold the viewfinder in place.

Now comes the fun part: disassembling the lens assembly. Start by taking off the little cover on top of the lens held by a spring if it hasn't fallen off already:


This should leave you with the following pieces held together by cables:




Slowly play with the back cover of the lens assembly. It's held together by a few latches so try not to break them. I broke one or two but I reassembled the lens assembly without any problems. The back cover snapped back into place.


Once you remove the back cover, a few gears will fall out that were connected to the little motor:


Taking apart the lens assembly is by far the trickiest and most painstaking part. The lens assembly is composed of a few plastic cylindrical pieces that have diagonal tracks to guide the other cylinders. When one of the cylinders reaches the end of the track, it pops off because the end of the track is open. Try to remember how every piece looked and fit in before you remove it. It will save you a lot of time and frustration.


Note, Piece 4 can be decomposed further.

Also, be very careful with the two ribbon cables that go into the lens assembly; I managed to break one of them at some point. I moved to another house while the camera was apart, so I stored everything in a few plastic bags and when I took them out two weeks later, one of these two ribbon cables was ripped.
It's also useful to have a can of compressed air to clean the lens of dust as you are reassembling it.

At the end of the process, you will have the following pieces:

As you see, I removed 37 screws to get to this point.

Here's my Crazy Glue job:

The plastic here doesn't work well with Crazy Glue. One solution is to go to CVS and among the instant glues they sell is a brand of glue that is packaged together with a tube of primer. The primer made the plastic surfaces hold together much better.

Okay, time to re-assemble everything! I'll refer to the cylindrical parts of the lens numbered in red in the previous pictures. The green line shows the ring edge of Piece 4. Rotate the ring until the opening in the ring lines up with the opening of the track in Piece 3. Place Piece 5 inside Piece 6 and rotate them until Piece 5 slips in. Take the combined piece and place it so that the pins from Piece 5 slips through the openings in the ring of Piece 4 and the openings in Piece 3. Push Piece 6 down so that its pins slip through the openings of Piece 2. Here's how it looks:


Rotate all the pieces back into place so the lens assembly shrinks into the retracted position.
Place the three gears inside and snap the back into place. To complete the lens, put the cover with the spring back in place--without permanently deforming the spring. Now go the steps in reverse order to re-assemble the camera.

Good luck!

Monday, May 5, 2008

Winamp Plugin - Music Archiver

Wondering where all your hard disk space is gone? Look at that, it's being taken up by all that Celine Dion music! That's easy to fix: just find the offending directory, press delete, and you just freed two gigabytes of hard disk space!

But it's not always that easy. I found myself with song after song I don't like anymore throughout my music collection and no easy way to clean up, so I created a Winamp plugin that allows me to eliminate those cheesy songs one by one as they come up when I listen with Winamp.

The plugin is at http://www.winamp.com/plugins/details/221543.

It adds a system tray icon that you can click when the currently playing song is NOT music to your ears. The main idea was to recreate the directory structure of the music collection in another root directory in order to archive the songs I don't like anymore and maybe even delete them. So for example, if I have "C:\My Music\New\Singles\Madonna - 4 Minutes.mp3", I click the system tray icon and the file moves to "C:\My Music to Archive\New\Singles\Madonna - 4 Minutes.mp3".