jueves, 3 de febrero de 2011

List to Tree (using whitespaces to indent) with Groovy

Supose you have a structure like this:













idnamefather
1N1null
11N111
12N121
121N12112
122N12212
13N131
131N13113
1311N1311131
13111N131111311
14N141
2N2null


public class Node {
def id
def name
def father
}

public class NodePrinter {
private void printLeftPad(def source,int pad)
{
for(int i=0;i<pad;i++) print " ";
println source;
}

public int indent(List items,int index,int pad)
{
printLeftPad("${items.get(index).name}",pad);

def current= items.get(index);
int i=index+1;
for(;i<items.size();i++)
{
if( items.get(i).father==current.id )
{
printLeftPad("{",pad);
i=indent(items,i,pad+4);
printLeftPad("}",pad);
}
else
{
if( current.father==items.get(i).father )
{
printLeftPad("${items.get(i).name}",pad);
current=items.get(i);
}
else
{
return i-1;
}
}
}

return i;
}

}

def nodes=[ new Node(id:1,name:'N1',father:null),
new Node(id:11,name:'N11',father:1),
new Node(id:12,name:'N12',father:1),
new Node(id:121,name:'N121',father:12),
new Node(id:122,name:'N122',father:12),
new Node(id:13,name:'N13',father:1),
new Node(id:131,name:'N131',father:13),
new Node(id:1311,name:'N1311',father:131),
new Node(id:13111,name:'N13111',father:1311),
new Node(id:14,name:'N14',father:1),
new Node(id:2,name:'N2',father:null)
];

def printer=new NodePrinter();
printer.indent(nodes,0,4);


This will give us the following result:

N1
{
N11
N12
{
N121
N122
}
N13
{
N131
{
N1311
{
N13111
}
}
}
N14
}
N2


Enjoy it!

martes, 20 de octubre de 2009

Source code formatter library for Java

Recently I was looking for a Java Library that could be include in an applet (with no any dependencies), after looking for awhile I could not find any library (there are a few out there but all of them have dependencies with other libraries). 

I found a C++ program named Artistic Style (http://astyle.sourceforge.net/) , so I decided to migrate the source code from C++ to Java, and the jastyle project arises (http://sourceforge.net/projects/jastyle/).

There are two ways in which jastyle could be used, as a library or as a console application. See how easy is to format Java source code with jastyle.

First, go to (http://sourceforge.net/projects/jastyle/) and download the lastest jastyle.jar , then choose the Java source file that you want to give format. The options will be shown if you just type:

java -jar jastyle.jar 

Now let's see how It works with an unformatted java source code:




package net.barenca.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Random;

import oracle.jdbc.OracleDriver;

public class OracleBlobTest{
public static void main(String[] args) throws Exception
{
String filename="/home/barenca/Documents/LDAP.pdf";
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream inputStream = new BufferedInputStream(fis);

int length = inputStream.available();
DriverManager.registerDriver(new OracleDriver());
String url="jdbc:oracle:thin:@192.168.1.253:1521:devdb";
Connection conn = DriverManager.getConnection(url
, "prueba",
"prueba");
String insert = "insert into test_blob(id,content)values(?,?)";

PreparedStatement pst = conn.prepareStatement(insert);
int x = new Random(new Date().getTime()).nextInt(100000);

pst.setInt(1, x);
pst.setBinaryStream(2, inputStream, length);
pst.execute();
pst.close();

conn.close();
inputStream.close();
}}

And after java -jar jastyle.jar --style=java OracleBlobTest.java this is the result:



package net.barenca.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Random;
import oracle.jdbc.OracleDriver;
public class OracleBlobTest {
public static void main(String[] args) throws Exception {
String filename ="/home/barenca/Documents/LDAP.pdf";
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream inputStream = new BufferedInputStream(fis);
int length = inputStream.available();
DriverManager.registerDriver(new OracleDriver());
String url ="jdbc:oracle:thin:@192.168.1.253:1521:devdb";
Connection conn = DriverManager.getConnection(url
, "prueba","prueba");
String insert = "insert into test_blob(id,content)values(?,?)";
PreparedStatement pst = conn.prepareStatement(insert);
int x = new Random(new Date().getTime()).nextInt(100000);
pst.setInt(1, x);
pst.setBinaryStream(2, inputStream, length);
pst.execute();
pst.close();
conn.close();
inputStream.close();
}
}

I think is not perfect, but It helps a lot :) .

Now, as a library :



import java.io.Reader;
import java.io.FileReader;
import java.io.BufferedReader;
import net.barenca.jastyle.ASFormatter;
import net.barenca.jastyle.FormatterHelper;

public static void main(String[] args) throws Exception
{

ASFormatter formatter = new ASFormatter();
formatter.setJavaStyle();
Reader in = new BufferedReader(new FileReader("OracleBlobTest.java"));

String formatted = FormatterHelper.format(in,formatter);

// and that's it!
}


The best thing is that this library does not need any additional jar library!!!.


I hope you enjoy it!.


Best regards.

Héctor.


jueves, 15 de octubre de 2009

Mapping Windows Key (Super) in Ubuntu

Probably one of the functions that a Windows user is accustom to is the mapping of the Windows key + E (this opens the Explorer -not the Internet Explorer), this combination of key mapping is not enabled by default in Ubuntu (I have tested it in Ubuntu 9.04, XUbuntu 8.10 and 9.04).

So, let me show you how easy is to map the windows key + E in order to have the same behavior in Ubuntu as Windows does it.

First go to option menu System -->Preferences-->Keyboard and then choose the tab "Layouts" and then "Layout options", and go to the Alt/Win key behavior subtree, open it and click on the radio button Supper key is mapped to Win keys and then click on close (or accept) and click on the another close button.




Second, go to System --> Preferences --> Keyboard Shortcuts and look for the Desktop section and click on the Home folder row, now press at the same time the two keys Windows + E (like in Windows - plus symbol should not be pressed), once you have done it, the combination of keys (the shortcut) to open Nautilus is ready!, accept the change.



Now continue mapping the keys you are accustom to. I hope this short description is useful for you.


Best regards.




lunes, 22 de junio de 2009

Error: No JDK found on PATH, Please correct the SetJavaHome directive or add the directive

If you have seen the following error while trying to execute Oracle SQL Developer:

Error: No JDK found on PATH
Please correct the SetJavaHome directive or add the directive
in /home/barenca/bin/sqldeveloper/sqldeveloper/bin/sqldeveloper.conf or
sqldeveloper-Linux.conf
to point to the JDK installation.

You just need to indicate the J2SDK path in the file sqldeveloper/bin/sqldeveloper.conf and that's it!.

Edit the file ./sqldeveloper/bin/sqldeveloper.conf and add the path of your j2sdk installation (in Linux/unix which java and then follow the symbolic links):

SetJavaHome /usr/lib/jvm/java-6-sun

And that's it!, enjoy it.

jueves, 18 de junio de 2009

Sound Volume keys in IceWM (tested with Latitude D620)

Some days ago I took sometime and I installed Antix 8, not everything is according with the features I was waiting for, but It is OK.

One thing It was missing was the configuration for the Sound Volume keys, so this is what I did for enabling the keys on my Laptop.

Go to the Antix Control Center, click on Edit IceWM settings, click on keys tab and append the following to the file:

key "XF86AudioLowerVolume" amixer -q set Master playback 5-

key "XF86AudioRaiseVolume" amixer -q set Master playback 5+

key "XF86AudioMute" amixer sset Master toggle

Then save the file.

Now click on the statup tab and append the following lines:

xmodmap -e 'keycode 176 = XF86AudioRaiseVolume'

xmodmap -e 'keycode 174 = XF86AudioLowerVolume'

xmodmap -e 'keycode 160 = XF86AudioMute'

Then save the file.

Close the session and It's done!.

martes, 23 de diciembre de 2008

Static IP on Xubuntu 8.10 (Intrepid Ibex)

1) Remove the network manager (this will remove the applet), go to the "install and Remove applications" available in the option menu System, find the Network Manager and remove.
2) Edit /etc/network/interfaces according to your needs. In my case my ethernet card is eth0 and my static IP will be 192.168.1.253

barenca@development:~$ sudo vi /etc/network/interfaces

auto lo eth0
iface lo inet loopback

iface eth0 inet static
address 192.168.1.253
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

gateway 192.168.1.1

3) Add your DNS in the file /etc/resolv.conf

barenca@development:~$ sudo vi /etc/resolv.conf

# Generated by NetworkManager
nameserver 200.33.146.241
nameserver 200.33.146.249

4) Restart the service

barenca@development:~$ sudo /etc/init.d/networking restart

5) Test your connection. If no connection, do not worry, just restart and that's it!.

viernes, 5 de septiembre de 2008

Netbeans Visual Library and a Multiline Label Widget

Some days ago I was trying to find a solution for a multiline label widget for the netbeans visual library, I tried some solutions (out of the box) like QLabel and others found on the web, without any success, after 2 weeks of frustrating research, I decided to continue with other features of my system, and then later on, I returned to this issue again and give it a try.

The problem:
I needed a Widget that supported Multiline label, auto-fit on resize, and drag and drop action, the first approach was to include QLabel within a ComponentWidget, It did not work for me, and then, I tried with a JTextArea in this way:


public class MultilineLabelWidget
extends ComponentWidget
{
public MultilineLabelWidget( Scene scene, String text )
{
super( scene, new JTextArea( text ) );
JTextArea jText = (JTextArea) this.getComponent();
jText.setOpaque( false );
jText.setEditable( false );
jText.setLineWrap( true );
jText.setWrapStyleWord( true );
jText.setHighlighter( null );
jText.setBorder( BorderFactory.createEmptyBorder() );

this.getActions().addAction( ActionFactory.createResizeAction() );
this.getActions().addAction( ActionFactory.createMoveAction() );
this.setBorder( org.netbeans.api.visual.border.BorderFactory.createLineBorder( 8 ) );
}
}

and It partially worked!!, the problem is that JTextArea is painted after the widget, so the MoveAction will not work! out-of-the-box, ummm, I tried another solution, not extending the ComponentWidget but creating an in-line ComponentWidget and trying to catch the Mouse Events and redirect the calls to its Action in the Widget, like this:

JTextArea jText = new JTextArea( hm );
jText.setOpaque( false );
jText.setEditable( false );
jText.setLineWrap( true );
jText.setWrapStyleWord( true );
jText.setBorder( BorderFactory.createEmptyBorder() );
final ComponentWidget widget = new ComponentWidget(scene,jText);
widget.getActions().addAction( ActionFactory.createResizeAction() );
widget.getActions().addAction( ActionFactory.createMoveAction() );
jText.addMouseMotionListener( new MouseMotionListener(){

@Override
public void mouseDragged( MouseEvent event )
{
widget.getActions().mouseDragged( widget, new WidgetAction.WidgetMouseEvent(new Date().getTime(),event) );
}

@Override
public void mouseMoved( MouseEvent event )
{
widget.getActions().mouseMoved( widget, new WidgetAction.WidgetMouseEvent(new Date().getTime(),event) );
}} );

jText.addMouseListener( new MouseListener(){

@Override
public void mouseClicked( MouseEvent event )
{
widget.getActions().mouseClicked( widget, new WidgetAction.WidgetMouseEvent(new Date().getTime(),event) );
}

@Override
public void mouseEntered( MouseEvent event )
{
widget.getActions().mouseEntered( widget, new WidgetAction.WidgetMouseEvent(new Date().getTime(),event) );
}

@Override
public void mouseExited( MouseEvent event )
{
widget.getActions().mouseExited( widget, new WidgetAction.WidgetMouseEvent(new Date().getTime(),event) );
}

@Override
public void mousePressed( MouseEvent event )
{
widget.getActions().mousePressed( widget, new WidgetAction.WidgetMouseEvent(new Date().getTime(),event) );
}

@Override
public void mouseReleased( MouseEvent event )
{
widget.getActions().mouseReleased( widget, new WidgetAction.WidgetMouseEvent(new Date().getTime(),event) );
}} );
jText.setHighlighter( null );
widget.setBorder( org.netbeans.api.visual.border.BorderFactory.createLineBorder( 8 ) );

It worked better than the previous solution!, BUT!!!!!!!!!, It just worked when I moved the mouse pointer slowly (of course, first click and then drag), but every time I moved the mouse pointer as I usually do, the component was not repainted (It did not followed the mouse pointer), and at the moment I released the left button on the mouse, the widget was painted!, frustrating for me because I think It should be easy to do it! (please developer of Visual Library, include a Multiline Label Widget Out-of-the-box).

The solution
Finally, I found a code on the net (It references to com.exalto.UI) and adapted it to fit within the Visual Library Widget, so the result It is this:

package test.designer.widget;

import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.Scene;

public class MultilineLabelWidget
extends LabelWidget
{
private boolean justify;

public MultilineLabelWidget( Scene scene, String label )
{
super( scene );
this.setLabel( label );
this.setJustified( true );
}

@Override
protected void paintWidget()
{
paintOrGetSize( this.getGraphics() );
}

private void paintOrGetSize( Graphics2D gr )
{
float width = (float) ( this.getBounds() != null ? this.getBounds().getWidth() : this.getPreferredSize()
.getWidth() );
Insets insets = this.getBorder().getInsets();
float rwidth = width - ( insets.left + insets.right );// + margin.left + margin.right;
Rectangle rec = this.calculateClientArea();
gr.setFont( getFont() );

float x = 0.0F;//+ margin.left;
float y = (float) rec.getY();//+ margin.top;

if ( rwidth > 0 && this.getLabel() != null && this.getLabel().length() > 0 )
{
AttributedString as = new AttributedString( this.getLabel() );
as.addAttribute( TextAttribute.FONT, getFont() );
AttributedCharacterIterator aci = as.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer( aci, gr.getFontRenderContext() );

while ( lbm.getPosition() < aci.getEndIndex() )
{
TextLayout textLayout = lbm.nextLayout( rwidth );
if ( gr != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * rwidth )
{
textLayout = textLayout.getJustifiedLayout( rwidth );
}
if ( gr != null )
{
textLayout.draw( gr, x, y + textLayout.getAscent() );
}
y += textLayout.getDescent() + textLayout.getLeading() + textLayout.getAscent();

}
}
}

public boolean isJustified()
{
return justify;
}

public void setJustified( boolean justify )
{
boolean old = this.justify;
this.justify = justify;
if ( old != this.justify )
{
repaint();
}
}
}

It worked for me, It worked for my purposes, I hope It works for you.
Please modify it, improve the code, see if It works for you, and feedback to me.

Thanks a lot.

References:
http://graph.netbeans.org/servlets/ReadMsg?listName=users&msgNo=1295
http://graph.netbeans.org/servlets/ReadMsg?list=users&msgNo=726
http://www.koders.com/java/fid9BE9B31AC9BED01828448BF91A61AFA5AE431E16.aspx