package samples.javaclient.analyzer;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * The purpose of this listener is to help track when a worksheet is dirty.
 * It listens to any changes in format to a worksheet when performed through
 * the Analyzer toolbar.
 *
 * On a related note, the Worksheet class itself tracks any changes made to the
 * worksheet's query.  In addition, changes made by the customizer are tracked by
 * the Analyzer main class
 */
public class ToolbarFormatListener implements PropertyChangeListener  {
  private Analyzer m_analyzer;
  private String validProperties =  "FontName" +
                                    "FontSize" +
                                    "FontBold" +
                                    "FontItalic" +
                                    "FontUnderline" +
                                    "FontAlignment" +
                                    "FontColor" +
                                    "FillColor" +
                                    "GraphType" +
                                    "LegendVisible" +
                                    "GridLineVisible" +
                                    "DecimalDigit" +
                                    "NumberType";

  /**
   * Create a toolbar format listener.
   * @param analyzer A reference to the main Analyzer application instance
   */
  public ToolbarFormatListener(Analyzer analyzer) {
    m_analyzer = analyzer;
  }


  /**
   * A toolbar format change was made to the worksheet.
   * Set the active worksheet to dirty.
   */
  public void propertyChange(PropertyChangeEvent evt) {
    // Filter the list of properties.  Only certain properties should cause
    // the worksheet to be marked as dirty
    if (m_analyzer.getActiveWorksheet() != null && isValidEvent(evt))
      m_analyzer.getActiveWorksheet().setDirty(true);

  }

  /**
   * Looks at the PropertyChangedEvent and determines if this event should cause
   * the worksheet to be marked as dirty.  A list of valid property changes
   * is listed in validProperties member variable
   */
  private boolean isValidEvent(PropertyChangeEvent evt) {
    if (evt.getPropertyName() == null)
      return false;
    if (validProperties.indexOf(evt.getPropertyName()) >= 0)
      return true;
    else 
      return false;
  }
}