001 /* 002 * $Id: AbstractHyperlinkAction.java 3294 2009-03-11 10:50:52Z kleopatra $ 003 * 004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 package org.jdesktop.swingx.hyperlink; 022 023 import java.awt.event.ItemEvent; 024 025 import org.jdesktop.swingx.action.AbstractActionExt; 026 027 /** 028 * Convenience implementation to simplify {@link org.jdesktop.swingx.JXHyperlink} configuration and 029 * provide minimal api. <p> 030 * 031 * PENDING: rename to AbstractLinkAction 032 * 033 * @author Jeanette Winzenburg 034 */ 035 public abstract class AbstractHyperlinkAction<T> extends AbstractActionExt { 036 037 /** 038 * Key for the visited property value. 039 */ 040 public static final String VISITED_KEY = "visited"; 041 /** 042 * the object the actionPerformed can act on. 043 */ 044 protected T target; 045 046 047 /** 048 * Instantiates a LinkAction with null target. 049 * 050 */ 051 public AbstractHyperlinkAction () { 052 this(null); } 053 054 /** 055 * Instantiates a LinkAction with a target of type targetClass. 056 * The visited property is initialized as defined by 057 * {@link AbstractHyperlinkAction#installTarget()} 058 * 059 * @param target the target this action should act on. 060 */ 061 public AbstractHyperlinkAction(T target) { 062 setTarget(target); 063 } 064 065 /** 066 * Set the visited property. 067 * 068 * @param visited 069 */ 070 public void setVisited(boolean visited) { 071 putValue(VISITED_KEY, visited); 072 } 073 074 /** 075 * 076 * @return visited state 077 */ 078 public boolean isVisited() { 079 Boolean visited = (Boolean) getValue(VISITED_KEY); 080 return Boolean.TRUE.equals(visited); 081 } 082 083 084 public T getTarget() { 085 return target; 086 } 087 088 /** 089 * PRE: isTargetable(target) 090 * @param target 091 */ 092 public void setTarget(T target) { 093 T oldTarget = getTarget(); 094 uninstallTarget(); 095 this.target = target; 096 installTarget(); 097 firePropertyChange("target", oldTarget, getTarget()); 098 099 } 100 101 /** 102 * hook for subclasses to update internal state after 103 * a new target has been set. <p> 104 * 105 * Subclasses are free to decide the details. 106 * Here: 107 * <ul> 108 * <li> the text property is set to target.toString or empty String if 109 * the target is null 110 * <li> visited is set to false. 111 * </ul> 112 */ 113 protected void installTarget() { 114 setName(target != null ? target.toString() : "" ); 115 setVisited(false); 116 } 117 118 /** 119 * hook for subclasses to cleanup before the old target 120 * is overwritten. <p> 121 * 122 * Subclasses are free to decide the details. 123 * Here: does nothing. 124 */ 125 protected void uninstallTarget() { 126 127 } 128 129 @Override 130 public void itemStateChanged(ItemEvent e) { 131 // do nothing 132 } 133 134 /** 135 * Set the state property. 136 * Overridden to to nothing. 137 * PENDING: really? 138 * @param state if true then this action will fire ItemEvents 139 */ 140 @Override 141 public void setStateAction(boolean state) { 142 } 143 144 145 146 }