1 19 20 package com.sshtools.ui.swing; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.net.URL ; 25 26 import javax.swing.AbstractAction ; 27 import javax.swing.Action ; 28 import javax.swing.Icon ; 29 import javax.swing.ImageIcon ; 30 import javax.swing.event.EventListenerList ; 31 32 33 46 public abstract class AppAction extends AbstractAction { 47 48 public final static String IMAGE_DIR = "/com/sshtools/sshterm/"; 49 50 51 public final static String ON_TOOLBAR = "OnToolBar"; 52 53 54 public final static String TOOLBAR_GROUP = "ToolBarGroup"; 55 56 57 public final static String TOOLBAR_WEIGHT = "ToolBarWeight"; 58 59 60 public final static String ON_MENUBAR = "OnMenuBar"; 61 62 63 public final static String MENU_NAME = "MenuName"; 64 65 66 public final static String MENU_ITEM_GROUP = "MenuItemGroup"; 67 68 69 public final static String MENU_ITEM_WEIGHT = "MmenuItemWeight"; 70 71 72 public final static String TEXT_ON_TOOLBAR = "HideToolbarText"; 73 74 75 public final static String IS_TOGGLE_BUTTON = "IsToggleButton"; 76 77 78 public final static String IS_SELECTED = "IsSelected"; 79 80 81 public final static String LARGE_ICON = "LargeIcon"; 82 83 84 public final static String ON_CONTEXT_MENU = "OnContextMenu"; 85 86 87 public final static String CONTEXT_MENU_GROUP = "ContextMenuGroup"; 88 89 90 public final static String CONTEXT_MENU_WEIGHT = "ContextMenuWeight"; 91 92 93 public final static String MEDIUM_ICON = "ToolIcon"; 94 95 96 97 public final static String CATEGORY = "Category"; 98 99 private EventListenerList listeners; 101 102 104 public AppAction() { 105 this(""); 106 } 107 108 111 public AppAction(String name) { 112 this(name, null); 113 } 114 115 118 public AppAction(String name, Icon smallIcon) { 119 if(name != null) { 120 putValue(AppAction.NAME, name); 121 } 122 if(smallIcon != null) { 123 putValue(AppAction.SMALL_ICON, smallIcon); 124 } 125 } 126 127 130 public String getActionCommand() { 131 return (String ) getValue(Action.ACTION_COMMAND_KEY); 132 } 133 134 137 public String getShortDescription() { 138 return (String ) getValue(Action.SHORT_DESCRIPTION); 139 } 140 141 144 public String getLongDescription() { 145 return (String ) getValue(Action.LONG_DESCRIPTION); 146 } 147 148 151 public String getName() { 152 return (String ) getValue(Action.NAME); 153 } 154 155 158 public ResourceIcon getSmallIcon() { 159 return (ResourceIcon) getValue(Action.SMALL_ICON); 160 } 161 162 166 public void actionPerformed(ActionEvent evt) { 167 if (listeners != null) { 168 Object [] listenerList = listeners.getListenerList(); 169 ActionEvent e = new ActionEvent (evt.getSource(), evt.getID(), 172 (String ) getValue(Action.ACTION_COMMAND_KEY)); 173 for (int i = 0; i <= (listenerList.length - 2); i += 2) { 174 ((ActionListener ) listenerList[i + 1]).actionPerformed(e); 175 } 176 } 177 } 178 179 183 public void addActionListener(ActionListener l) { 184 if (listeners == null) { 185 listeners = new EventListenerList (); 186 } 187 listeners.add(ActionListener .class, l); 188 } 189 190 194 public void removeActionListener(ActionListener l) { 195 if (listeners == null) { return; } 196 listeners.remove(ActionListener .class, l); 197 } 198 199 206 public ImageIcon getIcon(String name) { 207 String imagePath = name.startsWith("/") ? name : (IMAGE_DIR + name); 208 URL url = this.getClass().getResource(imagePath); 209 if (url != null) { return new ImageIcon (url); } 210 return null; 211 } 212 213 public boolean isSelected() { 214 return Boolean.TRUE.equals(getValue(IS_SELECTED)); 215 } 216 217 218 public void setSelected(boolean selected) { 219 putValue(IS_SELECTED, Boolean.valueOf(selected)); 220 } 221 } 222 | Popular Tags |