001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev, Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Point;
023: import java.awt.event.ActionEvent;
024: import java.util.Comparator;
025: import java.util.TreeMap;
026: import javax.swing.AbstractAction;
027: import javax.swing.Action;
028:
029: import org.apache.harmony.awt.text.AWTTextAction;
030: import org.apache.harmony.awt.text.ActionSet;
031: import org.apache.harmony.awt.text.TextKit;
032: import org.apache.harmony.awt.text.TextUtils;
033:
034: public abstract class TextAction extends AbstractAction {
035: public static final Action[] augmentList(final Action[] list1,
036: final Action[] list2) {
037: Comparator comparator = new Comparator() {
038: public int compare(final Object item1, final Object item2) {
039: return ((String) item2).compareTo((String) item1);
040: }
041: };
042:
043: TreeMap map = new TreeMap(comparator);
044: if (list2 != null) {
045: for (int i = 0; i < list2.length; i++) {
046: map.put(list2[i].getValue(Action.NAME), list2[i]);
047: }
048: }
049: if (list1 != null) {
050: for (int i = 0; i < list1.length; i++) {
051: String name = (String) list1[i].getValue(Action.NAME);
052: if (!map.containsKey(name)) {
053: map.put(name, list1[i]);
054: }
055: }
056: }
057:
058: return (Action[]) (map).values()
059: .toArray(new Action[map.size()]);
060: }
061:
062: public TextAction(final String name) {
063: putValue(Action.NAME, name);
064: }
065:
066: protected final JTextComponent getFocusedComponent() {
067: return JTextComponent.getLastFocusedTextComponent();
068: }
069:
070: protected final JTextComponent getTextComponent(final ActionEvent e) {
071: if (e != null) {
072: Object eventSource = e.getSource();
073: if (eventSource instanceof JTextComponent) {
074: return (JTextComponent) eventSource;
075: }
076: }
077:
078: return getFocusedComponent();
079: }
080:
081: final JTextComponent getEditableTextComponent(final ActionEvent e) {
082: JTextComponent c = getTextComponent(e);
083: return (c != null && c.isEditable()) ? c : null;
084: }
085:
086: /**
087: * sets new value to dot or mark of given component's caret
088: * depending on <code>isMovingCaret</code> value
089: */
090: final void changeCaretPosition(final JTextComponent component,
091: final int newPos, final boolean isMovingCaret) {
092: changeCaretPosition(component, newPos, isMovingCaret,
093: Position.Bias.Forward);
094: }
095:
096: /**
097: * sets new value to dot or mark of given component's caret
098: * depending on <code>isMovingCaret</code> value
099: */
100: final void changeCaretPosition(final JTextComponent component,
101: final int newPos, final boolean isMovingCaret,
102: final Position.Bias newBias) {
103: TextKit textKit = TextUtils.getTextKit(component);
104: TextUtils.changeCaretPosition(textKit, newPos, isMovingCaret,
105: newBias);
106: }
107:
108: /**
109: * determines and sets value to given component's caret magic position
110: *
111: * @param source - component which caret will be modified
112: * @param pos - caret position
113: * @param direction in which caret is moving one of
114: * <code>SwingConstants</code> values
115: * @param oldPoint - current magic position value
116: * @throws BadLocationException
117: */
118: final void setMagicPosition(final JTextComponent source,
119: final int pos, final int direction, final Point oldPoint)
120: throws BadLocationException {
121: TextKit textKit = TextUtils.getTextKit(source);
122: textKit.getCaret().setMagicCaretPosition(pos, direction,
123: oldPoint);
124: }
125:
126: final void setCurrentPositionAsMagic(final JTextComponent c) {
127: TextUtils.setCurrentPositionAsMagic(TextUtils.getTextKit(c));
128: }
129:
130: AWTTextAction action;
131:
132: TextAction(final String name, final boolean findAWTTextAction) {
133: super (name);
134: this .action = (AWTTextAction) ActionSet.actionMap.get(name);
135: }
136:
137: final void performTextAction(final ActionEvent e) {
138: JTextComponent source = getTextComponent(e);
139: if (source != null) {
140: action.performAction(TextUtils.getTextKit(source));
141: }
142: }
143: }
|