|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Copyright 2015 Matthijs Kooijman <[email protected]> |
| 5 | + * |
| 6 | + * Arduino 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 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + * |
| 20 | + * As a special exception, you may use this file as part of a free software |
| 21 | + * library without restriction. Specifically, if other files instantiate |
| 22 | + * templates or use macros or inline functions from this file, or you compile |
| 23 | + * this file and link it with other files to produce an executable, this |
| 24 | + * file does not by itself cause the resulting executable to be covered by |
| 25 | + * the GNU General Public License. This exception does not however |
| 26 | + * invalidate any other reasons why the executable file might be covered by |
| 27 | + * the GNU General Public License. |
| 28 | + */ |
| 29 | + |
| 30 | +package processing.app.helpers; |
| 31 | + |
| 32 | +import java.awt.event.ActionEvent; |
| 33 | +import java.awt.event.ActionListener; |
| 34 | + |
| 35 | +import javax.swing.AbstractAction; |
| 36 | +import javax.swing.KeyStroke; |
| 37 | + |
| 38 | +/** |
| 39 | + * Class to easily define instances of the Swing Action interface. |
| 40 | + * |
| 41 | + * When using AbstractAction, you have to create a subclass that implements the |
| 42 | + * actionPerformed() method, and sets attributes in the constructor, which gets |
| 43 | + * verbose quickly. This class implements actionPerformed for you, and forwards |
| 44 | + * it to the ActionListener passed to the constructor (intended to be a lambda |
| 45 | + * expression). Additional Action attributes can be set by passing constructor |
| 46 | + * arguments. |
| 47 | + * |
| 48 | + * The name of this class refers to the fact that it's simple to create an |
| 49 | + * action using this class, but perhaps a better name can be found for it. |
| 50 | + * |
| 51 | + * @see javax.swing.Action |
| 52 | + */ |
| 53 | +public class SimpleAction extends AbstractAction { |
| 54 | + private ActionListener listener; |
| 55 | + |
| 56 | + /** |
| 57 | + * Version of ActionListener that does not take an ActionEvent as an argument |
| 58 | + * This can be used when you do not care about the event itself, just that it |
| 59 | + * happened, typically for passing a argumentless lambda or method reference |
| 60 | + * to the SimpleAction constructor. |
| 61 | + */ |
| 62 | + public interface AnonymousActionListener { |
| 63 | + public void actionPerformed(); |
| 64 | + } |
| 65 | + |
| 66 | + public SimpleAction(String name, ActionListener listener) { |
| 67 | + this(name, null, null, listener); |
| 68 | + } |
| 69 | + |
| 70 | + public SimpleAction(String name, AnonymousActionListener listener) { |
| 71 | + this(name, null, null, listener); |
| 72 | + } |
| 73 | + |
| 74 | + public SimpleAction(String name, KeyStroke accelerator, |
| 75 | + ActionListener listener) { |
| 76 | + this(name, null, accelerator, listener); |
| 77 | + } |
| 78 | + |
| 79 | + public SimpleAction(String name, KeyStroke accelerator, |
| 80 | + AnonymousActionListener listener) { |
| 81 | + this(name, null, accelerator, listener); |
| 82 | + } |
| 83 | + |
| 84 | + public SimpleAction(String name, String description, |
| 85 | + ActionListener listener) { |
| 86 | + this(name, description, null, listener); |
| 87 | + } |
| 88 | + |
| 89 | + public SimpleAction(String name, String description, |
| 90 | + AnonymousActionListener listener) { |
| 91 | + this(name, description, null, listener); |
| 92 | + } |
| 93 | + |
| 94 | + public SimpleAction(String name, String description, KeyStroke accelerator, |
| 95 | + AnonymousActionListener listener) { |
| 96 | + this(name, description, accelerator, |
| 97 | + (ActionEvent) -> listener.actionPerformed()); |
| 98 | + } |
| 99 | + |
| 100 | + public SimpleAction(String name, String description, KeyStroke accelerator, |
| 101 | + ActionListener listener) { |
| 102 | + this.putValue(NAME, name); |
| 103 | + this.putValue(SHORT_DESCRIPTION, description); |
| 104 | + this.putValue(ACCELERATOR_KEY, accelerator); |
| 105 | + this.listener = listener; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void actionPerformed(ActionEvent e) { |
| 110 | + listener.actionPerformed(e); |
| 111 | + } |
| 112 | +} |
0 commit comments