James Moger
2011-10-02 19c634873b49dea8b49fc54ca393153f7eb0eb35
commit | author | age
19c634 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.client;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.swing.table.AbstractTableModel;
25
26 import com.gitblit.models.RepositoryModel;
27
28 public class RepositoriesModel extends AbstractTableModel {
29
30     private static final long serialVersionUID = 1L;
31
32     Map<String, RepositoryModel> repositories;
33
34     List<RepositoryModel> list;
35
36     enum Columns {
37         Name, Description, Owner, Last_Change, Size;
38
39         @Override
40         public String toString() {
41             return name().replace('_', ' ');
42         }
43     }
44
45     public RepositoriesModel(Map<String, RepositoryModel> repositories) {
46         this.repositories = repositories;
47         list = new ArrayList<RepositoryModel>(repositories.values());
48         Collections.sort(list);
49     }
50
51     @Override
52     public int getRowCount() {
53         return repositories.size();
54     }
55
56     @Override
57     public int getColumnCount() {
58         return Columns.values().length;
59     }
60
61     @Override
62     public String getColumnName(int column) {
63         Columns col = Columns.values()[column];
64         return col.toString();
65     }
66
67     /**
68      * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
69      * 
70      * @param columnIndex
71      *            the column being queried
72      * @return the Object.class
73      */
74     public Class<?> getColumnClass(int columnIndex) {
75         Columns col = Columns.values()[columnIndex];
76         switch (col) {
77         case Last_Change:
78             return Date.class;
79         }
80         return String.class;
81     }
82
83     @Override
84     public Object getValueAt(int rowIndex, int columnIndex) {
85         RepositoryModel model = list.get(rowIndex);
86         Columns col = Columns.values()[columnIndex];
87         switch (col) {
88         case Name:
89             return model.name;
90         case Description:
91             return model.description;
92         case Owner:
93             return model.owner;
94         case Last_Change:
95             return model.lastChange;
96         case Size:
97             return model.size;
98         }
99         return null;
100     }
101 }