001 /* 002 * Created on 28.03.2006 003 * 004 */ 005 package org.jdesktop.swingx.action; 006 007 import java.awt.event.ItemEvent; 008 009 /** 010 * Convenience implementation to simplify {@link org.jdesktop.swingx.JXHyperlink} configuration and 011 * provide minimal api as needed by a {@link org.jdesktop.swingx.LinkRenderer}. <p> 012 * 013 * PENDING: rename to AbstractLinkAction 014 * 015 * @author Jeanette Winzenburg 016 */ 017 public abstract class LinkAction<T> extends AbstractActionExt { 018 019 /** 020 * Key for the visited property value. 021 */ 022 public static final String VISITED_KEY = "visited"; 023 /** 024 * the object the actionPerformed can act on. 025 */ 026 protected T target; 027 028 029 /** 030 * Instantiates a LinkAction with null target. 031 * 032 */ 033 public LinkAction () { 034 this(null); } 035 036 /** 037 * Instantiates a LinkAction with a target of type targetClass. 038 * The visited property is initialized as defined by 039 * {@link LinkAction#installTarget()} 040 * 041 * @param target the target this action should act on. 042 */ 043 public LinkAction(T target) { 044 setTarget(target); 045 } 046 047 /** 048 * Set the visited property. 049 * 050 * @param visited 051 */ 052 public void setVisited(boolean visited) { 053 putValue(VISITED_KEY, visited); 054 } 055 056 /** 057 * 058 * @return visited state 059 */ 060 public boolean isVisited() { 061 Boolean visited = (Boolean) getValue(VISITED_KEY); 062 return Boolean.TRUE.equals(visited) ? true : false; 063 } 064 065 066 public T getTarget() { 067 return target; 068 } 069 070 /** 071 * PRE: isTargetable(target) 072 * @param target 073 */ 074 public void setTarget(T target) { 075 T oldTarget = getTarget(); 076 uninstallTarget(); 077 this.target = target; 078 installTarget(); 079 firePropertyChange("target", oldTarget, getTarget()); 080 081 } 082 083 /** 084 * hook for subclasses to update internal state after 085 * a new target has been set. <p> 086 * 087 * Subclasses are free to decide the details. 088 * Here: 089 * <ul> 090 * <li> the text property is set to target.toString or empty String if 091 * the target is null 092 * <li> visited is set to false. 093 * </ul> 094 */ 095 protected void installTarget() { 096 setName(target != null ? target.toString() : "" ); 097 setVisited(false); 098 } 099 100 /** 101 * hook for subclasses to cleanup before the old target 102 * is overwritten. <p> 103 * 104 * Subclasses are free to decide the details. 105 * Here: does nothing. 106 */ 107 protected void uninstallTarget() { 108 109 } 110 111 public void itemStateChanged(ItemEvent e) { 112 // do nothing 113 } 114 115 /** 116 * Set the state property. 117 * Overridden to to nothing. 118 * PENDING: really? 119 * @param state if true then this action will fire ItemEvents 120 */ 121 @Override 122 public void setStateAction(boolean state) { 123 } 124 125 126 127 }