Search Here

Custom Search
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, August 19, 2008

IntelliJ IDEA 8 Released!

Hurray! At last IntelliJ IDEA 8.0 has been released. I was waiting for a long time for it. For J2EE developers there are some new fruits. I was specially waiting for the FreeMarker(FTL) plugin. In IDEA 8.0 it has been incorporated.

Here is the link and the original post:

http://blogs.jetbrains.com/idea/2008/08/intellij-idea-8-milestone-1-is-here/

It is really a lucky concourse of circumstances that today, 8/8/08, we’re releasing IntelliJ IDEA 8 Milestone 1!

This release is a preview of IntelliJ IDEA 8, which is due out by the end of Fall 2008.
Milestone 1 demonstrates greatly extended support for modern technologies, frameworks and languages, plus noticeably better IDE performance.
Here’s the list of major new features that are already available to try out:

  • JBoss Seam Integration
  • Javascript/Flex Debugger
  • Greatly improved performance
  • Spring 2.5
  • FreeMarker, Velocity, GWT 1.5
  • REST WebServices support
  • Struts 2
  • Multi-dialect SQL Console
  • New Java refactorings
  • Even smarter code completion
  • Numerous new code inspections
  • and many more

Read more about all the new features, watch them in live action, and get your copy of the fresh release to try it all for yourself.

Thursday, February 7, 2008

How to show a pdf in landscape mode using iText API in Java

This post may help the pdf developers who use iText. I mean who use the com.lowagie.text and its sister packages.

I guess you (iText users) are familiar with the class
com.lowagie.text.Document

We have to tell this class the page size of the pdf in the following way.

document.setPageSize(PageSize.A4));   // suppose document is the instance of Document

But you will not find the word ‘Landscape’ anywhere in the context. So, how can you tell it to generate a pdf of landscape page size? Yes there is a simple but uncommon way to specify this.
See the solution below:

document.setPageSize(PageSize.A4.rotate());

Actually PageSize.A4 is a static variable of type com.lowagie.text.Rectangle

So by rotating it you are getting the landscape size of A4 page.

Wednesday, January 16, 2008

A simple method to compare versions

Sometimes we may need to compare two versions to know which one is newer and which one is older or whether both of those are same versions. Let me explain. Suppose you have two versions in hand as String “7.2.1″ and “7.1.1″. Now you wanna know which one is the later version. At the very first sight you may think that simple string comparison will give the correct decision. But unfortunately it won’t work correctly always. Think about this input : “10.0.1″ and “9.5.2″. String comparison will return the second one as latest version. But you expect the first one. So definitely we should change the algorithm.

A very simple algorithm is to split the input string by “.” and compare individual components. It guarantees you the correct output. Here is my implementation of the method. I have applied Unit Testing (www.junit.org , www.nunit.org) on it and it passes all possible test cases. You can use it without any doubt and it works for any length of versions.

Here is the implementation. Same code will run both in Java and C#.NET



/* This method compares two given versions and returns 0,1 or -1
It returns 0 if both versions are equal,
1 if first version is newer than second one,
-1 if first version is older than the second one.
Example:
compareVersion("7.1","7.1") will return 0
compareVersion("7.1.1","7.1.1") will return 0
compareVersion("7.1","7.1.1") will return -1
compareVersion("7.1","7.2") will return -1
compareVersion("7.1","7.2.2") will return -1
compareVersion("7.2","7.1") will return 1
compareVersion("7.2.0","7.1") will return 1
compareVersion("7.2.0","7.2") will return 0
compareVersion("7.abc.0","7.2") will throw exception
*/
public static int compareVersion(String v1, String v2) throws Exception {
String[] v1Tokens = v1.split("\\."); // for C#: String[] v1Tokens = v1.split(".");
String[] v2Tokens = v2.split("\\."); // for C#: String[] v2Tokens = v2.split(".");
int maxLen;

if (v1Tokens.length > v2Tokens.length)
maxLen = v1Tokens.length;
else
maxLen = v2Tokens.length;

for (int i = 0; i < maxLen; i++) {
int n1, n2;

n1 = n2 = 0;

try {
if (i < v1Tokens.length)
n1 = Integer.parseInt(v1Tokens[i]);

if (n1 < 0)
throw new Exception(v1 + " is not a valid version.");
} catch (Exception ex) {
throw new Exception(v1 + " is not a valid version.");
}

try {
if (i < v2Tokens.length)
n2 = Integer.parseInt(v2Tokens[i]);

if (n2 < 0)
throw new Exception(v2 + " is not a valid version.");
} catch (Exception ex) {
throw new Exception(v2 + " is not a valid version.");
}

if (n1 > n2)
return 1;
else if (n1 < n2)
return -1;
}

return 0;
}

Sunday, August 19, 2007

Generating Excel using POI in Java

May be you know excel files can be generated from Java by using an API POI.
( to know about POI visit http://poi.apache.org/ )

When I was using POI to generate excel file I saw a little problem after opening the generated excel file. Suppose I have to display the following text in a cell.

User name: John
Title : Programmer

You see there is a line break between the two lines. That means you have to write “\n” in the code. But when I opened the generated excel I found the two lines were displayed in a single line and there is a garbage box like [] in place of the new line.

But I expected two lines . So what the mistake I did? Can you guess?

Oh shit! I didn’t set the WrapText property of the cellStyle of the Cell.

So, keep in mind to write this

style.setWrapText(true);
(if the name of the cellStyle of the cell is ’style’.) if your text contains multiple lines.

One more point I observed this problem in MS Office , not in Linux’s Open Office. :-)