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: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.event.ActionEvent;
024:
025: import javax.swing.AbstractAction;
026: import javax.swing.Action;
027: import javax.swing.JComboBox;
028: import javax.swing.JComponent;
029: import javax.swing.JList;
030:
031: import org.apache.harmony.x.swing.Utilities;
032:
033: class BasicComboBoxKeyboardActions {
034: private abstract static class PassThroughAction extends
035: AbstractAction {
036: protected static void passThroughEvent(final ActionEvent event,
037: final String correspondingCommand) {
038: JComboBox comboBox = (JComboBox) event.getSource();
039: JList popupList = ((BasicComboBoxUI) comboBox.getUI()).popup
040: .getList();
041: Action action = popupList.getActionMap().get(
042: correspondingCommand);
043: if (action != null) {
044: action.actionPerformed(new ActionEvent(popupList,
045: ActionEvent.ACTION_PERFORMED,
046: correspondingCommand, event.getWhen(), event
047: .getModifiers()));
048: comboBox.setSelectedIndex(popupList.getSelectedIndex());
049: }
050: }
051: }
052:
053: private abstract static class SelectNextPreviousAction extends
054: AbstractAction {
055: private String command;
056:
057: private SelectNextPreviousAction(final String command) {
058: this .command = command;
059: }
060:
061: abstract void selectValue(final BasicComboBoxUI ui);
062:
063: public void actionPerformed(ActionEvent e) {
064: JComboBox comboBox = (JComboBox) e.getSource();
065: if (!comboBox.isPopupVisible()) {
066: comboBox.showPopup();
067: } else {
068: BasicComboBoxUI ui = ((BasicComboBoxUI) comboBox
069: .getUI());
070: JList popupList = ui.popup.getList();
071: Action action = popupList.getActionMap().get(command);
072: if (action != null) {
073: action.actionPerformed(new ActionEvent(popupList,
074: ActionEvent.ACTION_PERFORMED, command, e
075: .getWhen(), e.getModifiers()));
076: selectValue(ui);
077: }
078: }
079: }
080: }
081:
082: private static AbstractAction selectPreviousAction = new SelectNextPreviousAction(
083: "selectPreviousRow") {
084: void selectValue(final BasicComboBoxUI ui) {
085: ui.selectPreviousPossibleValue();
086: }
087: };
088: private static AbstractAction selectNextAction = new SelectNextPreviousAction(
089: "selectNextRow") {
090: void selectValue(final BasicComboBoxUI ui) {
091: ui.selectNextPossibleValue();
092: }
093: };
094:
095: private static AbstractAction togglePopupAction = new AbstractAction() {
096: public void actionPerformed(final ActionEvent e) {
097: JComboBox comboBox = (JComboBox) e.getSource();
098: comboBox.setPopupVisible(!comboBox.isPopupVisible());
099: }
100: };
101:
102: private static AbstractAction pageUpPassThroughAction = new PassThroughAction() {
103: public void actionPerformed(final ActionEvent e) {
104: passThroughEvent(e, "scrollUp");
105: }
106: };
107:
108: private static AbstractAction pageDownPassThroughAction = new PassThroughAction() {
109: public void actionPerformed(final ActionEvent e) {
110: passThroughEvent(e, "scrollDown");
111: }
112: };
113:
114: private static AbstractAction homePassThroughAction = new PassThroughAction() {
115: public void actionPerformed(final ActionEvent e) {
116: passThroughEvent(e, "selectFirstRow");
117: }
118: };
119:
120: private static AbstractAction endPassThroughAction = new PassThroughAction() {
121: public void actionPerformed(final ActionEvent e) {
122: passThroughEvent(e, "selectLastRow");
123: }
124: };
125:
126: private static class HidePopupAction extends AbstractAction {
127: protected final JComboBox comboBox;
128:
129: public HidePopupAction(final JComboBox comboBox) {
130: this .comboBox = comboBox;
131: }
132:
133: public void actionPerformed(final ActionEvent e) {
134: comboBox.hidePopup();
135: setValue();
136: }
137:
138: public boolean isEnabled() {
139: return comboBox.isPopupVisible();
140: }
141:
142: protected void setValue() {
143: }
144: }
145:
146: private static class HidePopupAndSetValueAction extends
147: HidePopupAction {
148: public HidePopupAndSetValueAction(final JComboBox comboBox) {
149: super (comboBox);
150: }
151:
152: public boolean isEnabled() {
153: return true;
154: }
155:
156: protected void setValue() {
157: BasicComboBoxUI ui = ((BasicComboBoxUI) comboBox.getUI());
158: if (ui.isTableEditor) {
159: comboBox.setSelectedIndex(ui.popup.getList()
160: .getSelectedIndex());
161: }
162: }
163: }
164:
165: public static void installKeyboardActions(final JComboBox comboBox) {
166: Utilities.installKeyboardActions(comboBox,
167: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
168: "ComboBox.ancestorInputMap", null);
169:
170: comboBox.getActionMap().put("hidePopup",
171: new HidePopupAction(comboBox));
172: comboBox.getActionMap().put("enterPressed",
173: new HidePopupAndSetValueAction(comboBox));
174: comboBox.getActionMap().put("selectNext", selectNextAction);
175: comboBox.getActionMap().put("selectPrevious",
176: selectPreviousAction);
177: comboBox.getActionMap().put("togglePopup", togglePopupAction);
178: comboBox.getActionMap().put("spacePopup", new AbstractAction() {
179: public void actionPerformed(final ActionEvent e) {
180: comboBox.setPopupVisible(!comboBox.isPopupVisible());
181: }
182:
183: public boolean isEnabled() {
184: return !comboBox.isEditable();
185: }
186: });
187: comboBox.getActionMap().put("pageUpPassThrough",
188: pageUpPassThroughAction);
189: comboBox.getActionMap().put("pageDownPassThrough",
190: pageDownPassThroughAction);
191: comboBox.getActionMap().put("homePassThrough",
192: homePassThroughAction);
193: comboBox.getActionMap().put("endPassThrough",
194: endPassThroughAction);
195: }
196:
197: public static void uninstallKeyboardActions(final JComboBox comboBox) {
198: Utilities.uninstallKeyboardActions(comboBox,
199: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
200: }
201: }
|