/**@(#) AttributeSetterUtil.java 1.0 99/07/24 Larry Rodrigues\
  * Copyright (c) 1998 Lawrence Rodrigues
  **/
package com.vistech.graphics;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.beans.*;
import java.lang.reflect.*;
/** A utility class that creates a variety of GUI components.
  * @version 1.0  24  July 1999
  * @author Larry Rodrigues
  * **/
public class AttributeSetterUtil {
    public static JPanel doGridbagLayout(Component[] comp, int column){
       JPanel pan = new JPanel();
       GridBagLayout   gb = new GridBagLayout() ;
       GridBagConstraints c = new GridBagConstraints();
       pan.setLayout(gb);
       c.insets = new Insets(10,10,0,0);
       c.anchor = GridBagConstraints.NORTHEAST;
       c.weighty = 0.0; c.weightx = 0.0;
       c.gridwidth = 1;
       for(int i=0;i<comp.length;i++){
           c.gridwidth =1;
           if(((i%column)-column+1)==0) c.gridwidth=GridBagConstraints.REMAINDER;
           gb.setConstraints(comp[i],c);
           pan.add(comp[i],c);
       }
       return pan;
    }

    public static JComboBox createChoice(Object obj,
                                        String prName,
                                        String[] tag,
                                        int[] value){
        final String propName = prName;
        final int[] values = value;
        final String[] tags = tag;
        final Object bean = obj;
        final Class cl = bean.getClass();
        String str = "get"+propName;
        JComboBox  cb = new JComboBox();
        for(int i=0;i <tags.length;i++){
            cb.addItem(tags[i]);
        }
        try{
             Method getM = cl.getMethod(str, null);
             Object args[] = { };
             Object val = getM.invoke(bean, args);
             int index =0;
             int  intval = ((Integer)val).intValue();
             for(int i=0;i <tags.length;i++){
               if(intval == values[i]){
                  index=i;
                  break;
               }
             }
             cb.setSelectedIndex(index);
        }catch(Exception e){
            e.printStackTrace();
            return cb;
        }
        cb.addItemListener(
            new ItemListener(){
               public void itemStateChanged(ItemEvent e){
                  try{
                     int ind =((JComboBox)(e.getSource())).getSelectedIndex();
                     Class[] cla = {int.class};
                     Method setM = cl.getMethod("set"+propName, cla);
                     Integer[] args ={new Integer(values[ind])};
                     setM.invoke(bean, args);
                   }catch(Exception x){System.out.println("choice exception"); }
                }
            }
        );
        return cb;
    }
    public static JComboBox createColorChoice(Object obj, String prName){
        final String propName = prName;
        final Object bean = obj;
        final Class cl = bean.getClass();
        JComboBox  cb = new JComboBox();
        for(int i=0;i <colors.length;i++){
            cb.addItem(colorNames[i]);
        }
        try{
             String str = "get"+propName;
             Method getM = cl.getMethod(str, null);
             Object args[] = { };
             Object clr =  getM.invoke(bean, args);
             int index =0;
             for(int i=0;i <colors.length;i++){
               if(clr == colors[i]){
                  index=i;
                  break;
               }
             }
             cb.setSelectedIndex(index);
        }catch(Exception e){
             System.out.println("Reflection exception");
             return cb;
        }
        cb.addItemListener(
            new ItemListener(){
               public void itemStateChanged(ItemEvent e){
                  try{
                     int ind =((JComboBox)(e.getSource())).getSelectedIndex();
                     Class[] cla = {Color.class};
                     Method setM = cl.getMethod("set"+propName, cla);
                     Color[] args ={colors[ind]};
                     setM.invoke(bean, args);
                   }catch(Exception x){   System.out.println("choice exception"); }
               }
            }
        );
        return cb;
    }
    static Color[] colors = {Color.black, Color.blue, Color.red, Color.green,
                      Color.yellow, Color.cyan, Color.magenta, Color.white};
    static String[] colorNames = {"black", "blue", "red", "green",
                      "yellow", "cyan", "magenta", "white"};
}