001/*
002 * $Id: AbstractHyperlinkAction.java 3484 2009-09-03 15:55:34Z 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 */
021package org.jdesktop.swingx.hyperlink;
022
023import java.awt.event.ItemEvent;
024
025import 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 * @author Jeanette Winzenburg
032 */
033public abstract class AbstractHyperlinkAction<T> extends AbstractActionExt {
034
035    /**
036     * Key for the visited property value.
037     */
038    public static final String VISITED_KEY = "visited";
039    /**
040     * the object the actionPerformed can act on.
041     */
042    protected T target;
043
044    
045    /**
046     * Instantiates a LinkAction with null target. 
047     * 
048     */
049    public AbstractHyperlinkAction () {
050        this(null);    }
051    
052    /**
053     * Instantiates a LinkAction with a target of type targetClass. 
054     * The visited property is initialized as defined by 
055     * {@link AbstractHyperlinkAction#installTarget()}
056     * 
057     * @param target the target this action should act on.
058     */
059    public AbstractHyperlinkAction(T target) {
060       setTarget(target);
061    }
062
063    /**
064     * Set the visited property.
065     * 
066     * @param visited
067     */
068    public void setVisited(boolean visited) {
069        putValue(VISITED_KEY, visited);
070    }
071
072    /**
073     * 
074     * @return visited state
075     */
076    public boolean isVisited() {
077        Boolean visited = (Boolean) getValue(VISITED_KEY);
078        return Boolean.TRUE.equals(visited);
079    }
080
081    
082    public T getTarget() {
083        return target;
084    }
085
086    /**
087     * PRE: isTargetable(target)
088     * @param target
089     */
090    public void setTarget(T target) {
091        T oldTarget = getTarget();
092        uninstallTarget();
093        this.target = target;
094        installTarget();
095        firePropertyChange("target", oldTarget, getTarget());
096        
097    }
098
099    /**
100     * hook for subclasses to update internal state after
101     * a new target has been set. <p>
102     * 
103     * Subclasses are free to decide the details. 
104     * Here: 
105     * <ul>
106     * <li> the text property is set to target.toString or empty String if
107     * the target is null
108     * <li> visited is set to false.
109     * </ul>
110     */
111    protected void installTarget() {
112        setName(target != null ? target.toString() : "" );
113        setVisited(false);
114    }
115
116    /**
117     * hook for subclasses to cleanup before the old target
118     * is overwritten. <p>
119     * 
120     * Subclasses are free to decide the details. 
121     * Here: does nothing.
122     */
123    protected void uninstallTarget() {
124        
125    }
126    
127    @Override
128    public void itemStateChanged(ItemEvent e) {
129        // do nothing
130    }
131
132    /**
133     * Set the state property.
134     * Overridden to to nothing.
135     * PENDING: really?
136     * @param state if true then this action will fire ItemEvents
137     */
138    @Override
139    public void setStateAction(boolean state) {
140    }
141
142    
143
144}