PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/InfoViewer/infoviewer/actions/InfoViewerAction.java

#
Java | 305 lines | 197 code | 36 blank | 72 comment | 48 complexity | a2c90f1e1b282d928363649da2a7a0bb MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * InfoViewerAction.java - jEdit action listener
  3. * Copyright (C) 2000-2002 Dirk Moebius
  4. * Contains portions of EditAction.java Copyright (C) 1998, 1999 by
  5. * Slava Pestov
  6. *
  7. * :tabSize=4:indentSize=4:noTabs=true:maxLineLen=0:
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package infoviewer.actions;
  24. import infoviewer.InfoViewer;
  25. import java.awt.Component;
  26. import java.awt.Frame;
  27. import java.awt.event.InputEvent;
  28. import java.awt.event.KeyEvent;
  29. import java.beans.PropertyChangeListener;
  30. import java.util.EventObject;
  31. import javax.swing.AbstractAction;
  32. import javax.swing.Action;
  33. import javax.swing.ButtonModel;
  34. import javax.swing.Icon;
  35. import javax.swing.JButton;
  36. import javax.swing.JCheckBoxMenuItem;
  37. import javax.swing.JMenu;
  38. import javax.swing.JMenuItem;
  39. import javax.swing.JPopupMenu;
  40. import javax.swing.JToggleButton;
  41. import javax.swing.KeyStroke;
  42. import javax.swing.JToggleButton.ToggleButtonModel;
  43. import javax.swing.event.ChangeEvent;
  44. import javax.swing.event.ChangeListener;
  45. import org.gjt.sp.jedit.GUIUtilities;
  46. import org.gjt.sp.jedit.jEdit;
  47. import org.gjt.sp.jedit.gui.DefaultInputHandler;
  48. import org.gjt.sp.jedit.gui.KeyEventTranslator;
  49. import org.gjt.sp.util.Log;
  50. /**
  51. * The class all InfoViewer actions must extend. It is an
  52. * <code>AbstractAction</code> with support for finding out the InfoViewer
  53. * that invoked the action.
  54. * <p>
  55. *
  56. * @author Dirk Moebius
  57. */
  58. public abstract class InfoViewerAction extends AbstractAction
  59. {
  60. /** Base name for properties */
  61. String name;
  62. JToggleButton.ToggleButtonModel toggleModel;
  63. // {{{ isToggle() method
  64. /**
  65. * Returns if this edit action should be displayed as a check box in
  66. * menus. This returns the value of the property named by
  67. * {@link #getName()} suffixed with <code>.toggle</code>.
  68. *
  69. * @since jEdit 2.2pre4
  70. */
  71. public boolean isToggle()
  72. {
  73. return jEdit.getBooleanProperty(name + ".toggle");
  74. } // }}}
  75. /* public void setSelected(boolean selected) {
  76. toggleModel.setSelected(selected);
  77. // jEdit.setBooleanProperty(name + ".selected", selected);
  78. }
  79. */
  80. public boolean isSelected()
  81. {
  82. return toggleModel.isSelected();
  83. // return jEdit.getBooleanProperty(name + ".selected");
  84. }
  85. public JMenuItem menuItem()
  86. {
  87. JMenuItem retval = null;
  88. if (isToggle())
  89. {
  90. JCheckBoxMenuItem cmi = new JCheckBoxMenuItem(this);
  91. cmi.setModel(toggleModel);
  92. retval = cmi;
  93. }
  94. else
  95. {
  96. retval = new JMenuItem(this);
  97. }
  98. return retval;
  99. }
  100. public static KeyStroke parseKeyStroke(String keyStroke)
  101. {
  102. if (keyStroke == null)
  103. return null;
  104. int modifiers = 0;
  105. int index = keyStroke.indexOf('+');
  106. if (index != -1)
  107. {
  108. for (int i = 0; i < index; i++)
  109. {
  110. switch (Character.toUpperCase(keyStroke.charAt(i)))
  111. {
  112. case 'A':
  113. modifiers |= InputEvent.ALT_MASK;
  114. break;
  115. case 'C':
  116. modifiers |= InputEvent.CTRL_MASK;
  117. break;
  118. case 'M':
  119. modifiers |= InputEvent.META_MASK;
  120. break;
  121. case 'S':
  122. modifiers |= InputEvent.SHIFT_MASK;
  123. break;
  124. }
  125. }
  126. }
  127. String key = keyStroke.substring(index + 1);
  128. if (key.length() == 1)
  129. {
  130. char ch = key.charAt(0);
  131. if (modifiers == 0)
  132. return KeyStroke.getKeyStroke(ch);
  133. else
  134. {
  135. return KeyStroke.getKeyStroke(Character.toUpperCase(ch), modifiers);
  136. }
  137. }
  138. else if (key.length() == 0)
  139. {
  140. Log.log(Log.ERROR, DefaultInputHandler.class, "Invalid key stroke: "
  141. + keyStroke);
  142. return null;
  143. }
  144. else
  145. {
  146. int ch;
  147. try
  148. {
  149. ch = KeyEvent.class.getField("VK_".concat(key)).getInt(null);
  150. }
  151. catch (Exception e)
  152. {
  153. Log.log(Log.ERROR, DefaultInputHandler.class,
  154. "Invalid key stroke: " + keyStroke);
  155. return null;
  156. }
  157. return KeyStroke.getKeyStroke(ch, modifiers);
  158. }
  159. }
  160. /**
  161. * Creates a new <code>InfoViewerAction</code>. This constructor
  162. * should be used by InfoViewer's own actions only.
  163. *
  164. * @param name_key
  165. * a jEdit property with the name for the action. Other
  166. * resources are determined by looking up the following
  167. * keys in the jEdit properties:
  168. * <ul>
  169. * <li><code>name.icon</code> the icon filename</li>
  170. * <li><code>name.description</code> a short
  171. * description</li>
  172. * <li><code>name.mnemonic</code> a menu mnemonic</li>
  173. * <li><code>name.shortcut</code> an keybord shortcut</li>
  174. * </ul>
  175. * @see java.awt.KeyStroke#getKeyStroke
  176. */
  177. InfoViewerAction(String name_key)
  178. {
  179. super(jEdit.getProperty(name_key));
  180. name = name_key;
  181. if (isToggle()) {
  182. toggleModel = new ToggleButtonModel();
  183. toggleModel.setSelected(true);
  184. }
  185. String icon = jEdit.getProperty(name_key + ".icon");
  186. String desc = jEdit.getProperty(name_key + ".description");
  187. String mnem = jEdit.getProperty(name_key + ".mnemonic");
  188. String shrt = jEdit.getProperty(name_key + ".shortcut");
  189. String label = jEdit.getProperty(name_key + ".label") ;
  190. if (icon != null)
  191. {
  192. Icon i = GUIUtilities.loadIcon(icon);
  193. if (i != null)
  194. putValue(SMALL_ICON, i);
  195. }
  196. if (desc != null)
  197. {
  198. putValue(SHORT_DESCRIPTION, desc);
  199. putValue(LONG_DESCRIPTION, desc);
  200. }
  201. if (label != null) {
  202. putValue(NAME, label);
  203. }
  204. if (mnem != null)
  205. putValue(MNEMONIC_KEY, new Integer(mnem.charAt(0)));
  206. if (shrt != null)
  207. {
  208. KeyStroke keyStroke = parseKeyStroke(shrt);
  209. putValue(ACCELERATOR_KEY, keyStroke);
  210. }
  211. }
  212. /**
  213. * Determines the InfoViewer to use for the action.
  214. */
  215. public static InfoViewer getViewer(EventObject evt)
  216. {
  217. if (evt == null)
  218. return null; // this shouldn't happen
  219. Object o = evt.getSource();
  220. if (o instanceof Component)
  221. return getViewer((Component) o);
  222. else
  223. return null;
  224. }
  225. /**
  226. * Finds the InfoViewer parent of the specified component.
  227. */
  228. public static InfoViewer getViewer(Component comp)
  229. {
  230. for (;;)
  231. {
  232. if (comp instanceof InfoViewer)
  233. return (InfoViewer) comp;
  234. else if (comp instanceof JPopupMenu)
  235. comp = ((JPopupMenu) comp).getInvoker();
  236. else if (comp != null)
  237. comp = comp.getParent();
  238. else
  239. break;
  240. }
  241. return null;
  242. }
  243. /**
  244. * Finds the Frame parent of the source component of the given
  245. * EventObject.
  246. */
  247. public static Frame getFrame(EventObject evt)
  248. {
  249. if (evt == null)
  250. return null; // this shouldn't happen
  251. Object source = evt.getSource();
  252. if (source instanceof Component)
  253. {
  254. Component comp = (Component) source;
  255. for (;;)
  256. {
  257. if (comp instanceof Frame)
  258. return (Frame) comp;
  259. else if (comp instanceof JPopupMenu)
  260. comp = ((JPopupMenu) comp).getInvoker();
  261. else if (comp != null)
  262. comp = comp.getParent();
  263. else
  264. break;
  265. }
  266. }
  267. return null;
  268. }
  269. }