Getting started with Apache Click and Eclipse Galileo

Posted by admin | Posted in Code Snippets, General Programming, Tools | Posted on 18-02-2010

3

Last couple of days I read here and there about Apache Click, which is now in version 2.1.0. It looks preety cool and easy to learn, so I decided to give it a try and see for myself.
I understand that its cool to learn things using vi and shell only, but when you tried many frameworks before, you know how things works, you just want to see how or how not, productive you can be with the new framework, thats why I jump straight to the IDE.
And here, first surprise, with apache click its’ not that easy, well, I’m mean its still easy but I have encountered a few problems, and right now I would like to share the solution.
First of all, I did not tried all the examples in the apache click user guide yet, haven’t build anything usefull and definietly I’m not an Apache Click expert, I think I have figured out how to plug it to Eclipse and I think this is good start :) .


Here you have a few points to build your Apache Click IDE:
1. First of all download latest and greatest Eclipse Galileo from here – choose Eclipse for Java EE developers
2. Download Apache Click libraries from here – choose version 2.1.0
3. Download Apache Click IDE from here – note that it contains Click version 1.5 and it was last updated in November 2008 – maybe I’m missing something here?
4. Unzip your ClickIDE and copy the ‘features’ and ‘plugins’ folders into your Eclipse home directory
5. Unzip your latest click libraries and keep it for later :)
6. Start your Eclipse IDE and create new ‘Dynamic Web Project’

Choose target name, in this case ‘ClickDemo2′, select your target runtime as Tomcat 6+ etc.
7. Under configuration combo box, select custom and click ‘Modify’

8. Select ‘Click’ in Project Facet List and save the configuration as click config

9. Once you have your configuration created, dynamic web project screen should look like in the following screenshot:

10. The rest of the settings you can leave as it is, just click ‘Next’ up to the point when you finish creating new dynamic web project.
11. After your project is created it will have the structure like in the following screen:



Notice that there are click libraries from version 1.5, which is no good, we want to use the latest version which is 2.1.0.
12. Copy the files ‘click-2.1.0.jar’ and ‘click-extras-2.1.0.jar’ into the same lib folder where the old libraries are found.
13. Delete old libraries and refresh the project tree under eclipse (right click on the project node and select ‘Refresh’), now you will see the new libraries come up in the lib folder.
14. Open your web.xml file under ‘WebContent/WEB-INF/’ folder and change the class name of the main Click Servlet, from ‘net.sf.click.ClickServlet’ to ‘org.apache.click.ClickServlet’ in ’servlet-class’ declaration.
15. With Eclipse, select the project node and click ‘New’ -> ‘Other’, choose ‘Click Page’ under ‘click’ directory.

16. Fill out the necessary fields for your new click page as below:

17. Name the file ‘index.htm’ and click ‘Finish’
18. Under the page java source file, fix the Page class by using the page from the: org.apache.click.Page instead of the old path.

package com.softwarepassion;

import java.util.Date;

import org.apache.click.Page;


public class Index extends Page {
   
    private Date time = new Date();
   
    public Index(){
        addModel("time", time);
    }

}

Modify your ‘index.htm’ using html editor to look something like this:

<html>
  <head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8">
    <title>Blank</title>
    $imports
  </head>
  <body>
  $time
  </body>
</html>

19. Start up your project in tomcat. The page should display current time!


Additional Comments:
I know that this is just a quickfix, and I just cannot belive that there is no proper eclipse plugin using the latest click framework, which would be up to date, if you know any, than please comment and save me and my readers precious time.

Thank you.

Jasperberry – the most dynamic reports ever.

Posted by admin | Posted in Code Snippets, General Programming, Tools | Posted on 17-02-2010

8

Today I will describe in short my latest and simplest quick and dirty trick, generating jasper report dynamically. Jasperberry is a component written very quickly to help me
generate pdf report in a situation where I don’t know the number of columns I will have, I don’t know their widths, the data they contain etc, all is generated at runtime dynamically.
I know that there is DynamicJasper project but it seems to be dead, and its dead in a very strange way, I mean, I see that the authors commit new code but there is no chance to get any support unless, I guess, you pay for it.
With dynamic jasper I couldn’t generate report with polish fonts displayed properly, tried many different tricks, no support on the forum, no support on stack overflow, no nothing. After 2 days I have realized that I will write my own library quicker to solve my problem than get an answer from the dynamic jasper community (if there is any).
So here we have: jasperberry!


Don’t expect too much from it but if you want to display simple table with data, dynamic columns, adjustable fonts and colors than this is it.

In fact you don’t have to even know object properties to generate the report, we will use Apache DynaBeans for that:

This is a simple example code snipped showing you how to get started with the simplest and most dynamic reporting library ever (as well as lacking features probably but we hope its enough for simple projects)

This example assumes the following:

1. You want simple report
2. You dont have to know the object which properties you want to display in advance, by this I mean you can create report columns dynamically, and by using apache dyna beans your objects don’t have to even exists. You just dynamically create them and inject them into the report.

Here is an example test class showing you how to use jasperberry, for more info please contact us directly at softberries.com
Code:

public class JRXMLBuilderTest {

    private static List<JBColumnBean> columns;

    public JRXMLBuilderTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
        columns = new ArrayList<JBColumnBean>();
        JBColumnBean col1 = new JBColumnBean();
        JBColumnBean col2 = new JBColumnBean();
        JBColumnBean col3 = new JBColumnBean();
        JBColumnBean col4 = new JBColumnBean();
        JBColumnBean col5 = new JBColumnBean();
        JBColumnBean col6 = new JBColumnBean();
        JBColumnBean col7 = new JBColumnBean();
        col1.setPreferredWidth(60);
        col1.setProperty("name");
        col1.setTitle("ImiÄ™");
        col2.setPreferredWidth(60);
        col2.setProperty("surname");
        col2.setTitle("Nazwisko");
        col3.setPreferredWidth(160);
        col3.setProperty("comment");
        col3.setTitle("Komentarz");
        col4.setPreferredWidth(100);
        col4.setProperty("comment2");
        col4.setTitle("Komentarz");
        col5.setPreferredWidth(100);
        col5.setProperty("comment3");
        col5.setTitle("Komentarz");
        col6.setPreferredWidth(100);
        col6.setProperty("comment4");
        col6.setTitle("Komentarz");
        col7.setPreferredWidth(100);
        col7.setProperty("comment5");
        col7.setTitle("Komentarz");
        columns.add(col1);
        columns.add(col2);
        columns.add(col3);
        columns.add(col4);
        columns.add(col5);
        columns.add(col6);
        columns.add(col7);
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /** * Test of build method, of class JRXMLBuilder. */
    @Test
    public void testBuild() {
        try {
            System.out.println("build");
            List<JBReportElement> elements = new ArrayList<JBReportElement>();
            //col header font
            JBFont colHeaderFont = new JBFont();
            colHeaderFont.setBold(true);
            colHeaderFont.setSize(10);
            colHeaderFont.setForegroundColor("#555555");
            //detail font
            JBFont detailFont = new JBFont();
            detailFont.setSize(6);
            detailFont.setForegroundColor("#000000");
            detailFont.setPdfEncoding("Cp1257");
            JBPage page = new JBPage();
            JBReport report = new JBReport(page, colHeaderFont, detailFont, "#CCCCFF", "#DEDEDE", "#FFFFFF");
            JBFont titleFont = new JBFont();
            titleFont.setBold(true);
            titleFont.setSize(20);
            titleFont.setForegroundColor("#FF0000");
            titleFont.setPdfEncoding("Cp1257");
            //add title elements
            elements.add(new JBStaticTextElement("Hello World!", 450, 10, 300, titleFont));
            elements.add(new JBImageElement("C:\\\\Documents and Settings\\\\admin\\\\Moje dokumenty\\\\NetBeansProjects\\\\JASPER_BERRY\\\\test.jpg", 0, 0, 350, 242));
            report.setTitleElements(elements);
            JRXMLBuilder instance = new JRXMLBuilder(columns, report);
            boolean expResult = true;
            boolean result = instance.build();
            JasperReport jr = JasperCompileManager.compileReport("jasperberry_by_softberries.jrxml");
            //PREPARE SOME DATA
            DynaProperty[] props = new DynaProperty[columns.size()];
            for (int i = 0; i < columns.size(); i++) {
                props[i] = new DynaProperty(columns.get(i).getProperty(), String.class);
            }
            BasicDynaClass dynaClass = new BasicDynaClass("tester", null, props);
            List<DynaBean> beans = new ArrayList<DynaBean>();
            for (int i = 0; i < 100; i++) {
                DynaBean db = dynaClass.newInstance();
                db.set("name", "Krzysztof " + i);
                db.set("surname", "Grajek " + i);
                db.set("comment", "Komentarz ");
                db.set("comment2", "Komentarz ");
                db.set("comment3", "Komentarz ");
                db.set("comment4", "Komentarz ");
                //
                db.set("comment5", "Komentarz ");
                beans.add(db);
            }
            JasperPrint jp = JasperFillManager.fillReport(jr, null, new JRBeanCollectionDataSource(beans));
            JasperExportManager.exportReportToPdfFile(jp, "jasperberry.pdf");
            assertEquals(expResult, result);
        } catch (Exception ex) {
            Logger.getLogger(JRXMLBuilderTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

You should find similar snippet in a test suite of the project.
The project itself is licensed under LGPL and its available from sourceforge here

Deploy your Java Application with IzPack Installer

Posted by admin | Posted in Code Snippets, General Programming | Posted on 06-02-2010

9

This is a short tutorial on how to create simple IzPack based installation solution on windows platform with all standard features like: desktop shortcut, start menu item shortcut, copying libraries, folders, documentation into one of the ProgramFiles folder on the windows machine.
I had some problems creating windows shortcuts mysefl and I wanted to share the experience so other people can find it quicker.
Here you go.


1. First download and install IzPack itself from: izpack website

2. Prepare the application you want to make installable, I use Netbeans to build most of my Java apps, but I guess for all other ide’s the results after all are the same, we end up with SomeApp.jar file and a bunch of libraries we used to build it.
On the Netbeans platform all the files needed to launch the application we have build end up in ‘dist’ folder as a subfolder of our project main folder. If you have used any libraries to build your desktop application they will be included in the lib folder (yourproject/dist/lib/).

3. Now, once we have all the main ingredients (our application and installer) we need to connect it together. Create the folder anywhere on your machine, and call it for example ’softwarepassion’.

4. Put your jar file together with the ‘lib’ folder into the ’softwarepassion’ catalog.

5. Now we need to add some native libraries for Izpack to be able to add windows shortcuts on the desktop and start menu. Create ‘bin’ folder under the ’softwarepassion’ folder and copy whole ‘native’ content found in your izpack installation into the ‘bin’ folder of your root ’softwarepassion’ catalog.
You can find the native folder inside your IzPack installation directory, normally under: ‘C:\Program Files\IzPack\bin’

6. Create your application icon file and add it to your ’softwarepasison’ catalog. I call mine ‘exe.ico’.

7. Now comes the hardest part to get initially but after you build your first installer it all gets clear and easy :)
We need to prepare two separate xml files: one called ‘install.xml’ and another one called ’shortcutSpec.xml’.

The content of both example files is listed below:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>

<!--
   A sample installation file.
   Use it as a base for your own installers :-)
   
   To compile it :
   - go in the bin directory where you installed IzPack
   - call "compile ../sample/install.xml -b ../sample"
-->

<installation version="1.0">

    <!--
       The info section.
       The meaning of the tags should be natural ...
   -->
    <info>
        <appname>SoftwarePassion Application</appname>
        <appversion>1.0</appversion>
        <authors>
            <author name="Softberries Krzysztof Grajek" email="customer.service@softberries.com"/>
        </authors>
        <url>http://www.softwarepassion.com/</url>
    </info>

    <!--
       The gui preferences indication.
       Sets the installer window to 640x480. It will not be able to change the size.
   -->
    <guiprefs width="640" height="480" resizable="yes"/>
    <variables>
        <variable name="DesktopShortcutCheckboxEnabled" value="true"/>
    </variables>
    <!--
       The locale section.
       Asks here to include the English and French langpacks.
   -->
    <locale>
        <langpack iso3="pol"/>
        <langpack iso3="eng"/>
    </locale>

    <!--
       The resources section.
       The ids must be these ones if you want to use the LicencePanel and/or the InfoPanel.
   -->
    <resources>
        <res src="shortcutSpec.xml" id="shortcutSpec.xml"/>
        <res id="LicencePanel.licence" src="Licence.txt"/>
        <res id="InfoPanel.info" src="Readme.txt"/>
    </resources>

    <!--
       The panels section.
       We indicate here which panels we want to use. The order will be respected.
   -->
    <panels>
        <panel classname="HelloPanel"/>
        <panel classname="LicencePanel"/>
        <panel classname="TargetPanel"/>
        <panel classname="PacksPanel"/>
        <panel classname="InstallPanel"/>
        <panel classname="ShortcutPanel"/>
        <panel classname="FinishPanel"/>
    </panels>

    <!--
       The packs section.
       We specify here our packs.
   -->
    <packs>
        <pack name="Base" required="yes">
            <description>The base files</description>
            <file src="Readme.txt" targetdir="$INSTALL_PATH"/>
            <file src="Licence.txt" targetdir="$INSTALL_PATH"/>
            <file src="exe.ico" targetdir="$INSTALL_PATH"/>
            <file src="SoftwarePassionHelloIzPack.jar" targetdir="$INSTALL_PATH"/>
            <fileset dir="lib" targetdir="$INSTALL_PATH\lib">
                <include name="**"/>
            </fileset>
        </pack>
    </packs>
    <native type="izpack" name="ShellLink.dll"/>
    <native type="3rdparty" name="COIOSHelper.dll" stage="both">
        <os family="windows"/>
    </native>
</installation>

And the one for windows shortcuts:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<shortcuts>

    <skipIfNotSupported/>

    <programGroup defaultName="Siemens AWORT" location="applications"/>

      <shortcut
        name="SoftwarePassion Application"
        programGroup="yes"
        desktop="yes"
        applications="no"
        startMenu="yes"
        startup="no"
        target="$INSTALL_PATH\SoftwarePassionHelloIzPack.jar"
        commandLine=""
        description="Software Passion Example App"
        iconFile="$INSTALL_PATH\exe.ico"
        iconIndex="0"
        initialState="noShow">

         <createForPack name="Base"/>

       </shortcut>
 

    <shortcut
           name="Software Passion Example App Uninstaller"
           programGroup="yes"
           desktop="no"
           applications="no"
           startMenu="no"
           startup="no"
           target="$INSTALL_PATH\Uninstaller\uninstaller.jar"
           commandLine=""
           iconFile="%SystemRoot%\system32\SHELL32.dll"
           iconIndex="31"
           description="Uninstall SoftwarePassion Example">

        <createForPack name="Base"/>
    </shortcut>

</shortcuts>

8. Example installation script contains both Readme.txt and Licence.txt which you can add to your ’softwarepassion’ folder.
9. Once you have it all in one place, you should have a structure like in the following screenshot:

izpack_directory

10. Now assuming that your ’softwarepassion’ folder has been placed directly on the c: drive, execute the following command:

C:\Program Files\IzPack\bin>compile c:\softwarepassion\install.xml -b c:\softwarepassion

Assuming that IzPack itself was installed at: C:\Program Files\IzPack

This will produce install.jar file which is your installer.

11. If you dont’ want your end users to click on the ‘jar’ file as not everybody knows they can do that, you can add Izpack launcher app which will launch it automatically, or use another tool to create an executable called launch4j

You have to admit that it was an easy one!!!!!

Happy Installing!


Download example files from here

Add ‘count #’ column to JTable

Posted by admin | Posted in Code Snippets, General Programming, Graphics And Design, Learning Materials | Posted on 05-02-2010

0

Important note first: This is mostly not my code, I was strugling with this problem for some time, I couldn’t google it at all, and one day, I’ve just found it. Now I use it in one of my projects but I cannot find the original post again, therefore I have decided to post it on my blog (slightly modified version of the original) so other people with similar problem will have a better chance of finding the solution.
So, the idea is very simple. The whole point of displaying additional column which is kind of independent from the other columns is to simply use another table. This way you can sort one table by column and the number column will not sort with it. – thats exactly what I was looking for and I guess its kind of popular problem.
Becouse you put two tables in one scroll pane the gui looks like its just one column!


I have created one simple method to apply this technique to any tables in my application (I use JXTable actually but JTable will do as well)

private void addLPColumn(JXTable table, JScrollPane scp){
        JTable rowTable = new RowNumberTable(table);
        scp.setRowHeaderView(rowTable);
        scp.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowTable.getTableHeader());
        scp.repaint();
    }


RowNumberTable is simple one column table displaying the ‘count’ number of rows in the second table.


public class RowNumberTable extends JTable
    implements ChangeListener, PropertyChangeListener
{
    private JTable main;

    public RowNumberTable(JTable table)
    {
        main = table;
        main.addPropertyChangeListener( this );

        setFocusable( false );
        setAutoCreateColumnsFromModel( false );
        setModel( main.getModel() );
        setSelectionModel( main.getSelectionModel() );

        TableColumn column = new TableColumn();
        column.setHeaderValue("Lp.");
        addColumn( column );
        column.setCellRenderer(new RowNumberRenderer());

        getColumnModel().getColumn(0).setPreferredWidth(40);
        setPreferredScrollableViewportSize(getPreferredSize());
    }

    @Override
    public void addNotify()
    {
        super.addNotify();

        Component c = getParent();

        //  Keep scrolling of the row table in sync with the main table.

        if (c instanceof JViewport)
        {
            JViewport viewport = (JViewport)c;
            viewport.addChangeListener( this );
        }
    }

    /*
     *  Delegate method to main table
     */

    @Override
    public int getRowCount()
    {
            if(main != null)
                return main.getRowCount();
            else
                return 0;
    }

    @Override
    public int getRowHeight(int row)
    {
        return main.getRowHeight(row);
    }

    /*
     *  This table does not use any data from the main TableModel,
     *  so just return a value based on the row parameter.
     */

    @Override
    public Object getValueAt(int row, int column)
    {
        return Integer.toString(row + 1);
    }

    /*
     *  Don't edit data in the main TableModel by mistake
     */

    @Override
    public boolean isCellEditable(int row, int column)
    {
        return false;
    }
//
//  Implement the ChangeListener
//
    public void stateChanged(ChangeEvent e)
    {
        //  Keep the scrolling of the row table in sync with main table

        JViewport viewport = (JViewport) e.getSource();
        JScrollPane scrollPane = (JScrollPane)viewport.getParent();
        scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
    }
//
//  Implement the PropertyChangeListener
//
    public void propertyChange(PropertyChangeEvent e)
    {
        //  Keep the row table in sync with the main table

        if ("selectionModel".equals(e.getPropertyName()))
        {
            setSelectionModel( main.getSelectionModel() );
        }

        if ("model".equals(e.getPropertyName()))
        {
            setModel( main.getModel() );
        }
    }

    /*
     *  Borrow the renderer from JDK1.4.2 table header
     */

    private static class RowNumberRenderer extends DefaultTableCellRenderer
    {
        public RowNumberRenderer()
        {
            setHorizontalAlignment(JLabel.CENTER);
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            if (table != null)
            {
                JTableHeader header = table.getTableHeader();

                if (header != null)
                {
                    setForeground(header.getForeground());
                    setBackground(header.getBackground());
                    setFont(header.getFont());
                }
            }

            if (isSelected)
            {
                setFont( getFont().deriveFont(Font.BOLD) );
            }

            setText((value == null) ? "" : value.toString());
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));

            return this;
        }
    }
}

count_jtable_column

In my case the ‘count’ column is called ‘Lp.’ – in polish



I have created one simple method to apply this technique to any tables in my application (I use JXTable actually but JTable will do as well)

private void addLPColumn(JXTable table, JScrollPane scp){
        JTable rowTable = new RowNumberTable(table);
        scp.setRowHeaderView(rowTable);
        scp.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowTable.getTableHeader());
        scp.repaint();
    }