src/macosx/classes/com/apple/laf/AquaKeyBindings.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 511                 if (keyIndex == -1) {
 512                     unifiedList.add(key);
 513                     unifiedList.add(value);
 514                 } else {
 515                     unifiedList.set(keyIndex, key);
 516                     unifiedList.set(keyIndex + 1, value);
 517                 }
 518             }
 519         }
 520     }
 521 
 522     void installAquaUpDownActions(final JTextComponent component) {
 523         final ActionMap actionMap = component.getActionMap();
 524         actionMap.put(upMultilineAction, moveUpMultilineAction);
 525         actionMap.put(downMultilineAction, moveDownMultilineAction);
 526         actionMap.put(pageUpMultiline, pageUpMultilineAction);
 527         actionMap.put(pageDownMultiline, pageDownMultilineAction);
 528     }
 529 
 530     // extracted and adapted from DefaultEditorKit in 1.6

 531     static abstract class DeleteWordAction extends TextAction {
 532         public DeleteWordAction(final String name) { super(name); }
 533 
 534         public void actionPerformed(final ActionEvent e) {
 535             if (e == null) return;
 536 
 537             final JTextComponent target = getTextComponent(e);
 538             if (target == null) return;
 539 
 540             if (!target.isEditable() || !target.isEnabled()) {
 541                 UIManager.getLookAndFeel().provideErrorFeedback(target);
 542                 return;
 543             }
 544 
 545             try {
 546                 final int start = target.getSelectionStart();
 547                 final Element line = Utilities.getParagraphElement(target, start);
 548                 final int end = getEnd(target, line, start);
 549 
 550                 final int offs = Math.min(start, end);
 551                 final int len = Math.abs(end - start);
 552                 if (offs >= 0) {
 553                     target.getDocument().remove(offs, len);
 554                     return;
 555                 }
 556             } catch (final BadLocationException ignore) {}
 557             UIManager.getLookAndFeel().provideErrorFeedback(target);
 558         }
 559 
 560         abstract int getEnd(final JTextComponent target, final Element line, final int start) throws BadLocationException;
 561     }
 562 
 563     final TextAction moveUpMultilineAction = new AquaMultilineAction(upMultilineAction, DefaultEditorKit.upAction, DefaultEditorKit.beginAction);
 564     final TextAction moveDownMultilineAction = new AquaMultilineAction(downMultilineAction, DefaultEditorKit.downAction, DefaultEditorKit.endAction);
 565     final TextAction pageUpMultilineAction = new AquaMultilineAction(pageUpMultiline, DefaultEditorKit.pageUpAction, DefaultEditorKit.beginAction);
 566     final TextAction pageDownMultilineAction = new AquaMultilineAction(pageDownMultiline, DefaultEditorKit.pageDownAction, DefaultEditorKit.endAction);
 567 

 568     static class AquaMultilineAction extends TextAction {
 569         final String targetActionName;
 570         final String proxyActionName;
 571 
 572         public AquaMultilineAction(final String actionName, final String targetActionName, final String proxyActionName) {
 573             super(actionName);
 574             this.targetActionName = targetActionName;
 575             this.proxyActionName = proxyActionName;
 576         }
 577 
 578         public void actionPerformed(final ActionEvent e) {
 579             final JTextComponent c = getTextComponent(e);
 580             final ActionMap actionMap = c.getActionMap();
 581             final Action targetAction = actionMap.get(targetActionName);
 582 
 583             final int startPosition = c.getCaretPosition();
 584             targetAction.actionPerformed(e);
 585             if (startPosition != c.getCaretPosition()) return;
 586 
 587             final Action proxyAction = actionMap.get(proxyActionName);
   1 /*
   2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 511                 if (keyIndex == -1) {
 512                     unifiedList.add(key);
 513                     unifiedList.add(value);
 514                 } else {
 515                     unifiedList.set(keyIndex, key);
 516                     unifiedList.set(keyIndex + 1, value);
 517                 }
 518             }
 519         }
 520     }
 521 
 522     void installAquaUpDownActions(final JTextComponent component) {
 523         final ActionMap actionMap = component.getActionMap();
 524         actionMap.put(upMultilineAction, moveUpMultilineAction);
 525         actionMap.put(downMultilineAction, moveDownMultilineAction);
 526         actionMap.put(pageUpMultiline, pageUpMultilineAction);
 527         actionMap.put(pageDownMultiline, pageDownMultilineAction);
 528     }
 529 
 530     // extracted and adapted from DefaultEditorKit in 1.6
 531     @SuppressWarnings("serial") // Superclass is not serializable across versions
 532     static abstract class DeleteWordAction extends TextAction {
 533         public DeleteWordAction(final String name) { super(name); }
 534 
 535         public void actionPerformed(final ActionEvent e) {
 536             if (e == null) return;
 537 
 538             final JTextComponent target = getTextComponent(e);
 539             if (target == null) return;
 540 
 541             if (!target.isEditable() || !target.isEnabled()) {
 542                 UIManager.getLookAndFeel().provideErrorFeedback(target);
 543                 return;
 544             }
 545 
 546             try {
 547                 final int start = target.getSelectionStart();
 548                 final Element line = Utilities.getParagraphElement(target, start);
 549                 final int end = getEnd(target, line, start);
 550 
 551                 final int offs = Math.min(start, end);
 552                 final int len = Math.abs(end - start);
 553                 if (offs >= 0) {
 554                     target.getDocument().remove(offs, len);
 555                     return;
 556                 }
 557             } catch (final BadLocationException ignore) {}
 558             UIManager.getLookAndFeel().provideErrorFeedback(target);
 559         }
 560 
 561         abstract int getEnd(final JTextComponent target, final Element line, final int start) throws BadLocationException;
 562     }
 563 
 564     final TextAction moveUpMultilineAction = new AquaMultilineAction(upMultilineAction, DefaultEditorKit.upAction, DefaultEditorKit.beginAction);
 565     final TextAction moveDownMultilineAction = new AquaMultilineAction(downMultilineAction, DefaultEditorKit.downAction, DefaultEditorKit.endAction);
 566     final TextAction pageUpMultilineAction = new AquaMultilineAction(pageUpMultiline, DefaultEditorKit.pageUpAction, DefaultEditorKit.beginAction);
 567     final TextAction pageDownMultilineAction = new AquaMultilineAction(pageDownMultiline, DefaultEditorKit.pageDownAction, DefaultEditorKit.endAction);
 568 
 569     @SuppressWarnings("serial") // Superclass is not serializable across versions
 570     static class AquaMultilineAction extends TextAction {
 571         final String targetActionName;
 572         final String proxyActionName;
 573 
 574         public AquaMultilineAction(final String actionName, final String targetActionName, final String proxyActionName) {
 575             super(actionName);
 576             this.targetActionName = targetActionName;
 577             this.proxyActionName = proxyActionName;
 578         }
 579 
 580         public void actionPerformed(final ActionEvent e) {
 581             final JTextComponent c = getTextComponent(e);
 582             final ActionMap actionMap = c.getActionMap();
 583             final Action targetAction = actionMap.get(targetActionName);
 584 
 585             final int startPosition = c.getCaretPosition();
 586             targetAction.actionPerformed(e);
 587             if (startPosition != c.getCaretPosition()) return;
 588 
 589             final Action proxyAction = actionMap.get(proxyActionName);