View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.hbtop.terminal;
19  
20  import edu.umd.cs.findbugs.annotations.Nullable;
21  import java.util.Objects;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  
25  /**
26   * Represents the user pressing a key on the keyboard.
27   */
28  @InterfaceAudience.Private
29  public class KeyPress {
30    public enum Type {
31      Character,
32      Escape,
33      Backspace,
34      ArrowLeft,
35      ArrowRight,
36      ArrowUp,
37      ArrowDown,
38      Insert,
39      Delete,
40      Home,
41      End,
42      PageUp,
43      PageDown,
44      ReverseTab,
45      Tab,
46      Enter,
47      F1,
48      F2,
49      F3,
50      F4,
51      F5,
52      F6,
53      F7,
54      F8,
55      F9,
56      F10,
57      F11,
58      F12,
59      Unknown
60    }
61  
62    private final Type type;
63    private final Character character;
64    private final boolean alt;
65    private final boolean ctrl;
66    private final boolean shift;
67  
68    public KeyPress(Type type, @Nullable Character character, boolean alt, boolean ctrl,
69      boolean shift) {
70      this.type = Objects.requireNonNull(type);
71      this.character = character;
72      this.alt = alt;
73      this.ctrl = ctrl;
74      this.shift = shift;
75    }
76  
77    public Type getType() {
78      return type;
79    }
80  
81    @Nullable
82    public Character getCharacter() {
83      return character;
84    }
85  
86    public boolean isAlt() {
87      return alt;
88    }
89  
90    public boolean isCtrl() {
91      return ctrl;
92    }
93  
94    public boolean isShift() {
95      return shift;
96    }
97  
98    @Override
99    public String toString() {
100     return "KeyPress{" +
101       "type=" + type +
102       ", character=" + escape(character) +
103       ", alt=" + alt +
104       ", ctrl=" + ctrl +
105       ", shift=" + shift +
106       '}';
107   }
108 
109   private String escape(Character character) {
110     if (character == null) {
111       return "null";
112     }
113 
114     switch (character) {
115       case '\n':
116         return "\\n";
117 
118       case '\b':
119         return "\\b";
120 
121       case '\t':
122         return "\\t";
123 
124       default:
125         return character.toString();
126     }
127   }
128 }