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.ArrayList;
21  import java.util.EnumMap;
22  import java.util.List;
23  import java.util.Objects;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.hbtop.field.Field;
27  import org.apache.hadoop.hbase.hbtop.screen.ScreenView;
28  
29  /**
30   * The presentation logic for the field screen.
31   */
32  @InterfaceAudience.Private
33  public class FieldScreenPresenter {
34  
35    public interface ResultListener {
36      void accept(Field sortField, List<Field> fields, EnumMap<Field, Boolean> fieldDisplayMap);
37    }
38  
39    private final FieldScreenView fieldScreenView;
40    private Field sortField;
41    private final List<Field> fields;
42    private final EnumMap<Field, Boolean> fieldDisplayMap;
43    private final ResultListener resultListener;
44    private final ScreenView nextScreenView;
45  
46    private final int headerMaxLength;
47    private final int descriptionMaxLength;
48  
49    private int currentPosition;
50    private boolean moveMode;
51  
52    public FieldScreenPresenter(FieldScreenView fieldScreenView, Field sortField, List<Field> fields,
53      EnumMap<Field, Boolean> fieldDisplayMap, ResultListener resultListener,
54      ScreenView nextScreenView) {
55      this.fieldScreenView = Objects.requireNonNull(fieldScreenView);
56      this.sortField = Objects.requireNonNull(sortField);
57      this.fields = new ArrayList<>(Objects.requireNonNull(fields));
58      this.fieldDisplayMap = new EnumMap<>(Objects.requireNonNull(fieldDisplayMap));
59      this.resultListener = Objects.requireNonNull(resultListener);
60      this.nextScreenView = Objects.requireNonNull(nextScreenView);
61  
62      int headerLength = 0;
63      int descriptionLength = 0;
64      for (int i = 0; i < fields.size(); i ++) {
65        Field field = fields.get(i);
66  
67        if (field == sortField) {
68          currentPosition = i;
69        }
70  
71        if (headerLength < field.getHeader().length()) {
72          headerLength = field.getHeader().length();
73        }
74  
75        if (descriptionLength < field.getDescription().length()) {
76          descriptionLength = field.getDescription().length();
77        }
78      }
79  
80      headerMaxLength = headerLength;
81      descriptionMaxLength = descriptionLength;
82    }
83  
84    public void init() {
85      fieldScreenView.hideCursor();
86      fieldScreenView.clearTerminal();
87      fieldScreenView.showFieldScreen(sortField.getHeader(), fields, fieldDisplayMap,
88        currentPosition, headerMaxLength, descriptionMaxLength, moveMode);
89      fieldScreenView.refreshTerminal();
90    }
91  
92    public void arrowUp() {
93      if (currentPosition > 0) {
94        currentPosition -= 1;
95  
96        if (moveMode) {
97          Field tmp = fields.remove(currentPosition);
98          fields.add(currentPosition + 1, tmp);
99        }
100 
101       showField(currentPosition);
102       showField(currentPosition + 1);
103       fieldScreenView.refreshTerminal();
104     }
105   }
106 
107   public void arrowDown() {
108     if (currentPosition < fields.size() - 1) {
109       currentPosition += 1;
110 
111       if (moveMode) {
112         Field tmp = fields.remove(currentPosition - 1);
113         fields.add(currentPosition, tmp);
114       }
115 
116       showField(currentPosition);
117       showField(currentPosition - 1);
118       fieldScreenView.refreshTerminal();
119     }
120   }
121 
122   public void pageUp() {
123     if (currentPosition > 0 && !moveMode) {
124       int previousPosition = currentPosition;
125       currentPosition = 0;
126       showField(previousPosition);
127       showField(currentPosition);
128       fieldScreenView.refreshTerminal();
129     }
130   }
131 
132   public void pageDown() {
133     if (currentPosition < fields.size() - 1  && !moveMode) {
134       int previousPosition = currentPosition;
135       currentPosition = fields.size() - 1;
136       showField(previousPosition);
137       showField(currentPosition);
138       fieldScreenView.refreshTerminal();
139     }
140   }
141 
142   public void turnOnMoveMode() {
143     moveMode = true;
144     showField(currentPosition);
145     fieldScreenView.refreshTerminal();
146   }
147 
148   public void turnOffMoveMode() {
149     moveMode = false;
150     showField(currentPosition);
151     fieldScreenView.refreshTerminal();
152   }
153 
154   public void switchFieldDisplay() {
155     if (!moveMode) {
156       Field field = fields.get(currentPosition);
157       fieldDisplayMap.put(field, !fieldDisplayMap.get(field));
158       showField(currentPosition);
159       fieldScreenView.refreshTerminal();
160     }
161   }
162 
163   private void showField(int pos) {
164     Field field = fields.get(pos);
165     fieldScreenView.showField(pos, field, fieldDisplayMap.get(field), pos == currentPosition,
166       headerMaxLength, descriptionMaxLength, moveMode);
167   }
168 
169   public void setSortField() {
170     if (!moveMode) {
171       Field newSortField = fields.get(currentPosition);
172       if (newSortField != this.sortField) {
173         this.sortField = newSortField;
174         fieldScreenView.showScreenDescription(sortField.getHeader());
175         fieldScreenView.refreshTerminal();
176       }
177     }
178   }
179 
180   public ScreenView transitionToNextScreen() {
181     resultListener.accept(sortField, fields, fieldDisplayMap);
182     return nextScreenView;
183   }
184 }