001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop.screen.top;
019
020import java.util.List;
021import java.util.function.Function;
022import org.apache.hadoop.hbase.hbtop.screen.AbstractScreenView;
023import org.apache.hadoop.hbase.hbtop.screen.Screen;
024import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
025import org.apache.hadoop.hbase.hbtop.terminal.KeyPress;
026import org.apache.hadoop.hbase.hbtop.terminal.Terminal;
027import org.apache.yetus.audience.InterfaceAudience;
028
029
030/**
031 * The input mode in the top screen.
032 */
033@InterfaceAudience.Private
034public class InputModeScreenView extends AbstractScreenView {
035
036  private final int row;
037  private final InputModeScreenPresenter inputModeScreenPresenter;
038
039  public InputModeScreenView(Screen screen, Terminal terminal, int row, String message,
040    List<String> histories, Function<String, ScreenView> resultListener) {
041    super(screen, terminal);
042    this.row = row;
043    this.inputModeScreenPresenter = new InputModeScreenPresenter(this, message, histories,
044      resultListener);
045  }
046
047  @Override
048  public void init() {
049    inputModeScreenPresenter.init();
050  }
051
052  @Override
053  public ScreenView handleKeyPress(KeyPress keyPress) {
054
055    switch (keyPress.getType()) {
056      case Enter:
057        return inputModeScreenPresenter.returnToNextScreen();
058
059      case Character:
060        inputModeScreenPresenter.character(keyPress.getCharacter());
061        break;
062
063      case Backspace:
064        inputModeScreenPresenter.backspace();
065        break;
066
067      case Delete:
068        inputModeScreenPresenter.delete();
069        break;
070
071      case ArrowLeft:
072        inputModeScreenPresenter.arrowLeft();
073        break;
074
075      case ArrowRight:
076        inputModeScreenPresenter.arrowRight();
077        break;
078
079      case Home:
080        inputModeScreenPresenter.home();
081        break;
082
083      case End:
084        inputModeScreenPresenter.end();
085        break;
086
087      case ArrowUp:
088        inputModeScreenPresenter.arrowUp();
089        break;
090
091      case ArrowDown:
092        inputModeScreenPresenter.arrowDown();
093        break;
094
095      default:
096        break;
097    }
098    return this;
099  }
100
101  public void showInput(String message, String inputString, int cursorPosition) {
102    getTerminalPrinter(row).startBold().print(message).stopBold().print(" ").print(inputString)
103      .endOfLine();
104    setCursorPosition(message.length() + 1 + cursorPosition, row);
105    refreshTerminal();
106  }
107}