2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import java.awt.Component;
42 import java.awt.KeyboardFocusManager;
43 import java.awt.Point;
44 import java.awt.event.ActionEvent;
45 import java.util.HashMap;
46 import java.util.Iterator;
48 import javax.swing.AbstractAction;
49 import javax.swing.Action;
53 * @author Andrew Selkirk
55 public abstract class TextAction extends AbstractAction
58 * Constructor TextAction
61 public TextAction(String name)
67 * Returns the <code>JTextComponent</code> object associated with the given
68 * <code>ActionEvent</code>. If the source of the event is not a
69 * <code>JTextComponent</code> the currently focused text component is returned.
71 * @param event the action event
73 * @return the <code>JTextComponent</code>
75 protected final JTextComponent getTextComponent(ActionEvent event)
77 JTextComponent target = null;
80 Object source = event.getSource();
81 if (source instanceof JTextComponent)
82 target = (JTextComponent) source;
85 target = getFocusedComponent();
90 * Creates a new array of <code>Action</code> containing both given arrays.
92 * @param list1 the first action array
93 * @param list2 the second action array
95 * @return the augmented array of actions
97 public static final Action[] augmentList(Action[] list1, Action[] list2)
99 HashMap<Object,Action> actions = new HashMap<Object,Action>();
101 for (int i = 0; i < list1.length; ++i)
104 Object name = a.getValue(Action.NAME);
105 actions.put(name != null ? name : "", a);
108 for (int i = 0; i < list2.length; ++i)
111 Object name = a.getValue(Action.NAME);
112 actions.put(name != null ? name : "", a);
114 Action[] augmented = new Action[actions.size()];
117 for (Iterator<Action> it = actions.values().iterator(); it.hasNext(); i++)
118 augmented[i] = it.next();
124 * Returns the current focused <code>JTextComponent</code> object.
126 * @return the <code>JTextComponent</code>
128 protected final JTextComponent getFocusedComponent()
130 KeyboardFocusManager kfm =
131 KeyboardFocusManager.getCurrentKeyboardFocusManager();
132 Component focused = kfm.getPermanentFocusOwner();
133 JTextComponent textComp = null;
134 if (focused instanceof JTextComponent)
135 textComp = (JTextComponent) focused;
139 /** Abstract helper class which implements everything needed for an
140 * Action implementation in <code>DefaultEditorKit</code> which
141 * does horizontal movement (and selection).
143 abstract static class HorizontalMovementAction extends TextAction
147 HorizontalMovementAction(String name, int direction)
153 public void actionPerformed(ActionEvent event)
155 JTextComponent t = getTextComponent(event);
161 = Utilities.getNextVisualPositionFrom(t,
162 t.getCaretPosition(),
165 Caret c = t.getCaret();
167 actionPerformedImpl(c, offs);
169 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
172 catch(BadLocationException ble)
175 (InternalError) new InternalError("Illegal offset").initCause(ble);
180 protected abstract void actionPerformedImpl(Caret c, int offs)
181 throws BadLocationException;
184 /** Abstract helper class which implements everything needed for an
185 * Action implementation in <code>DefaultEditorKit</code> which
186 * does vertical movement (and selection).
188 abstract static class VerticalMovementAction extends TextAction
192 VerticalMovementAction(String name, int direction)
198 public void actionPerformed(ActionEvent event)
200 JTextComponent t = getTextComponent(event);
205 Caret c = t.getCaret();
206 // The magic caret position may be null when the caret
207 // has not moved yet.
208 Point mcp = c.getMagicCaretPosition();
213 mcp.y = t.modelToView(c.getDot()).y;
214 pos = t.viewToModel(mcp);
219 pos = Utilities.getNextVisualPositionFrom(t,
220 t.getCaretPosition(),
224 actionPerformedImpl(c, pos);
227 catch(BadLocationException ble)
230 (InternalError) new InternalError("Illegal offset").initCause(ble);
234 protected abstract void actionPerformedImpl(Caret c, int offs)
235 throws BadLocationException;