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.screen.top;
19  
20  import edu.umd.cs.findbugs.annotations.Nullable;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Objects;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
27  
28  /**
29   * The presentation logic for the input mode.
30   */
31  @InterfaceAudience.Private
32  public class InputModeScreenPresenter {
33  
34    public interface ResultListener {
35      public ScreenView apply(String inputString);
36    }
37  
38    private final InputModeScreenView inputModeScreenView;
39    private final String message;
40    private final List<String> histories;
41    private final ResultListener resultListener;
42  
43    private StringBuilder inputString = new StringBuilder();
44    private int cursorPosition;
45    private int historyPosition = -1;
46  
47    public InputModeScreenPresenter(InputModeScreenView inputModeScreenView, String message,
48      @Nullable List<String> histories, ResultListener resultListener) {
49      this.inputModeScreenView = Objects.requireNonNull(inputModeScreenView);
50      this.message = Objects.requireNonNull(message);
51  
52      if (histories != null) {
53        this.histories = Collections.unmodifiableList(new ArrayList<>(histories));
54      } else {
55        this.histories = Collections.emptyList();
56      }
57  
58      this.resultListener = Objects.requireNonNull(resultListener);
59    }
60  
61    public void init() {
62      inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
63      inputModeScreenView.refreshTerminal();
64    }
65  
66    public ScreenView returnToNextScreen() {
67      inputModeScreenView.hideCursor();
68      String result = inputString.toString();
69  
70      return resultListener.apply(result);
71    }
72  
73    public void character(Character character) {
74      inputString.insert(cursorPosition, character);
75      cursorPosition += 1;
76      inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
77      inputModeScreenView.refreshTerminal();
78    }
79  
80    public void backspace() {
81      if (cursorPosition == 0) {
82        return;
83      }
84  
85      inputString.deleteCharAt(cursorPosition - 1);
86      cursorPosition -= 1;
87      inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
88      inputModeScreenView.refreshTerminal();
89    }
90  
91    public void delete() {
92      if (inputString.length() == 0 || cursorPosition > inputString.length() - 1) {
93        return;
94      }
95  
96      inputString.deleteCharAt(cursorPosition);
97      inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
98      inputModeScreenView.refreshTerminal();
99    }
100 
101   public void arrowLeft() {
102     if (cursorPosition == 0) {
103       return;
104     }
105 
106     cursorPosition -= 1;
107     inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
108     inputModeScreenView.refreshTerminal();
109   }
110 
111   public void arrowRight() {
112     if (cursorPosition > inputString.length() - 1) {
113       return;
114     }
115 
116     cursorPosition += 1;
117     inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
118     inputModeScreenView.refreshTerminal();
119   }
120 
121   public void home() {
122     cursorPosition = 0;
123     inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
124     inputModeScreenView.refreshTerminal();
125   }
126 
127   public void end() {
128     cursorPosition = inputString.length();
129     inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
130     inputModeScreenView.refreshTerminal();
131   }
132 
133   public void arrowUp() {
134     if (historyPosition == 0 || histories.isEmpty()) {
135       return;
136     }
137 
138     if (historyPosition == -1) {
139       historyPosition = histories.size() - 1;
140     } else {
141       historyPosition -= 1;
142     }
143 
144     inputString = new StringBuilder(histories.get(historyPosition));
145 
146     cursorPosition = inputString.length();
147     inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
148     inputModeScreenView.refreshTerminal();
149   }
150 
151   public void arrowDown() {
152     if (historyPosition == -1 || histories.isEmpty()) {
153       return;
154     }
155 
156     if (historyPosition == histories.size() - 1) {
157       historyPosition = -1;
158       inputString = new StringBuilder();
159     } else {
160       historyPosition += 1;
161       inputString = new StringBuilder(histories.get(historyPosition));
162     }
163 
164     cursorPosition = inputString.length();
165     inputModeScreenView.showInput(message, inputString.toString(), cursorPosition);
166     inputModeScreenView.refreshTerminal();
167   }
168 }