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.field;
19  
20  import java.util.EnumMap;
21  import java.util.List;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.hbtop.field.Field;
25  import org.apache.hadoop.hbase.hbtop.screen.AbstractScreenView;
26  import org.apache.hadoop.hbase.hbtop.screen.Screen;
27  import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
28  import org.apache.hadoop.hbase.hbtop.terminal.KeyPress;
29  import org.apache.hadoop.hbase.hbtop.terminal.Terminal;
30  import org.apache.hadoop.hbase.hbtop.terminal.TerminalPrinter;
31  
32  /**
33   * The screen where we can change the displayed fields, the sort key and the order of the fields.
34   */
35  @InterfaceAudience.Private
36  public class FieldScreenView extends AbstractScreenView {
37  
38    private static final int SCREEN_DESCRIPTION_START_ROW = 0;
39    private static final int FIELD_START_ROW = 5;
40  
41    private final FieldScreenPresenter fieldScreenPresenter;
42  
43    public FieldScreenView(Screen screen, Terminal terminal, Field sortField, List<Field> fields,
44      EnumMap<Field, Boolean> fieldDisplayMap, FieldScreenPresenter.ResultListener resultListener,
45      ScreenView nextScreenView) {
46      super(screen, terminal);
47      this.fieldScreenPresenter = new FieldScreenPresenter(this, sortField, fields, fieldDisplayMap,
48        resultListener, nextScreenView);
49    }
50  
51    @Override
52    public void init() {
53      fieldScreenPresenter.init();
54    }
55  
56    @Override
57    public ScreenView handleKeyPress(KeyPress keyPress) {
58      switch (keyPress.getType()) {
59        case Escape:
60          return fieldScreenPresenter.transitionToNextScreen();
61  
62        case ArrowUp:
63          fieldScreenPresenter.arrowUp();
64          return this;
65  
66        case ArrowDown:
67          fieldScreenPresenter.arrowDown();
68          return this;
69  
70        case PageUp:
71        case Home:
72          fieldScreenPresenter.pageUp();
73          return this;
74  
75        case PageDown:
76        case End:
77          fieldScreenPresenter.pageDown();
78          return this;
79  
80        case ArrowRight:
81          fieldScreenPresenter.turnOnMoveMode();
82          return this;
83  
84        case ArrowLeft:
85        case Enter:
86          fieldScreenPresenter.turnOffMoveMode();
87          return this;
88  
89        default:
90          // Do nothing
91          break;
92      }
93  
94      if (keyPress.getType() != KeyPress.Type.Character) {
95        return this;
96      }
97  
98      assert keyPress.getCharacter() != null;
99      switch (keyPress.getCharacter()) {
100       case 'd':
101       case ' ':
102         fieldScreenPresenter.switchFieldDisplay();
103         break;
104 
105       case 's':
106         fieldScreenPresenter.setSortField();
107         break;
108 
109       case 'q':
110         return fieldScreenPresenter.transitionToNextScreen();
111 
112       default:
113         // Do nothing
114         break;
115     }
116 
117     return this;
118   }
119 
120   public void showFieldScreen(String sortFieldHeader, List<Field> fields,
121     EnumMap<Field, Boolean> fieldDisplayMap, int currentPosition, int headerMaxLength,
122     int descriptionMaxLength, boolean moveMode) {
123     showScreenDescription(sortFieldHeader);
124 
125     for (int i = 0; i < fields.size(); i ++) {
126       Field field = fields.get(i);
127       showField(i, field, fieldDisplayMap.get(field), i == currentPosition, headerMaxLength,
128         descriptionMaxLength, moveMode);
129     }
130   }
131 
132   public void showScreenDescription(String sortKeyHeader) {
133     TerminalPrinter printer = getTerminalPrinter(SCREEN_DESCRIPTION_START_ROW);
134     printer.startBold().print("Fields Management").stopBold().endOfLine();
135     printer.print("Current Sort Field: ").startBold().print(sortKeyHeader).stopBold().endOfLine();
136     printer.print("Navigate with up/down, Right selects for move then <Enter> or Left commits,")
137       .endOfLine();
138     printer.print("'d' or <Space> toggles display, 's' sets sort. Use 'q' or <Esc> to end!")
139       .endOfLine();
140   }
141 
142   public void showField(int pos, Field field, boolean display, boolean selected,
143     int fieldHeaderMaxLength, int fieldDescriptionMaxLength, boolean moveMode) {
144 
145     String fieldHeader = String.format("%-" + fieldHeaderMaxLength + "s", field.getHeader());
146     String fieldDescription = String.format("%-" + fieldDescriptionMaxLength + "s",
147       field.getDescription());
148 
149     int row = FIELD_START_ROW + pos;
150     TerminalPrinter printer = getTerminalPrinter(row);
151     if (selected) {
152       String prefix = display ? "* " : "  ";
153       if (moveMode) {
154         printer.print(prefix);
155 
156         if (display) {
157           printer.startBold();
158         }
159 
160         printer.startHighlight()
161           .printFormat("%s = %s", fieldHeader, fieldDescription).stopHighlight();
162 
163         if (display) {
164           printer.stopBold();
165         }
166 
167         printer.endOfLine();
168       } else {
169         printer.print(prefix);
170 
171         if (display) {
172           printer.startBold();
173         }
174 
175         printer.startHighlight().print(fieldHeader).stopHighlight()
176           .printFormat(" = %s", fieldDescription);
177 
178         if (display) {
179           printer.stopBold();
180         }
181 
182         printer.endOfLine();
183       }
184     } else {
185       if (display) {
186         printer.print("* ").startBold().printFormat("%s = %s", fieldHeader, fieldDescription)
187           .stopBold().endOfLine();
188       } else {
189         printer.printFormat("  %s = %s", fieldHeader, fieldDescription).endOfLine();
190       }
191     }
192   }
193 }