001 /* 002 * $Id: TargetableAction.java 1846 2007-03-16 21:38:13Z rbair $ 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 */ 021 022 package org.jdesktop.swingx.action; 023 024 import javax.swing.*; 025 import java.awt.event.ActionEvent; 026 import java.awt.event.ItemEvent; 027 028 /** 029 * A class that represents a dynamically targetable action. The invocation of this 030 * action will be dispatched to the <code>TargetManager</code> instance. 031 * <p> 032 * You would create instances of this class to let the TargetManager handle the 033 * action invocations from the ui components constructed with this action. 034 * The TargetManager could be configured depending on application state to 035 * handle these actions. 036 * 037 * @see TargetManager 038 * @author Mark Davidson 039 */ 040 public class TargetableAction extends AbstractActionExt { 041 042 private TargetManager targetManager; 043 044 public TargetableAction() { 045 this("action"); 046 } 047 048 public TargetableAction(String name) { 049 super(name); 050 } 051 052 /** 053 * @param name display name of the action 054 * @param command the value of the action command key 055 */ 056 public TargetableAction(String name, String command) { 057 super(name, command); 058 } 059 060 /** 061 * @param name display name of the action 062 * @param command the value of the action command key 063 * @param icon icon to display 064 */ 065 public TargetableAction(String name, String command, Icon icon) { 066 super(name, command, icon); 067 } 068 069 public TargetableAction(String name, Icon icon) { 070 super(name, icon); 071 } 072 073 /** 074 * Set target manager which will handle this command. This action 075 * may be reset to use the singleton instance if set to null. 076 * 077 * @param tm the target manager instance to dispatch the actions 078 */ 079 public void setTargetManager(TargetManager tm) { 080 this.targetManager = tm; 081 } 082 083 /** 084 * Returns the target manager instance which will be used for action 085 * dispatch. If the target manager has not previously been set then the 086 * singleton instance will be returned. 087 * 088 * @return a non null target manager 089 */ 090 public TargetManager getTargetManager() { 091 if (targetManager == null) { 092 targetManager = TargetManager.getInstance(); 093 } 094 return targetManager; 095 } 096 097 // Callbacks... 098 099 /** 100 * Callback for command actions. This event will be redispatched to 101 * the target manager along with the value of the Action.ACTION_COMMAND_KEY 102 * 103 * @param evt event which will be forwarded to the TargetManager 104 * @see TargetManager 105 */ 106 public void actionPerformed(ActionEvent evt) { 107 if (!isStateAction()) { 108 // Do not process this event if it's a toggle action. 109 getTargetManager().doCommand(getActionCommand(), evt); 110 } 111 } 112 113 /** 114 * Callback for toggle actions. This event will be redispatched to 115 * the target manager along with value of the the Action.ACTION_COMMAND_KEY 116 * 117 * @param evt event which will be forwarded to the TargetManager 118 * @see TargetManager 119 */ 120 public void itemStateChanged(ItemEvent evt) { 121 // Update all objects that share this item 122 boolean newValue; 123 boolean oldValue = isSelected(); 124 125 newValue = evt.getStateChange() == ItemEvent.SELECTED; 126 127 if (oldValue != newValue) { 128 setSelected(newValue); 129 130 getTargetManager().doCommand(getActionCommand(), evt); 131 } 132 } 133 134 public String toString() { 135 return super.toString(); 136 } 137 }