1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50:
51:
55: public abstract class TextAction extends AbstractAction
56: {
57:
61: public TextAction(String name)
62: {
63: super(name);
64: }
65:
66:
75: protected final JTextComponent getTextComponent(ActionEvent event)
76: {
77: JTextComponent target = null;
78: if (event != null)
79: {
80: Object source = event.getSource();
81: if (source instanceof JTextComponent)
82: target = (JTextComponent) source;
83: }
84: if (target == null)
85: target = getFocusedComponent();
86: return target;
87: }
88:
89:
97: public static final Action[] augmentList(Action[] list1, Action[] list2)
98: {
99: HashMap<Object,Action> actions = new HashMap<Object,Action>();
100:
101: for (int i = 0; i < list1.length; ++i)
102: {
103: Action a = list1[i];
104: Object name = a.getValue(Action.NAME);
105: actions.put(name != null ? name : "", a);
106: }
107:
108: for (int i = 0; i < list2.length; ++i)
109: {
110: Action a = list2[i];
111: Object name = a.getValue(Action.NAME);
112: actions.put(name != null ? name : "", a);
113: }
114: Action[] augmented = new Action[actions.size()];
115:
116: int i = 0;
117: for (Iterator<Action> it = actions.values().iterator(); it.hasNext(); i++)
118: augmented[i] = it.next();
119: return augmented;
120:
121: }
122:
123:
128: protected final JTextComponent getFocusedComponent()
129: {
130: KeyboardFocusManager kfm =
131: KeyboardFocusManager.getCurrentKeyboardFocusManager();
132: Component focused = kfm.getPermanentFocusOwner();
133: JTextComponent textComp = null;
134: if (focused instanceof JTextComponent)
135: textComp = (JTextComponent) focused;
136: return textComp;
137: }
138:
139:
143: abstract static class HorizontalMovementAction extends TextAction
144: {
145: int dir;
146:
147: HorizontalMovementAction(String name, int direction)
148: {
149: super(name);
150: dir = direction;
151: }
152:
153: public void actionPerformed(ActionEvent event)
154: {
155: JTextComponent t = getTextComponent(event);
156: try
157: {
158: if (t != null)
159: {
160: int offs
161: = Utilities.getNextVisualPositionFrom(t,
162: t.getCaretPosition(),
163: dir);
164:
165: Caret c = t.getCaret();
166:
167: actionPerformedImpl(c, offs);
168:
169: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
170: }
171: }
172: catch(BadLocationException ble)
173: {
174: throw
175: (InternalError) new InternalError("Illegal offset").initCause(ble);
176: }
177:
178: }
179:
180: protected abstract void actionPerformedImpl(Caret c, int offs)
181: throws BadLocationException;
182: }
183:
184:
188: abstract static class VerticalMovementAction extends TextAction
189: {
190: int dir;
191:
192: VerticalMovementAction(String name, int direction)
193: {
194: super(name);
195: dir = direction;
196: }
197:
198: public void actionPerformed(ActionEvent event)
199: {
200: JTextComponent t = getTextComponent(event);
201: try
202: {
203: if (t != null)
204: {
205: Caret c = t.getCaret();
206:
207:
208: Point mcp = c.getMagicCaretPosition();
209:
210: int pos;
211: if (mcp != null)
212: {
213: mcp.y = t.modelToView(c.getDot()).y;
214: pos = t.viewToModel(mcp);
215: }
216: else
217: pos = c.getDot();
218:
219: pos = Utilities.getNextVisualPositionFrom(t,
220: t.getCaretPosition(),
221: dir);
222:
223: if (pos > -1)
224: actionPerformedImpl(c, pos);
225: }
226: }
227: catch(BadLocationException ble)
228: {
229: throw
230: (InternalError) new InternalError("Illegal offset").initCause(ble);
231: }
232: }
233:
234: protected abstract void actionPerformedImpl(Caret c, int offs)
235: throws BadLocationException;
236:
237: }
238:
239:
240: }