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.util.HashSet;
009    import java.util.Iterator;
010    import java.util.Set;
011    import java.util.Vector;
012    
013    import javax.swing.JPanel;
014    
015    import simuLCS.Agent;
016    import simuLCS.Classifier;
017    import simuLCS.Entity;
018    import simuLCS.Simulation;
019    import simuLCS.Template;
020    
021    /**
022     * Important class. Superclass of all the components, so that they can all "know"
023     * which entities are currently selected (static members of this class).
024     * @author Benoit
025     * 
026     */
027    public class G_Panel extends JPanel {
028    
029            protected static int maxSubPanels = 100;
030    
031            protected static Simulation pointerSimulation;
032            protected static G_MainWindow pointerMainWindow;
033            protected static G_PanelCustom pointerCustom;
034    
035            protected static int nbSelectedEntities;
036    
037            /**
038             * Current Selected Classifier
039             */
040            protected static Classifier currentClassifier;
041    
042            /**
043             * Current Entities selected for customization
044             */
045            protected static HashSet selectedEntities = new HashSet();
046    
047            /**
048             * Current Entity selected to watch the LCS working
049             */
050            protected static HashSet currentEntityToWatch = new HashSet();
051    
052    
053            public G_Panel() {
054            }
055    
056            /**
057             * This class provides an access to the Data Model - 
058             * the link shoud be made before, thanks to this function
059             * @param s
060             */
061            public static void linkSimulation(Simulation s) {
062                    pointerSimulation = s;
063            }
064    
065            /**
066             * This class provides an access to the Main Window
067             * the link should be made before, thanks to this function
068             * @param win
069             */
070            public void linkMainWindow(G_MainWindow win) {
071                    pointerMainWindow = win;
072            }
073    
074            /**
075             * All the components can access the Data Model through this function
076             * @return
077             */
078            public Simulation getSimu() {
079                    return pointerSimulation;
080            }
081    
082            /**
083             * Returns the current entity selected to watch the LCS working
084             * @return
085             */
086            public static Entity getCurrentEntityToWatch() {
087                    if (currentEntityToWatch.iterator().hasNext()) {
088                            return (Entity) currentEntityToWatch.iterator().next();
089                    } else {
090                            System.out.println("No current Entity to Watch.");
091                            return null;
092                    }
093            }
094    
095            public static Set getSetCurrentEntityToWatch() {
096                    return currentEntityToWatch;
097            }
098    
099            /**
100             * @return
101             */
102            public static Set getSetSelectedEntities() {
103                    return selectedEntities;
104            }
105    
106            /**
107             * Return only the Agents among the Entities selected.
108             * @return
109             */
110            public static Agent[] getAgentArraySelectedEntities() {
111                    Agent[] ag = new Agent[0];
112                    Vector v = new Vector();
113                    Iterator iter = ((Set) selectedEntities).iterator();
114                    Entity cur;
115                    while (iter.hasNext()) {
116                            cur = (Entity) iter.next();
117                            if (cur instanceof Agent)
118                                    v.add(cur);
119                    }
120                    ag = (Agent[]) v.toArray(ag);
121                    return ag;
122            }
123    
124            /**
125             * Return an Array of all the Selected Entities
126             * @return
127             */
128            public static Entity[] getArraySelectedEntities() {
129                    Entity[] en = new Entity[0];
130                    return (Entity[]) selectedEntities.toArray(en);
131            }
132            
133            /**
134             * This function returns a Template if all the selected entities
135             * share the same Template (in which case they can have common classifiers).
136             * Otherwise, it returns null.
137             * @return
138             */
139            public static Template getTemplateFromSelected() {
140                    if (selectedEntities.isEmpty()) {
141                            System.out.println("No entities selected.");
142                            return null;
143                    } else {
144                            
145                            Agent[] ag = getAgentArraySelectedEntities();
146    
147                            if (ag.length == 0) {
148                                    pointerMainWindow.getStatusBar().printMessage("No agent.");
149                                    return null;
150                            }
151    
152                            //                      System.out.println("in getTemplate:"+getSetSelectedEntities());
153                            //                      System.out.print("agents:");
154                            //                      for(int j=0;j<ag.length;j++)
155                            //                              System.out.print(ag[j]+" type"+ag[j].getClass().getName()+";");
156                            //                      System.out.println("");
157                            Template current;
158                            if (ag[0].getBehaviour() == null) {
159                                    pointerMainWindow.getStatusBar().printMessage("No behaviour.");
160                                    return null;
161                            } else {
162                                    current = ag[0].getBehaviour().getTemplate();
163                            }
164    //                      System.out.println("Current template:"+current.getClass().getName());
165    
166                            if (ag.length == 1) {
167                                    return current;
168                            } else {
169    
170                                    for(int k= 1; k < ag.length; k++)
171                                    {
172    //                                      System.out.println("test:"+ag[k].getBehaviour().getClass().getName());
173                                            if ((ag[k].getBehaviour() == null)
174                                                    || (!(ag[k]
175                                                            .getBehaviour()
176                                                            .getTemplate()
177                                                            .getClass()
178                                                            .getName()
179                                                            .equals(current.getClass().getName()))))
180                                            {
181                                                    pointerMainWindow.getStatusBar().printMessage(
182                                                            "Different templates.");
183                                                    return null;
184                                            }
185                                    }
186                                    return current;
187                            }
188    
189                    }
190    
191            }
192    
193    
194            public void updateValues() {
195            };
196    
197            
198            /**
199             * All the subclasses can request a recall of the Customization panel
200             * (if something has changed, to notify the other panels) through this function
201             *
202             */
203            public static void repaintCustom() {
204                    pointerCustom.repaint();
205            }
206    
207            /**
208             * All the subclasses can request a recall of the whole Window
209             * (if something has changed, to notify the other panels AND the drawing) 
210             * through this function
211             *
212             */
213            public static void repaintAll() {
214                    pointerMainWindow.repaint();
215            }
216    
217            /**
218             * @return the currently selected classifier
219             */
220            public static Classifier getCurrentClassifier() {
221                    return currentClassifier;
222            }
223    
224            /**
225             * @param classifier
226             */
227            public static void setCurrentClassifier(Classifier classifier) {
228                    currentClassifier = classifier;
229            }
230    
231            /**
232             * @return
233             */
234            public static G_MainWindow getPointerMainWindow() {
235                    return pointerMainWindow;
236            }
237    
238    }