ModelLogger.java :  » Math » migen » uk » ac » lkl » migen » system » expresser » Java Open Source

Java Open Source » Math » migen 
migen » uk » ac » lkl » migen » system » expresser » ModelLogger.java
package uk.ac.lkl.migen.system.expresser;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.*;

import uk.ac.lkl.common.util.ID;
import uk.ac.lkl.common.util.event.*;
import uk.ac.lkl.migen.system.expresser.model.*;
import uk.ac.lkl.migen.system.expresser.model.shape.block.*;
import uk.ac.lkl.migen.system.expresser.model.event.*;
import uk.ac.lkl.migen.system.expresser.ui.uievent.UIEvent;

public class ModelLogger {
    /**
     * The model that this logger is watching.
     * 
     */
    private ExpresserModel model;

    /**
     * The log4j logger for this class. Unless specified otherwise, 
     * it shares the configuration from the main logger (see
     * LoggerConfigurator). 
     */
    private static Logger logger = Logger.getLogger(ModelLogger.class);

    /**
     * A listener on attribute changes. When an attribute changes, 
     * a log line is generated.
     */
    private AttributeChangeListener<BlockShape> attributeChangeListener =
  new AttributeChangeListener<BlockShape>() {
  public void attributesChanged(
    AttributeChangeEvent<BlockShape> e) {
      Collection<Attribute<?>> attributes =
    e.getChangedAttributes();
      for (Attribute<?> attribute : attributes) {
    String logMsg = new String("(" + model.getId() + ") ");
    logMsg += "Attribute '" + attribute.getID() + "'";
    logMsg += " ('" + attribute.getName() + "')";
    logMsg += " has changed from '";
    logMsg += attribute.getPreviousValue();
    logMsg += "' to '";
    logMsg += attribute.getValue();
    logMsg += "'.";
    logger.info(logMsg);
      }
  }
    };
    
    /**
     * A listener for changes to global colour allocation attributes.
     * When an attribute changes it is logged.
     */
    private AttributeChangeListener<BlockShape> globalAllocationExpressionListener = 
  new AttributeChangeListener<BlockShape>() {

  public void attributesChanged(AttributeChangeEvent<BlockShape> e) {
      Collection<Attribute<?>> attributes =
    e.getChangedAttributes();
      for (Attribute<?> attribute : attributes) {
    AttributeHandle<?> handle = attribute.getHandle();
    if (handle instanceof ColorResourceAttributeHandle) {
        ColorResourceAttributeHandle colorResourceAttributeHandle =
      (ColorResourceAttributeHandle) handle;
        String logMsg = new String("(" + model.getId() + ") ");
        logMsg += "Global attribute '" + attribute.getID() + "'";
        logMsg += " ('" + colorResourceAttributeHandle.getColor() + "')";
        logMsg += " has changed from '";
        logMsg += attribute.getPreviousValue();
        logMsg += "' to '";
        logMsg += attribute.getValue();
        logMsg += "'.";
        logger.info(logMsg);
    }  
      }
  }
    };

    /**
     * A listener on addition of objects. When an object is added to, or 
     * removed from, the model, a log line is generated.
     */
    private ObjectListener objectListener = new ObjectListener() {
  public void objectAdded(ObjectEvent e) {
      Object o = e.getObject();
      if (o instanceof ExpressedObject<?>) {
    ExpressedObject<?> shape = (ExpressedObject<?>) o;
    if (shape instanceof PatternShape) {
        logger.info("(" + model.getId() + ") Pattern "
          + shape.getID() + " created. Base shape is " + ((PatternShape) shape).getShape().getId());
    } else if (shape instanceof GroupShape) {
        // this code is never called anymore -- group shapes are never added directly to a model
        List<BlockShape> shapes = ((GroupShape) shape).getShapes();
        logger.info("(" + model.getId() + ") Group " + shape.getID() + " created. " + UIEvent.getIDListAsString(shapes));
    } else {
        logger.info("(" + model.getId() + ") Shape "
          + shape.getID() + " created.");
    }
    for (Attribute<?> attribute : shape.getAttributes()) {
        if (attribute.getValue() instanceof ExpressedObject<?>) {
      ID id =
        ((ExpressedObject<?>) attribute.getValue())
          .getID();
      String logMsg = new String("    Attribute '");
      logMsg += attribute.getID() + "'";
      logMsg += " ('" + attribute.getName() + "')";
      logMsg += "' has ID '" + id + "'.";
      logger.info(logMsg);
        } else {
      String logMsg = new String("    Attribute '");
      logMsg += attribute.getID() + "'";
      logMsg += " ('" + attribute.getName() + "')";
      // logMsg += " with class " + attribute.getClass();
      logMsg += " has value '";
      logMsg += attribute.getValue() + "'.";
      logger.info(logMsg);
        }
    }
    if (shape instanceof GroupShape) {
        int idx = 0;
        GroupShape group = (GroupShape) shape;
        for (Iterator<BlockShape> itr = group.iterator(); itr
          .hasNext();) {
      BlockShape nextShape = itr.next();
      logger.info("    Shape-" + (++idx) + ": "
        + nextShape.getID() + ".");
        }
    }
      } else {
    logger.error("(" + model.getId()
      + ") There is an object that is not expressable.");
      }
  }

  public void objectRemoved(ObjectEvent e) {
      Object o = e.getObject();
      if (o instanceof ExpressedObject<?>) {
    logger.info("(" + model.getId() + ") Object "
      + ((ExpressedObject<?>) o).getID()
      + " removed from the model.");
      } else {
    logger.error("(" + model.getId()
      + ") There is an object that is not expressable.");
      }
  }
    };

    /**
     * A listener on updates of objects. When an object is updated,  
     * a log line is generated.
     */
    private UpdateListener<BlockShape> objectUpdateListener =
  new UpdateListener<BlockShape>() {
  public void objectUpdated(UpdateEvent<BlockShape> e) {
      Object source = e.getSource();
      if (source instanceof ExpressedObject<?>) {
    ExpressedObject<?> shape = (ExpressedObject<?>) source;
    ID objectID = ((ExpressedObject<?>) source).getID();
    // Log if the object is selected or not
    if (shape.isSelected()) {
        logger.info("(" + model.getId() + ") Object "
          + objectID + " has beeen selected.");
    } else {
        logger.info("(" + model.getId() + ") Object "
          + objectID + " has beeen deselected.");
    }
      }
  }
    };

    /**
     * A listener on (de/)selection of objects. When an object is 
     * (de/)selected, a log line is generated.
     */
    private UpdateListener<BlockShape> objectSelectionUpdateListener =
  new UpdateListener<BlockShape>() {
  public void objectUpdated(UpdateEvent<BlockShape> e) {
      // Is this event only started by the model?
      logger.debug("(" + model.getId() + "' Updated class: "
        + e.getSource().getClass());
  }
    };    

    /**
     * Creates a new ModelLogger.
     * 
     * @param model The model that this logger is watching.
     */
    public ModelLogger(ExpresserModel model) {
  this.model = model;
  addListeners();
    }

    /**
     * Creates a new ModelLogger for some particular name.
     * 
     * @param model
     *            The model that this logger is watching.
     * @param name
     *            The name (of a student, usually)
     */
    public ModelLogger(ExpresserModel model, String name) {
  // What is the name for? [asked by KK]
  this.model = model;
  addListeners();
    }

    /**
     * Add all relevant listeners to the model.
     * 
     */
    private void addListeners() {
  model.addAttributeChangeListener(attributeChangeListener);
  model.addObjectListener(objectListener);
  model.addObjectUpdateListener(objectUpdateListener);
  model.addSelectionUpdateListener(objectSelectionUpdateListener);
  model.addGlobalAllocationExpressionListener(globalAllocationExpressionListener);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.