James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
commit | author | age
841651 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.awt.Color;
19 import java.awt.Component;
20
92e2df 21 import javax.swing.JList;
841651 22 import javax.swing.JTable;
92e2df 23 import javax.swing.ListCellRenderer;
841651 24 import javax.swing.table.DefaultTableCellRenderer;
JM 25
26 /**
27  * Repository name cell renderer. This renderer shows the group name in a gray
28  * color and accentuates the repository name in a cornflower blue color.
29  * 
30  * @author James Moger
31  * 
32  */
92e2df 33 public class NameRenderer extends DefaultTableCellRenderer implements ListCellRenderer {
841651 34
JM 35     private static final long serialVersionUID = 1L;
92e2df 36
JM 37     private static final Color CORNFLOWER = new Color(0x00, 0x69, 0xD6);
841651 38
17820f 39     private final String groupSpan;
4cac0d 40
da0269 41     public NameRenderer() {
92e2df 42         this(Color.gray, CORNFLOWER);
da0269 43     }
JM 44
17820f 45     private NameRenderer(Color group, Color repo) {
841651 46         groupSpan = "<span style='color:" + getHexColor(group) + "'>";
JM 47         setForeground(repo);
48     }
49
50     String getHexColor(Color c) {
51         StringBuilder sb = new StringBuilder();
52         sb.append(Integer.toHexString((c.getRGB() & 0x00FFFFFF)));
53         while (sb.length() < 6)
54             sb.insert(0, '0');
55         sb.insert(0, '#');
56         return sb.toString();
57     }
58
59     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
60             boolean hasFocus, int row, int column) {
61         super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
41cffb 62         setValue(value == null ? "" : value, isSelected);
92e2df 63         return this;
JM 64     }
65
66     @Override
67     public Component getListCellRendererComponent(JList list, Object value, int index,
68             boolean isSelected, boolean cellHasFocus) {
41cffb 69         setValue(value == null ? "" : value, isSelected);
92e2df 70         if (isSelected) {
JM 71             setBackground(list.getSelectionBackground());
72             setForeground(list.getSelectionForeground());
73         } else {
74             setBackground(list.getBackground());
75             setForeground(CORNFLOWER);
76         }
77         return this;
78     }
79
80     private void setValue(Object value, boolean isSelected) {
17820f 81         String name = value.toString();
JM 82         int lastSlash = name.lastIndexOf('/');
83         if (!isSelected && lastSlash > -1) {
84             String group = name.substring(0, lastSlash + 1);
85             String repo = name.substring(lastSlash + 1);
86             setText("<html><body>" + groupSpan + group + "</span>" + repo);
841651 87         } else {
17820f 88             this.setText(name);
841651 89         }
JM 90     }
91 }