001/*
002 * $Id: LinkModelAction.java 3927 2011-02-22 16:34:11Z kleopatra $
003 *
004 * Copyright 2004 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.ActionEvent;
024import java.awt.event.ActionListener;
025import java.beans.PropertyChangeEvent;
026import java.beans.PropertyChangeListener;
027
028import javax.swing.Action;
029
030
031/**
032 * Specialized LinkAction for a target of type {@link LinkModel}. 
033 * <p>
034 * 
035 * This action delegates actionPerformed to vistingDelegate.
036 * 
037 * PENDING: move to swingx package?
038 * 
039 * @author Jeanette Winzenburg
040 */
041public class LinkModelAction<T extends LinkModel> extends AbstractHyperlinkAction<T> {
042    
043    private ActionListener delegate;
044    public static final String VISIT_ACTION = "visit";
045    private PropertyChangeListener linkListener;
046    
047
048    public LinkModelAction() {
049        this((T) null);
050    }
051
052    public LinkModelAction(ActionListener visitingDelegate) {
053        this(null, visitingDelegate);
054    }
055    
056    public LinkModelAction(T target) {
057        this(target, null);
058    }
059    
060    public LinkModelAction(T target, ActionListener visitingDelegate) {
061        super(target);
062        setVisitingDelegate(visitingDelegate);
063    }
064    
065    /**
066     * The delegate to invoke on actionPerformed.
067     * <p>
068     * The delegates actionPerformed is invoked with an ActionEvent
069     * having the target as source. Delegates are expected to
070     * cope gracefully with the T. 
071     * <p>
072     * 
073     * PENDING: JW - How to formalize? 
074     * 
075     * @param delegate the action invoked on the target.
076     */
077    public void setVisitingDelegate(ActionListener delegate) {
078        this.delegate = delegate;
079    }
080    
081    /**
082     * This action delegates to the visitingDelegate if both
083     * delegate and target are != null, does nothing otherwise.
084     * The actionEvent carries the target as source.
085     * 
086     * PENDING: pass through a null target? - most probably!
087     * 
088     * 
089     * 
090     */
091    @Override
092    public void actionPerformed(ActionEvent e) {
093        if ((delegate != null) && (getTarget() != null)) {
094            delegate.actionPerformed(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, VISIT_ACTION));
095        }
096        
097    }
098
099    /**
100     * installs a propertyChangeListener on the target and
101     * updates the visual properties from the target.
102     */
103    @Override
104    protected void installTarget() {
105        if (getTarget() != null) {
106            getTarget().addPropertyChangeListener(getTargetListener());
107        }
108        updateFromTarget();
109    }
110
111    /**
112     * removes the propertyChangeListener. <p>
113     * 
114     * Implementation NOTE: this does not clean-up internal state! There is
115     * no need to because updateFromTarget handles both null and not-null
116     * targets. Hmm...
117     * 
118     */
119    @Override
120    protected void uninstallTarget() {
121        if (getTarget() == null) return;
122       getTarget().removePropertyChangeListener(getTargetListener());
123    }
124
125    protected void updateFromTarget() {
126        if (getTarget() != null) {
127            putValue(Action.NAME, getTarget().getText());
128            putValue(Action.SHORT_DESCRIPTION, getTarget().getURL().toString());
129            putValue(VISITED_KEY, new Boolean(getTarget().getVisited()));
130        } else {
131            Object[] keys = getKeys();
132            if (keys == null) return;
133            for (int i = 0; i < keys.length; i++) {
134               putValue(keys[i].toString(), null); 
135            }
136        }
137    }
138
139    private PropertyChangeListener getTargetListener() {
140        if (linkListener == null) {
141         linkListener = new PropertyChangeListener() {
142
143            @Override
144            public void propertyChange(PropertyChangeEvent evt) {
145                updateFromTarget();
146            }
147            
148        };
149        }
150        return linkListener;
151    }
152
153    
154}