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