| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 6 | * This module is licensed under the GNU Lesser General Public License |
| 7 | * Version 3. Please see the file "COPYING" in this directory for more |
| 8 | * information about the GNU Lesser General Public License Version 3. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public License |
| 14 | * as published by the Free Software Foundation; either version 3 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this program; if not, see |
| 24 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer.event; |
| 32 | |
| 33 | import jexer.TCommand; |
| 34 | |
| 35 | /** |
| 36 | * This class encapsulates a user command event. User commands can be |
| 37 | * generated by menu actions, keyboard accelerators, and other UI elements. |
| 38 | * Commands can operate on both the application and individual widgets. |
| 39 | */ |
| 40 | public final class TCommandEvent extends TInputEvent { |
| 41 | |
| 42 | /** |
| 43 | * Command dispatched. |
| 44 | */ |
| 45 | private TCommand cmd; |
| 46 | |
| 47 | /** |
| 48 | * Get TCommand. |
| 49 | * |
| 50 | * @return the TCommand |
| 51 | */ |
| 52 | public TCommand getCmd() { |
| 53 | return cmd; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Public contructor. |
| 58 | * |
| 59 | * @param cmd the TCommand dispatched |
| 60 | */ |
| 61 | public TCommandEvent(final TCommand cmd) { |
| 62 | this.cmd = cmd; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Comparison check. All fields must match to return true. |
| 67 | * |
| 68 | * @param rhs another TCommandEvent or TCommand instance |
| 69 | * @return true if all fields are equal |
| 70 | */ |
| 71 | @Override |
| 72 | public boolean equals(final Object rhs) { |
| 73 | if (!(rhs instanceof TCommandEvent) |
| 74 | && !(rhs instanceof TCommand) |
| 75 | ) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if (rhs instanceof TCommandEvent) { |
| 80 | TCommandEvent that = (TCommandEvent) rhs; |
| 81 | return (cmd.equals(that.cmd) |
| 82 | && (getTime().equals(that.getTime()))); |
| 83 | } |
| 84 | |
| 85 | TCommand that = (TCommand) rhs; |
| 86 | return (cmd.equals(that)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Hashcode uses all fields in equals(). |
| 91 | * |
| 92 | * @return the hash |
| 93 | */ |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | int A = 13; |
| 97 | int B = 23; |
| 98 | int hash = A; |
| 99 | hash = (B * hash) + getTime().hashCode(); |
| 100 | hash = (B * hash) + cmd.hashCode(); |
| 101 | return hash; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Make human-readable description of this TCommandEvent. |
| 106 | * |
| 107 | * @return displayable String |
| 108 | */ |
| 109 | @Override |
| 110 | public String toString() { |
| 111 | return String.format("CommandEvent: %s", cmd.toString()); |
| 112 | } |
| 113 | } |