James Moger
2012-10-22 eba89539a29deba954035056437279088c3e047b
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
841651 18 import java.awt.Color;
19c634 19 import java.awt.Component;
JM 20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22
23 import javax.swing.JTable;
841651 24 import javax.swing.SwingConstants;
19c634 25 import javax.swing.table.DefaultTableCellRenderer;
JM 26
841651 27 import com.gitblit.utils.TimeUtils;
JM 28
29 /**
30  * Time ago cell renderer with real date tooltip.
31  * 
32  * @author James Moger
33  * 
34  */
19c634 35 public class DateCellRenderer extends DefaultTableCellRenderer {
JM 36
37     private static final long serialVersionUID = 1L;
38
39     private final String pattern;
9adf62 40     
841651 41     public DateCellRenderer(String pattern, Color foreground) {
19c634 42         this.pattern = (pattern == null ? "yyyy-MM-dd HH:mm" : pattern);
841651 43         setForeground(foreground);
JM 44         setHorizontalAlignment(SwingConstants.CENTER);
19c634 45     }
JM 46
47     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
48             boolean hasFocus, int row, int column) {
49         super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
50         if (value instanceof Date) {
841651 51             Date date = (Date) value;
4cac0d 52             String title;
JM 53             String dateString;
3f5a93 54             if (date.getTime() == 0) {
4cac0d 55                 title = "--";
JM 56                 dateString = "never";
3f5a93 57             } else {
9adf62 58                 title = Translation.getTimeUtils().timeAgo(date);
4cac0d 59                 dateString = new SimpleDateFormat(pattern).format((Date) value);
3f5a93 60             }
JM 61
4cac0d 62             if ((System.currentTimeMillis() - date.getTime()) > 10 * 24 * 60 * 60 * 1000L) {
JM 63                 String tmp = dateString;
64                 dateString = title;
65                 title = tmp;
66             }
67             this.setText(title);
68             this.setToolTipText(dateString);
19c634 69         }
JM 70         return this;
71     }
72 }