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.FlowLayout;
009    
010    import simuLCS.Classifier;
011    import simuLCS.ClassifierComponent;
012    
013    
014    /**
015     * Used as a GUI for a ClassifierComponent 
016     * This is the abstract superclass any component should implement
017     * @see simuLCS.ClassifierComponent
018     * @author Benoit
019     * 
020     */
021    public abstract class G_ClassifierComponent extends G_Panel {
022            
023            protected String name ;
024            protected int nbBits ;
025            protected int position ;
026            //      whichpart = 0 -> for Condition ; whichpart = 1 -> for Action
027            protected int whichpart ;
028            protected boolean isWithText = false;
029            
030            protected String previousValueBits = "";
031            
032            private ClassifierComponent pointerComponent ;
033            
034            /**
035             * A GUI for a ClassifierComponent.
036             * This GUI will be linked with the bits defined with the arguments
037             * @param c the ClassifierComponent which uses this Graphical Component
038             * @param n the Name of this Component 
039             * @param pos the position of the bits to modify/listen to
040             * @param nb the nb of bits in the group
041             * @param which tells if this is a component for the Condition or the Action Part 
042             * @see Classifier#PART_CONDITION, @see Classifier#PART_ACTION
043             */
044            public G_ClassifierComponent(ClassifierComponent c, String n, int pos,
045                     int nb, int which)
046            {
047                    super();
048                    pointerComponent = c ;
049                    name = n; 
050                    nbBits = nb ; position = pos ; whichpart = which ; 
051    //              isWithText = isTxt ;
052                    
053                    setLayout(new FlowLayout());
054                    
055                    G_Panel g = getUIComponent() ;
056                    
057                    add(g);
058                    
059                    if(pointerComponent.isWithText())
060                    {
061                            G_Panel txt = getTextAssociated() ;
062                            add(txt);
063                    }
064                    
065                            
066            }
067            
068            /**
069             * The subclasses should provide the GUI for their component
070             * @return
071             */
072            public abstract G_Panel getUIComponent();
073            
074            public G_Panel getTextAssociated()
075            {
076                    return null;
077            }
078            
079            public ClassifierComponent getClassifierComponent()
080            {
081                    return pointerComponent;
082            }
083            
084            /**
085             * The subclasses should provide a way to update their display
086             * according to the value of the bits in the String <code>s</code>
087             * @param s
088             */
089            public abstract void updateValuesFrom(String s);
090            
091            public void updateValues()
092            {
093                    Classifier currentClass = G_Panel.getCurrentClassifier() ;
094    
095                    if(currentClass != null)
096                    {
097                            String currentBits = currentClass.getBits(position,nbBits,whichpart);
098                            
099                            updateValuesFrom(currentBits);
100                            // TODO update Text ?
101                            // TODO is this working ?
102                            this.validate();
103                    }               
104            }
105            
106            /**
107             * The GUI are calling this action to update the Classifier
108             * (this is common to all the components, no matter how their display look like)
109             * @param newValue the newValue of the bits to put in the Classifier
110             */
111            public void setClassifierPart(String newValue)
112            {
113                    if(! newValue.equals(previousValueBits)) // new value -> Update !
114                    {
115                            Classifier currentClass = getCurrentClassifier();
116                            if(currentClass != null)
117                            {
118                                    currentClass.setBits(newValue,position,whichpart);
119                                    previousValueBits = newValue ;
120                                    repaintCustom();
121                            }                                                                       
122                    }
123            }
124            
125    
126            
127    
128    }