Package DisplayProject.swingdisplayer

Source Code of DisplayProject.swingdisplayer.PropertiesMonitor$HistoryEntry

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
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$HistoryEntry

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.