Thursday, May 28, 2009

A Swing component to display last keystroke and keystroke history

A Swing component to display last keystroke. Also shows the keystroke history.

How it works

The KeyLabel component registers a global listener for KeyEvents using:
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
On receiving KEY_RELEASED event formats the KeyStroke as a string and displays it. The dropdown shows history of last 100 keystrokes. Clicking on the X (Clear KeyStroke History) button clears the keystroke history. The KeyEvent listening can be suspended by deseleting the checkbox.

Usage
JComponent keyLabel = KeyLabelFactory.createKeyLabel();
// Use keyLabel in your application's statusbar
Demo

Download (KeyLabel.jar)
> java -jar KeyLabel.jar
Screenshot of KeyLabel Demo



Source Code

Wednesday, May 27, 2009

Error launching Mozilla (only from inside Eclipse) using simple Runtime.exec() on Ubuntu.

I discovered that, on Ubuntu 9.04, I cannot launch mozilla browser using the simple invocation of Runtime.getRuntime().exec("/usr/bin/mozilla"). It turns out, I have to use the variant of exec() where I can pass in some environment variables explicitly.

Here is the program that I used to test it:
import java.io.IOException;

public class MB {

/**
* @param args
*/
public static void main(String[] args) {
try {
Process exec = Runtime.getRuntime().exec("/usr/bin/mozilla"
// Uncommenting out following line will launch mozilla browser on Ubuntu
//, new String[] {"DISPLAY=:0.0", "HOME="+System.getProperty("user.home")}
);
System.out.println(exec.waitFor());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
Very interesting. May be that is why Eclipse has trouble launching help in default external browser on Ubuntu.

UPDATE: It turns out that this fails only if I run the above program from inside Eclipse. If I run it from a terminal window it works. It turns out that Eclipse sets an environment variable MOZILLA_FIVE_HOME to point to /usr/lib[64]/xulrunner-addon to make the SWT's embedded browser widget work correctly. However this environment variable is inherited by any process that is launched from within Eclipse. And apparently MOZILLA_FIVE_HOME inteferes with the Mozilla browser (/usr/bin/mozilla) launch.

Two issues have been filed in Eclipse Bugzilla:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=278415
https://bugs.eclipse.org/bugs/show_bug.cgi?id=278296

Sunday, May 17, 2009

MultiPage Editor Tab Traverse Eclipse Plug-in

The MultiPage editors such as plug-in manifest editor do not support navigation of tabs (pages of the editor) using keyboard. This plug-in implements that functionality for any MultiPage editor that subclasses org.eclipse.ui.part.MultiPageEditorPart and uses org.eclipse.swt.custom.CTabFolder to implement the tab folder.

Screenshots:

Starting with Overview tab:



Type CTRL+TAB to go to Dependencies tab:



Type CTRL+SHIFT+TAB instead to go to build.properties tab:

Saturday, April 25, 2009

Move and Resize windows on Mac OS X

I have been always extremely annoyed by the fact that one cannot resize the windows on Mac OS X using all four edges of the window. In fact that is one of the most aggravating thing for me about using a Mac. Another issue is that one cannot use the keyboard to move and resize the windows. I was aggravated enough to make me write a tool to address just those issue. I wrote the MoveResize tool. It make synergistic use of Applescript and Java to fix IMHO a major usability flaw in Mac OS.

How it works (Swing version)

The implementation uses Applescript to get the frontmost window and it's bound. It sends the bounds rectangle to a server implemented in Java over a socket connection. The Java server takes the screen shot of the full Desktop and uses it as the Image label (a JLabel with ImageIcon) as the content pane of an undecorated JFrame which has the same bounds as the Desktop. A JPanel with semitransparent background and a dark rounded rectangular border is given the same bounds that were received over the socket. This JPanel is added to the PALETTE_LAYER of the JFrame's layered pane - which makes it appear floating in front of the front window. Mouse and Key listeners on the JPanel allows moving and resizing of the JPanel. When the user types the ENTER key the JFrame is hidden and the new bounds of the JPanel are sent back to the Applescript over the socket connection which moves and resizes the frontmost window.

Initially I had implemented the original version using SWT. However, it turns out that it is implementable in Swing also - once again proving that Swing is a very good toolkit. Added advantage is that Swing is part of the JDK making the implementation much much smaller. BTW the Swing implementation is much faster and plays nicely with log off and shutdown.

Watch Screencast


Here is a screenshot:



Saturday, April 18, 2009

Wednesday, April 15, 2009

Check out the Mac OS X Finder and Terminal scripts here.



Screencast

Sunday, March 29, 2009

Sampler Eclipse Plug-in

Check out the Sampler Eclipse Plug-in. The sampler plug-in adds a Color Sampler Toolbar. Color of any pixel on the desktop can be sampled by simply dragging the cursor from the Color Sampler label and copied to the clipboard in various formats as shown in the pop-up menu.

Saturday, March 21, 2009

Reverse Text Selection Eclipse Plug-in

Check out the Reverse Text Selection Eclipse Plug-in.

The following screencast shows how you can start with a initial selection and then extend it in both directions using the Reverse Text Selection command (Alt+Shift+/).

Thursday, March 12, 2009

Thursday, January 01, 2009

Copy paths of selected items in Mac OS X Finder to clipboard

copypaths.app

(*
Script: copypaths.app

This script sets the clipboard to the paths of items selected in Finder window.
If there is no selection the clipboard is set to the path of the target
of the Finder window.

Installation:

1. Paste this script in Script editor
2. Save it as an application - ~/scripts/finder/copypaths.app
3. Drag and drop copypaths.app onto the Finder widow toolbar.

@author Sandip V. Chitale
@version 1.0
@date 1/1/2009
*)
tell application "Finder"
set paths to ""
set selected to selection
if (count of selected) is 0 then
set paths to POSIX path of (target of front window as alias)
else
repeat with aPath in every item in selected
set aPathString to POSIX path of (aPath as alias)
if paths is "" then
set paths to aPathString
else
set paths to paths & "
" & aPathString
end if
end repeat
end if
if paths is not "" then
set the clipboard to paths as text
end if
end tell

Go to parent folder in Mac OS X Finder

up.app

(*
Script: up.app

This script sets the target of the Finder window to the
parent of the current target of the Finder window.

Installation:

1. Paste this script in Script editor
2. Save it as an application - ~/scripts/finder/up.app
3. Drag and drop up.app onto the Finder widow toolbar.

@author Sandip V. Chitale
@version 1.0
@date 1/1/2009
*)
tell application "Finder"
set target of front window to (parent of target of front window)
end tell

Keep Mac OS X Finder and Terminal in sync

findercd.scpt

(*
Script: findercd.scpt

This script sets the target of the front window of Finder
to the directory path passed as the first argument. If
there is no Finder window, one is created. Optionally
the finder window can be activated by passing in "true"
as second argument. If no argument is passed
the user is prompted to select a directory
to go to.

The following two bash functions can be used to
invoke this script from bash running in Terminal
window:

# Change the directory of front window of
# Finder to pwd of shell
fcd() {
cd "${1}"
osascript ~/scripts/finder/findercd.scpt "`pwd`" ${2}
}

# Change the directory of front window of
# Finder to pwd of shell and activate the
# Finder window
fcda() {
fcd "${1}" true
}

Installation:

1. Paste this script in Script editor
2. Save it as a script - ~/scripts/finder/findercd.scpt
3. Add the fcd() and fcda() functions mentioned above
to your .bashrc or .bashrc_profile

This script complements the terminalcd.scpt script.

@author Sandip V. Chitale
@version 1.0
@date 1/1/2009
*)
on run argv
if (count of argv) is 0 then
try
set directory to POSIX path of (choose folder with prompt "Go to")
on error
return
end try
else
set directory to (item 1 of argv)
end if
set doActivate to false
if (count of argv) is greater than 1 then
set doActivate to (item 2 of argv)
end if
tell application "Finder"
if (count of windows) is 0 then
make new Finder window
end if
set target of front window to (POSIX file directory)
if doActivate = "true" then
activate
end if
end tell
end run

terminalcd.app

(*
Script: terminalcd.app

This script sets the directory of the shell in front window of Terminal to the
target directory of front window of Finder. If there is no Terminal window
is there a new window is created. If the the front window of
Terminal is busy new window is created.

Installation:

1. Paste this script in Script editor
2. Save it as an application - ~/scripts/finder/temrinalcd.app
3. Drag and drop terminalcd.app onto the Finder widow toolbar.

This script compliments the findercd.scpt script.

@author Sandip V. Chitale
@version 1.0
@date 1/1/2009
*)
tell application "Finder"
if (count of windows) is greater than 0 then
set cdTo to POSIX path of (target of front window as alias)
set terminalWasRunning to false
tell application "System Events"
if exists process "Terminal" then
set terminalWasRunning to true
end if
end tell
tell application "Terminal"
activate
if (count of windows) is 0 then
do script ""
else if window 1 is busy then
do script ""
end if
do script "cd '" & cdTo & "'" in front window
end tell
end if
end tell

Tuesday, December 16, 2008

Sunday, December 07, 2008

Hooking into Eclipse command execution

When I was working on implementing the Clips plug-in I wanted to intercept the Cut and Copy actions and automatically create clips from the selection. To my delight I discovered the necessary mechanism to exactly do that. Basically I had to add the following listener:
// Add listener to monitor Cut and Copy commands
ICommandService commandService = (ICommandService) PlatformUI
.getWorkbench().getAdapter(ICommandService.class);
if (commandService != null) {
commandService.addExecutionListener(new IExecutionListener() {

public void notHandled(String commandId,
NotHandledException exception) {
}

public void postExecuteFailure(String commandId,
ExecutionException exception) {
}

public void postExecuteSuccess(String commandId,
Object returnValue) {
// Is it a Cut or Copy command
if ("org.eclipse.ui.edit.copy".equals(commandId)
|| "org.eclipse.ui.edit.cut".equals(commandId)) {
Clipboard clipboard = new Clipboard(PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
.getShell().getDisplay());
Object contents = clipboard
.getContents(TextTransfer.getInstance());
if (contents instanceof String) {
// Now do something with text selection
}
}

}

public void preExecute(String commandId, ExecutionEvent event) {
}

});
}

Cool huh?

Thursday, November 27, 2008

Graphical cd

The following bash function allows you to change directory using a Directory chooser:
gcd() {
local CDTO=`zenity --title="cd" --file-selection --directory`
if [ -n "${CDTO}" ] ; then
cd "${CDTO}"
fi
}