001/* 002 * $Id: ConsoleAction.java,v 1.14 2011/03/24 18:13:11 davep Exp $ 003 * 004 * This file is part of McIDAS-V 005 * 006 * Copyright 2007-2011 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 031package edu.wisc.ssec.mcidasv.jython; 032 033import java.awt.event.ActionEvent; 034import java.awt.event.KeyEvent; 035import java.util.Map; 036import java.util.concurrent.ConcurrentHashMap; 037 038import javax.swing.Action; 039import javax.swing.JTextPane; 040import javax.swing.text.DefaultEditorKit; 041import javax.swing.text.Keymap; 042import javax.swing.text.TextAction; 043 044import edu.wisc.ssec.mcidasv.jython.Console.Actions; 045 046// TODO(jon): there has to be a less repetitive way... 047public 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 089class EnterAction extends ConsoleAction { 090 private static final long serialVersionUID = 6866731355386212866L; 091 092 public EnterAction(final Console console, final Actions type) { 093 super(console, type); 094 } 095 096 public void actionPerformed(final ActionEvent e) { 097 getSourceConsole(e).handleEnter(); 098 } 099} 100 101class DeleteAction extends ConsoleAction { 102 private static final long serialVersionUID = 4084595316508126660L; 103 104 public DeleteAction(final Console console, final Actions type) { 105 super(console, type); 106 } 107 108 public void actionPerformed(final ActionEvent e) { 109 getSourceConsole(e).handleDelete(); 110 } 111} 112 113class HomeAction extends ConsoleAction { 114 private static final long serialVersionUID = 8416339385843554054L; 115 116 public HomeAction(final Console console, final Actions type) { 117 super(console, type); 118 } 119 120 public void actionPerformed(final ActionEvent e) { 121 getSourceConsole(e).handleHome(); 122 } 123} 124 125class EndAction extends ConsoleAction { 126 private static final long serialVersionUID = 5990936637916440399L; 127 128 public EndAction(final Console console, final Actions type) { 129 super(console, type); 130 } 131 132 public void actionPerformed(final ActionEvent e) { 133 getSourceConsole(e).handleEnd(); 134 } 135} 136 137class TabAction extends ConsoleAction { 138 private static final long serialVersionUID = 2303773619117479801L; 139 140 public TabAction(final Console console, final Actions type) { 141 super(console, type); 142 } 143 public void actionPerformed(final ActionEvent e) { 144 getSourceConsole(e).handleTab(); 145 } 146} 147 148class UpAction extends ConsoleAction { 149 private static final long serialVersionUID = 6710943250074726107L; 150 private Action defaultAction; 151 152 public UpAction(final Console console, final Actions type) { 153 super(console, type); 154 defaultAction = console.getTextPane().getActionMap().get(DefaultEditorKit.upAction); 155 } 156 157 public void actionPerformed(final ActionEvent e) { 158 defaultAction.actionPerformed(e); 159 } 160} 161 162class DownAction extends ConsoleAction { 163 private static final long serialVersionUID = 5700659549452276829L; 164 private Action defaultAction; 165 166 public DownAction(final Console console, final Actions type) { 167 super(console, type); 168 defaultAction = console.getTextPane().getActionMap().get(DefaultEditorKit.downAction); 169 } 170 171 public void actionPerformed(final ActionEvent e) { 172 defaultAction.actionPerformed(e); 173 } 174}