Creating Custom Code Panels using InstallAnywhere

In this write-up lets look at the way to create a Custom Choose File Panel using InstallAnywhere(IA) tool.

IA is a multi-platform software tool for creating customized installers.There are many documents available on IA that gives us a clear picture of IA and its features.

This write-up assumes that the reader of this blog is aware of the basic features available in IA and has a licensed InstallAnywhere enterprise Edition and not InstallAnywhere standard edition.In this blog I am adding a small piece of code that tells us how to develop custom panels. In the Advanced Designer we can add a file chooser by using "Panel: Choose File" action. The code below produces the same output as IA's Choose File action minus 'Restore Default File'.

To write a custom code panel ,it is necessary to write a class that extends CustomCodePanel class and override a couple of methods present in it.

Here is the custom code that demonstrates a custom choose file panel.



package com.myproduct.ia.custom.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Rectangle;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.zerog.ia.api.pub.CustomCodePanel;
import com.zerog.ia.api.pub.CustomCodePanelProxy;

public class CustomChooseFilePanel extends CustomCodePanel
{

 JTextArea summary = null;
 JTextArea textArea = null;
 private Font plainFont;
 private Font boldFont;
 JFileChooser fileChooser = null;
 public JTextField  tf  = null;
 JButton button = null ;
 JPanel jdataEntryPanel = null;
 File file = null;
 private JPanel [] jpanel = new JPanel[3];
 Label label = null;


 public CustomChooseFilePanel() {
   
  jdataEntryPanel = new JPanel(); 
  tf  = new JTextField();
  fileChooser = new JFileChooser();
  button = new JButton();
  label = new Label("Please Choose a File:", Label.LEFT);

  button.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(java.awt.event.ActionEvent e) {
    fileChooser.setDialogTitle("Please Choose a File:");
    int retval = fileChooser.showOpenDialog(CustomChooseFilePanel.this);
    if (retval == JFileChooser.APPROVE_OPTION) {
     File file = fileChooser.getSelectedFile();
     tf.setText(file.getAbsolutePath());
    }
   }
  });

  for(int i=0;i<jpanel.length;i++){
   jpanel[i] = new JPanel(new BorderLayout(3,1));
  }
 }

 public boolean setupUI(CustomCodePanelProxy ccpp)
 {
  final String fontName = "Dialog";
  final int fontSize = 12;
  plainFont = new Font(fontName, Font.PLAIN, fontSize);
  boldFont = new Font(fontName, Font.BOLD, fontSize);
  setLayout(new BorderLayout(20, 1));


  textArea = new JTextArea("Please Choose a File.");;
  textArea.setRows(6);
  textArea.setEditable(false);
  textArea.setFont(plainFont);

  JPanel panel = new JPanel(){
   public Insets getInsets() {
    return new Insets(1,2,10,10);
   }

  };
  panel.setLayout(new BorderLayout());
  panel.add(textArea, BorderLayout.CENTER);
  panel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
  panel.setBackground(Color.WHITE);
  add(panel, BorderLayout.NORTH);

  GridBagConstraints  gbc = new GridBagConstraints();
  add(jdataEntryPanel, BorderLayout.CENTER);
  jdataEntryPanel.setLayout(new GridBagLayout());
  jdataEntryPanel.setBackground(Color.WHITE);
  gbc.anchor = GridBagConstraints.NORTHEAST;

  gbc.gridx = 0;
  gbc.gridy = 0;
  gbc.insets = new Insets(1, 0, 1, 2);
  gbc.fill = GridBagConstraints.HORIZONTAL;
  label.setFont(boldFont);
  jpanel[0].add(label, BorderLayout.WEST);
  jpanel[0].setBackground(Color.WHITE);
  jdataEntryPanel.add(jpanel[0], gbc);

  gbc.gridx = 0;
  gbc.gridy = 1;
  gbc.insets = new Insets(1, 2, 1, 2);
  gbc.fill = GridBagConstraints.HORIZONTAL;
  tf.setFont(plainFont);
  tf.setBackground(new Color(243,243,227));
  tf.setEnabled(false);
  tf.setBounds(new Rectangle(4, 32, 373, 29));
  jpanel[1].add(tf, BorderLayout.CENTER);
  jpanel[1].setBackground(Color.WHITE);
  jdataEntryPanel.add(jpanel[1], gbc);

  gbc.weightx = 0.001;
  gbc.weighty = 0.001;
  gbc.gridx = 0;
  gbc.gridy = 2;
  gbc.insets = new Insets(1, 2, 1, 2);
  gbc.fill = GridBagConstraints.HORIZONTAL;
  button.setBounds(new Rectangle(202, 66, 174, 30));
  button.setFont(new Font("Arial", Font.PLAIN, 12));
  button.setText("Choose...");
  jpanel[2].add(button, BorderLayout.EAST);
  jpanel[2].setBackground(Color.WHITE);
  jdataEntryPanel.add(jpanel[2],gbc);

  return true;
 }

 public String getTitle() {
  return "Choose File";
 }

 @Override
 public boolean okToContinue() {
  customCodePanelProxy.setVariable("$CUSTOM_FILE_PATH$", tf.getText());
  return true;
 }

}


The look and feel of this custom panel in Windows will be similar to IA's 'Panel: Choose File' action.The only major difference that can be noticed here is the absence of 'Restore Default File' button.


                                       fig 1: Custom Choose File Panel in Windows

How ever the look and feel in Linux platform will be deviated. I am setting the Panel background to White. This needs to be modified [along with few other parameters] for Linux platforms to make the panel look similar to the one provided by IA.

When JFileChooser is used with Windows vista and Java-1.5, during run time we will encounter the following error.This happens if the installer tries to use WindowsLookAndFeel.


java.lang.ArrayIndexOutOfBoundsException: 3184
at sun.awt.shell.Win32ShellFolder2.getFileChooserIcon(Unknown Source)
at sun.awt.shell.Win32ShellFolderManager2.get(Unknown Source)
at sun.awt.shell.ShellFolder.get(Unknown Source)
at com.sun.java.swing.plaf.windows.WindowsLookAndFeel$LazyWindowsIcon.createValue(Unknown Source)
at javax.swing.UIDefaults.getFromHashtable(Unknown Source)
at javax.swing.UIDefaults.get(Unknown Source)
at javax.swing.MultiUIDefaults.get(Unknown Source)


This is a known issue for a combination of Java-1.5 and Windows Vista OS. The above problem will not appear, if we use Java-1.6

However, if this problem is encountered in vista with Java-1.5, then, add fileChooser = new JFileChooser(); in try-catch block, and forcefully set the look and feel to MetalLookAndFeel in the catch block.

i.e the code fileChooser = new JFileChooser(); in the above code can be rewritten as below.

try {
 fileChooser = new JFileChooser();
} catch(Exception ex) {
   try {
  UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  fileChooser = new JFileChooser();
   } catch (Exception e) {
    
   }
}

Hope this helps someone looking for a custom code panel.

Comments

  1. Thanks for this example. However, where in the IA project Advanced Designer does this get added?

    ReplyDelete

Post a Comment

Popular posts from this blog

Get JBoss Version using Java Code

Server Migration from JBoss-4.0.5 to JBoss-4.2.3.GA