The vortex

21 Φεβ 2009

Popup Menus in Groovy

Κατηγορίες: Groovy — Tags:, , — vortex7 @ 02:26

While trying to figure out how to create a Popu Menu in Groovy, I started my search from google searching for PopupMenu, JPopupMenu tutorial etc. Some of the results were the very good tutorial How to Use Menus in Java and the An example of the JPopupMenu in action, but it was not easy to translate/rewrite the Java examples in Groovy.

Searching about popup menus and groovy gave no fruits.

Rethinking about my request, I understood that the problem was I had no idea how to handle, the Groovy way, the MouseListeners that would give me the ability to show the popup menu.

Finally while searching about Groovy and MouseListener I found this thread at MarkMail with an answer from Erik Husby and two other versions from Danno Ferrin. I had all the needed things for my popup menu

These are two compact Groovy scripts for a menu and a popup menu.


The smallest code for a menu

Very compact Groovy : a frame with an exit on menu bar

new groovy.swing.SwingBuilder().frame(title:'v1.0',pack:true,show:true){
    menuBar {
        menuItem{
            action(name:'Click here to Exit', closure:{System.exit(0)})
        }
    }
}

The smallest code for a popup menu

Very compact Groovy : a frame with an exit on popup menu

new groovy.swing.SwingBuilder().frame(title:'v1.1',pack:true,show:true){
    panel(mousePressed: {e->
        if (e.isPopupTrigger()) {
            popupMenu {
                menuItem{
                    action(name:'Exit', closure:{System.exit(0)})
                }
            }.show(e.getComponent(), e.getX(), e.getY())
        }
    }){
        label("Right click here to get a popup menu to Exit")
    }
}

These are some other scripts for popup menu in Groovy.


A frame with menu and a call of dialog box

A separator in the first level acts like a spring between the menu items
Mnemonics can be used for keyboard access (Alt-Mnemonic)
A predefined closure with a dialog box can used as menuItem closure

import groovy.swing.SwingBuilder

def frame
swing = new SwingBuilder()

showAbout = {
    def pane = swing.optionPane(message:'Testing Menus v1.2')
    def dialog = pane.createDialog(frame, 'About Dialog')
    dialog.show()
}

frame = swing.frame(title:'Main Frame',location:[100,100],size:[500,150]){
    menuBar {
        menu(text:'File', mnemonic:'F') {
            menuItem{
                action(name:'Exit', mnemonic:'E', closure:{System.exit(0)})}
        }
        separator()
        menu(text:'Help', mnemonic:'H') {
            menuItem(
                action(name:'About', mnemonic:'A', closure:{showAbout()})}
        }
    }
    panel {
        label(text: "A label with a message")
    }
}
frame.show()

A frame with a full menu and a same popup menu

For the Popup Menu we need to
- store the menuItems in a closure (we need to use it twice)
- make the frame a MouseListener
Note: menuBar uses parentheses to incorporate the menuItems

import groovy.swing.SwingBuilder

def frame
swing = new SwingBuilder()

def showAbout = {
    def pane = swing.optionPane(message:'Testing Menus v1.3')
    def dialog = pane.createDialog(frame, 'About Dialog')
    dialog.show()
}

def menuItems = {
    menu(text:'File', mnemonic:'F') {
    menuItem() {
        action( name:'Exit', mnemonic:'E', closure:{ System.exit(0) } ) }
    }
    separator()
    menu(text:'Help', mnemonic:'H') {
        menuItem() {
            action(name:'About', mnemonic:'A', closure: { showAbout() } ) }
    }
}

frame=swing.frame(title:'Main Frame v1.4',location:[100,100],size:[500,150]){
    menuBar ( menuItems )
    panel {
        label(text: "A label with a message")
    }
}

frame.addMouseListener( { e->
    if (e.isPopupTrigger())
        swing.popupMenu(menuItems).show(e.getComponent(), e.getX(), e.getY());
} as java.awt.event.MouseListener);

frame.show()

This is a very simple way to have popup menus.
This is the Groovy way for a popup menu.

9 Φεβ 2009

Editing Groovy with the Κate editor

Κατηγορίες: KDE — Tags:, — vortex7 @ 00:43

Some time ago I stumbled upon Groovy.

Groovy is a relatively new language. It is an agile dynamic language for the Java Platform with almost-zero learning curve for Java programmers.

Someone said that Groovy is a super Java. It’s a laconic and powerful language with closures, builders, support for unit testing and mocking out-of-the-box. It can use any Java class or library and can be used anywhere Java can be used.

I started to learn Groovy and I used, to edit my scripts, my favorite lightweight editor kate. Besides vim/gvim (yes my favorite text editor is vi), one of my best gui editors is kate, a KDE editor.

Kate, among other things (Regular expressions, Codepage conversions, Multiple files, Synchronized terminal etc) is also able to highlight the keywords in a file, based on the file type.

But I had no support for groovy in kate, or I thought so.

After some search I found a Syntax xml file for KDE 4 Kate at Other Plugins at the Groovy home site.

The first problem was that the file was for KDE 4 Kate. I decided to try using it in my KDE 3.5, I had nothing to loose by trying it.

The second problem was that I had to find how to use it, how to convince my kate to use it.

At first I thought that I had to search about “kate plugins” (I had found it under groovy plugins) but this was a dead end, with compilations on KDE 4 etc. It had to be easier, after all I was using KDE 3.5.

After searching in my machine (packages, installed files, etc) and the internet, I found this excellent article about Writing a Kate Highlighting XML File which explained everything to me.

Of course the only thing I needed was which was the place of the syntax file, but I learned a lot of things. By the way the syntax file is good for KDE 3.5 kate also, it works for me.

So, following are the steps needed to make kate able to highlight groovy scripts.

  1. Download this Kate Syntax xml file for Groovy (link found at Other Plugins at the Groovy home site)
  2. Run 'kde-config --prefix' (to find $KDEDIR needed in the next step). For my Debian/Sid it gives /usr .
  3. Move/copy the file to

      - (if KDE3) : $KDEDIR/share/apps/katepart/syntax

        - (if KDE4) : $KDEDIR/share/kde4/apps/katepart/syntax

        17 Δεκ 2008

        Debian Iceweasel Identity

        Κατηγορίες: Debian — Tags:, , , — vortex7 @ 22:46

        While I was playing with the yahoo pipes, the following warning popup appeared :
        Icwweasel not  recognized as Firefox by yahoo pipes editor
        The same warning was presented to me at many different places.

        Being an old Debian user, I realized that the problem was with my Iceweasel, the Debian way Firefox, (see the history at Wikipedia) and I had to do something manually to correct the problem.

        After some google search, the simple solution to overcome the problem, was found at kmandla.wordpress.com (rephrased a little for clarity):

        I needed to follow these steps

        1. Open a page or tab in Iceweasel, write “about:config” in the address bar and press Enter
        2. In the Filter box write “agent” and wait for the selections to appear
        3. Double-click the line with Preference Name general.useragent.extra.firefox
        4. Replace the word “Iceweasel” with “Firefox” and press OK
        5. Close the page or tab

        and everything was OK and the only thing I had to do was to reload the pipe editor.

        Because Iceweasel 3 was installed, after step 1, l was presented with this annoying warning:

        Firefox 3 warning about configuration settings

        I unchecked the “Show this warning next time”, pressed the button and proceed.

        15 Δεκ 2008

        Just another blog …

        Κατηγορίες: κόσμος — vortex7 @ 19:05

        Για μια πιο εν-ήμερη κοινωνία…

        Blog στο WordPress.com.