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.mode; 019 020import java.util.Arrays; 021import java.util.List; 022import java.util.Objects; 023import java.util.function.Consumer; 024import org.apache.hadoop.hbase.hbtop.mode.Mode; 025import org.apache.hadoop.hbase.hbtop.screen.ScreenView; 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * The presentation logic for the mode screen. 030 */ 031@InterfaceAudience.Private 032public class ModeScreenPresenter { 033 034 private final ModeScreenView modeScreenView; 035 private final Mode currentMode; 036 private final Consumer<Mode> resultListener; 037 private final ScreenView nextScreenView; 038 039 private final int modeHeaderMaxLength; 040 private final int modeDescriptionMaxLength; 041 private final List<Mode> modes = Arrays.asList(Mode.values()); 042 043 private int currentPosition; 044 045 public ModeScreenPresenter(ModeScreenView modeScreenView, Mode currentMode, 046 Consumer<Mode> resultListener, ScreenView nextScreenView) { 047 this.modeScreenView = Objects.requireNonNull(modeScreenView); 048 this.currentMode = Objects.requireNonNull(currentMode); 049 this.resultListener = Objects.requireNonNull(resultListener); 050 this.nextScreenView = Objects.requireNonNull(nextScreenView); 051 052 int modeHeaderLength = 0; 053 int modeDescriptionLength = 0; 054 for (int i = 0; i < modes.size(); i++) { 055 Mode mode = modes.get(i); 056 if (mode == currentMode) { 057 currentPosition = i; 058 } 059 060 if (modeHeaderLength < mode.getHeader().length()) { 061 modeHeaderLength = mode.getHeader().length(); 062 } 063 064 if (modeDescriptionLength < mode.getDescription().length()) { 065 modeDescriptionLength = mode.getDescription().length(); 066 } 067 } 068 069 modeHeaderMaxLength = modeHeaderLength; 070 modeDescriptionMaxLength = modeDescriptionLength; 071 } 072 073 public void init() { 074 modeScreenView.hideCursor(); 075 modeScreenView.clearTerminal(); 076 modeScreenView.showModeScreen(currentMode, modes, currentPosition, modeHeaderMaxLength, 077 modeDescriptionMaxLength); 078 modeScreenView.refreshTerminal(); 079 } 080 081 public void arrowUp() { 082 if (currentPosition > 0) { 083 currentPosition -= 1; 084 showMode(currentPosition); 085 showMode(currentPosition + 1); 086 modeScreenView.refreshTerminal(); 087 } 088 } 089 090 public void arrowDown() { 091 if (currentPosition < modes.size() - 1) { 092 currentPosition += 1; 093 showMode(currentPosition); 094 showMode(currentPosition - 1); 095 modeScreenView.refreshTerminal(); 096 } 097 } 098 099 public void pageUp() { 100 if (currentPosition > 0) { 101 int previousPosition = currentPosition; 102 currentPosition = 0; 103 showMode(previousPosition); 104 showMode(currentPosition); 105 modeScreenView.refreshTerminal(); 106 } 107 } 108 109 public void pageDown() { 110 if (currentPosition < modes.size() - 1) { 111 int previousPosition = currentPosition; 112 currentPosition = modes.size() - 1; 113 showMode(previousPosition); 114 showMode(currentPosition); 115 modeScreenView.refreshTerminal(); 116 } 117 } 118 119 private void showMode(int pos) { 120 modeScreenView.showMode(pos, modes.get(pos), pos == currentPosition, modeHeaderMaxLength, 121 modeDescriptionMaxLength); 122 } 123 124 public ScreenView transitionToNextScreen(boolean changeMode) { 125 Mode selectedMode = modes.get(currentPosition); 126 if (changeMode && currentMode != selectedMode) { 127 resultListener.accept(selectedMode); 128 } 129 return nextScreenView; 130 } 131}