/* * Created on 27-Oct-2004 by Ryan McNally */ package com.speckled.specksim.gui.imp; import java.awt.event.MouseEvent; import java.util.Iterator; import com.ryanm.logging.LoggingManager; import com.speckled.specksim.Speck; import com.speckled.specksim.SpeckSim; import com.speckled.specksim.gui.SimGUI; import com.speckled.specksim.gui.visualiser.GLSelectionListener; import com.speckled.specksim.gui.visualiser.SpeckVisualiser; import com.speckled.specksim.imp.specks.AbstractSpeck; /** * This class listens to selection changes in the visualiser, and * toggles logging in the selected speck * * @author ryanm */ public class SpeckLoggingController extends GLSelectionListener { private SpeckSim simulator; private SpeckVisualiser visualiser; /** * Constructs a controller */ public SpeckLoggingController() { } @Override public void init( SpeckSim simulator, SimGUI gui, SpeckVisualiser visualiser ) { // we need to save a reference to the simulator in order to // search through the speck populations later on this.simulator = simulator; this.visualiser = visualiser; } @Override public void mouseClicked( MouseEvent e ) { int[] nameStack = visualiser.getNameStackUnderMouse(); // what appears in the namestack depends on what the active // StateRenderers put there. For instance, the SpeckRenderer // will put on the id of the speck being rendered, the // NeighbourhoodRenderer puts on the ids of the two specks // involved in a neighbourhood link if( nameStack != null && nameStack.length >= 1 ) { int id = nameStack[ 0 ]; // search the simulator for the speck with the selected id. // When iterating over the specks in the simulator, you MUST // synchronize on the simulator object. This prevents specks // being added or removed during your iteration synchronized( simulator ) { Iterator specks = simulator.getSpecks(); while( specks.hasNext() ) { Speck speck = specks.next(); if( speck.id() == id && speck instanceof AbstractSpeck ) { // Only AbstractSpecks have the setLoggingEnabled() // method, so we must check the type of the speck // with instanceof before proceeding AbstractSpeck as = ( AbstractSpeck ) speck; if( as.isLoggingEnabled() ) { as.setLoggingEnabled( false ); LoggingManager.log( SimGUI.SIM_GUI_LOG_SOURCE, "Logging disabled in speck " + speck.id() ); } else { as.setLoggingEnabled( true ); LoggingManager.log( SimGUI.SIM_GUI_LOG_SOURCE, "Logging enabled in speck " + speck.id() ); } } } } } } @Override public String getName() { return "Speck logging switch"; } }