001 /* 002 * LAPIS lightweight structured text processing system 003 * 004 * Copyright (C) 1998-2002 Carnegie Mellon University, 005 * Copyright (C) 2003 Massachusetts Institute of Technology. 006 * All rights reserved. 007 * 008 * This library is free software; you can redistribute it 009 * and/or modify it under the terms of the GNU General 010 * Public License as published by the Free Software 011 * Foundation, version 2. 012 * 013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/ 014 */ 015 016 package lapisx.swing; 017 018 import java.awt.event.*; 019 import javax.swing.*; 020 021 public class AcceleratedAction extends AbstractAction { 022 public Action action; 023 024 public static String MNEMONIC = "MNEMONIC"; 025 public static String ACCELERATOR = "ACCELERATOR"; 026 027 public AcceleratedAction (String name) { 028 int ampersand = name.indexOf ('&'); 029 if (ampersand > -1 && ampersand < name.length()) { 030 char mnemonic = Character.toUpperCase (name.charAt (ampersand+1)); 031 putValue (MNEMONIC, new Character (mnemonic)); 032 name = name.substring (0, ampersand) + name.substring (ampersand + 1); 033 } 034 putValue (NAME, name); 035 } 036 037 public AcceleratedAction (String name, String tooltip) { 038 this (name); 039 putValue (SHORT_DESCRIPTION, tooltip); 040 } 041 042 public AcceleratedAction (String name, Icon icon) { 043 this (name); 044 putValue (SMALL_ICON, icon); 045 } 046 047 public AcceleratedAction (String name, Icon icon, String tooltip) { 048 this (name, icon); 049 putValue (SHORT_DESCRIPTION, tooltip); 050 } 051 052 public AcceleratedAction (String name, int keycode, int modifiers) { 053 this (name); 054 putValue (ACCELERATOR, KeyStroke.getKeyStroke (keycode, modifiers)); 055 } 056 057 public AcceleratedAction (String name, int keycode, int modifiers, 058 String tooltip) { 059 this (name, keycode, modifiers); 060 putValue (SHORT_DESCRIPTION, tooltip); 061 } 062 063 public AcceleratedAction (String name, Icon icon, 064 int keycode, int modifiers) { 065 this (name, keycode, modifiers); 066 putValue (SMALL_ICON, icon); 067 } 068 069 070 public AcceleratedAction (String name, Icon icon, 071 int keycode, int modifiers, String tooltip) { 072 this (name, icon, keycode, modifiers); 073 putValue (SHORT_DESCRIPTION, tooltip); 074 } 075 076 public AcceleratedAction (String name, int keycode, int modifiers, 077 Action action) { 078 this (name, keycode, modifiers); 079 this.action = action; 080 } 081 082 public AcceleratedAction (String name, 083 Action action) { 084 this (name); 085 this.action = action; 086 } 087 088 public void actionPerformed (ActionEvent event) { 089 if (action != null) 090 action.actionPerformed (event); 091 } 092 }