001 /* 002 * $Id: ActionFactory.java 585 2005-10-26 14:31:21Z 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 */ 021 022 package org.jdesktop.swingx.action; 023 024 import javax.swing.AbstractAction; 025 import javax.swing.Action; 026 import javax.swing.Icon; 027 import javax.swing.KeyStroke; 028 029 /** 030 * A collection of static methods to make it easier to construct 031 * Actions. Not sure how usefull they are in reality but it saves a 032 * lot of typing. 033 * 034 * @author Mark Davidson 035 */ 036 public class ActionFactory { 037 038 /** 039 * Factory Methods for creating BoundActions 040 */ 041 public static BoundAction createBoundAction(String id, String name, 042 String mnemonic) { 043 return createBoundAction(id, name, mnemonic, false); 044 } 045 046 public static BoundAction createBoundAction(String id, String name, 047 String mnemonic, boolean toggle) { 048 return createBoundAction(id, name, mnemonic, toggle, null); 049 } 050 051 052 public static BoundAction createBoundAction(String id, String name, 053 String mnemonic, boolean toggle, 054 String group) { 055 return (BoundAction)configureAction(new BoundAction(name, id), 056 mnemonic, toggle, group); 057 } 058 059 /** 060 * Factory Methods for creating <code>CompositeAction</code> 061 * @see CompositeAction 062 */ 063 public static CompositeAction createCompositeAction(String id, String name, 064 String mnemonic) { 065 return createCompositeAction(id, name, mnemonic, false); 066 } 067 068 public static CompositeAction createCompositeAction(String id, String name, 069 String mnemonic, boolean toggle) { 070 return createCompositeAction(id, name, mnemonic, toggle, null); 071 } 072 073 public static CompositeAction createCompositeAction(String id, String name, 074 String mnemonic, boolean toggle, 075 String group) { 076 return (CompositeAction)configureAction(new CompositeAction(name, id), 077 mnemonic, toggle, group); 078 } 079 080 081 public static ServerAction createServerAction(String id, String name, 082 String mnemonic) { 083 ServerAction action = new ServerAction(name, id); 084 if (mnemonic != null && !mnemonic.equals("")) { 085 action.putValue(Action.MNEMONIC_KEY, new Integer(mnemonic.charAt(0))); 086 } 087 return action; 088 } 089 090 091 /** 092 * These methods are usefull for creating targetable actions 093 */ 094 public static TargetableAction createTargetableAction(String id, String name) { 095 return createTargetableAction(id, name, null); 096 } 097 098 public static TargetableAction createTargetableAction(String id, String name, 099 String mnemonic) { 100 return createTargetableAction(id, name, mnemonic, false); 101 } 102 103 public static TargetableAction createTargetableAction(String id, String name, 104 String mnemonic, boolean toggle) { 105 return createTargetableAction(id, name, mnemonic, toggle, null); 106 } 107 108 public static TargetableAction createTargetableAction(String id, String name, 109 String mnemonic, boolean toggle, 110 String group) { 111 return (TargetableAction)configureAction(new TargetableAction(name, id), 112 mnemonic, toggle, group); 113 } 114 115 private static Action configureAction(AbstractActionExt action, 116 String mnemonic, boolean toggle, 117 String group) { 118 action.setMnemonic(mnemonic); 119 String description = action.getName() + " action with comand " + action.getActionCommand(); 120 action.setShortDescription(description); 121 action.setLongDescription(description); 122 123 if (toggle) { 124 action.setStateAction(); 125 } 126 if (group != null) { 127 action.setGroup(group); 128 } 129 return action; 130 } 131 132 /** 133 * Add additional attributes to the action. If any of these attributes 134 * are null then they will still be set on the action. Many of these 135 * attributes map to the set methods on <code>AbstractActionExt</code> 136 * 137 * @see AbstractActionExt 138 * @param action the action which will all the attributes will be applied 139 */ 140 public static void decorateAction(AbstractAction action, 141 String shortDesc, String longDesc, 142 Icon smallIcon, Icon largeIcon, 143 KeyStroke accel) { 144 if (action instanceof AbstractActionExt) { 145 AbstractActionExt a = (AbstractActionExt)action; 146 a.setShortDescription(shortDesc); 147 a.setLongDescription(longDesc); 148 a.setSmallIcon(smallIcon); 149 a.setLargeIcon(largeIcon); 150 a.setAccelerator(accel); 151 } 152 else { 153 action.putValue(Action.SHORT_DESCRIPTION, shortDesc); 154 action.putValue(Action.LONG_DESCRIPTION, longDesc); 155 action.putValue(Action.SMALL_ICON, smallIcon); 156 action.putValue(AbstractActionExt.LARGE_ICON, largeIcon); 157 action.putValue(Action.ACCELERATOR_KEY, accel); 158 } 159 } 160 }