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.field; 019 020import java.util.EnumMap; 021import java.util.List; 022import org.apache.hadoop.hbase.hbtop.field.Field; 023import org.apache.hadoop.hbase.hbtop.screen.AbstractScreenView; 024import org.apache.hadoop.hbase.hbtop.screen.Screen; 025import org.apache.hadoop.hbase.hbtop.screen.ScreenView; 026import org.apache.hadoop.hbase.hbtop.terminal.KeyPress; 027import org.apache.hadoop.hbase.hbtop.terminal.Terminal; 028import org.apache.hadoop.hbase.hbtop.terminal.TerminalPrinter; 029import org.apache.yetus.audience.InterfaceAudience; 030 031 032/** 033 * The screen where we can change the displayed fields, the sort key and the order of the fields. 034 */ 035@InterfaceAudience.Private 036public class FieldScreenView extends AbstractScreenView { 037 038 private static final int SCREEN_DESCRIPTION_START_ROW = 0; 039 private static final int FIELD_START_ROW = 5; 040 041 private final FieldScreenPresenter fieldScreenPresenter; 042 043 public FieldScreenView(Screen screen, Terminal terminal, Field sortField, List<Field> fields, 044 EnumMap<Field, Boolean> fieldDisplayMap, FieldScreenPresenter.ResultListener resultListener, 045 ScreenView nextScreenView) { 046 super(screen, terminal); 047 this.fieldScreenPresenter = new FieldScreenPresenter(this, sortField, fields, fieldDisplayMap, 048 resultListener, nextScreenView); 049 } 050 051 @Override 052 public void init() { 053 fieldScreenPresenter.init(); 054 } 055 056 @Override 057 public ScreenView handleKeyPress(KeyPress keyPress) { 058 switch (keyPress.getType()) { 059 case Escape: 060 return fieldScreenPresenter.transitionToNextScreen(); 061 062 case ArrowUp: 063 fieldScreenPresenter.arrowUp(); 064 return this; 065 066 case ArrowDown: 067 fieldScreenPresenter.arrowDown(); 068 return this; 069 070 case PageUp: 071 case Home: 072 fieldScreenPresenter.pageUp(); 073 return this; 074 075 case PageDown: 076 case End: 077 fieldScreenPresenter.pageDown(); 078 return this; 079 080 case ArrowRight: 081 fieldScreenPresenter.turnOnMoveMode(); 082 return this; 083 084 case ArrowLeft: 085 case Enter: 086 fieldScreenPresenter.turnOffMoveMode(); 087 return this; 088 089 default: 090 // Do nothing 091 break; 092 } 093 094 if (keyPress.getType() != KeyPress.Type.Character) { 095 return this; 096 } 097 098 assert keyPress.getCharacter() != null; 099 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}