• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.views.trace.component;
17 
18 import javax.swing.table.AbstractTableModel;
19 import java.util.ArrayList;
20 import java.util.List;
21 
22 /**
23  * FTableModel
24  *
25  * @param <T> obj
26  * @since 2021/5/24 11:57
27  */
28 public class FTableModel<T extends Object> extends AbstractTableModel {
29     private List<Column<T>> columnNames;
30     private List<T> dataSource = new ArrayList<>();
31 
32     /**
33      * set columns
34      *
35      * @param columns columns
36      */
setColumns(List<Column<T>> columns)37     public void setColumns(List<Column<T>> columns) {
38         this.columnNames = columns;
39     }
40 
setDataSource(final List<T> param)41     public void setDataSource(final List<T> param) {
42         this.dataSource = param;
43     }
44 
45     @Override
getRowCount()46     public int getRowCount() {
47         return dataSource.size();
48     }
49 
50     @Override
getColumnCount()51     public int getColumnCount() {
52         return columnNames == null ? 0 : columnNames.size();
53     }
54 
55     @Override
getColumnName(int column)56     public String getColumnName(int column) {
57         return columnNames.get(column).name;
58     }
59 
60     @Override
getValueAt(int rowIndex, int columnIndex)61     public Object getValueAt(int rowIndex, int columnIndex) {
62         return columnNames.get(columnIndex).callable.map(dataSource.get(rowIndex));
63     }
64 
65     /**
66      * Process map
67      *
68      * @param <T> Data
69      */
70     public interface Process<T extends Object> {
71         /**
72          * map
73          *
74          * @param obj obj
75          * @return Object object
76          */
map(T obj)77         Object map(T obj);
78     }
79 
80     /**
81      * Column
82      *
83      * @param <T> obj
84      * @since 2021/5/24 11:57
85      */
86     public static class Column<T extends Object> {
87         /**
88          * name
89          */
90         private String name;
91         private Process callable;
92 
93         /**
94          * construct
95          *
96          * @param name name
97          * @param callable callable
98          */
Column(String name, Process<T> callable)99         public Column(String name, Process<T> callable) {
100             this.name = name;
101             this.callable = callable;
102         }
103     }
104 }
105