001    /*
002     * SimuCS - Simulator to use with Classifier Systems 
003     * MSc project - Oxford University 
004     * by Benoit Isaac - Summer 2005
005     */
006    
007    package simuLCS.graphics;
008    import java.awt.Graphics;
009    import java.awt.event.ActionEvent;
010    
011    import javax.swing.AbstractAction;
012    import javax.swing.JComboBox;
013    import javax.swing.JLabel;
014    
015    import simuLCS.ClassifierComponent;
016    import simuLCS.Utils;
017    
018    
019    /**
020     * Used as a GUI for a ClassifierComponent - A list of values
021     * @see simuLCS.ClassifierComponent
022     * @author Benoit
023     * 
024     */
025    public class G_ClassifierComboBox extends G_ClassifierComponent {
026            
027            
028            private JComboBox cb ;
029    //      private String[] listOfValues ;
030    //      private boolean isEditable ;
031            
032            /**
033             * Creates a ComboBox displaying different values 
034             * @param name
035             * @param pos
036             * @param which
037             * @param isTxt
038             * @param list By convention, list of strings where the first characters are
039             *      a list of 0,1 or #, and just after there should be a space character
040             *  that announces that the following characters are a comment describing the
041             *  value
042             * @param nbBits
043             * @param isEditable
044             */
045            
046            public G_ClassifierComboBox(ClassifierComponent c, String name, int pos, 
047                    int nbBits, int which)
048            {
049            
050                    super(c,name,pos,nbBits,which); 
051    //              System.out.println("Constructor, list of Values:"+c.getListOfValues());
052            }
053            
054            /**
055             * Returns the panel with the list of Values
056             */
057            public G_Panel getUIComponent()
058            {
059                    G_Panel p = new G_Panel();
060                    p.setOpaque(true);
061    //              System.out.println("getUI, list of Values:"+
062    //                      getClassifierComponent().getListOfValues());
063                    cb = new JComboBox(getClassifierComponent().getListOfValues());
064                    cb.setOpaque(true);
065                    cb.setMaximumRowCount(3);
066                    cb.setEditable(getClassifierComponent().isEditable());
067                    cb.addActionListener(new ActComboBox());
068                    JLabel l = new JLabel(name);
069                    p.add(l);
070                    p.add(cb);
071                    return p;
072            }
073            
074            protected String findItem(String s)
075            {
076                    String current ;
077                    for(int i=0;i<cb.getItemCount();i++)
078                    {
079                            current = (String) cb.getItemAt(i);
080                            if(current.indexOf(s) != -1)
081                            {       return current ;
082                            }
083                    }
084                    return "";
085            }
086            
087            
088            public void updateValuesFrom(String s)
089            {
090                    int item = Utils.getValueFromBits(s) ;
091                    if(item == -1) // there is a # symbol
092                    {
093                            // try to find the same item
094                            String r = findItem(s) ;
095                            if(! r.equals(""))
096                            { // the item has been found
097                                    cb.setSelectedItem(r);
098                            }
099                            else
100                            {       cb.setSelectedItem(s);
101                            }
102                    }
103                    else
104                    {       // TODO handle if item > listValues.length (outside range)
105                            cb.setSelectedIndex(item);
106                    }       
107                    
108            }
109            
110            public void paintComponents(Graphics g)
111            {
112                    updateValues();
113            }
114            
115            class ActComboBox extends AbstractAction {
116                    public void actionPerformed(ActionEvent e) {
117                            JComboBox cb = (JComboBox)e.getSource();
118                            String value = (String)cb.getSelectedItem();
119                            String finalvalue ;
120                            // we extract only the first characters before a space
121                            int pos_space = value.indexOf(' ');
122                            if(pos_space == -1) // no space, all the string is taken into account
123                            {       finalvalue = value ; } 
124                            else
125                            {       // there is a space, all the characters before are taken into account
126                                    finalvalue = value.substring(0,pos_space);
127                            }
128                            setClassifierPart(finalvalue);
129                    }
130    
131            }
132    
133            
134    }