James Moger
2012-02-16 3cc6e2de29a0fa33dd585e938e1614a6dd5f9755
commit | author | age
486ee1 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.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.swing.table.AbstractTableModel;
25
26 /**
27  * Table model of a map of properties.
28  * 
29  * @author James Moger
30  * 
31  */
32 public class PropertiesTableModel extends AbstractTableModel {
33
34     private static final long serialVersionUID = 1L;
35
36     List<String> keys;
37
38     Map<String, String> map;
39
40     enum Columns {
41         Name, Value;
42
43         @Override
44         public String toString() {
45             return name().replace('_', ' ');
46         }
47     }
48
49     public PropertiesTableModel() {
50         this(new HashMap<String, String>());
51     }
52
53     public PropertiesTableModel(Map<String, String> map) {
54         setProperties(map);
55     }
56
57     public void setProperties(Map<String, String> map) {
58         this.map = map;
59         keys = new ArrayList<String>(map.keySet());
60         Collections.sort(this.keys);
61     }
62
63     @Override
64     public int getRowCount() {
65         return keys.size();
66     }
67
68     @Override
69     public int getColumnCount() {
70         return Columns.values().length;
71     }
72
73     @Override
74     public String getColumnName(int column) {
75         Columns col = Columns.values()[column];
76         switch (col) {
77         case Name:
78             return Translation.get("gb.name");
79         }
80         return "";
81     }
82
83     /**
84      * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
85      * 
86      * @param columnIndex
87      *            the column being queried
88      * @return the Object.class
89      */
90     public Class<?> getColumnClass(int columnIndex) {
91         return String.class;
92     }
93
94     @Override
95     public Object getValueAt(int rowIndex, int columnIndex) {
96         String key = keys.get(rowIndex);
97         Columns col = Columns.values()[columnIndex];
98         switch (col) {
99         case Name:
100             return key;
101         case Value:
102             return map.get(key);
103         }
104         return null;
105     }
106 }