Package DisplayProject.swingdisplayer

Source Code of DisplayProject.swingdisplayer.PropertiesMonitor

package DisplayProject.swingdisplayer;

import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import DisplayProject.actions.ActionMgr;
import DisplayProject.actions.PendingAction;

public class PropertiesMonitor implements PropertyChangeListener, ComponentListener {
  private Map<String, List<HistoryEntry>> history = new HashMap<String, List<HistoryEntry>>();
 
  public class HistoryEntry {
    private Object value;
    private Date date;
    private String traceback;
    public HistoryEntry(Object value) {
      this.value = value;
      this.date = new Date();
      StackTraceElement[] trace = Thread.currentThread().getStackTrace();
      StringBuilder builder = new StringBuilder();
      for (int i = 5; i < trace.length; i++) {
        builder.append(trace[i].toString()).append("\n");
      }
      traceback = builder.toString();
     
      // Replace the trace with the real traceback if we can get it.
      PendingAction action = ActionMgr.getCurrentAction();
      if (action != null) {
        traceback = action.getTraceback();
      }
    }
   
    public String getValue() {
      return String.valueOf(value);
    }
   
    public Date getDate() {
      return new Date(date.getTime());
    }
   
    public String getStackTrace() {
      return traceback;
    }
  }
 
  public PropertiesMonitor(Component component) {
    component.addPropertyChangeListener(this);
    component.addComponentListener(this);
  }

  private void addHistoryEntry(String property, HistoryEntry entry) {
    List<HistoryEntry> entries = history.get(property);
    if (entries == null) {
      entries = new ArrayList<HistoryEntry>();
      history.put(property, entries);
    }
    entries.add(entry);
  }
 
  public void propertyChange(PropertyChangeEvent evt) {
    HistoryEntry entry = new HistoryEntry(evt.getNewValue());
    addHistoryEntry(evt.getPropertyName(), entry);
  }
 
  public List<HistoryEntry> getHistoryList(String property) {
    List<HistoryEntry> result = history.get(property);
    if (result == null) {
      return new ArrayList<HistoryEntry>();
    }
    else {
      return result;
    }
  }
 
  public static PropertiesMonitor monitor(Component component) {
    PropertiesMonitor result = getMonitor(component);
    if (result != null) {
      return result;
    }
    return new PropertiesMonitor(component);
  }
 
  public static PropertiesMonitor getMonitor(Component component) {
    PropertyChangeListener[] list = component.getPropertyChangeListeners();
    for (int i = 0; i < list.length; i++) {
      if (list[i] instanceof PropertiesMonitor) {
        // We're already monitoring, don't monitor it twice
        return (PropertiesMonitor)list[i];
      }
    }
    return null;
  }

  public void componentHidden(ComponentEvent e) {
    HistoryEntry entry = new HistoryEntry(Boolean.valueOf(false));
    addHistoryEntry("visible", entry);
  }

  public void componentMoved(ComponentEvent e) {
    if (e.getComponent() != null) {
      HistoryEntry entry = new HistoryEntry(Integer.valueOf(e.getComponent().getX()));
      addHistoryEntry("x", entry);
      entry = new HistoryEntry(Integer.valueOf(e.getComponent().getY()));
      addHistoryEntry("y", entry);
    }
  }

  public void componentResized(ComponentEvent e) {
    if (e.getComponent() != null) {
      HistoryEntry entry = new HistoryEntry(Integer.valueOf(e.getComponent().getWidth()));
      addHistoryEntry("width", entry);
      entry = new HistoryEntry(Integer.valueOf(e.getComponent().getHeight()));
      addHistoryEntry("height", entry);
    }
  }

  public void componentShown(ComponentEvent e) {
    HistoryEntry entry = new HistoryEntry(Boolean.valueOf(false));
    addHistoryEntry("true", entry);
  }
}
TOP

Related Classes of DisplayProject.swingdisplayer.PropertiesMonitor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.