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.mode;
19  
20  import java.util.Arrays;
21  import java.util.List;
22  import java.util.Objects;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.hbtop.mode.Mode;
25  import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
26  
27  /**
28   * The presentation logic for the mode screen.
29   */
30  @InterfaceAudience.Private
31  public class ModeScreenPresenter {
32  
33    public interface ResultListener {
34      void accept(Mode mode);
35    }
36  
37    private final ModeScreenView modeScreenView;
38    private final Mode currentMode;
39    private final ResultListener resultListener;
40    private final ScreenView nextScreenView;
41  
42    private final int modeHeaderMaxLength;
43    private final int modeDescriptionMaxLength;
44    private final List<Mode> modes = Arrays.asList(Mode.values());
45  
46    private int currentPosition;
47  
48    public ModeScreenPresenter(ModeScreenView modeScreenView, Mode currentMode,
49      ResultListener resultListener, ScreenView nextScreenView) {
50      this.modeScreenView = Objects.requireNonNull(modeScreenView);
51      this.currentMode = Objects.requireNonNull(currentMode);
52      this.resultListener = Objects.requireNonNull(resultListener);
53      this.nextScreenView = Objects.requireNonNull(nextScreenView);
54  
55      int modeHeaderLength = 0;
56      int modeDescriptionLength = 0;
57      for (int i = 0; i < modes.size(); i++) {
58        Mode mode = modes.get(i);
59        if (mode == currentMode) {
60          currentPosition = i;
61        }
62  
63        if (modeHeaderLength < mode.getHeader().length()) {
64          modeHeaderLength = mode.getHeader().length();
65        }
66  
67        if (modeDescriptionLength < mode.getDescription().length()) {
68          modeDescriptionLength = mode.getDescription().length();
69        }
70      }
71  
72      modeHeaderMaxLength = modeHeaderLength;
73      modeDescriptionMaxLength = modeDescriptionLength;
74    }
75  
76    public void init() {
77      modeScreenView.hideCursor();
78      modeScreenView.clearTerminal();
79      modeScreenView.showModeScreen(currentMode, modes, currentPosition, modeHeaderMaxLength,
80        modeDescriptionMaxLength);
81      modeScreenView.refreshTerminal();
82    }
83  
84    public void arrowUp() {
85      if (currentPosition > 0) {
86        currentPosition -= 1;
87        showMode(currentPosition);
88        showMode(currentPosition + 1);
89        modeScreenView.refreshTerminal();
90      }
91    }
92  
93    public void arrowDown() {
94      if (currentPosition < modes.size() - 1) {
95        currentPosition += 1;
96        showMode(currentPosition);
97        showMode(currentPosition - 1);
98        modeScreenView.refreshTerminal();
99      }
100   }
101 
102   public void pageUp() {
103     if (currentPosition > 0) {
104       int previousPosition = currentPosition;
105       currentPosition = 0;
106       showMode(previousPosition);
107       showMode(currentPosition);
108       modeScreenView.refreshTerminal();
109     }
110   }
111 
112   public void pageDown() {
113     if (currentPosition < modes.size() - 1) {
114       int previousPosition = currentPosition;
115       currentPosition = modes.size() - 1;
116       showMode(previousPosition);
117       showMode(currentPosition);
118       modeScreenView.refreshTerminal();
119     }
120   }
121 
122   private void showMode(int pos) {
123     modeScreenView.showMode(pos, modes.get(pos), pos == currentPosition, modeHeaderMaxLength,
124       modeDescriptionMaxLength);
125   }
126 
127   public ScreenView transitionToNextScreen(boolean changeMode) {
128     Mode selectedMode = modes.get(currentPosition);
129     if (changeMode && currentMode != selectedMode) {
130       resultListener.accept(selectedMode);
131     }
132     return nextScreenView;
133   }
134 }