001 /* 002 * $Id: ConsoleAction.java,v 1.16 2012/02/19 17:35:47 davep Exp $ 003 * 004 * This file is part of McIDAS-V 005 * 006 * Copyright 2007-2012 007 * Space Science and Engineering Center (SSEC) 008 * University of Wisconsin - Madison 009 * 1225 W. Dayton Street, Madison, WI 53706, USA 010 * https://www.ssec.wisc.edu/mcidas 011 * 012 * All Rights Reserved 013 * 014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and 015 * some McIDAS-V source code is based on IDV and VisAD source code. 016 * 017 * McIDAS-V is free software; you can redistribute it and/or modify 018 * it under the terms of the GNU Lesser Public License as published by 019 * the Free Software Foundation; either version 3 of the License, or 020 * (at your option) any later version. 021 * 022 * McIDAS-V is distributed in the hope that it will be useful, 023 * but WITHOUT ANY WARRANTY; without even the implied warranty of 024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 025 * GNU Lesser Public License for more details. 026 * 027 * You should have received a copy of the GNU Lesser Public License 028 * along with this program. If not, see http://www.gnu.org/licenses. 029 */ 030 031 package edu.wisc.ssec.mcidasv.jython; 032 033 import java.awt.event.ActionEvent; 034 import java.awt.event.KeyEvent; 035 import java.util.Map; 036 import java.util.concurrent.ConcurrentHashMap; 037 038 import javax.swing.Action; 039 import javax.swing.JTextPane; 040 import javax.swing.text.DefaultEditorKit; 041 import javax.swing.text.Keymap; 042 import javax.swing.text.TextAction; 043 044 import edu.wisc.ssec.mcidasv.jython.Console.Actions; 045 046 // TODO(jon): there has to be a less repetitive way... 047 public abstract class ConsoleAction extends TextAction { 048 protected static final Map<JTextPane, Console> mapping = new ConcurrentHashMap<JTextPane, Console>(); 049 protected Console console; 050 051 protected ConsoleAction(final Console console, final Actions type) { 052 super(type.getId()); 053 this.console = console; 054 055 JTextPane textPane = console.getTextPane(); 056 Keymap keyMap = textPane.getKeymap(); 057 keyMap.addActionForKeyStroke(type.getKeyStroke(), this); 058 mapping.put(textPane, console); 059 } 060 061 /** 062 * Attempts to return the console where the {@code ActionEvent} originated. 063 * 064 * <p>Note: {@code TextAction}s are <i>weird</i>. The only 065 * somewhat-accurate way to determine where the action occurred is to check 066 * {@link ActionEvent#getSource()}. Since that will be a {@code JTextPane}, 067 * you have to keep an updated mapping of {@code JTextPane}s to {@code Console}s. 068 * 069 * @param e The action whose source {@code Console} you want. 070 * 071 * @return Either the actual source {@code Console}, or the {@code Console} 072 * provided to the constructor. 073 * 074 * @see TextAction 075 */ 076 protected Console getSourceConsole(final ActionEvent e) { 077 if (console.getTextPane() != e.getSource()) { 078 Console other = mapping.get((JTextPane)e.getSource()); 079 if (other != null) { 080 return other; 081 } 082 } 083 return console; 084 } 085 086 public abstract void actionPerformed(final ActionEvent e); 087 } 088 089 class PasteAction extends ConsoleAction { 090 public PasteAction(final Console console, final Actions type) { 091 super(console, type); 092 } 093 094 public void actionPerformed(final ActionEvent e) { 095 getSourceConsole(e).handlePaste(); 096 } 097 } 098 099 class EnterAction extends ConsoleAction { 100 private static final long serialVersionUID = 6866731355386212866L; 101 102 public EnterAction(final Console console, final Actions type) { 103 super(console, type); 104 } 105 106 public void actionPerformed(final ActionEvent e) { 107 getSourceConsole(e).handleEnter(); 108 } 109 } 110 111 class DeleteAction extends ConsoleAction { 112 private static final long serialVersionUID = 4084595316508126660L; 113 114 public DeleteAction(final Console console, final Actions type) { 115 super(console, type); 116 } 117 118 public void actionPerformed(final ActionEvent e) { 119 getSourceConsole(e).handleDelete(); 120 } 121 } 122 123 class HomeAction extends ConsoleAction { 124 private static final long serialVersionUID = 8416339385843554054L; 125 126 public HomeAction(final Console console, final Actions type) { 127 super(console, type); 128 } 129 130 public void actionPerformed(final ActionEvent e) { 131 getSourceConsole(e).handleHome(); 132 } 133 } 134 135 class EndAction extends ConsoleAction { 136 private static final long serialVersionUID = 5990936637916440399L; 137 138 public EndAction(final Console console, final Actions type) { 139 super(console, type); 140 } 141 142 public void actionPerformed(final ActionEvent e) { 143 getSourceConsole(e).handleEnd(); 144 } 145 } 146 147 class TabAction extends ConsoleAction { 148 private static final long serialVersionUID = 2303773619117479801L; 149 150 public TabAction(final Console console, final Actions type) { 151 super(console, type); 152 } 153 public void actionPerformed(final ActionEvent e) { 154 getSourceConsole(e).handleTab(); 155 } 156 } 157 158 class UpAction extends ConsoleAction { 159 private static final long serialVersionUID = 6710943250074726107L; 160 private Action defaultAction; 161 162 public UpAction(final Console console, final Actions type) { 163 super(console, type); 164 defaultAction = console.getTextPane().getActionMap().get(DefaultEditorKit.upAction); 165 } 166 167 public void actionPerformed(final ActionEvent e) { 168 defaultAction.actionPerformed(e); 169 } 170 } 171 172 class DownAction extends ConsoleAction { 173 private static final long serialVersionUID = 5700659549452276829L; 174 private Action defaultAction; 175 176 public DownAction(final Console console, final Actions type) { 177 super(console, type); 178 defaultAction = console.getTextPane().getActionMap().get(DefaultEditorKit.downAction); 179 } 180 181 public void actionPerformed(final ActionEvent e) { 182 defaultAction.actionPerformed(e); 183 } 184 }