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.List;
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.hbtop.mode.Mode;
23  import org.apache.hadoop.hbase.hbtop.screen.AbstractScreenView;
24  import org.apache.hadoop.hbase.hbtop.screen.Screen;
25  import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
26  import org.apache.hadoop.hbase.hbtop.terminal.KeyPress;
27  import org.apache.hadoop.hbase.hbtop.terminal.Terminal;
28  import org.apache.hadoop.hbase.hbtop.terminal.TerminalPrinter;
29  
30  /**
31   * The screen where we can choose the {@link Mode} in the top screen.
32   */
33  @InterfaceAudience.Private
34  public class ModeScreenView extends AbstractScreenView {
35  
36    private static final int SCREEN_DESCRIPTION_START_ROW = 0;
37    private static final int MODE_START_ROW = 4;
38  
39    private final ModeScreenPresenter modeScreenPresenter;
40  
41    public ModeScreenView(Screen screen, Terminal terminal, Mode currentMode,
42      ModeScreenPresenter.ResultListener resultListener, ScreenView nextScreenView) {
43      super(screen, terminal);
44      this.modeScreenPresenter = new ModeScreenPresenter(this, currentMode, resultListener,
45        nextScreenView);
46    }
47  
48    @Override
49    public void init() {
50      modeScreenPresenter.init();
51    }
52  
53    @Override
54    public ScreenView handleKeyPress(KeyPress keyPress) {
55      switch (keyPress.getType()) {
56        case Escape:
57          return modeScreenPresenter.transitionToNextScreen(false);
58  
59        case Enter:
60          return modeScreenPresenter.transitionToNextScreen(true);
61  
62        case ArrowUp:
63          modeScreenPresenter.arrowUp();
64          return this;
65  
66        case ArrowDown:
67          modeScreenPresenter.arrowDown();
68          return this;
69  
70        case PageUp:
71        case Home:
72          modeScreenPresenter.pageUp();
73          return this;
74  
75        case PageDown:
76        case End:
77          modeScreenPresenter.pageDown();
78          return this;
79  
80        default:
81          // Do nothing
82          break;
83      }
84  
85      if (keyPress.getType() != KeyPress.Type.Character) {
86        return this;
87      }
88  
89      assert keyPress.getCharacter() != null;
90      switch (keyPress.getCharacter()) {
91        case 'q':
92          return modeScreenPresenter.transitionToNextScreen(false);
93  
94        default:
95          // Do nothing
96          break;
97      }
98  
99      return this;
100   }
101 
102   public void showModeScreen(Mode currentMode, List<Mode> modes, int currentPosition,
103     int modeHeaderMaxLength, int modeDescriptionMaxLength) {
104     showScreenDescription(currentMode);
105 
106     for (int i = 0; i < modes.size(); i++) {
107       showMode(i, modes.get(i), i == currentPosition,
108         modeHeaderMaxLength, modeDescriptionMaxLength);
109     }
110   }
111 
112   private void showScreenDescription(Mode currentMode) {
113     TerminalPrinter printer = getTerminalPrinter(SCREEN_DESCRIPTION_START_ROW);
114     printer.startBold().print("Mode Management").stopBold().endOfLine();
115     printer.print("Current mode: ")
116       .startBold().print(currentMode.getHeader()).stopBold().endOfLine();
117     printer.print("Select mode followed by <Enter>").endOfLine();
118   }
119 
120   public void showMode(int pos, Mode mode, boolean selected, int modeHeaderMaxLength,
121     int modeDescriptionMaxLength) {
122 
123     String modeHeader = String.format("%-" + modeHeaderMaxLength + "s", mode.getHeader());
124     String modeDescription = String.format("%-" + modeDescriptionMaxLength + "s",
125       mode.getDescription());
126 
127     int row = MODE_START_ROW + pos;
128     TerminalPrinter printer = getTerminalPrinter(row);
129     if (selected) {
130       printer.startHighlight().print(modeHeader).stopHighlight()
131         .printFormat(" = %s", modeDescription).endOfLine();
132     } else {
133       printer.printFormat("%s = %s", modeHeader, modeDescription).endOfLine();
134     }
135   }
136 }